Skip to content

Commit f7115b1

Browse files
committed
day 14
1 parent 3bf7a68 commit f7115b1

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Minimum Operations to Reduce X to Zero
3+
=========================================
4+
5+
You are given an integer array nums and an integer x. In one operation, you can either remove the leftmost or the rightmost element from the array nums and subtract its value from x. Note that this modifies the array for future operations.
6+
7+
Return the minimum number of operations to reduce x to exactly 0 if it's possible, otherwise, return -1.
8+
9+
10+
Example 1:
11+
Input: nums = [1,1,4,2,3], x = 5
12+
Output: 2
13+
Explanation: The optimal solution is to remove the last two elements to reduce x to zero.
14+
15+
Example 2:
16+
Input: nums = [5,6,7,8,9], x = 4
17+
Output: -1
18+
19+
Example 3:
20+
Input: nums = [3,2,20,1,1,3], x = 10
21+
Output: 5
22+
Explanation: The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero.
23+
24+
Constraints:
25+
1 <= nums.length <= 105
26+
1 <= nums[i] <= 104
27+
1 <= x <= 109
28+
29+
Hint #1
30+
Think in reverse; instead of finding the minimum prefix + suffix, find the maximum subarray.
31+
32+
Hint #2
33+
Finding the maximum subarray is standard and can be done greedily.
34+
*/
35+
36+
class Solution
37+
{
38+
public:
39+
int minOperations(vector<int> &nums, int x)
40+
{
41+
unordered_map<int, int> left;
42+
int res = INT_MAX;
43+
for (auto l = 0, sum = 0; l < nums.size() && sum <= x; ++l)
44+
{
45+
left[sum] = l;
46+
sum += nums[l];
47+
}
48+
for (int r = nums.size() - 1, sum = 0; r >= 0 && sum <= x; --r)
49+
{
50+
auto it = left.find(x - sum);
51+
if (it != end(left) && r + 1 >= it->second)
52+
{
53+
res = min(res, (int)nums.size() - r - 1 + it->second);
54+
}
55+
sum += nums[r];
56+
}
57+
return res == INT_MAX ? -1 : res;
58+
}
59+
};

Diff for: Leetcode Daily Challenge/January-2021/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
| 11. | [Merge Sorted Array](https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/580/week-2-january-8th-january-14th/3600/) | [cpp](./11.%20Merge%20Sorted%20Array.cpp) |
1616
| 12. | [Add Two Numbers](https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/580/week-2-january-8th-january-14th/3601/) | [cpp](./12.%20Add%20Two%20Numbers.cpp) |
1717
| 13. | [Boats to Save People](https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/580/week-2-january-8th-january-14th/3602/) | [cpp](./13.%20Boats%20to%20Save%20People.cpp) |
18-
| 14. | []() | [cpp](./14.cpp) |
18+
| 14. | [Minimum Operations to Reduce X to Zero](https://leetcode.com/explore/challenge/card/january-leetcoding-challenge-2021/580/week-2-january-8th-january-14th/3603/) | [cpp](./14.%20Minimum%20Operations%20to%20Reduce%20X%20to%20Zero.cpp) |
1919
| . | []() | [cpp](./.cpp) |

0 commit comments

Comments
 (0)