Skip to content

Commit 3c456ce

Browse files
authored
feat: update solutions to lc problems: No.0848,0849 (#3408)
1 parent cd6eed5 commit 3c456ce

File tree

6 files changed

+27
-79
lines changed

6 files changed

+27
-79
lines changed

solution/0800-0899/0848.Shifting Letters/README.md

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ tags:
7171

7272
### 方法一:后缀和
7373

74-
对于字符串 $s$ 中的每个字符,我们需要计算其最终的偏移量,即 `shifts[i]``shifts[i + 1]``shifts[i + 2]` ... 的和。我们可以使用后缀和的思想,从后往前遍历 `shifts`,计算每个字符的最终偏移量,然后对 $26$ 取模,得到最终的字符。
74+
对于字符串 $s$ 中的每个字符,我们需要计算其最终的偏移量,即 $\textit{shifts}[i]$$\textit{shifts}[i + 1]$$\textit{shifts}[i + 2]$ ... 的和。我们可以使用后缀和的思想,从后往前遍历 $\textit{shifts}$,计算每个字符的最终偏移量,然后对 $26$ 取模,得到最终的字符。
7575

7676
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。
7777

@@ -91,29 +91,6 @@ class Solution:
9191
return ''.join(s)
9292
```
9393

94-
#### Python3
95-
96-
```python
97-
class Solution:
98-
def shiftingLetters(self, s: str, shifts: List[int]) -> str:
99-
n = len(s)
100-
d = [0] * (n + 1)
101-
for i, c in enumerate(s):
102-
v = ord(c) - ord('a')
103-
d[i] += v
104-
d[i + 1] -= v
105-
for i, x in enumerate(shifts):
106-
d[0] += x
107-
d[i + 1] -= x
108-
t = 0
109-
ans = []
110-
for i in range(n):
111-
d[i] %= 26
112-
ans.append(ascii_lowercase[d[i]])
113-
d[i + 1] += d[i]
114-
return ''.join(ans)
115-
```
116-
11794
#### Java
11895

11996
```java

solution/0800-0899/0848.Shifting Letters/README_EN.md

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ After shifting the first 3 letters of s by 9, we have "rpl", the answe
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Suffix Sum
69+
70+
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.
71+
72+
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)$.
6973

7074
<!-- tabs:start -->
7175

@@ -83,29 +87,6 @@ class Solution:
8387
return ''.join(s)
8488
```
8589

86-
#### Python3
87-
88-
```python
89-
class Solution:
90-
def shiftingLetters(self, s: str, shifts: List[int]) -> str:
91-
n = len(s)
92-
d = [0] * (n + 1)
93-
for i, c in enumerate(s):
94-
v = ord(c) - ord('a')
95-
d[i] += v
96-
d[i + 1] -= v
97-
for i, x in enumerate(shifts):
98-
d[0] += x
99-
d[i + 1] -= x
100-
t = 0
101-
ans = []
102-
for i in range(n):
103-
d[i] %= 26
104-
ans.append(ascii_lowercase[d[i]])
105-
d[i + 1] += d[i]
106-
return ''.join(ans)
107-
```
108-
10990
#### Java
11091

11192
```java
Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
11
class Solution:
22
def shiftingLetters(self, s: str, shifts: List[int]) -> str:
3-
n = len(s)
4-
d = [0] * (n + 1)
5-
for i, c in enumerate(s):
6-
v = ord(c) - ord('a')
7-
d[i] += v
8-
d[i + 1] -= v
9-
for i, x in enumerate(shifts):
10-
d[0] += x
11-
d[i + 1] -= x
12-
t = 0
13-
ans = []
14-
for i in range(n):
15-
d[i] %= 26
16-
ans.append(ascii_lowercase[d[i]])
17-
d[i + 1] += d[i]
18-
return ''.join(ans)
3+
n, t = len(s), 0
4+
s = list(s)
5+
for i in range(n - 1, -1, -1):
6+
t += shifts[i]
7+
j = (ord(s[i]) - ord("a") + t) % 26
8+
s[i] = ascii_lowercase[j]
9+
return "".join(s)

solution/0800-0899/0848.Shifting Letters/Solution2.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

solution/0800-0899/0849.Maximize Distance to Closest Person/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ tags:
3434
<strong>解释:
3535
</strong>如果亚历克斯坐在第二个空位(seats[2])上,他到离他最近的人的距离为 2 。
3636
如果亚历克斯坐在其它任何一个空位上,他到离他最近的人的距离为 1 。
37-
因此,他到离他最近的人的最大距离是 2 。
37+
因此,他到离他最近的人的最大距离是 2 。
3838
</pre>
3939

4040
<p><strong>示例 2:</strong></p>
@@ -73,13 +73,13 @@ tags:
7373

7474
### 方法一:一次遍历
7575

76-
我们定义两个变量 $first$ 和 $last$ 分别表示第一个人和最后一个人的位置,用变量 $d$ 表示两个人之间的最大距离。
76+
我们定义两个变量 $\textit{first}$ 和 $\textit{last}$ 分别表示第一个人和最后一个人的位置,用变量 $d$ 表示两个人之间的最大距离。
7777

78-
然后遍历数组 $seats$,如果当前位置有人,如果此前 $last$ 更新过,说明此前有人,此时更新 $d = \max(d, i - last)$;如果此前 $first$ 没有更新过,说明此前没有人,此时更新 $first = i$。接下来更新 $last = i$。
78+
然后遍历数组 $\textit{seats}$,如果当前位置有人,如果此前 $\textit{last}$ 更新过,说明此前有人,此时更新 $d = \max(d, i - \textit{last})$;如果此前 $\textit{first}$ 没有更新过,说明此前没有人,此时更新 $\textit{first} = i$。接下来更新 $\textit{last} = i$。
7979

80-
最后返回 $\max(first, n - last - 1, d / 2)$ 即可。
80+
最后返回 $\max(\textit{first}, n - \textit{last} - 1, d / 2)$ 即可。
8181

82-
时间复杂度 $O(n)$,其中 $n$ 为数组 $seats$ 的长度。空间复杂度 $O(1)$。
82+
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{seats}$ 的长度。空间复杂度 $O(1)$。
8383

8484
<!-- tabs:start -->
8585

solution/0800-0899/0849.Maximize Distance to Closest Person/README_EN.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,15 @@ This is the maximum distance possible, so the answer is 3.
6969

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

72-
### Solution 1
72+
### Solution 1: Single Traversal
73+
74+
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.
75+
76+
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$.
77+
78+
Finally, we return $\max(\textit{first}, n - \textit{last} - 1, d / 2)$.
79+
80+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{seats}$. The space complexity is $O(1)$.
7381

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

0 commit comments

Comments
 (0)