Skip to content

Commit 2c6cb71

Browse files
authoredJan 7, 2025··
feat: add solutions to lc problems (#3935)
1 parent 64a8514 commit 2c6cb71

File tree

8 files changed

+39
-11
lines changed

8 files changed

+39
-11
lines changed
 

‎solution/2200-2299/2260.Minimum Consecutive Cards to Pick Up/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ tags:
5555

5656
### 方法一:哈希表
5757

58-
我们初始化答案为 $+\infty$,遍历数组,对于每个数字 $x$,如果 $last[x]$ 存在,则表示 $x$ 有一对匹配卡牌,此时更新答案为 $ans = min(ans, i - last[x] + 1)$,最后如果答案为 $+\infty$,则返回 $-1$,否则返回答案。
58+
我们初始化答案为 $+\infty$,遍历数组,对于每个数字 $x$,如果 $\textit{last}[x]$ 存在,则表示 $x$ 有一对匹配卡牌,此时更新答案为 $\textit{ans} = \min(\textit{ans}, i - \textit{last}[x] + 1)$,最后如果答案为 $+\infty$,则返回 $-1$,否则返回答案。
5959

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

‎solution/2200-2299/2260.Minimum Consecutive Cards to Pick Up/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ tags:
5555

5656
<!-- solution:start -->
5757

58-
### Solution 1
58+
### Solution 1: Hash Table
59+
60+
We initialize the answer as $+\infty$. We traverse the array, and for each number $x$, if $\textit{last}[x]$ exists, it means $x$ has a matching pair of cards. In this case, we update the answer to $\textit{ans} = \min(\textit{ans}, i - \textit{last}[x] + 1)$. Finally, if the answer is $+\infty$, we return $-1$; otherwise, we return the answer.
61+
62+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array.
5963

6064
<!-- tabs:start -->
6165

‎solution/2200-2299/2297.Jump Game VIII/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ tags:
7474

7575
### 方法一:单调栈 + 动态规划
7676

77-
根据题目描述,我们实际上需要找到 $nums[i]$ 的下一个大于等于 $nums[i]$ 的位置 $j$,以及下一个小于 $nums[i]$ 的位置 $j$。我们利用单调栈可以在 $O(n)$ 的时间内找到这两个位置,然后构建邻接表 $g$,其中 $g[i]$ 表示下标 $i$ 可以跳转到的下标。
77+
根据题目描述,我们实际上需要找到 $\textit{nums}[i]$ 的下一个大于等于 $\textit{nums}[i]$ 的位置 $j$,以及下一个小于 $\textit{nums}[i]$ 的位置 $j$。我们利用单调栈可以在 $O(n)$ 的时间内找到这两个位置,然后构建邻接表 $g$,其中 $g[i]$ 表示下标 $i$ 可以跳转到的下标。
7878

79-
然后我们使用动态规划求解最小代价。设 $f[i]$ 表示跳转到下标 $i$ 的最小代价,初始时 $f[0] = 0$,其余 $f[i] = \infty$。我们从小到大枚举下标 $i$,对于每个 $i$,我们枚举 $g[i]$ 中的每个下标 $j$,进行状态转移 $f[j] = \min(f[j], f[i] + costs[j])$。答案为 $f[n - 1]$。
79+
然后我们使用动态规划求解最小代价。设 $f[i]$ 表示跳转到下标 $i$ 的最小代价,初始时 $f[0] = 0$,其余 $f[i] = \infty$。我们从小到大枚举下标 $i$,对于每个 $i$,我们枚举 $g[i]$ 中的每个下标 $j$,进行状态转移 $f[j] = \min(f[j], f[i] + \textit{costs}[j])$。答案为 $f[n - 1]$。
8080

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

‎solution/2200-2299/2297.Jump Game VIII/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ The total cost is 2. Note that you cannot jump directly from index 0 to index 2
7272

7373
<!-- solution:start -->
7474

75-
### Solution 1
75+
### Solution 1: Monotonic Stack + Dynamic Programming
76+
77+
According to the problem description, we need to find the next position $j$ where $\textit{nums}[j]$ is greater than or equal to $\textit{nums}[i]$, and the next position $j$ where $\textit{nums}[j]$ is less than $\textit{nums}[i]$. We can use a monotonic stack to find these two positions in $O(n)$ time, and then construct an adjacency list $g$, where $g[i]$ represents the indices that index $i$ can jump to.
78+
79+
Then we use dynamic programming to find the minimum cost. Let $f[i]$ represent the minimum cost to jump to index $i$. Initially, $f[0] = 0$ and the rest $f[i] = \infty$. We enumerate the indices $i$ from small to large. For each $i$, we enumerate each index $j$ in $g[i]$ and perform the state transition $f[j] = \min(f[j], f[i] + \textit{costs}[j])$. The answer is $f[n - 1]$.
80+
81+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array.
7682

7783
<!-- tabs:start -->
7884

‎solution/2200-2299/2299.Strong Password Checker II/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ tags:
7272

7373
根据题目描述,我们可以模拟检查密码是否满足题目要求的过程。
7474

75-
首先,我们检查密码的长度是否小于 $8$,如果是,则返回 `false`
75+
首先,我们检查密码的长度是否小于 $8$,如果是,则返回 $\textit{false}$
7676

77-
接下来,我们用一个掩码 `mask` 来记录密码是否包含小写字母、大写字母、数字和特殊字符。我们遍历密码,每次遍历到一个字符,先判断它是否和前一个字符相同,如果是,则返回 `false`。然后,根据字符的类型更新掩码 `mask`。最后,我们检查掩码 `mask` 是否为 $15$,如果是,则返回 `true`,否则返回 `false`
77+
接下来,我们用一个掩码 $\textit{mask}$ 来记录密码是否包含小写字母、大写字母、数字和特殊字符。我们遍历密码,每次遍历到一个字符,先判断它是否和前一个字符相同,如果是,则返回 $\textit{false}$。然后,根据字符的类型更新掩码 $\textit{mask}$。最后,我们检查掩码 $\textit{mask}$ 是否为 $15$,如果是,则返回 $\textit{true}$,否则返回 $\textit{false}$
7878

7979
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为密码的长度。
8080

‎solution/2200-2299/2299.Strong Password Checker II/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,15 @@ tags:
6969

7070
<!-- solution:start -->
7171

72-
### Solution 1
72+
### Solution 1: Simulation + Bit Manipulation
73+
74+
According to the problem description, we can simulate the process of checking whether the password meets the requirements.
75+
76+
First, we check if the length of the password is less than $8$. If it is, we return $\textit{false}$.
77+
78+
Next, we use a mask $\textit{mask}$ to record whether the password contains lowercase letters, uppercase letters, digits, and special characters. We traverse the password, and for each character, we first check if it is the same as the previous character. If it is, we return $\textit{false}$. Then, we update the mask $\textit{mask}$ based on the character type. Finally, we check if the mask $\textit{mask}$ is $15$. If it is, we return $\textit{true}$; otherwise, we return $\textit{false}$.
79+
80+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the password.
7381

7482
<!-- tabs:start -->
7583

‎solution/2300-2399/2310.Sum of Numbers With Units Digit K/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,14 @@ tags:
8282

8383
### 方法一:数学 + 枚举
8484

85-
符合拆分条件的每个数都可以表示成 $10x_i+k$,若总共有 $n$ 个数,那么 $num-n*k$ 必然是 $10$ 的倍数。
85+
符合拆分条件的每个数都可以表示成 $10x_i+k$,若总共有 $n$ 个数,那么 $\textit{num}-n \times k$ 必然是 $10$ 的倍数。
8686

87-
我们从小到达枚举 $n$,找到第一个满足 $num-n*k$ 是 $10$ 的倍数的 $n$。由于 $n$ 不会超过 $num$,因此 $n$ 最大枚举至 $num$。
87+
我们从小到达枚举 $n$,找到第一个满足 $\textit{num}-n \times k$ 是 $10$ 的倍数的 $n$。由于 $n$ 不会超过 $\textit{num}$,因此 $n$ 最大枚举至 $\textit{num}$。
8888

8989
也可以只考虑个位,个位满足,高位随意。
9090

91+
时间复杂度 $O(n)$,其中 $n$ 为 $\textit{num}$ 的大小。空间复杂度 $O(1)$。
92+
9193
<!-- tabs:start -->
9294

9395
#### Python3

‎solution/2300-2399/2310.Sum of Numbers With Units Digit K/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@ It can be shown that 2 is the minimum possible size of a valid set.
7979

8080
<!-- solution:start -->
8181

82-
### Solution 1
82+
### Solution 1: Math + Enumeration
83+
84+
Each number that meets the splitting condition can be represented as $10x_i + k$. If there are $n$ such numbers, then $\textit{num} - n \times k$ must be a multiple of $10$.
85+
86+
We enumerate $n$ from small to large, and find the first $n$ that satisfies $\textit{num} - n \times k$ being a multiple of $10$. Since $n$ cannot exceed $\textit{num}$, the maximum value of $n$ is $\textit{num}$.
87+
88+
We can also only consider the units digit. If the units digit satisfies the condition, the higher digits can be arbitrary.
89+
90+
The time complexity is $O(n)$, where $n$ is the size of $\textit{num}$. The space complexity is $O(1)$.
8391

8492
<!-- tabs:start -->
8593

0 commit comments

Comments
 (0)