Skip to content

feat: update solutions to lc problem: No.0848,0849 #3408

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
Aug 13, 2024
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
25 changes: 1 addition & 24 deletions solution/0800-0899/0848.Shifting Letters/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)$。

Expand All @@ -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
Expand Down
29 changes: 5 additions & 24 deletions solution/0800-0899/0848.Shifting Letters/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ After shifting the first 3 letters of s by 9, we have "rpl", the answe

<!-- solution:start -->

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

<!-- tabs:start -->

Expand All @@ -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
Expand Down
23 changes: 7 additions & 16 deletions solution/0800-0899/0848.Shifting Letters/Solution.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 0 additions & 9 deletions solution/0800-0899/0848.Shifting Letters/Solution2.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ tags:
<strong>解释:
</strong>如果亚历克斯坐在第二个空位(seats[2])上,他到离他最近的人的距离为 2 。
如果亚历克斯坐在其它任何一个空位上,他到离他最近的人的距离为 1 。
因此,他到离他最近的人的最大距离是 2 。
因此,他到离他最近的人的最大距离是 2 。
</pre>

<p><strong>示例 2:</strong></p>
Expand Down Expand Up @@ -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)$。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ This is the maximum distance possible, so the answer is 3.

<!-- solution:start -->

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

<!-- tabs:start -->

Expand Down
Loading