diff --git a/solution/0800-0899/0830.Positions of Large Groups/README.md b/solution/0800-0899/0830.Positions of Large Groups/README.md index 51b8de3282a0c..389aa0187ce17 100644 --- a/solution/0800-0899/0830.Positions of Large Groups/README.md +++ b/solution/0800-0899/0830.Positions of Large Groups/README.md @@ -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; } @@ -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; +} +``` + diff --git a/solution/0800-0899/0830.Positions of Large Groups/README_EN.md b/solution/0800-0899/0830.Positions of Large Groups/README_EN.md index 34473b9fdbac5..7f6c880c414fd 100644 --- a/solution/0800-0899/0830.Positions of Large Groups/README_EN.md +++ b/solution/0800-0899/0830.Positions of Large Groups/README_EN.md @@ -65,7 +65,11 @@ tags: -### 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$. @@ -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; } @@ -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; +} +``` + diff --git a/solution/0800-0899/0830.Positions of Large Groups/Solution.java b/solution/0800-0899/0830.Positions of Large Groups/Solution.java index 07451deabe1a3..72135bef15d35 100644 --- a/solution/0800-0899/0830.Positions of Large Groups/Solution.java +++ b/solution/0800-0899/0830.Positions of Large Groups/Solution.java @@ -9,10 +9,10 @@ public List> 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; } -} \ No newline at end of file +} diff --git a/solution/0800-0899/0830.Positions of Large Groups/Solution.ts b/solution/0800-0899/0830.Positions of Large Groups/Solution.ts new file mode 100644 index 0000000000000..5fdaeb9ca7816 --- /dev/null +++ b/solution/0800-0899/0830.Positions of Large Groups/Solution.ts @@ -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; +} diff --git a/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README.md b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README.md index 67edbe2fc8b12..6b71721997a17 100644 --- a/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README.md +++ b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README.md @@ -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}$。 我们将操作次数累加到答案中,最后返回答案即可。 diff --git a/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README_EN.md b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README_EN.md index 93c7473258aa7..cd3632d672823 100644 --- a/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README_EN.md +++ b/solution/3200-3299/3240.Minimum Number of Flips to Make Binary Grid Palindromic II/README_EN.md @@ -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.