我对编码还很陌生,我正在尽力而为,但是经过数小时的研究,我仍然无法弄清这一点。我正在尝试通过最少的移动使这两个单独的数组相同。我一次只能++或-一个数字。
这是挑战:
不允许对数字重新排序例如,考虑两个数组:Andrea的[123, 543]
和Maria的[321, 279]
。对于第一个数字,Andrea可以将1递增两次以达到3。2已经相等。最后,她将3减2等于1。花了4步才能达到目标。对于第二个整数,她递减5 3次,递增4 3次和3 6次。花了12步来转换第二个数组元素。总共花了16步来转换组成整个数组的两个值。
let a = [1234, 4321]
let m = [2345, 3214]
function minimumMoves(a, m) {
// Write your code here
let numMoves = 0;
let num1 = '' ;
let num2 = '' ;
let digit1 = '';
let digit2= '';
for (let i = 0; i < a.length; i++)
{
num1 = a[i];
while (num1 != 0) {
digit1 = num1 % 10;
digit2 = num2 % 10;
num1 = Math.trunc(num1 / 10);
num2 = Math.trunc(num2 / 10);
numMoves = numMoves + Math.abs(digit1 - digit2);
}
}
return numMoves
}
答案 0 :(得分:1)
我认为您应该创建一个有效地使用一位数字的函数,而该数字要比另一个数字大(又需要递减),则可以这样做:
const incrementWhileNeeded = (target, currentValue) =>
Math.abs(target - currentValue)
然后,您需要将数字分解为数字(可以像看起来一样使用%
进行数学处理,但为简单起见,将使用诸如String(num1).split('').map(Number)
451,并将其更改为[4, 5, 1]
。
然后,下一步是将该函数(incrementWhileNeeded)映射到每个数字:只关注第一个数字(然后应用forEach或.map将该函数应用于所有数字。
所以看起来像这样:
firstNumberArray.map(incrementWhileNeeded)
如您所解释的,[1, 0, 2]
会回复您。
然后.reduce()这样,您可以获取计数的总和。
因此,这会将使用[1,0,2].reduce((accumulator, current) => accumulator + current)
减少到3。
因此要获得全部功能:
const incrementWhileNeeded = (target, currentValue) =>
Math.abs(target - currentValue)
const calculateMinimumMoves = (fullNumber, targetNumber) => {
const numArray = String(fullNumber).split('').map(Number)
const targetArray = String(targetNumber).split('').map(Number)
const diffArray = numArray.map((currentElement, targetArray[index]) => incrementWhileNeeded(currentElement, targetArray[index])
return diffArray.reduce((accumulator, current) => accumulator + current, 0)
}
const minimumMoves = (array1, array2) =>
array1.reduce((accumulator, current, index) =>
accumulator + calculateMinimumMoves(current, array2[index]),
0)
答案 1 :(得分:1)
要仅将changina一串数字的计数获取到另一个数字,可以在某个位置添加数字的绝对增量。
function count(a, b) {
return Array.from(a).reduce((s, v, i) => s + Math.abs(v - b[i]), 0);
}
console.log(count('123', '321'));
答案 2 :(得分:0)
签出此代码:
a = [1234, 4321]
b = [2345, 3214]
function minimumMoves(a, m) {
let numMoves1 = 0, numMoves2 = 0;
let num1 = '', num2 = '';
let digit1 = '', digit2 = '';
//Forward
for (let i = 0 ; i < a.length ; i++)
{
num1 = a[i];
num2 = m[i];
for (let j = 0 ; j < a.length ; j++)
{
digit1 = num1 % 10;
digit2 = num2 % 10;
numMoves1 += Math.abs(digit1-digit2);
num1 = (num1 - digit1) / 10;
num2 = (num2 - digit2) / 10;
}
}
//Backward
for (let i = 0 ; i < a.length ; i++)
{
num1 = m[i];
num2 = a[i];
for (let j = 0 ; j < a.length ; j++)
{
digit1 = num1 % 10;
digit2 = num2 % 10;
numMoves2 += Math.abs(digit1-digit2);
num1 = (num1 - digit1) / 10;
num2 = (num2 - digit2) / 10;
}
}
if (numMoves1>numMoves2)
{
//Answer is numMoves1
} else if (numMoves1<numMoves2)
{
//Answer is numMoves2
} else {
//Answer is any one, i.e, either numMoves1 or numMoves2
}
}
如果您需要对此代码进行快速验证,请导航Here。
然后粘贴以下代码:
/******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
public class Main
{
public static void main(String[] args) {
Integer[] a = {1234, 4321};
Integer[] m = {2345, 3214};
Integer numMoves1 = 0, numMoves2 = 0;
Integer num1 = 0, num2 = 0;
Integer digit1 = 0, digit2 = 0;
//Forward
for (Integer i = 0 ; i < a.length ; i++)
{
num1 = a[i];
num2 = m[i];
for (Integer j = 0 ; j < a.length ; j++)
{
digit1 = num1 % 10;
digit2 = num2 % 10;
numMoves1 += Math.abs(digit1-digit2);
num1 = (num1 - digit1) / 10;
num2 = (num2 - digit2) / 10;
}
}
//Backward
for (Integer i = 0 ; i < a.length ; i++)
{
num1 = m[i];
num2 = a[i];
for (Integer j = 0 ; j < a.length ; j++)
{
digit1 = num1 % 10;
digit2 = num2 % 10;
numMoves2 += Math.abs(digit1-digit2);
num1 = (num1 - digit1) / 10;
num2 = (num2 - digit2) / 10;
}
}
if (numMoves1>numMoves2)
{
//Answer is numMoves1
} else if (numMoves1<numMoves2)
{
//Answer is numMoves2
} else
{
//Answer is any one, i.e, either numMoves1 or numMoves2
}
System.out.println(numMoves1 + " & " + numMoves2);
}
}
我希望此算法有帮助;)
答案 3 :(得分:0)
//This code works....
// Check this out ....
public class Main
{
public static void main(String[] args) {
Integer[] a = {1234, 4321};
Integer[] m = {2345, 3214};
Integer numMoves1 = 0;
Integer num1 = 0, num2 = 0;
Integer digit1 = 0, digit2 = 0;
//Forward
for (Integer i = 0 ; i < a.length ; i++)
{
num1 = a[i];
num2 = m[i];
while(num1>0)
{
digit1 = num1 % 10;
digit2 = num2 % 10;
numMoves1 += Math.abs(digit1-digit2);
num1 = (num1 - digit1) / 10;
num2 = (num2 - digit2) / 10;
}
}
System.out.println(numMoves1);
}
}
答案 4 :(得分:0)
这是找到最小移动以匹配两个不同数组的每个元素的解决方案。
let a = [1234, 4321]
let m = [2345, 3214]
function minimumMoves(a, m) {
// Write your code here
let numMoves = 0;
let num1 = '' ;
let num2 = '' ;
let digit1 = '';
let digit2= '';
for (let i = 0; i < a.length; i++)
{
num1 = a[i];
num2 = m[i];
while (num1 != 0) {
digit1 = num1 % 10;
digit2 = num2 % 10;
num1 = Math.trunc(num1 / 10);
num2 = Math.trunc(num2 / 10);
numMoves = numMoves + Math.abs(digit1 - digit2);
}
}
return numMoves;
}
console.log(minimumMoves(a, m));