From 7cdeb92d36b0e10707a6b44c09a68cb2df04ec13 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 13 Aug 2024 12:36:34 +0800 Subject: [PATCH] feat: update solutions to lc problem: No.0848,0849 --- .../0800-0899/0848.Shifting Letters/README.md | 25 +--------------- .../0848.Shifting Letters/README_EN.md | 29 ++++--------------- .../0848.Shifting Letters/Solution.py | 23 +++++---------- .../0848.Shifting Letters/Solution2.py | 9 ------ .../README.md | 10 +++---- .../README_EN.md | 10 ++++++- 6 files changed, 27 insertions(+), 79 deletions(-) delete mode 100644 solution/0800-0899/0848.Shifting Letters/Solution2.py diff --git a/solution/0800-0899/0848.Shifting Letters/README.md b/solution/0800-0899/0848.Shifting Letters/README.md index 7a47204150a0b..297344cd8d955 100644 --- a/solution/0800-0899/0848.Shifting Letters/README.md +++ b/solution/0800-0899/0848.Shifting Letters/README.md @@ -71,7 +71,7 @@ tags: ### 方法一:后缀和 -对于字符串 $s$ 中的每个字符,我们需要计算其最终的偏移量,即 `shifts[i]` 与 `shifts[i + 1]` 与 `shifts[i + 2]` ... 的和。我们可以使用后缀和的思想,从后往前遍历 `shifts`,计算每个字符的最终偏移量,然后对 $26$ 取模,得到最终的字符。 +对于字符串 $s$ 中的每个字符,我们需要计算其最终的偏移量,即 $\textit{shifts}[i]$ 与 $\textit{shifts}[i + 1]$ 与 $\textit{shifts}[i + 2]$ ... 的和。我们可以使用后缀和的思想,从后往前遍历 $\textit{shifts}$,计算每个字符的最终偏移量,然后对 $26$ 取模,得到最终的字符。 时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。 @@ -91,29 +91,6 @@ class Solution: return ''.join(s) ``` -#### Python3 - -```python -class Solution: - def shiftingLetters(self, s: str, shifts: List[int]) -> str: - n = len(s) - d = [0] * (n + 1) - for i, c in enumerate(s): - v = ord(c) - ord('a') - d[i] += v - d[i + 1] -= v - for i, x in enumerate(shifts): - d[0] += x - d[i + 1] -= x - t = 0 - ans = [] - for i in range(n): - d[i] %= 26 - ans.append(ascii_lowercase[d[i]]) - d[i + 1] += d[i] - return ''.join(ans) -``` - #### Java ```java diff --git a/solution/0800-0899/0848.Shifting Letters/README_EN.md b/solution/0800-0899/0848.Shifting Letters/README_EN.md index 47a0f2a01423b..56ad1fea51b3a 100644 --- a/solution/0800-0899/0848.Shifting Letters/README_EN.md +++ b/solution/0800-0899/0848.Shifting Letters/README_EN.md @@ -65,7 +65,11 @@ After shifting the first 3 letters of s by 9, we have "rpl", the answe -### Solution 1 +### Solution 1: Suffix Sum + +For each character in the string $s$, we need to calculate its final shift amount, which is the sum of $\textit{shifts}[i]$, $\textit{shifts}[i + 1]$, $\textit{shifts}[i + 2]$, and so on. We can use the concept of suffix sum, traversing $\textit{shifts}$ from back to front, calculating the final shift amount for each character, and then taking modulo $26$ to get the final character. + +The time complexity is $O(n)$, where $n$ is the length of the string $s$. Ignoring the space consumption of the answer, the space complexity is $O(1)$. @@ -83,29 +87,6 @@ class Solution: return ''.join(s) ``` -#### Python3 - -```python -class Solution: - def shiftingLetters(self, s: str, shifts: List[int]) -> str: - n = len(s) - d = [0] * (n + 1) - for i, c in enumerate(s): - v = ord(c) - ord('a') - d[i] += v - d[i + 1] -= v - for i, x in enumerate(shifts): - d[0] += x - d[i + 1] -= x - t = 0 - ans = [] - for i in range(n): - d[i] %= 26 - ans.append(ascii_lowercase[d[i]]) - d[i + 1] += d[i] - return ''.join(ans) -``` - #### Java ```java diff --git a/solution/0800-0899/0848.Shifting Letters/Solution.py b/solution/0800-0899/0848.Shifting Letters/Solution.py index f0e0e8f174153..44ff7f0c73667 100644 --- a/solution/0800-0899/0848.Shifting Letters/Solution.py +++ b/solution/0800-0899/0848.Shifting Letters/Solution.py @@ -1,18 +1,9 @@ class Solution: def shiftingLetters(self, s: str, shifts: List[int]) -> str: - n = len(s) - d = [0] * (n + 1) - for i, c in enumerate(s): - v = ord(c) - ord('a') - d[i] += v - d[i + 1] -= v - for i, x in enumerate(shifts): - d[0] += x - d[i + 1] -= x - t = 0 - ans = [] - for i in range(n): - d[i] %= 26 - ans.append(ascii_lowercase[d[i]]) - d[i + 1] += d[i] - return ''.join(ans) + n, t = len(s), 0 + s = list(s) + for i in range(n - 1, -1, -1): + t += shifts[i] + j = (ord(s[i]) - ord("a") + t) % 26 + s[i] = ascii_lowercase[j] + return "".join(s) diff --git a/solution/0800-0899/0848.Shifting Letters/Solution2.py b/solution/0800-0899/0848.Shifting Letters/Solution2.py deleted file mode 100644 index 24835fc8e6a8e..0000000000000 --- a/solution/0800-0899/0848.Shifting Letters/Solution2.py +++ /dev/null @@ -1,9 +0,0 @@ -class Solution: - def shiftingLetters(self, s: str, shifts: List[int]) -> str: - n, t = len(s), 0 - s = list(s) - for i in range(n - 1, -1, -1): - t += shifts[i] - j = (ord(s[i]) - ord('a') + t) % 26 - s[i] = ascii_lowercase[j] - return ''.join(s) diff --git a/solution/0800-0899/0849.Maximize Distance to Closest Person/README.md b/solution/0800-0899/0849.Maximize Distance to Closest Person/README.md index 6c5b0fc0941ea..3017704b4bd90 100644 --- a/solution/0800-0899/0849.Maximize Distance to Closest Person/README.md +++ b/solution/0800-0899/0849.Maximize Distance to Closest Person/README.md @@ -34,7 +34,7 @@ tags: 解释: 如果亚历克斯坐在第二个空位(seats[2])上,他到离他最近的人的距离为 2 。 如果亚历克斯坐在其它任何一个空位上,他到离他最近的人的距离为 1 。 -因此,他到离他最近的人的最大距离是 2 。 +因此,他到离他最近的人的最大距离是 2 。

示例 2:

@@ -73,13 +73,13 @@ tags: ### 方法一:一次遍历 -我们定义两个变量 $first$ 和 $last$ 分别表示第一个人和最后一个人的位置,用变量 $d$ 表示两个人之间的最大距离。 +我们定义两个变量 $\textit{first}$ 和 $\textit{last}$ 分别表示第一个人和最后一个人的位置,用变量 $d$ 表示两个人之间的最大距离。 -然后遍历数组 $seats$,如果当前位置有人,如果此前 $last$ 更新过,说明此前有人,此时更新 $d = \max(d, i - last)$;如果此前 $first$ 没有更新过,说明此前没有人,此时更新 $first = i$。接下来更新 $last = i$。 +然后遍历数组 $\textit{seats}$,如果当前位置有人,如果此前 $\textit{last}$ 更新过,说明此前有人,此时更新 $d = \max(d, i - \textit{last})$;如果此前 $\textit{first}$ 没有更新过,说明此前没有人,此时更新 $\textit{first} = i$。接下来更新 $\textit{last} = i$。 -最后返回 $\max(first, n - last - 1, d / 2)$ 即可。 +最后返回 $\max(\textit{first}, n - \textit{last} - 1, d / 2)$ 即可。 -时间复杂度 $O(n)$,其中 $n$ 为数组 $seats$ 的长度。空间复杂度 $O(1)$。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{seats}$ 的长度。空间复杂度 $O(1)$。 diff --git a/solution/0800-0899/0849.Maximize Distance to Closest Person/README_EN.md b/solution/0800-0899/0849.Maximize Distance to Closest Person/README_EN.md index 70476b9f54f1b..165ab1b9418e9 100644 --- a/solution/0800-0899/0849.Maximize Distance to Closest Person/README_EN.md +++ b/solution/0800-0899/0849.Maximize Distance to Closest Person/README_EN.md @@ -69,7 +69,15 @@ This is the maximum distance possible, so the answer is 3. -### Solution 1 +### Solution 1: Single Traversal + +We define two variables $\textit{first}$ and $\textit{last}$ to represent the positions of the first and last person, respectively. We use the variable $d$ to represent the maximum distance between two people. + +Then, we traverse the array $\textit{seats}$. If the current position is occupied, and if $\textit{last}$ has been updated before, it means there was someone before, so we update $d = \max(d, i - \textit{last})$. If $\textit{first}$ has not been updated before, it means there was no one before, so we update $\textit{first} = i$. Next, we update $\textit{last} = i$. + +Finally, we return $\max(\textit{first}, n - \textit{last} - 1, d / 2)$. + +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{seats}$. The space complexity is $O(1)$.