Similar Problems

Similar Problems not available

Minimum Numbers Of Function Calls To Make Target Array - Leetcode Solution

Companies:

LeetCode:  Minimum Numbers Of Function Calls To Make Target Array Leetcode Solution

Difficulty: Medium

Topics: greedy bit-manipulation array  

Problem Statement:

Given an array target and an integer n. In each iteration, you can read a number from list and replace any element of the list with the sum of two previous elements from the list. You can repeatedly perform this operation on the list till you get the target array. Return the minimum number of function calls to make the target array.

Example 1:

Input: target = [1,3,5] Output: 3 Explanation: We need to perform 3 function calls in order to get the target array: - Call #1: sum([0, 0, 1]) => [0, 0, 1] - Call #2: sum([0, 1, 1]) => [0, 1, 1] - Call #3: sum([1, 1, 4]) => [1, 3, 5]

Example 2:

Input: target = [1,5,10] Output: 4 Explanation: We need to perform 4 function calls in order to get the target array: - Call #1: sum([0, 0, 1]) => [0, 0, 1] - Call #2: sum([0, 1, 1]) => [0, 1, 1] - Call #3: sum([1, 1, 2]) => [1, 1, 2] - Call #4: sum([0, 2, 3]) => [1, 5, 10]

Solution:

The problem description itself gives a rough hint to start solving the question by looking at it in reverse. We can start with an array [0, 0, ..., 0] and convert it into the target array. By doing so, we can easily solve the problem. The solution strategy will be as follows:

  1. We create a cache dictionary to store the number of calls required to convert any new array into the target array.
  2. We create a function "calls" that will take an input array and return the minimum number of calls to convert it into a target array.
  3. The calls() function will first sort the input array in decreasing order and check if the input array is in the cache dictionary. If present, it returns the value.
  4. Otherwise, we create a new array "temp" of the same length as the input array and fill it with zero. We then create two pointers i and j starting at index 0 and index 1 of the input array.
  5. We set the first element of "temp" to input[0] and increment the pointer i.
  6. We then start iterating through the input array until we reach the end. For each element of input array, we check whether the sum of the elements pointed to by i and j of the input array is equal to the current element.
  7. If so, then we set temp[k] to the current element and increment both i and j.
  8. Otherwise, we increment the value of temp[k] such that it is equal to twice the value of the element pointed to by i of the temp array and we increment the pointer i.
  9. We also increment the count of number of calls so far.
  10. Once we have iterated through the entire input array, we store the minimum number of calls to convert the temp array into the target array in the cache dictionary and return the count of calls.

Code:

Here is the Python code to solve the aforementioned problem:

def minOperations(target: List[int]) -> int:

# create a cache dictionary to store the number of calls for each new array encountered
cache = {}

# Recursive function to return the minimum number of calls to convert a given array into the target array
def calls(nums):
    # Base Condition
    if tuple(nums) in cache:
        return cache[tuple(nums)]

    # Sort the given array
    nums = sorted(nums, reverse=True)

    # Create a new array of the same length and fill with zeros
    temp = [0] * len(nums)

    # Set the first element of the new array
    temp[0] = nums[0]
    i, j = 0, 1
    count = 1

    # Iterating through the input array
    for k in range(1, len(nums)):
        if nums[k] == temp[i] + temp[j]:
            temp[k] = nums[k]
            i += 1
            j += 1
        else:
            temp[k] = 2 * temp[i]
            i += 1

        count += 1

    # Store the minimum number of calls needed to convert temp into the target array
    cache[tuple(nums)] = count

    return count

# Call the recursive function and return the minimum number of calls to convert the target array into an array consisting only of zeros.
return calls(target) - 1

Time Complexity:

The time complexity of the calls() function is O(N log N), where N is the length of the input array. The overall time complexity of the solution is O(N^2 log N), where N is the length of the input array.

Space Complexity:

The space complexity of the solution is O(N), where N is the length of the input array. This is due to the creation of the cache dictionary and the temp array.

Minimum Numbers Of Function Calls To Make Target Array Solution Code

1