Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 931aaf5

Browse files
authored
feat: add solutions to lc problem: No.830 (doocs#3764)
1 parent 5e1e43e commit 931aaf5

File tree

6 files changed

+73
-8
lines changed

6 files changed

+73
-8
lines changed

solution/0800-0899/0830.Positions of Large Groups/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class Solution {
110110
++j;
111111
}
112112
if (j - i >= 3) {
113-
ans.add(Arrays.asList(i, j - 1));
113+
ans.add(List.of(i, j - 1));
114114
}
115115
i = j;
116116
}
@@ -163,6 +163,28 @@ func largeGroupPositions(s string) [][]int {
163163
}
164164
```
165165

166+
#### TypeScript
167+
168+
```ts
169+
function largeGroupPositions(s: string): number[][] {
170+
const n = s.length;
171+
const ans: number[][] = [];
172+
173+
for (let i = 0; i < n; ) {
174+
let j = i;
175+
while (j < n && s[j] === s[i]) {
176+
++j;
177+
}
178+
if (j - i >= 3) {
179+
ans.push([i, j - 1]);
180+
}
181+
i = j;
182+
}
183+
184+
return ans;
185+
}
186+
```
187+
166188
<!-- tabs:end -->
167189

168190
<!-- solution:end -->

solution/0800-0899/0830.Positions of Large Groups/README_EN.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Two Pointers
69+
70+
We use two pointers $i$ and $j$ to find the start and end positions of each group, then check if the group length is greater than or equal to $3$. If so, we add it to the result array.
71+
72+
The time complexity is $O(n)$, where $n$ is the length of the string $s$.
6973

7074
<!-- tabs:start -->
7175

@@ -100,7 +104,7 @@ class Solution {
100104
++j;
101105
}
102106
if (j - i >= 3) {
103-
ans.add(Arrays.asList(i, j - 1));
107+
ans.add(List.of(i, j - 1));
104108
}
105109
i = j;
106110
}
@@ -153,6 +157,28 @@ func largeGroupPositions(s string) [][]int {
153157
}
154158
```
155159

160+
#### TypeScript
161+
162+
```ts
163+
function largeGroupPositions(s: string): number[][] {
164+
const n = s.length;
165+
const ans: number[][] = [];
166+
167+
for (let i = 0; i < n; ) {
168+
let j = i;
169+
while (j < n && s[j] === s[i]) {
170+
++j;
171+
}
172+
if (j - i >= 3) {
173+
ans.push([i, j - 1]);
174+
}
175+
i = j;
176+
}
177+
178+
return ans;
179+
}
180+
```
181+
156182
<!-- tabs:end -->
157183

158184
<!-- solution:end -->

solution/0800-0899/0830.Positions of Large Groups/Solution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public List<List<Integer>> largeGroupPositions(String s) {
99
++j;
1010
}
1111
if (j - i >= 3) {
12-
ans.add(Arrays.asList(i, j - 1));
12+
ans.add(List.of(i, j - 1));
1313
}
1414
i = j;
1515
}
1616
return ans;
1717
}
18-
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function largeGroupPositions(s: string): number[][] {
2+
const n = s.length;
3+
const ans: number[][] = [];
4+
5+
for (let i = 0; i < n; ) {
6+
let j = i;
7+
while (j < n && s[j] === s[i]) {
8+
++j;
9+
}
10+
if (j - i >= 3) {
11+
ans.push([i, j - 1]);
12+
}
13+
i = j;
14+
}
15+
16+
return ans;
17+
}

solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ tags:
9797
对于后两种情况,我们需要统计最中间的一行或一列中对应位置不相同的格子对数 $\text{diff}$,以及对应位置相同且为 $1$ 的格子个数 $\text{cnt1}$,然后再分情况讨论:
9898

9999
- 如果 $\text{cnt1} \bmod 4 = 0$,那么我们只需要将 $\text{diff}$ 个格子变成 $0$ 即可,操作次数为 $\text{diff}$;
100-
- 否则,说明 $\text{cnt1} = 2$,此时如果 $\text{diff} \lt 0$,我们可以将其中一个格子变成 $1$,使得 $\text{cnt1} = 4$,那么剩下的 $\text{diff} - 1$ 个格子变成 $0$ 即可,操作次数一共为 $\text{diff}$。
100+
- 否则,说明 $\text{cnt1} = 2$,此时如果 $\text{diff} \gt 0$,我们可以将其中一个格子变成 $1$,使得 $\text{cnt1} = 4$,那么剩下的 $\text{diff} - 1$ 个格子变成 $0$ 即可,操作次数一共为 $\text{diff}$。
101101
- 否则,如果 $\text{diff} = 0$,我们就把 $\text{2}$ 个格子变成 $0$,使得 $\text{cnt1} \bmod 4 = 0$,操作次数为 $\text{2}$。
102102

103103
我们将操作次数累加到答案中,最后返回答案即可。

solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ Next, we discuss the parity of $m$ and $n$:
9494

9595
For the latter two cases, we need to count the number of differing pairs of cells $\text{diff}$ in the middle row or column, and the number of cells that are the same and equal to $1$ $\text{cnt1}$. Then we discuss the following cases:
9696

97-
- If $\text{cnt1} \bmod 4 = 0$, we only need to change the $\text{diff}$ cells to $0$, and the number of operations is $\text{diff}$.
98-
- Otherwise, if $\text{cnt1} = 2$, if $\text{diff} \lt 0$, we can change one of the cells to $1$ to make $\text{cnt1} = 4$, and then change the remaining $\text{diff} - 1$ cells to $0$. The total number of operations is $\text{diff}$.
97+
- If $\text{cnt1} \bmod 4 = 0$, we only need to change the $\text{diff}$ cells to $0$, and the number of operations is $\text{diff }$.
98+
- Otherwise, if $\text{cnt1} = 2$, if $\text{diff} \gt 0$, we can change one of the cells to $1$ to make $\text{cnt1} = 4$, and then change the remaining $\text{diff} - 1$ cells to $0$. The total number of operations is $\text{diff}$.
9999
- Otherwise, if $\text{diff} = 0$, we change the $2$ cells to $0$ to make $\text{cnt1} \bmod 4 = 0$, and the number of operations is $2$.
100100

101101
We add the number of operations to the answer and finally return the answer.

0 commit comments

Comments
 (0)