-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1658_minimum_operations_to_reduce_x_to_zero.cpp
More file actions
49 lines (47 loc) · 1.16 KB
/
1658_minimum_operations_to_reduce_x_to_zero.cpp
File metadata and controls
49 lines (47 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <unordered_map>
#include <queue>
#include <numeric>
class Solution
{
public:
int minOperations(std::vector<int> &nums, int x)
{
int target = std::reduce(nums.begin(), nums.end()) - x;
if (target == 0)
{
return nums.size();
}
int max_len = 0;
int currSum = 0;
int left = 0;
for (int right = 0; right != nums.size(); ++right)
{
currSum += nums[right];
while (currSum > target & left <= right)
{
currSum -= nums[left];
left++;
}
if (currSum == target)
{
max_len = std::max(max_len, right - left + 1);
}
}
if (max_len == 0)
return -1;
return nums.size() - max_len;
}
};
int main()
{
Solution s;
std::vector<int> nums = {8828, 9581, 49, 9818, 9974, 9869, 9991, 10000, 10000, 10000, 9999, 9993, 9904, 8819, 1231, 6309};
int x = 134365;
int res = s.minOperations(nums, x);
std::cout << res << std::endl;
return 0;
}