Skip to content

feat: add solutions to lc problem: No.830 #3764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion solution/0800-0899/0830.Positions of Large Groups/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class Solution {
++j;
}
if (j - i >= 3) {
ans.add(Arrays.asList(i, j - 1));
ans.add(List.of(i, j - 1));
}
i = j;
}
Expand Down Expand Up @@ -163,6 +163,28 @@ func largeGroupPositions(s string) [][]int {
}
```

#### TypeScript

```ts
function largeGroupPositions(s: string): number[][] {
const n = s.length;
const ans: number[][] = [];

for (let i = 0; i < n; ) {
let j = i;
while (j < n && s[j] === s[i]) {
++j;
}
if (j - i >= 3) {
ans.push([i, j - 1]);
}
i = j;
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
30 changes: 28 additions & 2 deletions solution/0800-0899/0830.Positions of Large Groups/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Two Pointers

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.

The time complexity is $O(n)$, where $n$ is the length of the string $s$.

<!-- tabs:start -->

Expand Down Expand Up @@ -100,7 +104,7 @@ class Solution {
++j;
}
if (j - i >= 3) {
ans.add(Arrays.asList(i, j - 1));
ans.add(List.of(i, j - 1));
}
i = j;
}
Expand Down Expand Up @@ -153,6 +157,28 @@ func largeGroupPositions(s string) [][]int {
}
```

#### TypeScript

```ts
function largeGroupPositions(s: string): number[][] {
const n = s.length;
const ans: number[][] = [];

for (let i = 0; i < n; ) {
let j = i;
while (j < n && s[j] === s[i]) {
++j;
}
if (j - i >= 3) {
ans.push([i, j - 1]);
}
i = j;
}

return ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public List<List<Integer>> largeGroupPositions(String s) {
++j;
}
if (j - i >= 3) {
ans.add(Arrays.asList(i, j - 1));
ans.add(List.of(i, j - 1));
}
i = j;
}
return ans;
}
}
}
17 changes: 17 additions & 0 deletions solution/0800-0899/0830.Positions of Large Groups/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function largeGroupPositions(s: string): number[][] {
const n = s.length;
const ans: number[][] = [];

for (let i = 0; i < n; ) {
let j = i;
while (j < n && s[j] === s[i]) {
++j;
}
if (j - i >= 3) {
ans.push([i, j - 1]);
}
i = j;
}

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

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

我们将操作次数累加到答案中,最后返回答案即可。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ Next, we discuss the parity of $m$ and $n$:

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:

- 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}$.
- 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}$.
- 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 }$.
- 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}$.
- 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$.

We add the number of operations to the answer and finally return the answer.
Expand Down
Loading