Skip to content

feat: update lc problems #3556

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
Sep 24, 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
29 changes: 29 additions & 0 deletions solution/1200-1299/1246.Palindrome Removal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,35 @@ func minimumMoves(arr []int) int {
}
```

#### TypeScript

```ts
function minimumMoves(arr: number[]): number {
const n = arr.length;
const f: number[][] = Array.from({ length: n }, () => Array(n).fill(0));

for (let i = 0; i < n; ++i) {
f[i][i] = 1;
}

for (let i = n - 2; i >= 0; --i) {
for (let j = i + 1; j < n; ++j) {
if (i + 1 === j) {
f[i][j] = arr[i] === arr[j] ? 1 : 2;
} else {
let t = arr[i] === arr[j] ? f[i + 1][j - 1] : Infinity;
for (let k = i; k < j; ++k) {
t = Math.min(t, f[i][k] + f[k + 1][j]);
}
f[i][j] = t;
}
}
}

return f[0][n - 1];
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
29 changes: 29 additions & 0 deletions solution/1200-1299/1246.Palindrome Removal/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,35 @@ func minimumMoves(arr []int) int {
}
```

#### TypeScript

```ts
function minimumMoves(arr: number[]): number {
const n = arr.length;
const f: number[][] = Array.from({ length: n }, () => Array(n).fill(0));

for (let i = 0; i < n; ++i) {
f[i][i] = 1;
}

for (let i = n - 2; i >= 0; --i) {
for (let j = i + 1; j < n; ++j) {
if (i + 1 === j) {
f[i][j] = arr[i] === arr[j] ? 1 : 2;
} else {
let t = arr[i] === arr[j] ? f[i + 1][j - 1] : Infinity;
for (let k = i; k < j; ++k) {
t = Math.min(t, f[i][k] + f[k + 1][j]);
}
f[i][j] = t;
}
}
}

return f[0][n - 1];
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
24 changes: 24 additions & 0 deletions solution/1200-1299/1246.Palindrome Removal/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function minimumMoves(arr: number[]): number {
const n = arr.length;
const f: number[][] = Array.from({ length: n }, () => Array(n).fill(0));

for (let i = 0; i < n; ++i) {
f[i][i] = 1;
}

for (let i = n - 2; i >= 0; --i) {
for (let j = i + 1; j < n; ++j) {
if (i + 1 === j) {
f[i][j] = arr[i] === arr[j] ? 1 : 2;
} else {
let t = arr[i] === arr[j] ? f[i + 1][j - 1] : Infinity;
for (let k = i; k < j; ++k) {
t = Math.min(t, f[i][k] + f[k + 1][j]);
}
f[i][j] = t;
}
}
}

return f[0][n - 1];
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ tags:

### 方法一:贪心

根据题目描述,两个字符串 $s1$ 和 $s2$ 都只包含字符 $x$ 和 $y$,且长度相同,因此可以将 $s1$ 和 $s2$ 中的字符一一对应起来,即 $s1[i]$ 和 $s2[i]$。
根据题目描述,两个字符串 $s_1$ 和 $s_2$ 都只包含字符 $x$ 和 $y$,且长度相同,因此可以将 $s_1$ 和 $s_2$ 中的字符一一对应起来,即 $s_1[i]$ 和 $s_2[i]$。

如果 $s1[i] = s2[i]$,则不需要交换,直接跳过即可。如果 $s1[i] \neq s2[i]$,则需要交换,我们统计 $s1[i]$ 和 $s2[i]$ 的组合情况,即 $s1[i] = x$ 且 $s2[i] = y$ 的情况,记为 $xy$,对于 $s1[i] = y$ 且 $s2[i] = x$ 的情况,记为 $yx$。
如果 $s_1[i] = s_2[i]$,则不需要交换,直接跳过即可。如果 $s_1[i] \neq s_2[i]$,则需要交换,我们统计 $s_1[i]$ 和 $s_2[i]$ 的组合情况,即 $s_1[i] = x$ 且 $s_2[i] = y$ 的情况,记为 $xy$,对于 $s_1[i] = y$ 且 $s_2[i] = x$ 的情况,记为 $yx$。

如果 $xy + yx$ 为奇数,则无法完成交换,返回 $-1$。如果 $xy + yx$ 为偶数,则需要交换的次数为 $\left \lfloor \frac{x}{2} \right \rfloor$ + $\left \lfloor \frac{y}{2} \right \rfloor$ + $xy \bmod{2}$ + $yx \bmod{2}$。

时间复杂度 $O(n)$,其中 $n$ 为字符串 $s1$ 和 $s2$ 的长度。空间复杂度 $O(1)$。
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s_1$ 和 $s_2$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -160,6 +160,28 @@ func minimumSwap(s1 string, s2 string) int {
}
```

#### TypeScript

```ts
function minimumSwap(s1: string, s2: string): number {
let xy = 0,
yx = 0;

for (let i = 0; i < s1.length; ++i) {
const a = s1[i],
b = s2[i];
xy += a < b ? 1 : 0;
yx += a > b ? 1 : 0;
}

if ((xy + yx) % 2 !== 0) {
return -1;
}

return Math.floor(xy / 2) + Math.floor(yx / 2) + (xy % 2) + (yx % 2);
}
```

#### JavaScript

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ Note that you cannot swap s1[0] and s1[1] to make s1 equal to &quot;yx&quot;, ca

### Solution 1: Greedy

According to the problem description, both strings $s1$ and $s2$ only contain characters $x$ and $y$, and have the same length. Therefore, we can pair the characters in $s1$ and $s2$, i.e., $s1[i]$ and $s2[i]$.
According to the problem description, both strings $s_1$ and $s_2$ contain only the characters $x$ and $y$, and they have the same length. Therefore, we can match the characters in $s_1$ and $s_2$ one by one, i.e., $s_1[i]$ and $s_2[i]$.

If $s1[i] = s2[i]$, no swap is needed, and we can skip it. If $s1[i] \neq s2[i]$, a swap is needed. We count the combination of $s1[i]$ and $s2[i]$, i.e., the situation where $s1[i] = x$ and $s2[i] = y$, denoted as $xy$, and the situation where $s1[i] = y$ and $s2[i] = x$, denoted as $yx$.
If $s_1[i] = s_2[i]$, no swap is needed, and we can skip to the next character. If $s_1[i] \neq s_2[i]$, a swap is needed. We count the combinations of $s_1[i]$ and $s_2[i]$: if $s_1[i] = x$ and $s_2[i] = y$, we denote it as $xy$; if $s_1[i] = y$ and $s_2[i] = x$, we denote it as $yx$.

If $xy + yx$ is odd, the swap cannot be completed, and we return $-1$. If $xy + yx$ is even, the number of swaps needed is $\left \lfloor \frac{x}{2} \right \rfloor$ + $\left \lfloor \frac{y}{2} \right \rfloor$ + $xy \bmod{2}$ + $yx \bmod{2}$.
If $xy + yx$ is odd, it is impossible to complete the swaps, and we return $-1$. If $xy + yx$ is even, the number of swaps needed is $\left \lfloor \frac{xy}{2} \right \rfloor + \left \lfloor \frac{yx}{2} \right \rfloor + xy \bmod{2} + yx \bmod{2}$.

The time complexity is $O(n)$, where $n$ is the length of the strings $s1$ and $s2$. The space complexity is $O(1)$.
The time complexity is $O(n)$, where $n$ is the length of the strings $s_1$ and $s_2$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -154,6 +154,28 @@ func minimumSwap(s1 string, s2 string) int {
}
```

#### TypeScript

```ts
function minimumSwap(s1: string, s2: string): number {
let xy = 0,
yx = 0;

for (let i = 0; i < s1.length; ++i) {
const a = s1[i],
b = s2[i];
xy += a < b ? 1 : 0;
yx += a > b ? 1 : 0;
}

if ((xy + yx) % 2 !== 0) {
return -1;
}

return Math.floor(xy / 2) + Math.floor(yx / 2) + (xy % 2) + (yx % 2);
}
```

#### JavaScript

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function minimumSwap(s1: string, s2: string): number {
let xy = 0,
yx = 0;

for (let i = 0; i < s1.length; ++i) {
const a = s1[i],
b = s2[i];
xy += a < b ? 1 : 0;
yx += a > b ? 1 : 0;
}

if ((xy + yx) % 2 !== 0) {
return -1;
}

return Math.floor(xy / 2) + Math.floor(yx / 2) + (xy % 2) + (yx % 2);
}
12 changes: 12 additions & 0 deletions solution/1200-1299/1250.Check If It Is a Good Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ func gcd(a, b int) int {
}
```

#### TypeScript

```ts
function isGoodArray(nums: number[]): boolean {
return nums.reduce(gcd) === 1;
}

function gcd(a: number, b: number): number {
return b === 0 ? a : gcd(b, a % b);
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
12 changes: 12 additions & 0 deletions solution/1200-1299/1250.Check If It Is a Good Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ func gcd(a, b int) int {
}
```

#### TypeScript

```ts
function isGoodArray(nums: number[]): boolean {
return nums.reduce(gcd) === 1;
}

function gcd(a: number, b: number): number {
return b === 0 ? a : gcd(b, a % b);
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function isGoodArray(nums: number[]): boolean {
return nums.reduce(gcd) === 1;
}

function gcd(a: number, b: number): number {
return b === 0 ? a : gcd(b, a % b);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3294.Convert%20Doubly%20Linked%20List%20to%20Array%20II/README.md
tags:
- 数组
- 链表
- 双向链表
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3294.Convert%20Doubly%20Linked%20List%20to%20Array%20II/README_EN.md
tags:
- Array
- Linked List
- Doubly-Linked List
---

<!-- problem:start -->
Expand Down
4 changes: 4 additions & 0 deletions solution/3200-3299/3295.Report Spam Message/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3295.Report%20Spam%20Message/README.md
tags:
- 数组
- 哈希表
- 字符串
---

<!-- problem:start -->
Expand Down
4 changes: 4 additions & 0 deletions solution/3200-3299/3295.Report Spam Message/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3295.Report%20Spam%20Message/README_EN.md
tags:
- Array
- Hash Table
- String
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3296.Minimum%20Number%20of%20Seconds%20to%20Make%20Mountain%20Height%20Zero/README.md
tags:
- 数组
- 数学
- 二分查找
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3296.Minimum%20Number%20of%20Seconds%20to%20Make%20Mountain%20Height%20Zero/README_EN.md
tags:
- Array
- Math
- Binary Search
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3297.Count%20Substrings%20That%20Can%20Be%20Rearranged%20to%20Contain%20a%20String%20I/README.md
tags:
- 哈希表
- 字符串
- 滑动窗口
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: Medium
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3297.Count%20Substrings%20That%20Can%20Be%20Rearranged%20to%20Contain%20a%20String%20I/README_EN.md
tags:
- Hash Table
- String
- Sliding Window
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: 困难
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3298.Count%20Substrings%20That%20Can%20Be%20Rearranged%20to%20Contain%20a%20String%20II/README.md
tags:
- 哈希表
- 字符串
- 滑动窗口
---

<!-- problem:start -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
comments: true
difficulty: Hard
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3298.Count%20Substrings%20That%20Can%20Be%20Rearranged%20to%20Contain%20a%20String%20II/README_EN.md
tags:
- Hash Table
- String
- Sliding Window
---

<!-- problem:start -->
Expand Down
10 changes: 5 additions & 5 deletions solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3304,11 +3304,11 @@
| 3291 | [形成目标字符串需要的最少字符串数 I](/solution/3200-3299/3291.Minimum%20Number%20of%20Valid%20Strings%20to%20Form%20Target%20I/README.md) | `字典树`,`线段树`,`数组`,`字符串`,`二分查找`,`动态规划`,`字符串匹配`,`哈希函数`,`滚动哈希` | 中等 | 第 415 场周赛 |
| 3292 | [形成目标字符串需要的最少字符串数 II](/solution/3200-3299/3292.Minimum%20Number%20of%20Valid%20Strings%20to%20Form%20Target%20II/README.md) | `线段树`,`数组`,`字符串`,`二分查找`,`动态规划`,`字符串匹配`,`哈希函数`,`滚动哈希` | 困难 | 第 415 场周赛 |
| 3293 | [计算产品最终价格](/solution/3200-3299/3293.Calculate%20Product%20Final%20Price/README.md) | `数据库` | 中等 | 🔒 |
| 3294 | [将双链表转换为数组 II](/solution/3200-3299/3294.Convert%20Doubly%20Linked%20List%20to%20Array%20II/README.md) | | 中等 | 🔒 |
| 3295 | [举报垃圾信息](/solution/3200-3299/3295.Report%20Spam%20Message/README.md) | | 中等 | 第 416 场周赛 |
| 3296 | [移山所需的最少秒数](/solution/3200-3299/3296.Minimum%20Number%20of%20Seconds%20to%20Make%20Mountain%20Height%20Zero/README.md) | | 中等 | 第 416 场周赛 |
| 3297 | [统计重新排列后包含另一个字符串的子字符串数目 I](/solution/3200-3299/3297.Count%20Substrings%20That%20Can%20Be%20Rearranged%20to%20Contain%20a%20String%20I/README.md) | | 中等 | 第 416 场周赛 |
| 3298 | [统计重新排列后包含另一个字符串的子字符串数目 II](/solution/3200-3299/3298.Count%20Substrings%20That%20Can%20Be%20Rearranged%20to%20Contain%20a%20String%20II/README.md) | | 困难 | 第 416 场周赛 |
| 3294 | [将双链表转换为数组 II](/solution/3200-3299/3294.Convert%20Doubly%20Linked%20List%20to%20Array%20II/README.md) | `数组`,`链表`,`双向链表` | 中等 | 🔒 |
| 3295 | [举报垃圾信息](/solution/3200-3299/3295.Report%20Spam%20Message/README.md) | `数组`,`哈希表`,`字符串` | 中等 | 第 416 场周赛 |
| 3296 | [移山所需的最少秒数](/solution/3200-3299/3296.Minimum%20Number%20of%20Seconds%20to%20Make%20Mountain%20Height%20Zero/README.md) | `数组`,`数学`,`二分查找` | 中等 | 第 416 场周赛 |
| 3297 | [统计重新排列后包含另一个字符串的子字符串数目 I](/solution/3200-3299/3297.Count%20Substrings%20That%20Can%20Be%20Rearranged%20to%20Contain%20a%20String%20I/README.md) | `哈希表`,`字符串`,`滑动窗口` | 中等 | 第 416 场周赛 |
| 3298 | [统计重新排列后包含另一个字符串的子字符串数目 II](/solution/3200-3299/3298.Count%20Substrings%20That%20Can%20Be%20Rearranged%20to%20Contain%20a%20String%20II/README.md) | `哈希表`,`字符串`,`滑动窗口` | 困难 | 第 416 场周赛 |

## 版权

Expand Down
10 changes: 5 additions & 5 deletions solution/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3302,11 +3302,11 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
| 3291 | [Minimum Number of Valid Strings to Form Target I](/solution/3200-3299/3291.Minimum%20Number%20of%20Valid%20Strings%20to%20Form%20Target%20I/README_EN.md) | `Trie`,`Segment Tree`,`Array`,`String`,`Binary Search`,`Dynamic Programming`,`String Matching`,`Hash Function`,`Rolling Hash` | Medium | Weekly Contest 415 |
| 3292 | [Minimum Number of Valid Strings to Form Target II](/solution/3200-3299/3292.Minimum%20Number%20of%20Valid%20Strings%20to%20Form%20Target%20II/README_EN.md) | `Segment Tree`,`Array`,`String`,`Binary Search`,`Dynamic Programming`,`String Matching`,`Hash Function`,`Rolling Hash` | Hard | Weekly Contest 415 |
| 3293 | [Calculate Product Final Price](/solution/3200-3299/3293.Calculate%20Product%20Final%20Price/README_EN.md) | `Database` | Medium | 🔒 |
| 3294 | [Convert Doubly Linked List to Array II](/solution/3200-3299/3294.Convert%20Doubly%20Linked%20List%20to%20Array%20II/README_EN.md) | | Medium | 🔒 |
| 3295 | [Report Spam Message](/solution/3200-3299/3295.Report%20Spam%20Message/README_EN.md) | | Medium | Weekly Contest 416 |
| 3296 | [Minimum Number of Seconds to Make Mountain Height Zero](/solution/3200-3299/3296.Minimum%20Number%20of%20Seconds%20to%20Make%20Mountain%20Height%20Zero/README_EN.md) | | Medium | Weekly Contest 416 |
| 3297 | [Count Substrings That Can Be Rearranged to Contain a String I](/solution/3200-3299/3297.Count%20Substrings%20That%20Can%20Be%20Rearranged%20to%20Contain%20a%20String%20I/README_EN.md) | | Medium | Weekly Contest 416 |
| 3298 | [Count Substrings That Can Be Rearranged to Contain a String II](/solution/3200-3299/3298.Count%20Substrings%20That%20Can%20Be%20Rearranged%20to%20Contain%20a%20String%20II/README_EN.md) | | Hard | Weekly Contest 416 |
| 3294 | [Convert Doubly Linked List to Array II](/solution/3200-3299/3294.Convert%20Doubly%20Linked%20List%20to%20Array%20II/README_EN.md) | `Array`,`Linked List`,`Doubly-Linked List` | Medium | 🔒 |
| 3295 | [Report Spam Message](/solution/3200-3299/3295.Report%20Spam%20Message/README_EN.md) | `Array`,`Hash Table`,`String` | Medium | Weekly Contest 416 |
| 3296 | [Minimum Number of Seconds to Make Mountain Height Zero](/solution/3200-3299/3296.Minimum%20Number%20of%20Seconds%20to%20Make%20Mountain%20Height%20Zero/README_EN.md) | `Array`,`Math`,`Binary Search` | Medium | Weekly Contest 416 |
| 3297 | [Count Substrings That Can Be Rearranged to Contain a String I](/solution/3200-3299/3297.Count%20Substrings%20That%20Can%20Be%20Rearranged%20to%20Contain%20a%20String%20I/README_EN.md) | `Hash Table`,`String`,`Sliding Window` | Medium | Weekly Contest 416 |
| 3298 | [Count Substrings That Can Be Rearranged to Contain a String II](/solution/3200-3299/3298.Count%20Substrings%20That%20Can%20Be%20Rearranged%20to%20Contain%20a%20String%20II/README_EN.md) | `Hash Table`,`String`,`Sliding Window` | Hard | Weekly Contest 416 |

## Copyright

Expand Down
Loading