Skip to content

Commit cda5464

Browse files
committed
feat: add solutions to lc problem: No.1658
No.1658.Minimum Operations to Reduce X to Zero
1 parent 80f7848 commit cda5464

File tree

6 files changed

+286
-2
lines changed

6 files changed

+286
-2
lines changed

solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README.md

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,122 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
前缀和 + 哈希表。
53+
54+
题目可以转换为求中间连续子数组的最大长度,使得子数组的和为 `sum(nums) - x`
55+
5256
<!-- tabs:start -->
5357

5458
### **Python3**
5559

5660
<!-- 这里可写当前语言的特殊实现逻辑 -->
5761

5862
```python
59-
63+
class Solution:
64+
def minOperations(self, nums: List[int], x: int) -> int:
65+
x = sum(nums) - x
66+
n = len(nums)
67+
s = 0
68+
seen = {0: -1}
69+
ans = float('inf')
70+
for i, v in enumerate(nums):
71+
s += v
72+
if s not in seen:
73+
seen[s] = i
74+
if s - x in seen:
75+
j = seen[s - x]
76+
ans = min(ans, n - (i - j))
77+
return -1 if ans == float('inf') else ans
6078
```
6179

6280
### **Java**
6381

6482
<!-- 这里可写当前语言的特殊实现逻辑 -->
6583

6684
```java
85+
class Solution {
86+
public int minOperations(int[] nums, int x) {
87+
x = -x;
88+
for (int v : nums) {
89+
x += v;
90+
}
91+
int s = 0;
92+
int n = nums.length;
93+
Map<Integer, Integer> seen = new HashMap<>();
94+
seen.put(0, -1);
95+
int ans = Integer.MAX_VALUE;
96+
for (int i = 0; i < n; ++i) {
97+
s += nums[i];
98+
seen.putIfAbsent(s, i);
99+
if (seen.containsKey(s - x)) {
100+
int j = seen.get(s - x);
101+
ans = Math.min(ans, n - (i - j));
102+
}
103+
}
104+
return ans == Integer.MAX_VALUE ? -1 : ans;
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int minOperations(vector<int>& nums, int x) {
115+
x = -x;
116+
for (int& v : nums) x += v;
117+
int s = 0, n = nums.size();
118+
unordered_map<int, int> seen;
119+
seen[0] = -1;
120+
int ans = INT_MAX;
121+
for (int i = 0; i < n; ++i)
122+
{
123+
s += nums[i];
124+
if (!seen.count(s)) seen[s] = i;
125+
if (seen.count(s - x))
126+
{
127+
int j = seen[s - x];
128+
ans = min(ans, n - (i - j));
129+
}
130+
}
131+
return ans == INT_MAX ? -1 : ans;
132+
}
133+
};
134+
```
67135
136+
### **Go**
137+
138+
```go
139+
func minOperations(nums []int, x int) int {
140+
x = -x
141+
for _, v := range nums {
142+
x += v
143+
}
144+
s, n := 0, len(nums)
145+
seen := map[int]int{0: -1}
146+
ans := math.MaxInt32
147+
for i, v := range nums {
148+
s += v
149+
if _, ok := seen[s]; !ok {
150+
seen[s] = i
151+
}
152+
if j, ok := seen[s-x]; ok {
153+
ans = min(ans, n-(i-j))
154+
}
155+
}
156+
if ans == math.MaxInt32 {
157+
return -1
158+
}
159+
return ans
160+
}
161+
162+
func min(a, b int) int {
163+
if a < b {
164+
return a
165+
}
166+
return b
167+
}
68168
```
69169

70170
### **...**

solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/README_EN.md

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,109 @@
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def minOperations(self, nums: List[int], x: int) -> int:
53+
x = sum(nums) - x
54+
n = len(nums)
55+
s = 0
56+
seen = {0: -1}
57+
ans = float('inf')
58+
for i, v in enumerate(nums):
59+
s += v
60+
if s not in seen:
61+
seen[s] = i
62+
if s - x in seen:
63+
j = seen[s - x]
64+
ans = min(ans, n - (i - j))
65+
return -1 if ans == float('inf') else ans
5266
```
5367

5468
### **Java**
5569

5670
```java
71+
class Solution {
72+
public int minOperations(int[] nums, int x) {
73+
x = -x;
74+
for (int v : nums) {
75+
x += v;
76+
}
77+
int s = 0;
78+
int n = nums.length;
79+
Map<Integer, Integer> seen = new HashMap<>();
80+
seen.put(0, -1);
81+
int ans = Integer.MAX_VALUE;
82+
for (int i = 0; i < n; ++i) {
83+
s += nums[i];
84+
seen.putIfAbsent(s, i);
85+
if (seen.containsKey(s - x)) {
86+
int j = seen.get(s - x);
87+
ans = Math.min(ans, n - (i - j));
88+
}
89+
}
90+
return ans == Integer.MAX_VALUE ? -1 : ans;
91+
}
92+
}
93+
```
94+
95+
### **C++**
96+
97+
```cpp
98+
class Solution {
99+
public:
100+
int minOperations(vector<int>& nums, int x) {
101+
x = -x;
102+
for (int& v : nums) x += v;
103+
int s = 0, n = nums.size();
104+
unordered_map<int, int> seen;
105+
seen[0] = -1;
106+
int ans = INT_MAX;
107+
for (int i = 0; i < n; ++i)
108+
{
109+
s += nums[i];
110+
if (!seen.count(s)) seen[s] = i;
111+
if (seen.count(s - x))
112+
{
113+
int j = seen[s - x];
114+
ans = min(ans, n - (i - j));
115+
}
116+
}
117+
return ans == INT_MAX ? -1 : ans;
118+
}
119+
};
120+
```
57121
122+
### **Go**
123+
124+
```go
125+
func minOperations(nums []int, x int) int {
126+
x = -x
127+
for _, v := range nums {
128+
x += v
129+
}
130+
s, n := 0, len(nums)
131+
seen := map[int]int{0: -1}
132+
ans := math.MaxInt32
133+
for i, v := range nums {
134+
s += v
135+
if _, ok := seen[s]; !ok {
136+
seen[s] = i
137+
}
138+
if j, ok := seen[s-x]; ok {
139+
ans = min(ans, n-(i-j))
140+
}
141+
}
142+
if ans == math.MaxInt32 {
143+
return -1
144+
}
145+
return ans
146+
}
147+
148+
func min(a, b int) int {
149+
if a < b {
150+
return a
151+
}
152+
return b
153+
}
58154
```
59155

60156
### **...**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int minOperations(vector<int>& nums, int x) {
4+
x = -x;
5+
for (int& v : nums) x += v;
6+
int s = 0, n = nums.size();
7+
unordered_map<int, int> seen;
8+
seen[0] = -1;
9+
int ans = INT_MAX;
10+
for (int i = 0; i < n; ++i)
11+
{
12+
s += nums[i];
13+
if (!seen.count(s)) seen[s] = i;
14+
if (seen.count(s - x))
15+
{
16+
int j = seen[s - x];
17+
ans = min(ans, n - (i - j));
18+
}
19+
}
20+
return ans == INT_MAX ? -1 : ans;
21+
}
22+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func minOperations(nums []int, x int) int {
2+
x = -x
3+
for _, v := range nums {
4+
x += v
5+
}
6+
s, n := 0, len(nums)
7+
seen := map[int]int{0: -1}
8+
ans := math.MaxInt32
9+
for i, v := range nums {
10+
s += v
11+
if _, ok := seen[s]; !ok {
12+
seen[s] = i
13+
}
14+
if j, ok := seen[s-x]; ok {
15+
ans = min(ans, n-(i-j))
16+
}
17+
}
18+
if ans == math.MaxInt32 {
19+
return -1
20+
}
21+
return ans
22+
}
23+
24+
func min(a, b int) int {
25+
if a < b {
26+
return a
27+
}
28+
return b
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int minOperations(int[] nums, int x) {
3+
x = -x;
4+
for (int v : nums) {
5+
x += v;
6+
}
7+
int s = 0;
8+
int n = nums.length;
9+
Map<Integer, Integer> seen = new HashMap<>();
10+
seen.put(0, -1);
11+
int ans = Integer.MAX_VALUE;
12+
for (int i = 0; i < n; ++i) {
13+
s += nums[i];
14+
seen.putIfAbsent(s, i);
15+
if (seen.containsKey(s - x)) {
16+
int j = seen.get(s - x);
17+
ans = Math.min(ans, n - (i - j));
18+
}
19+
}
20+
return ans == Integer.MAX_VALUE ? -1 : ans;
21+
}
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def minOperations(self, nums: List[int], x: int) -> int:
3+
x = sum(nums) - x
4+
n = len(nums)
5+
s = 0
6+
seen = {0: -1}
7+
ans = float('inf')
8+
for i, v in enumerate(nums):
9+
s += v
10+
if s not in seen:
11+
seen[s] = i
12+
if s - x in seen:
13+
j = seen[s - x]
14+
ans = min(ans, n - (i - j))
15+
return -1 if ans == float('inf') else ans

0 commit comments

Comments
 (0)