#2809

Minimum Time to Make Array Sum At Most x

international master · 2050 · lc hard +32 · verified · 27.1% accepted · 249 likes · top 5%

Description

Two 0-indexed integer arrays nums1 and nums2 of equal length are given. Each second, every nums1[i] is incremented by nums2[i]. After incrementing, you may choose one index and set nums1[i] = 0.

Given an integer x, return the minimum number of seconds needed to bring the sum of nums1 to at most x, or -1 if it is not possible.

Example 1:

Input: nums1 = [1,2,3], nums2 = [1,2,3], x = 4
Output: 3
Explanation:
For the 1st second, we apply the operation on i = 0. Therefore nums1 = [0,2+2,3+3] = [0,4,6].
For the 2nd second, we apply the operation on i = 1. Therefore nums1 = [0+1,0,6+3] = [1,0,9].
For the 3rd second, we apply the operation on i = 2. Therefore nums1 = [1+1,0+2,0] = [2,2,0].
Now sum of nums1 = 4. It can be shown that these operations are optimal, so we return 3.

Example 2:

Input: nums1 = [1,2,3], nums2 = [3,3,3], x = 4
Output: -1
Explanation: It can be shown that the sum of nums1 will always be greater than x, no matter which operations are performed.

Code

1
2
3