Skip to content

Commit 85aa18f

Browse files
authored
feat: add solutions to lc problems: No.2138,2140,2141 (doocs#3158)
* No.2138.Divide a String Into Groups of Size k * No.2140.Solving Questions With Brainpower * No.2141.Maximum Running Time of N Computers
1 parent 6c44148 commit 85aa18f

File tree

8 files changed

+104
-24
lines changed

8 files changed

+104
-24
lines changed

solution/2100-2199/2138.Divide a String Into Groups of Size k/README.md

+25-7
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ tags:
7171

7272
<!-- solution:start -->
7373

74-
### 方法一
74+
### 方法一:模拟
75+
76+
我们可以直接模拟题目描述的过程,将字符串 $s$ 按照长度 $k$ 进行分组,然后对于最后一组不足 $k$ 个字符的情况,使用字符 $\text{fill}$ 进行填充。
77+
78+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
7579

7680
<!-- tabs:start -->
7781

@@ -108,10 +112,13 @@ class Solution {
108112
public:
109113
vector<string> divideString(string s, int k, char fill) {
110114
int n = s.size();
111-
if (n % k)
112-
for (int i = 0; i < k - n % k; ++i) s.push_back(fill);
115+
if (n % k) {
116+
s += string(k - n % k, fill);
117+
}
113118
vector<string> ans;
114-
for (int i = 0; i < s.size() / k; ++i) ans.push_back(s.substr(i * k, k));
119+
for (int i = 0; i < s.size() / k; ++i) {
120+
ans.push_back(s.substr(i * k, k));
121+
}
115122
return ans;
116123
}
117124
};
@@ -120,16 +127,27 @@ public:
120127
#### Go
121128
122129
```go
123-
func divideString(s string, k int, fill byte) []string {
130+
func divideString(s string, k int, fill byte) (ans []string) {
124131
n := len(s)
125132
if n%k != 0 {
126133
s += strings.Repeat(string(fill), k-n%k)
127134
}
128-
var ans []string
129135
for i := 0; i < len(s)/k; i++ {
130136
ans = append(ans, s[i*k:(i+1)*k])
131137
}
132-
return ans
138+
return
139+
}
140+
```
141+
142+
#### TypeScript
143+
144+
```ts
145+
function divideString(s: string, k: number, fill: string): string[] {
146+
const ans: string[] = [];
147+
for (let i = 0; i < s.length; i += k) {
148+
ans.push(s.slice(i, i + k).padEnd(k, fill));
149+
}
150+
return ans;
133151
}
134152
```
135153

solution/2100-2199/2138.Divide a String Into Groups of Size k/README_EN.md

+25-7
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ Thus, the 4 groups formed are &quot;abc&quot;, &quot;def&quot;, &quot;ghi&quot;,
7171

7272
<!-- solution:start -->
7373

74-
### Solution 1
74+
### Solution 1: Simulation
75+
76+
We can directly simulate the process described in the problem statement, dividing the string $s$ into groups of length $k$. For the last group, if it contains fewer than $k$ characters, we use the character $\text{fill}$ to pad it.
77+
78+
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$.
7579

7680
<!-- tabs:start -->
7781

@@ -108,10 +112,13 @@ class Solution {
108112
public:
109113
vector<string> divideString(string s, int k, char fill) {
110114
int n = s.size();
111-
if (n % k)
112-
for (int i = 0; i < k - n % k; ++i) s.push_back(fill);
115+
if (n % k) {
116+
s += string(k - n % k, fill);
117+
}
113118
vector<string> ans;
114-
for (int i = 0; i < s.size() / k; ++i) ans.push_back(s.substr(i * k, k));
119+
for (int i = 0; i < s.size() / k; ++i) {
120+
ans.push_back(s.substr(i * k, k));
121+
}
115122
return ans;
116123
}
117124
};
@@ -120,16 +127,27 @@ public:
120127
#### Go
121128
122129
```go
123-
func divideString(s string, k int, fill byte) []string {
130+
func divideString(s string, k int, fill byte) (ans []string) {
124131
n := len(s)
125132
if n%k != 0 {
126133
s += strings.Repeat(string(fill), k-n%k)
127134
}
128-
var ans []string
129135
for i := 0; i < len(s)/k; i++ {
130136
ans = append(ans, s[i*k:(i+1)*k])
131137
}
132-
return ans
138+
return
139+
}
140+
```
141+
142+
#### TypeScript
143+
144+
```ts
145+
function divideString(s: string, k: number, fill: string): string[] {
146+
const ans: string[] = [];
147+
for (let i = 0; i < s.length; i += k) {
148+
ans.push(s.slice(i, i + k).padEnd(k, fill));
149+
}
150+
return ans;
133151
}
134152
```
135153

solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ class Solution {
22
public:
33
vector<string> divideString(string s, int k, char fill) {
44
int n = s.size();
5-
if (n % k)
6-
for (int i = 0; i < k - n % k; ++i) s.push_back(fill);
5+
if (n % k) {
6+
s += string(k - n % k, fill);
7+
}
78
vector<string> ans;
8-
for (int i = 0; i < s.size() / k; ++i) ans.push_back(s.substr(i * k, k));
9+
for (int i = 0; i < s.size() / k; ++i) {
10+
ans.push_back(s.substr(i * k, k));
11+
}
912
return ans;
1013
}
1114
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
func divideString(s string, k int, fill byte) []string {
1+
func divideString(s string, k int, fill byte) (ans []string) {
22
n := len(s)
33
if n%k != 0 {
44
s += strings.Repeat(string(fill), k-n%k)
55
}
6-
var ans []string
76
for i := 0; i < len(s)/k; i++ {
87
ans = append(ans, s[i*k:(i+1)*k])
98
}
10-
return ans
9+
return
1110
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function divideString(s: string, k: number, fill: string): string[] {
2+
const ans: string[] = [];
3+
for (let i = 0; i < s.length; i += k) {
4+
ans.push(s.slice(i, i + k).padEnd(k, fill));
5+
}
6+
return ans;
7+
}

solution/2100-2199/2140.Solving Questions With Brainpower/README_EN.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,18 @@ Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.
7777

7878
<!-- solution:start -->
7979

80-
### Solution 1
80+
### Solution 1: Memoization Search
81+
82+
We design a function $dfs(i)$, which represents the maximum score that can be obtained starting from the $i$-th problem. Therefore, the answer is $dfs(0)$.
83+
84+
The calculation method of the function $dfs(i)$ is as follows:
85+
86+
- If $i \geq n$, it means that all problems have been solved, return $0$;
87+
- Otherwise, let the score of the $i$-th problem be $p$, and the number of problems to skip be $b$, then $dfs(i) = \max(p + dfs(i + b + 1), dfs(i + 1))$.
88+
89+
To avoid repeated calculations, we can use the method of memoization search, using an array $f$ to record the values of all already computed $dfs(i)$.
90+
91+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of problems.
8192

8293
<!-- tabs:start -->
8394

@@ -196,7 +207,19 @@ function mostPoints(questions: number[][]): number {
196207

197208
<!-- solution:start -->
198209

199-
### Solution 2
210+
### Solution 2: Dynamic Programming
211+
212+
We define $f[i]$ as the maximum score that can be obtained starting from the $i$-th problem. Therefore, the answer is $f[0]$.
213+
214+
Considering $f[i]$, let the score of the $i$-th problem be $p$, and the number of problems to skip be $b$. If we solve the $i$-th problem, then we need to solve the problem after skipping $b$ problems, thus $f[i] = p + f[i + b + 1]$. If we skip the $i$-th problem, then we start solving from the $(i + 1)$-th problem, thus $f[i] = f[i + 1]$. We take the maximum value of the two. The state transition equation is as follows:
215+
216+
$$
217+
f[i] = \max(p + f[i + b + 1], f[i + 1])
218+
$$
219+
220+
We calculate the values of $f$ from back to front, and finally return $f[0]$.
221+
222+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of problems.
200223

201224
<!-- tabs:start -->
202225

solution/2100-2199/2141.Maximum Running Time of N Computers/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ We can run the two computers simultaneously for at most 2 minutes, so we return
7070

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

73-
### Solution 1
73+
### Solution 1: Binary Search
74+
75+
We notice that if we can run $n$ computers simultaneously for $t$ minutes, then we can also run $n$ computers simultaneously for $t' \le t$ minutes, which shows monotonicity. Therefore, we can use the binary search method to find the maximum $t$.
76+
77+
We define the left boundary of the binary search as $l=0$ and the right boundary as $r=\sum_{i=0}^{n-1} batteries[i]$. During each binary search iteration, we use a variable $mid$ to represent the current middle value, i.e., $mid = (l + r + 1) >> 1$. We check if there exists a scheme that allows $n$ computers to run simultaneously for $mid$ minutes. If such a scheme exists, then we update $l$ to $mid$; otherwise, we update $r$ to $mid - 1$. Finally, we return $l$ as the answer.
78+
79+
The problem is transformed into how to determine if there exists a scheme that allows $n$ computers to run simultaneously for $mid$ minutes. If a battery can run for more minutes than $mid$, since the computers run simultaneously for $mid$ minutes and a battery can only power one computer at a time, we can only use this battery for $mid$ minutes. If a battery can run for minutes less than or equal to $mid$, we can use all the power of this battery. Therefore, we calculate the total minutes $s$ that all batteries can power, and if $s \ge n \times mid$, then we can make $n$ computers run simultaneously for $mid$ minutes.
80+
81+
The time complexity is $O(n \times \log M)$, where $M$ is the total power of all batteries, and the space complexity is $O(1)$.
7482

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

solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/README_EN.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ Hence, the minimum cost to buy all candies is 5 + 5 = 10.
7878

7979
<!-- solution:start -->
8080

81-
### Solution 1
81+
### Solution 1: Greedy Algorithm
82+
83+
We can first sort the candies by price in descending order, then for every three candies, we take two. This ensures that the candies we get for free are the most expensive, thereby minimizing the total cost.
84+
85+
The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of candies.
8286

8387
<!-- tabs:start -->
8488

0 commit comments

Comments
 (0)