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:
3
💡 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
Understanding the Visualization
1
Initial State
nums1=[1,2,3], nums2=[1,4,2], x=4. Sum=6 > 4
2
Growth & Operations
Each second: increment by nums2, then optionally reset one element
3
Goal
Find minimum time to achieve sum ≤ 4
Key Takeaway
🎯 Key Insight: Sort by growth rates and use DP to find optimal operation schedule
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code