Skip to content

Commit a403b6b

Browse files
committed
feat: add solutions to lc problem: No.1695
No.1695.Maximum Erasure Value
1 parent 34ee939 commit a403b6b

File tree

10 files changed

+248
-32
lines changed

10 files changed

+248
-32
lines changed

solution/1600-1699/1695.Maximum Erasure Value/README.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,108 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:数组或哈希表 + 前缀和**
47+
48+
我们用数组或哈希表 $d$ 记录每个数字最后一次出现的位置,用 $s$ 记录前缀和,用 $j$ 记录当前不重复子数组的左端点。
49+
50+
遍历数组,对于每个数字 $v$,如果 $d[v]$ 存在,那么我们更新 $j$ 为 $max(j, d[v])$,这样就保证了当前不重复子数组不包含 $v$,然后更新答案为 $max(ans, s[i] - s[j])$,最后更新 $d[v]$ 为 $i$。
51+
52+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
53+
4654
<!-- tabs:start -->
4755

4856
### **Python3**
4957

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

5260
```python
53-
61+
class Solution:
62+
def maximumUniqueSubarray(self, nums: List[int]) -> int:
63+
d = defaultdict(int)
64+
s = list(accumulate(nums, initial=0))
65+
ans = j = 0
66+
for i, v in enumerate(nums, 1):
67+
j = max(j, d[v])
68+
ans = max(ans, s[i] - s[j])
69+
d[v] = i
70+
return ans
5471
```
5572

5673
### **Java**
5774

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

6077
```java
78+
class Solution {
79+
public int maximumUniqueSubarray(int[] nums) {
80+
int[] d = new int[10001];
81+
int n = nums.length;
82+
int[] s = new int[n + 1];
83+
for (int i = 0; i < n; ++i) {
84+
s[i + 1] = s[i] + nums[i];
85+
}
86+
int ans = 0, j = 0;
87+
for (int i = 1; i <= n; ++i) {
88+
int v = nums[i - 1];
89+
j = Math.max(j, d[v]);
90+
ans = Math.max(ans, s[i] - s[j]);
91+
d[v] = i;
92+
}
93+
return ans;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
int maximumUniqueSubarray(vector<int>& nums) {
104+
int d[10001]{};
105+
int n = nums.size();
106+
int s[n + 1];
107+
s[0] = 0;
108+
for (int i = 0; i < n; ++i) {
109+
s[i + 1] = s[i] + nums[i];
110+
}
111+
int ans = 0, j = 0;
112+
for (int i = 1; i <= n; ++i) {
113+
int v = nums[i - 1];
114+
j = max(j, d[v]);
115+
ans = max(ans, s[i] - s[j]);
116+
d[v] = i;
117+
}
118+
return ans;
119+
}
120+
};
121+
```
61122
123+
### **Go**
124+
125+
```go
126+
func maximumUniqueSubarray(nums []int) (ans int) {
127+
d := [10001]int{}
128+
n := len(nums)
129+
s := make([]int, n+1)
130+
for i, v := range nums {
131+
s[i+1] = s[i] + v
132+
}
133+
for i, j := 1, 0; i <= n; i++ {
134+
v := nums[i-1]
135+
j = max(j, d[v])
136+
ans = max(ans, s[i]-s[j])
137+
d[v] = i
138+
}
139+
return
140+
}
141+
142+
func max(a, b int) int {
143+
if a > b {
144+
return a
145+
}
146+
return b
147+
}
62148
```
63149

64150
### **...**

solution/1600-1699/1695.Maximum Erasure Value/README_EN.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,91 @@
4242
### **Python3**
4343

4444
```python
45-
45+
class Solution:
46+
def maximumUniqueSubarray(self, nums: List[int]) -> int:
47+
d = defaultdict(int)
48+
s = list(accumulate(nums, initial=0))
49+
ans = j = 0
50+
for i, v in enumerate(nums, 1):
51+
j = max(j, d[v])
52+
ans = max(ans, s[i] - s[j])
53+
d[v] = i
54+
return ans
4655
```
4756

4857
### **Java**
4958

5059
```java
60+
class Solution {
61+
public int maximumUniqueSubarray(int[] nums) {
62+
int[] d = new int[10001];
63+
int n = nums.length;
64+
int[] s = new int[n + 1];
65+
for (int i = 0; i < n; ++i) {
66+
s[i + 1] = s[i] + nums[i];
67+
}
68+
int ans = 0, j = 0;
69+
for (int i = 1; i <= n; ++i) {
70+
int v = nums[i - 1];
71+
j = Math.max(j, d[v]);
72+
ans = Math.max(ans, s[i] - s[j]);
73+
d[v] = i;
74+
}
75+
return ans;
76+
}
77+
}
78+
```
79+
80+
### **C++**
81+
82+
```cpp
83+
class Solution {
84+
public:
85+
int maximumUniqueSubarray(vector<int>& nums) {
86+
int d[10001]{};
87+
int n = nums.size();
88+
int s[n + 1];
89+
s[0] = 0;
90+
for (int i = 0; i < n; ++i) {
91+
s[i + 1] = s[i] + nums[i];
92+
}
93+
int ans = 0, j = 0;
94+
for (int i = 1; i <= n; ++i) {
95+
int v = nums[i - 1];
96+
j = max(j, d[v]);
97+
ans = max(ans, s[i] - s[j]);
98+
d[v] = i;
99+
}
100+
return ans;
101+
}
102+
};
103+
```
51104
105+
### **Go**
106+
107+
```go
108+
func maximumUniqueSubarray(nums []int) (ans int) {
109+
d := [10001]int{}
110+
n := len(nums)
111+
s := make([]int, n+1)
112+
for i, v := range nums {
113+
s[i+1] = s[i] + v
114+
}
115+
for i, j := 1, 0; i <= n; i++ {
116+
v := nums[i-1]
117+
j = max(j, d[v])
118+
ans = max(ans, s[i]-s[j])
119+
d[v] = i
120+
}
121+
return
122+
}
123+
124+
func max(a, b int) int {
125+
if a > b {
126+
return a
127+
}
128+
return b
129+
}
52130
```
53131

54132
### **...**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int maximumUniqueSubarray(vector<int>& nums) {
4+
int d[10001]{};
5+
int n = nums.size();
6+
int s[n + 1];
7+
s[0] = 0;
8+
for (int i = 0; i < n; ++i) {
9+
s[i + 1] = s[i] + nums[i];
10+
}
11+
int ans = 0, j = 0;
12+
for (int i = 1; i <= n; ++i) {
13+
int v = nums[i - 1];
14+
j = max(j, d[v]);
15+
ans = max(ans, s[i] - s[j]);
16+
d[v] = i;
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func maximumUniqueSubarray(nums []int) (ans int) {
2+
d := [10001]int{}
3+
n := len(nums)
4+
s := make([]int, n+1)
5+
for i, v := range nums {
6+
s[i+1] = s[i] + v
7+
}
8+
for i, j := 1, 0; i <= n; i++ {
9+
v := nums[i-1]
10+
j = max(j, d[v])
11+
ans = max(ans, s[i]-s[j])
12+
d[v] = i
13+
}
14+
return
15+
}
16+
17+
func max(a, b int) int {
18+
if a > b {
19+
return a
20+
}
21+
return b
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int maximumUniqueSubarray(int[] nums) {
3+
int[] d = new int[10001];
4+
int n = nums.length;
5+
int[] s = new int[n + 1];
6+
for (int i = 0; i < n; ++i) {
7+
s[i + 1] = s[i] + nums[i];
8+
}
9+
int ans = 0, j = 0;
10+
for (int i = 1; i <= n; ++i) {
11+
int v = nums[i - 1];
12+
j = Math.max(j, d[v]);
13+
ans = Math.max(ans, s[i] - s[j]);
14+
d[v] = i;
15+
}
16+
return ans;
17+
}
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def maximumUniqueSubarray(self, nums: List[int]) -> int:
3+
d = defaultdict(int)
4+
s = list(accumulate(nums, initial=0))
5+
ans = j = 0
6+
for i, v in enumerate(nums, 1):
7+
j = max(j, d[v])
8+
ans = max(ans, s[i] - s[j])
9+
d[v] = i
10+
return ans

solution/1800-1899/1801.Number of Orders in the Backlog/README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ class Solution:
119119

120120
```java
121121
class Solution {
122-
private static final int MOD = (int) 1e9 + 7;
123-
124122
public int getNumberOfBacklogOrders(int[][] orders) {
125123
PriorityQueue<int[]> buy = new PriorityQueue<>((a, b) -> b[0] - a[0]);
126124
PriorityQueue<int[]> sell = new PriorityQueue<>((a, b) -> a[0] - b[0]);
@@ -157,15 +155,14 @@ class Solution {
157155
}
158156
}
159157
long ans = 0;
158+
final int mod = (int) 1e9 + 7;
160159
while (!buy.isEmpty()) {
161160
ans += buy.poll()[1];
162-
ans %= MOD;
163161
}
164162
while (!sell.isEmpty()) {
165163
ans += sell.poll()[1];
166-
ans %= MOD;
167164
}
168-
return (int) ans;
165+
return (int) (ans % mod);
169166
}
170167
}
171168
```
@@ -175,8 +172,6 @@ class Solution {
175172
```cpp
176173
class Solution {
177174
public:
178-
const int mod = 1e9 + 7;
179-
180175
int getNumberOfBacklogOrders(vector<vector<int>>& orders) {
181176
using pii = pair<int, int>;
182177
priority_queue<pii, vector<pii>, greater<pii>> sell;
@@ -216,15 +211,14 @@ public:
216211
long ans = 0;
217212
while (!buy.empty()) {
218213
ans += buy.top().second;
219-
ans %= mod;
220214
buy.pop();
221215
}
222216
while (!sell.empty()) {
223217
ans += sell.top().second;
224-
ans %= mod;
225218
sell.pop();
226219
}
227-
return ans;
220+
const int mod = 1e9 + 7;
221+
return ans % mod;
228222
}
229223
};
230224
```

solution/1800-1899/1801.Number of Orders in the Backlog/README_EN.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,6 @@ class Solution:
126126

127127
```java
128128
class Solution {
129-
private static final int MOD = (int) 1e9 + 7;
130-
131129
public int getNumberOfBacklogOrders(int[][] orders) {
132130
PriorityQueue<int[]> buy = new PriorityQueue<>((a, b) -> b[0] - a[0]);
133131
PriorityQueue<int[]> sell = new PriorityQueue<>((a, b) -> a[0] - b[0]);
@@ -164,15 +162,14 @@ class Solution {
164162
}
165163
}
166164
long ans = 0;
165+
final int mod = (int) 1e9 + 7;
167166
while (!buy.isEmpty()) {
168167
ans += buy.poll()[1];
169-
ans %= MOD;
170168
}
171169
while (!sell.isEmpty()) {
172170
ans += sell.poll()[1];
173-
ans %= MOD;
174171
}
175-
return (int) ans;
172+
return (int) (ans % mod);
176173
}
177174
}
178175
```
@@ -182,8 +179,6 @@ class Solution {
182179
```cpp
183180
class Solution {
184181
public:
185-
const int mod = 1e9 + 7;
186-
187182
int getNumberOfBacklogOrders(vector<vector<int>>& orders) {
188183
using pii = pair<int, int>;
189184
priority_queue<pii, vector<pii>, greater<pii>> sell;
@@ -223,15 +218,14 @@ public:
223218
long ans = 0;
224219
while (!buy.empty()) {
225220
ans += buy.top().second;
226-
ans %= mod;
227221
buy.pop();
228222
}
229223
while (!sell.empty()) {
230224
ans += sell.top().second;
231-
ans %= mod;
232225
sell.pop();
233226
}
234-
return ans;
227+
const int mod = 1e9 + 7;
228+
return ans % mod;
235229
}
236230
};
237231
```

0 commit comments

Comments
 (0)