Skip to content

feat: add solutions to lc problems: No.2000+ #2095

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
Dec 13, 2023
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
6 changes: 6 additions & 0 deletions solution/2000-2099/2000.Reverse Prefix of Word/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ You should not do any reverse operation, the resulting string is "abcd&quot

## Solutions

**Solution 1: Simulation**

First, we find the index $i$ where the character $ch$ first appears. Then, we reverse the characters from index $0$ to index $i$ (including index $i$). Finally, we concatenate the reversed string with the string starting from index $i + 1$.

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

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

为了能够唯一表示矩形,我们需要将矩形的宽高比化简为最简分数。因此,我们可以求出每个矩形的宽高比的最大公约数,然后将宽高比化简为最简分数。接下来,我们使用哈希表统计每个最简分数的矩形数量,然后计算每个最简分数的矩形数量的组合数,即可得到答案。

时间复杂度 $O(n \log M)$,空间复杂度 $O(n)$。其中 $n$ 和 $M$ 分别是矩形的数量和矩形的最大边长。
时间复杂度 $O(n \times \log M)$,空间复杂度 $O(n)$。其中 $n$ 和 $M$ 分别是矩形的数量和矩形的最大边长。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@

## Solutions

**Solution 1: Mathematics + Hash Table**

In order to uniquely represent a rectangle, we need to simplify the width-to-height ratio of the rectangle to a simplest fraction. Therefore, we can find the greatest common divisor of the width-to-height ratio of each rectangle, and then simplify the width-to-height ratio to the simplest fraction. Next, we use a hash table to count the number of rectangles for each simplest fraction, and then calculate the combination of the number of rectangles for each simplest fraction to get the answer.

The time complexity is $O(n \times \log M)$, and the space complexity is $O(n)$. Here, $n$ and $M$ are the number of rectangles and the maximum side length of the rectangles, respectively.

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ The product of their lengths is: 5 * 5 = 25.

## Solutions

**Solution 1: Binary Enumeration**

We notice that the length of the string $s$ does not exceed $12$, so we can use the method of binary enumeration to enumerate all subsequences of $s$. Suppose the length of $s$ is $n$, we can use $2^n$ binary numbers of length $n$ to represent all subsequences of $s$. For each binary number, the $i$-th bit being $1$ means the $i$-th character of $s$ is in the subsequence, and $0$ means it is not in the subsequence. For each binary number, we judge whether it is a palindrome subsequence and record it in the array $p$.

Next, we enumerate each number $i$ in $p$. If $i$ is a palindrome subsequence, then we can enumerate a number $j$ from the complement of $i$, $mx = (2^n - 1) \oplus i$. If $j$ is also a palindrome subsequence, then $i$ and $j$ are the two palindrome subsequences we are looking for. Their lengths are the number of $1$s in the binary representation of $i$ and $j$, denoted as $a$ and $b$, respectively. Then their product is $a \times b$. We take the maximum of all possible $a \times b$.

The time complexity is $(2^n \times n + 3^n)$, and the space complexity is $O(2^n)$. Here, $n$ is the length of the string $s$.

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@

**方法一:暴力枚举**

我们注意到,数组 `nums` 的长度不超过 $200$,因此我们可以枚举所有的数对 $(i, j)$,其中 $i < j$,并判断 $|nums[i] - nums[j]|$ 是否等于 $k$,是则答案加一。
我们注意到,数组 $nums$ 的长度不超过 $200$,因此我们可以枚举所有的数对 $(i, j)$,其中 $i < j$,并判断 $|nums[i] - nums[j]|$ 是否等于 $k$,是则答案加一。

最后返回答案即可。

时间复杂度 $O(n^2)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
时间复杂度 $O(n^2)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $nums$ 的长度。

**方法二:哈希表或数组**

我们可以使用哈希表或数组记录数组 `nums` 中每个数出现的次数,然后枚举数组 `nums` 中的每个数 $x$,判断 $x + k$ 和 $x - k$ 是否在数组 `nums` 中,是则答案加上 $x+k$ 和 $x-k$ 出现的次数之和。
我们可以使用哈希表或数组记录数组 $nums$ 中每个数出现的次数,然后枚举数组 $nums$ 中的每个数 $x$,判断 $x + k$ 和 $x - k$ 是否在数组 $nums$ 中,是则答案加上 $x+k$ 和 $x-k$ 出现的次数之和。

最后返回答案即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@

## Solutions

**Solution 1: Brute Force Enumeration**

We notice that the length of the array $nums$ does not exceed $200$, so we can enumerate all pairs $(i, j)$, where $i < j$, and check if $|nums[i] - nums[j]|$ equals $k$. If it does, we increment the answer by one.

Finally, we return the answer.

The time complexity is $O(n^2)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $nums$.

**Solution 2: Hash Table or Array**

We can use a hash table or array to record the occurrence count of each number in the array $nums$. Then, we enumerate each number $x$ in the array $nums$, and check if $x + k$ and $x - k$ are in the array $nums$. If they are, we increment the answer by the sum of the occurrence counts of $x + k$ and $x - k$.

Finally, we return the answer.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ Other original arrays could be [4,3,1] or [3,1,4].

## Solutions

**Solution 1: Sorting + Counting + Traversal**

First, we check if the length $n$ of the array `changed` is odd. If it is, we directly return an empty array.

Then, we sort the array `changed`, and use a hash table or array `cnt` to count the occurrence of each element in `changed`.

Next, we traverse the array `changed`. For each element $x$ in `changed`, we first check if $x$ exists in the hash table `cnt`. If it does not exist, we directly skip this element. Otherwise, we check if $x \times 2$ exists in `cnt`. If it does not exist, we directly return an empty array. Otherwise, we add $x$ to the answer array `ans`, and decrease the occurrence counts of $x$ and $x \times 2$ in `cnt` by $1$ each.

After the traversal, we check if the length of the answer array `ans` is $\frac{n}{2}$. If it is, we return `ans`, otherwise we return an empty array.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array `changed`.

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ The resulting array is [1,2,3,4], which is continuous.

## Solutions

**Solution 1: Sorting + Deduplication + Binary Search**

First, we sort the array and remove duplicates.

Then, we traverse the array, enumerating the current element $nums[i]$ as the minimum value of the consecutive array. We use binary search to find the first position $j$ that is greater than $nums[i] + n - 1$. Then, $j-i$ is the length of the consecutive array when the current element is the minimum value. We update the answer, i.e., $ans = \min(ans, n - (j - i))$.

Finally, we return $ans$.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array.

**Solution 2: Sorting + Deduplication + Two Pointers**

Similar to Solution 1, we first sort the array and remove duplicates.

Then, we traverse the array, enumerating the current element $nums[i]$ as the minimum value of the consecutive array. We use two pointers to find the first position $j$ that is greater than $nums[i] + n - 1$. Then, $j-i$ is the length of the consecutive array when the current element is the minimum value. We update the answer, i.e., $ans = \min(ans, n - (j - i))$.

Finally, we return $ans$.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array.

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ X--:X 减 1 ,X = 1 - 1 = 0

遍历数组 `operations`,对于每个操作 $operations[i]$,如果包含 `'+'`,那么答案加 $1$,否则答案减 $1$。

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ X--: X is decremented by 1, X = 1 - 1 = 0.

## Solutions

**Solution 1: Simulation**

Traverse the array `operations`. For each operation $operations[i]$, if it contains `'+'`, then the answer increases by $1$, otherwise the answer decreases by $1$.

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

<!-- tabs:start -->

### **Python3**
Expand Down
129 changes: 68 additions & 61 deletions solution/2000-2099/2012.Sum of Beauty in the Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:预处理右侧最小值 + 遍历维护左侧最大值**

我们可以预处理出右侧最小值数组 $right$,其中 $right[i]$ 表示 $nums[i..n-1]$ 中的最小值。

然后我们从左到右遍历数组 $nums$,同时维护左侧最大值 $l$。对于每个位置 $i$,我们判断 $l < nums[i] < right[i + 1]$ 是否成立,如果成立则将 $2$ 累加至答案,否则判断 $nums[i - 1] < nums[i] < nums[i + 1]$ 是否成立,如果成立则将 $1$ 累加至答案。

遍历结束后即可得到答案。

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

<!-- tabs:start -->

### **Python3**
Expand All @@ -66,18 +76,18 @@
class Solution:
def sumOfBeauties(self, nums: List[int]) -> int:
n = len(nums)
lmx = [0] * n
for i in range(1, n):
lmx[i] = max(lmx[i - 1], nums[i - 1])
rmi = [100001] * n
right = [nums[-1]] * n
for i in range(n - 2, -1, -1):
rmi[i] = min(rmi[i + 1], nums[i + 1])
right[i] = min(right[i + 1], nums[i])
ans = 0
l = nums[0]
for i in range(1, n - 1):
if lmx[i] < nums[i] < rmi[i]:
r = right[i + 1]
if l < nums[i] < r:
ans += 2
elif nums[i - 1] < nums[i] < nums[i + 1]:
ans += 1
l = max(l, nums[i])
return ans
```

Expand All @@ -89,70 +99,47 @@ class Solution:
class Solution {
public int sumOfBeauties(int[] nums) {
int n = nums.length;
int[] lmx = new int[n];
int[] rmi = new int[n];
rmi[n - 1] = 100001;
for (int i = 1; i < n; ++i) {
lmx[i] = Math.max(lmx[i - 1], nums[i - 1]);
}
for (int i = n - 2; i >= 0; --i) {
rmi[i] = Math.min(rmi[i + 1], nums[i + 1]);
int[] right = new int[n];
right[n - 1] = nums[n - 1];
for (int i = n - 2; i > 0; --i) {
right[i] = Math.min(right[i + 1], nums[i]);
}
int ans = 0;
int l = nums[0];
for (int i = 1; i < n - 1; ++i) {
if (lmx[i] < nums[i] && nums[i] < rmi[i]) {
int r = right[i + 1];
if (l < nums[i] && nums[i] < r) {
ans += 2;
} else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {
ans += 1;
}
l = Math.max(l, nums[i]);
}
return ans;
}
}
```

### \*\*TypeScript

```ts
function sumOfBeauties(nums: number[]): number {
let n = nums.length;
let prefix = new Array(n),
postfix = new Array(n);
prefix[0] = nums[0];
postfix[n - 1] = nums[n - 1];
for (let i = 1, j = n - 2; i < n; ++i, --j) {
prefix[i] = Math.max(nums[i], prefix[i - 1]);
postfix[j] = Math.min(nums[j], postfix[j + 1]);
}
let ans = 0;
for (let i = 1; i < n - 1; ++i) {
if (prefix[i - 1] < nums[i] && nums[i] < postfix[i + 1]) {
ans += 2;
} else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {
ans += 1;
}
}
return ans;
}
```

### **C++**

```cpp
class Solution {
public:
int sumOfBeauties(vector<int>& nums) {
int n = nums.size();
vector<int> lmx(n);
vector<int> rmi(n, 100001);
for (int i = 1; i < n; ++i) lmx[i] = max(lmx[i - 1], nums[i - 1]);
for (int i = n - 2; i >= 0; --i) rmi[i] = min(rmi[i + 1], nums[i + 1]);
vector<int> right(n, nums[n - 1]);
for (int i = n - 2; i; --i) {
right[i] = min(right[i + 1], nums[i]);
}
int ans = 0;
for (int i = 1; i < n - 1; ++i) {
if (lmx[i] < nums[i] && nums[i] < rmi[i])
for (int i = 1, l = nums[0]; i < n - 1; ++i) {
int r = right[i + 1];
if (l < nums[i] && nums[i] < r) {
ans += 2;
else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1])
} else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {
ans += 1;
}
l = max(l, nums[i]);
}
return ans;
}
Expand All @@ -162,26 +149,46 @@ public:
### **Go**

```go
func sumOfBeauties(nums []int) int {
func sumOfBeauties(nums []int) (ans int) {
n := len(nums)
lmx := make([]int, n)
rmi := make([]int, n)
rmi[n-1] = 100001
for i := 1; i < n; i++ {
lmx[i] = max(lmx[i-1], nums[i-1])
}
for i := n - 2; i >= 0; i-- {
rmi[i] = min(rmi[i+1], nums[i+1])
right := make([]int, n)
right[n-1] = nums[n-1]
for i := n - 2; i > 0; i-- {
right[i] = min(right[i+1], nums[i])
}
ans := 0
for i := 1; i < n-1; i++ {
if lmx[i] < nums[i] && nums[i] < rmi[i] {
for i, l := 1, nums[0]; i < n-1; i++ {
r := right[i+1]
if l < nums[i] && nums[i] < r {
ans += 2
} else if nums[i-1] < nums[i] && nums[i] < nums[i+1] {
ans += 1
ans++
}
l = max(l, nums[i])
}
return ans
return
}
```

### **TypeScript**

```ts
function sumOfBeauties(nums: number[]): number {
const n = nums.length;
const right: number[] = Array(n).fill(nums[n - 1]);
for (let i = n - 2; i; --i) {
right[i] = Math.min(right[i + 1], nums[i]);
}
let ans = 0;
for (let i = 1, l = nums[0]; i < n - 1; ++i) {
const r = right[i + 1];
if (l < nums[i] && nums[i] < r) {
ans += 2;
} else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) {
ans += 1;
}
l = Math.max(l, nums[i]);
}
return ans;
}
```

Expand Down
Loading