Minimum Time to Make Array Sum At Most x - Problem
You are given two 0-indexed integer arrays nums1 and nums2 of equal length. Every second, for all indices 0 <= i < nums1.length, the value of nums1[i] is incremented by nums2[i].
After this increment operation is done, you can perform the following operation:
- Choose an index
0 <= i < nums1.lengthand makenums1[i] = 0.
You are also given an integer x.
Return the minimum time in which you can make the sum of all elements of nums1 to be less than or equal to x, or -1 if this is not possible.
Input & Output
Example 1 — Basic Case
$
Input:
nums1 = [1,2,3], nums2 = [1,4,2], x = 4
›
Output:
2
💡 Note:
At time 3: We can operate on index 1 (highest growth rate) at time 1, index 2 at time 2, and index 0 at time 3. After simulation, the final sum becomes ≤ 4.
Example 2 — Already Satisfied
$
Input:
nums1 = [1,1,1], nums2 = [1,1,1], x = 5
›
Output:
0
💡 Note:
Initial sum is 3 ≤ 5, so no operations needed.
Example 3 — Impossible Case
$
Input:
nums1 = [1,2,3], nums2 = [3,3,3], x = 4
›
Output:
-1
💡 Note:
Even with optimal operations, we cannot reduce the sum to ≤ 4 due to high growth rates.
Constraints
- 1 ≤ nums1.length == nums2.length ≤ 1000
- 1 ≤ nums1[i], nums2[i] ≤ 1000
- 1 ≤ x ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code