Skip to content

Commit

Permalink
feat: add solutions to lc problems
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Jan 7, 2025
1 parent 64a8514 commit f5fa97f
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ tags:

### 方法一:哈希表

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Hash Table

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.

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

<!-- tabs:start -->

Expand Down
4 changes: 2 additions & 2 deletions solution/2200-2299/2297.Jump Game VIII/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ tags:

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

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

然后我们使用动态规划求解最小代价。设 $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]$。
然后我们使用动态规划求解最小代价。设 $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]$。

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

Expand Down
8 changes: 7 additions & 1 deletion solution/2200-2299/2297.Jump Game VIII/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ The total cost is 2. Note that you cannot jump directly from index 0 to index 2

<!-- solution:start -->

### Solution 1
### Solution 1: Monotonic Stack + Dynamic Programming

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.

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]$.

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

<!-- tabs:start -->

Expand Down
4 changes: 2 additions & 2 deletions solution/2200-2299/2299.Strong Password Checker II/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ tags:

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

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

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

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

Expand Down
10 changes: 9 additions & 1 deletion solution/2200-2299/2299.Strong Password Checker II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation + Bit Manipulation

According to the problem description, we can simulate the process of checking whether the password meets the requirements.

First, we check if the length of the password is less than $8$. If it is, we return $\textit{false}$.

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}$.

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

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ tags:

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

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

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

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

时间复杂度 $O(n)$,其中 $n$ 为 $\textit{num}$ 的大小。空间复杂度 $O(1)$。

<!-- tabs:start -->

#### Python3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,15 @@ It can be shown that 2 is the minimum possible size of a valid set.

<!-- solution:start -->

### Solution 1
### Solution 1: Math + Enumeration

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$.

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}$.

We can also only consider the units digit. If the units digit satisfies the condition, the higher digits can be arbitrary.

The time complexity is $O(n)$, where $n$ is the size of $\textit{num}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down

0 comments on commit f5fa97f

Please sign in to comment.