465. Minimizing Array After Replacing Pairs With Their Product
Given an array of n integers nums and an integers k. Below operation can be applied any number of times :
- Replace both of the array's adjacent members, such as x and y, with a single element of value x * y, so that x * y <= k.
After any number of operations, return the smallest length of nums that can be used.
Example 1:
Input : nums = [1, 2, 2, 4, 3, 7] , k = 6
Output : 4
Explanation :
We perform these operations:
1. [1, 2, 2, 4, 3, 7] -> [2, 2, 4, 3, 7]
2. [2, 2, 4, 3, 7] -> [4, 4, 3, 7]
It can be shown that 4 is the minimum length possible to achieve with the given operation.
Example 2:
Input : nums = [1, 2, 3, 4, 5] , k = 10
Output : 3
Explanation :
We perform these operations:
1. [1, 2, 3, 4, 5] -> [2, 3, 4, 5]
2. [2, 3, 4, 5] -> [6, 4, 5]
It can be shown that 3 is the minimum length possible to achieve with the given operation.
Now Your Turn!
Pick the correct output for the given inputInput : nums = [4, 2, 3, 2, 4, 5] , k = 12
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= nums.length <= 105
- 0 <= nums[i] <= 109
- 1 <= k <= 109