Skip to content
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

feat: add solutions to lc problem: No.1974 #4118

Merged
merged 1 commit into from
Feb 28, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:贪心

我们初始化答案变量 $\textit{ans}$ 为字符串的长度,表示我们至少需要 $\textit{ans}$ 秒来键入字符串。

接下来,我们遍历字符串,对于每个字符,我们计算当前字符和前一个字符之间的最小距离,将这个距离加到答案中。然后我们更新当前字符为前一个字符,继续遍历。

时间复杂度 $O(n)$,其中 $n$ 为字符串的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -102,13 +108,11 @@ tags:
```python
class Solution:
def minTimeToType(self, word: str) -> int:
ans = prev = 0
for c in word:
curr = ord(c) - ord('a')
t = abs(prev - curr)
t = min(t, 26 - t)
ans += t + 1
prev = curr
ans, a = len(word), ord("a")
for c in map(ord, word):
d = abs(c - a)
ans += min(d, 26 - d)
a = c
return ans
```

Expand All @@ -117,14 +121,12 @@ class Solution:
```java
class Solution {
public int minTimeToType(String word) {
int ans = 0;
int prev = 0;
int ans = word.length();
char a = 'a';
for (char c : word.toCharArray()) {
int curr = c - 'a';
int t = Math.abs(prev - curr);
t = Math.min(t, 26 - t);
ans += t + 1;
prev = curr;
int d = Math.abs(a - c);
ans += Math.min(d, 26 - d);
a = c;
}
return ans;
}
Expand All @@ -137,14 +139,12 @@ class Solution {
class Solution {
public:
int minTimeToType(string word) {
int ans = 0;
int prev = 0;
for (char& c : word) {
int curr = c - 'a';
int t = abs(prev - curr);
t = min(t, 26 - t);
ans += t + 1;
prev = curr;
int ans = word.length();
char a = 'a';
for (char c : word) {
int d = abs(a - c);
ans += min(d, 26 - d);
a = c;
}
return ans;
}
Expand All @@ -155,22 +155,29 @@ public:

```go
func minTimeToType(word string) int {
ans, prev := 0, 0
ans := len(word)
a := rune('a')
for _, c := range word {
curr := int(c - 'a')
t := abs(prev - curr)
t = min(t, 26-t)
ans += t + 1
prev = curr
d := int(max(a-c, c-a))
ans += min(d, 26-d)
a = c
}
return ans
}
```

func abs(x int) int {
if x < 0 {
return -x
}
return x
#### TypeScript

```ts
function minTimeToType(word: string): number {
let a = 'a'.charCodeAt(0);
let ans = word.length;
for (const c of word) {
const d = Math.abs(c.charCodeAt(0) - a);
ans += Math.min(d, 26 - d);
a = c.charCodeAt(0);
}
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tags:
<pre>
<strong>Input:</strong> word = &quot;abc&quot;
<strong>Output:</strong> 5
<strong>Explanation:
<strong>Explanation:
</strong>The characters are printed as follows:
- Type the character &#39;a&#39; in 1 second since the pointer is initially on &#39;a&#39;.
- Move the pointer clockwise to &#39;b&#39; in 1 second.
Expand Down Expand Up @@ -91,7 +91,13 @@ The characters are printed as follows:

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy

We initialize the answer variable $\textit{ans}$ to the length of the string, indicating that we need at least $\textit{ans}$ seconds to type the string.

Next, we traverse the string. For each character, we calculate the minimum distance between the current character and the previous character, and add this distance to the answer. Then we update the current character to the previous character and continue traversing.

The time complexity is $O(n)$, where $n$ is the length of the string. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -100,13 +106,11 @@ The characters are printed as follows:
```python
class Solution:
def minTimeToType(self, word: str) -> int:
ans = prev = 0
for c in word:
curr = ord(c) - ord('a')
t = abs(prev - curr)
t = min(t, 26 - t)
ans += t + 1
prev = curr
ans, a = len(word), ord("a")
for c in map(ord, word):
d = abs(c - a)
ans += min(d, 26 - d)
a = c
return ans
```

Expand All @@ -115,14 +119,12 @@ class Solution:
```java
class Solution {
public int minTimeToType(String word) {
int ans = 0;
int prev = 0;
int ans = word.length();
char a = 'a';
for (char c : word.toCharArray()) {
int curr = c - 'a';
int t = Math.abs(prev - curr);
t = Math.min(t, 26 - t);
ans += t + 1;
prev = curr;
int d = Math.abs(a - c);
ans += Math.min(d, 26 - d);
a = c;
}
return ans;
}
Expand All @@ -135,14 +137,12 @@ class Solution {
class Solution {
public:
int minTimeToType(string word) {
int ans = 0;
int prev = 0;
for (char& c : word) {
int curr = c - 'a';
int t = abs(prev - curr);
t = min(t, 26 - t);
ans += t + 1;
prev = curr;
int ans = word.length();
char a = 'a';
for (char c : word) {
int d = abs(a - c);
ans += min(d, 26 - d);
a = c;
}
return ans;
}
Expand All @@ -153,22 +153,29 @@ public:

```go
func minTimeToType(word string) int {
ans, prev := 0, 0
ans := len(word)
a := rune('a')
for _, c := range word {
curr := int(c - 'a')
t := abs(prev - curr)
t = min(t, 26-t)
ans += t + 1
prev = curr
d := int(max(a-c, c-a))
ans += min(d, 26-d)
a = c
}
return ans
}
```

func abs(x int) int {
if x < 0 {
return -x
}
return x
#### TypeScript

```ts
function minTimeToType(word: string): number {
let a = 'a'.charCodeAt(0);
let ans = word.length;
for (const c of word) {
const d = Math.abs(c.charCodeAt(0) - a);
ans += Math.min(d, 26 - d);
a = c.charCodeAt(0);
}
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
class Solution {
public:
int minTimeToType(string word) {
int ans = 0;
int prev = 0;
for (char& c : word) {
int curr = c - 'a';
int t = abs(prev - curr);
t = min(t, 26 - t);
ans += t + 1;
prev = curr;
int ans = word.length();
char a = 'a';
for (char c : word) {
int d = abs(a - c);
ans += min(d, 26 - d);
a = c;
}
return ans;
}
};
};
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
func minTimeToType(word string) int {
ans, prev := 0, 0
ans := len(word)
a := rune('a')
for _, c := range word {
curr := int(c - 'a')
t := abs(prev - curr)
t = min(t, 26-t)
ans += t + 1
prev = curr
d := int(max(a-c, c-a))
ans += min(d, 26-d)
a = c
}
return ans
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
class Solution {
public int minTimeToType(String word) {
int ans = 0;
int prev = 0;
int ans = word.length();
char a = 'a';
for (char c : word.toCharArray()) {
int curr = c - 'a';
int t = Math.abs(prev - curr);
t = Math.min(t, 26 - t);
ans += t + 1;
prev = curr;
int d = Math.abs(a - c);
ans += Math.min(d, 26 - d);
a = c;
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
class Solution:
def minTimeToType(self, word: str) -> int:
ans = prev = 0
for c in word:
curr = ord(c) - ord('a')
t = abs(prev - curr)
t = min(t, 26 - t)
ans += t + 1
prev = curr
ans, a = len(word), ord("a")
for c in map(ord, word):
d = abs(c - a)
ans += min(d, 26 - d)
a = c
return ans
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function minTimeToType(word: string): number {
let a = 'a'.charCodeAt(0);
let ans = word.length;
for (const c of word) {
const d = Math.abs(c.charCodeAt(0) - a);
ans += Math.min(d, 26 - d);
a = c.charCodeAt(0);
}
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Dynamic Programming + Prefix Sum

Define $dp[i][j]$ to represent the number of ways to partition the first $i$ characters of the string `num` such that the length of the last number is $j$. Clearly, the answer is $\sum_{j=0}^{n} dp[n][j]$. The initial value is $dp[0][0] = 1$.

For $dp[i][j]$, the end of the previous number should be $i-j$. We can enumerate $dp[i-j][k]$, where $k \le j$. For the part where $k < j$, i.e., the number of ways with a length less than $j$ can be directly added to $dp[i][j]$, i.e., $dp[i][j] = \sum_{k=0}^{j-1} dp[i-j][k]$. Because the previous number is shorter, it means it is smaller than the current number. Here, prefix sum can be used for optimization.

However, when $k = j$, we need to compare the sizes of the two numbers of the same length. If the previous number is larger than the current number, this situation is invalid, and we should not add it to $dp[i][j]$. Otherwise, we can add it to $dp[i][j]$. Here, we can preprocess the "longest common prefix" in $O(n^2)$ time, and then compare the sizes of two numbers of the same length in $O(1)$ time.

The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Where $n$ is the length of the string `num`.

<!-- tabs:start -->

Expand Down