Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1fc0cbd

Browse files
committed
feat: add solutions to lc problem: No.2272
No.2272.Substring With Largest Variance
1 parent 624501e commit 1fc0cbd

File tree

11 files changed

+343
-38
lines changed

11 files changed

+343
-38
lines changed

‎solution/0900-0999/0934.Shortest Bridge/README.md‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,21 @@ class Solution:
7878
def dfs(i, j):
7979
q.append((i, j))
8080
grid[i][j] = 2
81-
for a, b in dirs:
81+
for a, b in pairwise(dirs):
8282
x, y = i + a, j + b
8383
if 0 <= x < n and 0 <= y < n and grid[x][y] == 1:
8484
dfs(x, y)
8585

8686
n = len(grid)
87-
dirs = ((0, 1), (0, -1), (1, 0), (-1, 0))
87+
dirs = (-1, 0, 1, 0, -1)
8888
q = deque()
8989
i, j = next((i, j) for i in range(n) for j in range(n) if grid[i][j])
9090
dfs(i, j)
9191
ans = 0
9292
while 1:
9393
for _ in range(len(q)):
9494
i, j = q.popleft()
95-
for a, b in dirs:
95+
for a, b in pairwise(dirs):
9696
x, y = i + a, j + b
9797
if 0 <= x < n and 0 <= y < n:
9898
if grid[x][y] == 1:
@@ -163,15 +163,13 @@ class Solution {
163163
### **C++**
164164

165165
```cpp
166-
using pii = pair<int, int>;
167-
168166
class Solution {
169167
public:
170168
const static inline vector<int> dirs = {-1, 0, 1, 0, -1};
171169

172170
int shortestBridge(vector<vector<int>>& grid) {
173171
int n = grid.size();
174-
queue<pii> q;
172+
queue<pair<int, int>> q;
175173
function<void(int, int)> dfs = [&](int i, int j) {
176174
grid[i][j] = 2;
177175
q.emplace(i, j);
@@ -193,11 +191,11 @@ public:
193191
}
194192
int ans = 0;
195193
while (1) {
196-
for (int k = q.size(); k; --k) {
194+
for (int h = q.size(); h; --h) {
197195
auto [i, j] = q.front();
198196
q.pop();
199-
for (int h = 0; h < 4; ++h) {
200-
int x = i + dirs[h], y = j + dirs[h + 1];
197+
for (int k = 0; k < 4; ++k) {
198+
int x = i + dirs[k], y = j + dirs[k + 1];
201199
if (x >= 0 && x < n && y >= 0 && y < n) {
202200
if (grid[x][y] == 1) return ans;
203201
if (grid[x][y] == 0) {
@@ -216,7 +214,7 @@ public:
216214
### **Go**
217215

218216
```go
219-
func shortestBridge(grid [][]int) int {
217+
func shortestBridge(grid [][]int) (ansint) {
220218
n := len(grid)
221219
dirs := []int{-1, 0, 1, 0, -1}
222220
type pair struct{ i, j int }
@@ -241,7 +239,6 @@ func shortestBridge(grid [][]int) int {
241239
}
242240
}
243241
}
244-
ans := 0
245242
for {
246243
for i := len(q); i > 0; i-- {
247244
p := q[0]
@@ -250,7 +247,7 @@ func shortestBridge(grid [][]int) int {
250247
x, y := p.i+dirs[k], p.j+dirs[k+1]
251248
if x >= 0 && x < n && y >= 0 && y < n {
252249
if grid[x][y] == 1 {
253-
return ans
250+
return
254251
}
255252
if grid[x][y] == 0 {
256253
grid[x][y] = 2

‎solution/0900-0999/0934.Shortest Bridge/README_EN.md‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,21 @@ class Solution:
5858
def dfs(i, j):
5959
q.append((i, j))
6060
grid[i][j] = 2
61-
for a, b in dirs:
61+
for a, b in pairwise(dirs):
6262
x, y = i + a, j + b
6363
if 0 <= x < n and 0 <= y < n and grid[x][y] == 1:
6464
dfs(x, y)
6565

6666
n = len(grid)
67-
dirs = ((0, 1), (0, -1), (1, 0), (-1, 0))
67+
dirs = (-1, 0, 1, 0, -1)
6868
q = deque()
6969
i, j = next((i, j) for i in range(n) for j in range(n) if grid[i][j])
7070
dfs(i, j)
7171
ans = 0
7272
while 1:
7373
for _ in range(len(q)):
7474
i, j = q.popleft()
75-
for a, b in dirs:
75+
for a, b in pairwise(dirs):
7676
x, y = i + a, j + b
7777
if 0 <= x < n and 0 <= y < n:
7878
if grid[x][y] == 1:
@@ -141,15 +141,13 @@ class Solution {
141141
### **C++**
142142

143143
```cpp
144-
using pii = pair<int, int>;
145-
146144
class Solution {
147145
public:
148146
const static inline vector<int> dirs = {-1, 0, 1, 0, -1};
149147

150148
int shortestBridge(vector<vector<int>>& grid) {
151149
int n = grid.size();
152-
queue<pii> q;
150+
queue<pair<int, int>> q;
153151
function<void(int, int)> dfs = [&](int i, int j) {
154152
grid[i][j] = 2;
155153
q.emplace(i, j);
@@ -171,11 +169,11 @@ public:
171169
}
172170
int ans = 0;
173171
while (1) {
174-
for (int k = q.size(); k; --k) {
172+
for (int h = q.size(); h; --h) {
175173
auto [i, j] = q.front();
176174
q.pop();
177-
for (int h = 0; h < 4; ++h) {
178-
int x = i + dirs[h], y = j + dirs[h + 1];
175+
for (int k = 0; k < 4; ++k) {
176+
int x = i + dirs[k], y = j + dirs[k + 1];
179177
if (x >= 0 && x < n && y >= 0 && y < n) {
180178
if (grid[x][y] == 1) return ans;
181179
if (grid[x][y] == 0) {
@@ -194,7 +192,7 @@ public:
194192
### **Go**
195193

196194
```go
197-
func shortestBridge(grid [][]int) int {
195+
func shortestBridge(grid [][]int) (ansint) {
198196
n := len(grid)
199197
dirs := []int{-1, 0, 1, 0, -1}
200198
type pair struct{ i, j int }
@@ -219,7 +217,6 @@ func shortestBridge(grid [][]int) int {
219217
}
220218
}
221219
}
222-
ans := 0
223220
for {
224221
for i := len(q); i > 0; i-- {
225222
p := q[0]
@@ -228,7 +225,7 @@ func shortestBridge(grid [][]int) int {
228225
x, y := p.i+dirs[k], p.j+dirs[k+1]
229226
if x >= 0 && x < n && y >= 0 && y < n {
230227
if grid[x][y] == 1 {
231-
return ans
228+
return
232229
}
233230
if grid[x][y] == 0 {
234231
grid[x][y] = 2

‎solution/0900-0999/0934.Shortest Bridge/Solution.cpp‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
using pii = pair<int, int>;
2-
31
class Solution {
42
public:
53
const static inline vector<int> dirs = {-1, 0, 1, 0, -1};
64

75
int shortestBridge(vector<vector<int>>& grid) {
86
int n = grid.size();
9-
queue<pii> q;
7+
queue<pair<int, int>> q;
108
function<void(int, int)> dfs = [&](int i, int j) {
119
grid[i][j] = 2;
1210
q.emplace(i, j);
@@ -28,11 +26,11 @@ class Solution {
2826
}
2927
int ans = 0;
3028
while (1) {
31-
for (int k = q.size(); k; --k) {
29+
for (int h = q.size(); h; --h) {
3230
auto [i, j] = q.front();
3331
q.pop();
34-
for (int h = 0; h < 4; ++h) {
35-
int x = i + dirs[h], y = j + dirs[h + 1];
32+
for (int k = 0; k < 4; ++k) {
33+
int x = i + dirs[k], y = j + dirs[k + 1];
3634
if (x >= 0 && x < n && y >= 0 && y < n) {
3735
if (grid[x][y] == 1) return ans;
3836
if (grid[x][y] == 0) {

‎solution/0900-0999/0934.Shortest Bridge/Solution.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func shortestBridge(grid [][]int) int {
1+
func shortestBridge(grid [][]int) (ansint) {
22
n := len(grid)
33
dirs := []int{-1, 0, 1, 0, -1}
44
type pair struct{ i, j int }
@@ -23,7 +23,6 @@ func shortestBridge(grid [][]int) int {
2323
}
2424
}
2525
}
26-
ans := 0
2726
for {
2827
for i := len(q); i > 0; i-- {
2928
p := q[0]
@@ -32,7 +31,7 @@ func shortestBridge(grid [][]int) int {
3231
x, y := p.i+dirs[k], p.j+dirs[k+1]
3332
if x >= 0 && x < n && y >= 0 && y < n {
3433
if grid[x][y] == 1 {
35-
returnans
34+
return
3635
}
3736
if grid[x][y] == 0 {
3837
grid[x][y] = 2

‎solution/0900-0999/0934.Shortest Bridge/Solution.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ def shortestBridge(self, grid: List[List[int]]) -> int:
33
def dfs(i, j):
44
q.append((i, j))
55
grid[i][j] = 2
6-
for a, b in dirs:
6+
for a, b in pairwise(dirs):
77
x, y = i + a, j + b
88
if 0 <= x < n and 0 <= y < n and grid[x][y] == 1:
99
dfs(x, y)
1010

1111
n = len(grid)
12-
dirs = ((0, 1), (0, -1), (1, 0), (-1, 0))
12+
dirs = (-1, 0, 1, 0, -1)
1313
q = deque()
1414
i, j = next((i, j) for i in range(n) for j in range(n) if grid[i][j])
1515
dfs(i, j)
1616
ans = 0
1717
while 1:
1818
for _ in range(len(q)):
1919
i, j = q.popleft()
20-
for a, b in dirs:
20+
for a, b in pairwise(dirs):
2121
x, y = i + a, j + b
2222
if 0 <= x < n and 0 <= y < n:
2323
if grid[x][y] == 1:

‎solution/2200-2299/2272.Substring With Largest Variance/README.md‎

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,140 @@ s 中没有字母出现超过 1 次,所以 s 中每个子字符串的波动值
5050

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

53+
**方法一:枚举 + 动态规划**
54+
55+
由于字符集只包含小写字母,我们可以考虑枚举出现次数最多的字符 $a$ 以及出现次数最少的字符 $b$。对于一个子串来说,这两种字符出现的次数之差就是子串的波动值。
56+
57+
具体实现上,我们使用双重循环枚举 $a$ 和 $b,ドル用 $f[0]$ 记录以当前字符结尾的连续出现字符 $a$ 的个数,用 $f[1]$ 记录以当前字符结尾的,并且包含 $a$ 和 $b$ 的子串的波动值。迭代取 $f[1]$ 的最大值即可。
58+
59+
递推公式如下:
60+
61+
1. 如果当前字符为 $a,ドル则 $f[0]$ 和 $f[1]$ 都加 1ドル$;
62+
1. 如果当前字符为 $b,ドル则 $f[1]=\max(f[1]-1, f[0]-1),ドル而 $f[0]=0$;
63+
1. 否则,无需考虑。
64+
65+
注意,初始时将 $f[1]$ 赋值为一个负数最大值,可以保证更新答案时是合法的。
66+
67+
时间复杂度 $O(n\times C^2),ドル其中 $n$ 表示字符串 $s$ 的长度,而 $C$ 为字符集大小,本题中 $C=26$。
68+
5369
<!-- tabs:start -->
5470

5571
### **Python3**
5672

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

5975
```python
60-
76+
class Solution:
77+
def largestVariance(self, s: str) -> int:
78+
ans = 0
79+
for a, b in permutations(ascii_lowercase, 2):
80+
if a == b:
81+
continue
82+
f = [0, -inf]
83+
for c in s:
84+
if c == a:
85+
f[0], f[1] = f[0] + 1, f[1] + 1
86+
elif c == b:
87+
f[1] = max(f[1] - 1, f[0] - 1)
88+
f[0] = 0
89+
if ans < f[1]:
90+
ans = f[1]
91+
return ans
6192
```
6293

6394
### **Java**
6495

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

6798
```java
99+
class Solution {
100+
public int largestVariance(String s) {
101+
int n = s.length();
102+
int ans = 0;
103+
for (char a = 'a'; a <= 'z'; ++a) {
104+
for (char b = 'a'; b <= 'z'; ++b) {
105+
if (a == b) {
106+
continue;
107+
}
108+
int[] f = new int[] {0, -n};
109+
for (int i = 0; i < n; ++i) {
110+
if (s.charAt(i) == a) {
111+
f[0]++;
112+
f[1]++;
113+
} else if (s.charAt(i) == b) {
114+
f[1] = Math.max(f[0] - 1, f[1] - 1);
115+
f[0] = 0;
116+
}
117+
ans = Math.max(ans, f[1]);
118+
}
119+
}
120+
}
121+
return ans;
122+
}
123+
}
124+
```
125+
126+
### **C++**
127+
128+
```cpp
129+
class Solution {
130+
public:
131+
int largestVariance(string s) {
132+
int n = s.size();
133+
int ans = 0;
134+
for (char a = 'a'; a <= 'z'; ++a) {
135+
for (char b = 'a'; b <= 'z'; ++b) {
136+
if (a == b) continue;
137+
int f[2] = {0, -n};
138+
for (char c : s) {
139+
if (c == a) {
140+
f[0]++;
141+
f[1]++;
142+
} else if (c == b) {
143+
f[1] = max(f[1] - 1, f[0] - 1);
144+
f[0] = 0;
145+
}
146+
ans = max(ans, f[1]);
147+
}
148+
}
149+
}
150+
return ans;
151+
}
152+
};
153+
```
68154
155+
### **Go**
156+
157+
```go
158+
func largestVariance(s string) int {
159+
ans, n := 0, len(s)
160+
for a := 'a'; a <= 'z'; a++ {
161+
for b := 'a'; b <= 'z'; b++ {
162+
if a == b {
163+
continue
164+
}
165+
f := [2]int{0, -n}
166+
for _, c := range s {
167+
if c == a {
168+
f[0]++
169+
f[1]++
170+
} else if c == b {
171+
f[1] = max(f[1]-1, f[0]-1)
172+
f[0] = 0
173+
}
174+
ans = max(ans, f[1])
175+
}
176+
}
177+
}
178+
return ans
179+
}
180+
181+
func max(a, b int) int {
182+
if a > b {
183+
return a
184+
}
185+
return b
186+
}
69187
```
70188

71189
### **TypeScript**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /