Skip to content
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

feat: add solutions to lc problems: No.1790+ #2126

Merged
merged 1 commit into from
Dec 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@

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

**方法一:简单计数**
**方法一:计数**

我们用变量 $cnt$ 记录两个字符串中相同位置字符不同的个数,两个字符串若满足题目要求,那么 $cnt$ 一定为 $0$ 或 $2$。另外用两个字符变量 $c1$ 和 $c2$ 记录两个字符串中相同位置字符不同的字符。

同时遍历两个字符串,对于相同位置的两个字符 $a$ 和 $b$,如果 $a \ne b$,那么 $cnt$ 自增 $1$。如果此时 $cnt$ 大于 $2$,或者 $cnt$ 为 $2$ 且 $a \ne c2$ 或 $b \ne c1$,那么直接返回 `false`。注意记录一下 $c1$ 和 $c2$。

遍历结束,若 $cnt\neq 1$,返回 `true`。
遍历结束,若 $cnt \neq 1$,返回 `true`。

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@

## Solutions

**Solution 1: Counting**

We use a variable $cnt$ to record the number of characters at the same position in the two strings that are different. If the two strings meet the requirements of the problem, then $cnt$ must be $0$ or $2$. We also use two character variables $c1$ and $c2$ to record the characters that are different at the same position in the two strings.

While traversing the two strings simultaneously, for two characters $a$ and $b$ at the same position, if $a \ne b$, then $cnt$ is incremented by $1$. If at this time $cnt$ is greater than $2$, or $cnt$ is $2$ and $a \ne c2$ or $b \ne c1$, then we directly return `false`. Note to record $c1$ and $c2$.

At the end of the traversal, if $cnt \neq 1$, return `true`.

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

<!-- tabs:start -->

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

中心点的特点是,它与其他所有点都相连,因此只要比较前两条边的点,如果有相同的点,那么这个点就是中心点。

时间复杂度 $O(1)$。
时间复杂度 $O(1)$,空间复杂度 $O(1)$

<!-- tabs:start -->

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

## Solutions

**Solution 1: Directly Compare the Points of the First Two Edges**

The characteristic of the center point is that it is connected to all other points. Therefore, as long as we compare the points of the first two edges, if there are the same points, then this point is the center point.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

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

最后,我们将所有班级的通过率求和,然后除以班级数目,即为答案。

时间复杂度 $O(n\log n)$,其中 $n$ 为班级数目。
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为班级数目。

<!-- tabs:start -->

Expand Down
12 changes: 12 additions & 0 deletions solution/1700-1799/1792.Maximum Average Pass Ratio/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@

## Solutions

**Solution 1: Priority Queue (Max-Heap of Increment)**

Suppose a class currently has a pass rate of $\frac{a}{b}$. If we arrange a smart student into this class, then the pass rate of the class will become $\frac{a+1}{b+1}$. We can find that the increment of the pass rate is $\frac{a+1}{b+1} - \frac{a}{b}$.

We maintain a max-heap, which stores the increment of the pass rate for each class.

Perform `extraStudents` operations, each time taking a class from the top of the heap, adding $1$ to both the number of students and the number of passes in this class, then recalculating the increment of the pass rate of this class and putting it back into the heap. Repeat this process until all students are allocated.

Finally, we sum up the pass rates of all classes, and then divide by the number of classes to get the answer.

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

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ func countQuadruples(firstString string, secondString string) (ans int) {
}
```

### **TypeScript**

```ts
function countQuadruples(firstString: string, secondString: string): number {
const last: number[] = new Array(26).fill(0);
for (let i = 0; i < secondString.length; ++i) {
last[secondString.charCodeAt(i) - 97] = i + 1;
}
let [ans, mi] = [0, Infinity];
for (let i = 0; i < firstString.length; ++i) {
const j = last[firstString.charCodeAt(i) - 97];
if (j) {
const t = i - j;
if (mi > t) {
mi = t;
ans = 1;
} else if (mi === t) {
++ans;
}
}
}
return ans;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@

## Solutions

**Solution 1: Greedy + Hash Table**

The problem actually asks us to find a smallest index $i$ and a largest index $j$ such that $firstString[i]$ equals $secondString[j]$, and the value of $i - j$ is the smallest among all index pairs that meet the conditions.

Therefore, we first use a hash table $last$ to record the index of the last occurrence of each character in $secondString$. Then we traverse $firstString$. For each character $c$, if $c$ has appeared in $secondString$, we calculate $i - last[c]$. If the value of $i - last[c]$ is less than the current minimum value, we update the minimum value and set the answer to 1. If the value of $i - last[c]$ equals the current minimum value, we increment the answer by 1.

The time complexity is $O(m + n)$, and the space complexity is $O(C)$. Here, $m$ and $n$ are the lengths of $firstString$ and $secondString$ respectively, and $C$ is the size of the character set. In this problem, $C = 26$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -142,6 +150,31 @@ func countQuadruples(firstString string, secondString string) (ans int) {
}
```

### **TypeScript**

```ts
function countQuadruples(firstString: string, secondString: string): number {
const last: number[] = new Array(26).fill(0);
for (let i = 0; i < secondString.length; ++i) {
last[secondString.charCodeAt(i) - 97] = i + 1;
}
let [ans, mi] = [0, Infinity];
for (let i = 0; i < firstString.length; ++i) {
const j = last[firstString.charCodeAt(i) - 97];
if (j) {
const t = i - j;
if (mi > t) {
mi = t;
ans = 1;
} else if (mi === t) {
++ans;
}
}
}
return ans;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function countQuadruples(firstString: string, secondString: string): number {
const last: number[] = new Array(26).fill(0);
for (let i = 0; i < secondString.length; ++i) {
last[secondString.charCodeAt(i) - 97] = i + 1;
}
let [ans, mi] = [0, Infinity];
for (let i = 0; i < firstString.length; ++i) {
const j = last[firstString.charCodeAt(i) - 97];
if (j) {
const t = i - j;
if (mi > t) {
mi = t;
ans = 1;
} else if (mi === t) {
++ans;
}
}
}
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

遍历结束,返回 $b$ 即可。

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

**方法二:位运算**

Expand All @@ -59,7 +59,7 @@

最后,我们从高位向低位遍历 $mask$,找到第二个为 $1$ 的二进制位,其对应的数字即为第二大数字。如果不存在第二大数字,返回 $-1$。

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@

## Solutions

**Solution 1: One Pass**

We define $a$ and $b$ to represent the largest and second largest numbers in the string, initially $a = b = -1$.

We traverse the string $s$. If the current character is a digit, we convert it to a number $v$. If $v > a$, it means that $v$ is the largest number currently appearing, we update $b$ to $a$, and update $a$ to $v$; if $v < a$, it means that $v$ is the second largest number currently appearing, we update $b$ to $v$.

After the traversal, we return $b$.

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

**Solution 2: Bit Manipulation**

We can use an integer $mask$ to mark the numbers that appear in the string, where the $i$-th bit of $mask$ indicates whether the number $i$ has appeared.

We traverse the string $s$. If the current character is a digit, we convert it to a number $v$, and set the $v$-th bit of $mask$ to $1$.

Finally, we traverse $mask$ from high to low, find the second bit that is $1$, and the corresponding number is the second largest number. If there is no second largest number, return $-1$.

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

<!-- tabs:start -->

### **Python3**
Expand Down
12 changes: 12 additions & 0 deletions solution/1700-1799/1797.Design Authentication Manager/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ authenticationManager.<code>countUnexpiredTokens</code>(15); // The token with t

## Solutions

**Solution 1: Hash Table**

We can simply maintain a hash table $d$, where the key is `tokenId` and the value is the expiration time.

- During the `generate` operation, we store `tokenId` as the key and `currentTime + timeToLive` as the value in the hash table $d$.
- During the `renew` operation, if `tokenId` is not in the hash table $d$, or `currentTime >= d[tokenId]`, we ignore this operation; otherwise, we update `d[tokenId]` to `currentTime + timeToLive`.
- During the `countUnexpiredTokens` operation, we traverse the hash table $d$ and count the number of unexpired `tokenId`.

In terms of time complexity, both `generate` and `renew` operations have a time complexity of $O(1)$, and the `countUnexpiredTokens` operation has a time complexity of $O(n)$, where $n$ is the number of key-value pairs in the hash table $d$.

The space complexity is $O(n)$, where $n$ is the number of key-value pairs in the hash table $d$.

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ func getMaximumConsecutive(coins []int) int {
}
```

### **TypeScript**

```ts
function getMaximumConsecutive(coins: number[]): number {
coins.sort((a, b) => a - b);
let ans = 1;
for (const v of coins) {
if (v > ans) {
break;
}
ans += v;
}
return ans;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ You can make 8 consecutive integer values starting from 0.</pre>

## Solutions

**Solution 1: Sorting + Greedy**

First, we sort the array. Then we define $ans$ as the current number of consecutive integers that can be constructed, initialized to $1$.

We traverse the array, for the current element $v$, if $v > ans$, it means that we cannot construct $ans+1$ consecutive integers, so we directly break the loop and return $ans$. Otherwise, it means that we can construct $ans+v$ consecutive integers, so we update $ans$ to $ans+v$.

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 Expand Up @@ -120,6 +130,22 @@ func getMaximumConsecutive(coins []int) int {
}
```

### **TypeScript**

```ts
function getMaximumConsecutive(coins: number[]): number {
coins.sort((a, b) => a - b);
let ans = 1;
for (const v of coins) {
if (v > ans) {
break;
}
ans += v;
}
return ans;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function getMaximumConsecutive(coins: number[]): number {
coins.sort((a, b) => a - b);
let ans = 1;
for (const v of coins) {
if (v > ans) {
break;
}
ans += v;
}
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@

## Solutions

**Solution 1: State Compression + Dynamic Programming**

We can preprocess to get the greatest common divisor of any two numbers in the array `nums`, stored in the two-dimensional array $g$, where $g[i][j]$ represents the greatest common divisor of $nums[i]$ and $nums[j]$.

Then define $f[k]$ to represent the maximum score that can be obtained when the state after the current operation is $k$. Suppose $m$ is the number of elements in the array `nums`, then there are a total of $2^m$ states, that is, the range of $k$ is $[0, 2^m - 1]$.

Enumerate all states from small to large, for each state $k$, first determine whether the number of $1$s in the binary bits of this state $cnt$ is even, if so, perform the following operations:

Enumerate the positions where the binary bits in $k$ are 1, suppose they are $i$ and $j$, then the elements at positions $i$ and $j$ can perform one operation, and the score that can be obtained at this time is $\frac{cnt}{2} \times g[i][j]$, update the maximum value of $f[k]$.

The final answer is $f[2^m - 1]$.

The time complexity is $O(2^m \times m^2)$, and the space complexity is $O(2^m)$. Here, $m$ is the number of elements in the array `nums`.

<!-- tabs:start -->

### **Python3**
Expand Down
10 changes: 5 additions & 5 deletions solution/1800-1899/1800.Maximum Ascending Subarray Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@

**方法一:直接模拟**

我们用变量 `t` 记录当前升序子数组的和,用变量 `ans` 记录最大的升序子数组和。
我们用变量 $t$ 记录当前升序子数组的和,用变量 $ans$ 记录最大的升序子数组和。

遍历数组 `nums`
遍历数组 $nums$

如果当前元素是数组的第一个元素,或者当前元素大于前一个元素,那么将当前元素加入到当前升序子数组的和,即 `t += nums[i]`,并且更新最大升序子数组和 `ans = max(ans, t)`;否则,当前元素不满足升序子数组的条件,那么将当前升序子数组的和 `t` 重置为当前元素,即 `t = nums[i]`
如果当前元素是数组的第一个元素,或者当前元素大于前一个元素,那么将当前元素加入到当前升序子数组的和,即 $t = t + nums[i]$,并且更新最大升序子数组和 $ans = \max(ans, t)$;否则,当前元素不满足升序子数组的条件,那么将当前升序子数组的和 $t$ 重置为当前元素,即 $t = nums[i]$

遍历结束,返回最大升序子数组和 `ans`
遍历结束,返回最大升序子数组和 $ans$

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

<!-- tabs:start -->

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

## Solutions

**Solution 1: Direct Simulation**

We use a variable $t$ to record the current sum of the ascending subarray, and a variable $ans$ to record the maximum sum of the ascending subarray.

Traverse the array $nums$:

If the current element is the first element of the array, or the current element is greater than the previous one, then add the current element to the sum of the current ascending subarray, i.e., $t = t + nums[i]$, and update the maximum sum of the ascending subarray $ans = \max(ans, t)$. Otherwise, the current element does not satisfy the condition of the ascending subarray, so reset the sum $t$ of the current ascending subarray to the current element, i.e., $t = nums[i]$.

After the traversal, return the maximum sum of the ascending subarray $ans$.

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

<!-- tabs:start -->

### **Python3**
Expand Down
Loading