admin管理员组

文章数量:1323348

I am pretty new to coding and I'm trying my best, but after hours and hours of research i still cant figure this out. I'm trying to make these two separate arrays the same with the minimum number of moves. I can only ++ or -- one number at a time.

This is the challenge:

No reordering of the digits is allowed For example, consider two arrays: Andrea's [123, 543] and Maria's [321, 279]. For the first digit, Andrea can increment the 1 twice to achieve 3. The 2's are equal already. Finally, she decrements her 3 twice to equal 1. It took 4 moves to reach her goal. For the second integer, she decrements 5 three times, increments 4 three times and 3 six times. It took 12 moves to convert the second array element. In total, it took 16 moves to convert both values prising the plete array.

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
}

I am pretty new to coding and I'm trying my best, but after hours and hours of research i still cant figure this out. I'm trying to make these two separate arrays the same with the minimum number of moves. I can only ++ or -- one number at a time.

This is the challenge:

No reordering of the digits is allowed For example, consider two arrays: Andrea's [123, 543] and Maria's [321, 279]. For the first digit, Andrea can increment the 1 twice to achieve 3. The 2's are equal already. Finally, she decrements her 3 twice to equal 1. It took 4 moves to reach her goal. For the second integer, she decrements 5 three times, increments 4 three times and 3 six times. It took 12 moves to convert the second array element. In total, it took 16 moves to convert both values prising the plete array.

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
}
Share Improve this question edited Mar 15, 2019 at 19:40 adiga 35.3k9 gold badges65 silver badges87 bronze badges asked Mar 15, 2019 at 19:31 zrwhite151zrwhite151 791 gold badge2 silver badges8 bronze badges 1
  • It's not clear to me what you're trying to do. please add a better description and examples of input and expected output, thanks! – Nir Alfasi Commented Mar 15, 2019 at 19:39
Add a ment  | 

5 Answers 5

Reset to default 1

For getting only the count for changina a string of digits to another, you could add the absolute delta of the digits at a place.

function count(a, b) {
    return Array.from(a).reduce((s, v, i) => s + Math.abs(v - b[i]), 0);
}

console.log(count('123', '321'));

In my opinion you should make a function that effectively takes a single digit and while it is greater than the other number aka needs to be decremented it does that:

const incrementWhileNeeded = (target, currentValue) =>
  Math.abs(target - currentValue)

Then, you need split the numbers into their digits (you can do this the mathematical way using % like it looks like you've done, but just for simplicity something like: String(num1).split('').map(Number) will take 451 and change it to [4, 5, 1].

Then, your next step is to map that function (incrementWhileNeeded) to each individual digit: just focus on the first number (and then apply forEach or .map to apply that function to all of them.

So that will look something like: firstNumberArray.map(incrementWhileNeeded)

Which will respond with as you explained [1, 0, 2].

Then .reduce() this so that you can get the sum of the counts. So this will reduce using [1,0,2].reduce((accumulator, current) => accumulator + current) to 3.

So for the full functionality:

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)

Check this code out:

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
    }
}

If you need quick verification for this code, navigate Here.

And then paste this code:

/******************************************************************************

                            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);
    }
}

I hope this algorithm helps ;)

//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);
    }
}

Here is the solution for finding the minimum moves to match each of the elements of the two different array.

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));

本文标签: javascriptMinimum number of digit changes to get two arrays to have the same valueStack Overflow