Skip to content

feat: add solutions to lc problems: No.0921~0923 #2433

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 2 commits into from
Mar 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ func minAddToMakeValid(s string) int {
}
```

```ts
function minAddToMakeValid(s: string): number {
const stk: string[] = [];
for (const c of s) {
if (c === ')' && stk.length > 0 && stk.at(-1)! === '(') {
stk.pop();
} else {
stk.push(c);
}
}
return stk.length;
}
```

<!-- tabs:end -->

### 方法二:贪心 + 计数
Expand All @@ -139,7 +153,7 @@ func minAddToMakeValid(s string) int {

遍历结束后,将 `cnt` 的值加到 `ans` 中,即为答案。

时间复杂度为 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
时间复杂度为 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。空间复杂度 $O(1)$

<!-- tabs:start -->

Expand Down Expand Up @@ -213,6 +227,23 @@ func minAddToMakeValid(s string) int {
}
```

```ts
function minAddToMakeValid(s: string): number {
let [ans, cnt] = [0, 0];
for (const c of s) {
if (c === '(') {
++cnt;
} else if (cnt) {
--cnt;
} else {
++ans;
}
}
ans += cnt;
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,18 @@

## Solutions

### Solution 1
### Solution 1: Greedy + Stack

This problem is a classic parenthesis matching problem, which can be solved using "Greedy + Stack".

Iterate through each character $c$ in the string $s$:

- If $c$ is a left parenthesis, directly push $c$ into the stack;
- If $c$ is a right parenthesis, at this point if the stack is not empty, and the top element of the stack is a left parenthesis, then pop the top element of the stack, indicating a successful match; otherwise, push $c$ into the stack.

After the iteration ends, the number of remaining elements in the stack is the number of parentheses that need to be added.

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

<!-- tabs:start -->

Expand Down Expand Up @@ -109,9 +120,36 @@ func minAddToMakeValid(s string) int {
}
```

```ts
function minAddToMakeValid(s: string): number {
const stk: string[] = [];
for (const c of s) {
if (c === ')' && stk.length > 0 && stk.at(-1)! === '(') {
stk.pop();
} else {
stk.push(c);
}
}
return stk.length;
}
```

<!-- tabs:end -->

### Solution 2
### Solution 2: Greedy + Counting

Solution 1 uses a stack to implement parenthesis matching, but we can also directly implement it through counting.

Define a variable `cnt` to represent the current number of left parentheses to be matched, and a variable `ans` to record the answer. Initially, both variables are set to $0$.

Iterate through each character $c$ in the string $s$:

- If $c$ is a left parenthesis, increase the value of `cnt` by $1$;
- If $c$ is a right parenthesis, at this point if $cnt > 0$, it means that there are left parentheses that can be matched, so decrease the value of `cnt` by $1$; otherwise, it means that the current right parenthesis cannot be matched, so increase the value of `ans` by $1$.

After the iteration ends, add the value of `cnt` to `ans`, which is the answer.

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

<!-- tabs:start -->

Expand Down Expand Up @@ -185,6 +223,23 @@ func minAddToMakeValid(s string) int {
}
```

```ts
function minAddToMakeValid(s: string): number {
let [ans, cnt] = [0, 0];
for (const c of s) {
if (c === '(') {
++cnt;
} else if (cnt) {
--cnt;
} else {
++ans;
}
}
ans += cnt;
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function minAddToMakeValid(s: string): number {
const stk: string[] = [];
for (const c of s) {
if (c === ')' && stk.length > 0 && stk.at(-1)! === '(') {
stk.pop();
} else {
stk.push(c);
}
}
return stk.length;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function minAddToMakeValid(s: string): number {
let [ans, cnt] = [0, 0];
for (const c of s) {
if (c === '(') {
++cnt;
} else if (cnt) {
--cnt;
} else {
++ans;
}
}
ans += cnt;
return ans;
}
42 changes: 31 additions & 11 deletions solution/0900-0999/0922.Sort Array By Parity II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@

## 解法

### 方法一
### 方法一:双指针

我们用两个指针 $i$ 和 $j$ 分别指向偶数下标和奇数下标。

当 $i$ 指向偶数下标时,如果 $nums[i]$ 是奇数,那么我们需要找到一个奇数下标 $j$,使得 $nums[j]$ 是偶数,然后交换 $nums[i]$ 和 $nums[j]$。继续遍历,直到 $i$ 指向数组末尾。

时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -57,8 +63,8 @@ class Solution:
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
n, j = len(nums), 1
for i in range(0, n, 2):
if (nums[i] & 1) == 1:
while (nums[j] & 1) == 1:
if nums[i] % 2:
while nums[j] % 2:
j += 2
nums[i], nums[j] = nums[j], nums[i]
return nums
Expand All @@ -68,8 +74,8 @@ class Solution:
class Solution {
public int[] sortArrayByParityII(int[] nums) {
for (int i = 0, j = 1; i < nums.length; i += 2) {
if ((nums[i] & 1) == 1) {
while ((nums[j] & 1) == 1) {
if (nums[i] % 2 == 1) {
while (nums[j] % 2 == 1) {
j += 2;
}
int t = nums[i];
Expand All @@ -87,8 +93,8 @@ class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& nums) {
for (int i = 0, j = 1; i < nums.size(); i += 2) {
if ((nums[i] & 1) == 1) {
while ((nums[j] & 1) == 1) {
if (nums[i] % 2) {
while (nums[j] % 2) {
j += 2;
}
swap(nums[i], nums[j]);
Expand All @@ -102,8 +108,8 @@ public:
```go
func sortArrayByParityII(nums []int) []int {
for i, j := 0, 1; i < len(nums); i += 2 {
if (nums[i] & 1) == 1 {
for (nums[j] & 1) == 1 {
if nums[i]%2 == 1 {
for nums[j]%2 == 1 {
j += 2
}
nums[i], nums[j] = nums[j], nums[i]
Expand All @@ -113,15 +119,29 @@ func sortArrayByParityII(nums []int) []int {
}
```

```ts
function sortArrayByParityII(nums: number[]): number[] {
for (let i = 0, j = 1; i < nums.length; i += 2) {
if (nums[i] % 2) {
while (nums[j] % 2) {
j += 2;
}
[nums[i], nums[j]] = [nums[j], nums[i]];
}
}
return nums;
}
```

```js
/**
* @param {number[]} nums
* @return {number[]}
*/
var sortArrayByParityII = function (nums) {
for (let i = 0, j = 1; i < nums.length; i += 2) {
if ((nums[i] & 1) == 1) {
while ((nums[j] & 1) == 1) {
if (nums[i] % 2) {
while (nums[j] % 2) {
j += 2;
}
[nums[i], nums[j]] = [nums[j], nums[i]];
Expand Down
42 changes: 31 additions & 11 deletions solution/0900-0999/0922.Sort Array By Parity II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@

## Solutions

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

We use two pointers $i$ and $j$ to point to even and odd indices respectively.

When $i$ points to an even index, if $nums[i]$ is odd, then we need to find an odd index $j$ such that $nums[j]$ is even, and then swap $nums[i]$ and $nums[j]$. Continue to iterate until $i$ points to the end of the array.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -52,8 +58,8 @@ class Solution:
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
n, j = len(nums), 1
for i in range(0, n, 2):
if (nums[i] & 1) == 1:
while (nums[j] & 1) == 1:
if nums[i] % 2:
while nums[j] % 2:
j += 2
nums[i], nums[j] = nums[j], nums[i]
return nums
Expand All @@ -63,8 +69,8 @@ class Solution:
class Solution {
public int[] sortArrayByParityII(int[] nums) {
for (int i = 0, j = 1; i < nums.length; i += 2) {
if ((nums[i] & 1) == 1) {
while ((nums[j] & 1) == 1) {
if (nums[i] % 2 == 1) {
while (nums[j] % 2 == 1) {
j += 2;
}
int t = nums[i];
Expand All @@ -82,8 +88,8 @@ class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& nums) {
for (int i = 0, j = 1; i < nums.size(); i += 2) {
if ((nums[i] & 1) == 1) {
while ((nums[j] & 1) == 1) {
if (nums[i] % 2) {
while (nums[j] % 2) {
j += 2;
}
swap(nums[i], nums[j]);
Expand All @@ -97,8 +103,8 @@ public:
```go
func sortArrayByParityII(nums []int) []int {
for i, j := 0, 1; i < len(nums); i += 2 {
if (nums[i] & 1) == 1 {
for (nums[j] & 1) == 1 {
if nums[i]%2 == 1 {
for nums[j]%2 == 1 {
j += 2
}
nums[i], nums[j] = nums[j], nums[i]
Expand All @@ -108,15 +114,29 @@ func sortArrayByParityII(nums []int) []int {
}
```

```ts
function sortArrayByParityII(nums: number[]): number[] {
for (let i = 0, j = 1; i < nums.length; i += 2) {
if (nums[i] % 2) {
while (nums[j] % 2) {
j += 2;
}
[nums[i], nums[j]] = [nums[j], nums[i]];
}
}
return nums;
}
```

```js
/**
* @param {number[]} nums
* @return {number[]}
*/
var sortArrayByParityII = function (nums) {
for (let i = 0, j = 1; i < nums.length; i += 2) {
if ((nums[i] & 1) == 1) {
while ((nums[j] & 1) == 1) {
if (nums[i] % 2) {
while (nums[j] % 2) {
j += 2;
}
[nums[i], nums[j]] = [nums[j], nums[i]];
Expand Down
4 changes: 2 additions & 2 deletions solution/0900-0999/0922.Sort Array By Parity II/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class Solution {
public:
vector<int> sortArrayByParityII(vector<int>& nums) {
for (int i = 0, j = 1; i < nums.size(); i += 2) {
if ((nums[i] & 1) == 1) {
while ((nums[j] & 1) == 1) {
if (nums[i] % 2) {
while (nums[j] % 2) {
j += 2;
}
swap(nums[i], nums[j]);
Expand Down
4 changes: 2 additions & 2 deletions solution/0900-0999/0922.Sort Array By Parity II/Solution.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
func sortArrayByParityII(nums []int) []int {
for i, j := 0, 1; i < len(nums); i += 2 {
if (nums[i] & 1) == 1 {
for (nums[j] & 1) == 1 {
if nums[i]%2 == 1 {
for nums[j]%2 == 1 {
j += 2
}
nums[i], nums[j] = nums[j], nums[i]
Expand Down
4 changes: 2 additions & 2 deletions solution/0900-0999/0922.Sort Array By Parity II/Solution.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Solution {
public int[] sortArrayByParityII(int[] nums) {
for (int i = 0, j = 1; i < nums.length; i += 2) {
if ((nums[i] & 1) == 1) {
while ((nums[j] & 1) == 1) {
if (nums[i] % 2 == 1) {
while (nums[j] % 2 == 1) {
j += 2;
}
int t = nums[i];
Expand Down
4 changes: 2 additions & 2 deletions solution/0900-0999/0922.Sort Array By Parity II/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
*/
var sortArrayByParityII = function (nums) {
for (let i = 0, j = 1; i < nums.length; i += 2) {
if ((nums[i] & 1) == 1) {
while ((nums[j] & 1) == 1) {
if (nums[i] % 2) {
while (nums[j] % 2) {
j += 2;
}
[nums[i], nums[j]] = [nums[j], nums[i]];
Expand Down
4 changes: 2 additions & 2 deletions solution/0900-0999/0922.Sort Array By Parity II/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class Solution:
def sortArrayByParityII(self, nums: List[int]) -> List[int]:
n, j = len(nums), 1
for i in range(0, n, 2):
if (nums[i] & 1) == 1:
while (nums[j] & 1) == 1:
if nums[i] % 2:
while nums[j] % 2:
j += 2
nums[i], nums[j] = nums[j], nums[i]
return nums
Loading