Understanding the Problem

Minimum Numbers of Function Calls to Make Target Array is a medium problem at leetcode, I hope you have read the problem carefully and have tried solving it. This problem requires you to get the least number of operations that you can do in order to get to the desired array. The question now, how we can do such operations to count its numbers to reach the target array in the least possible way.

Initial thoughts

Let us take this example, having [2, 2] as the target array and we want to know the minimum number of operations to reach that array. So we start off with [0, 0] adding 1 to each element (2 operations now) reaching [1, 1] and then multiply the whole array by 2 (1 operation) reaching the target array we want [2, 2] so we have 3 operations to reach the desired array for this example as shown below:

But it’s hard to move from [0, 0] to reach [2, 2] because of the possibility of wrong guessing of the next array we want to reach. What we can better do, is to move backward from [2, 2] to [0, 0] as indicated below:

Image for post

Image by the Author

In this case, we can divide instead of multiplying and subtracting instead of adding as shown in orange and we’ll end up with the same number of operations.

#mathematics #programming #javascript #python #problem-solving

Minimum Numbers of Function Calls to Make Target Array With Python and Javascript
1.45 GEEK