You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2100-2199/2138.Divide a String Into Groups of Size k/README_EN.md
+25-7
Original file line number
Diff line number
Diff line change
@@ -71,7 +71,11 @@ Thus, the 4 groups formed are "abc", "def", "ghi",
71
71
72
72
<!-- solution:start -->
73
73
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$.
75
79
76
80
<!-- tabs:start -->
77
81
@@ -108,10 +112,13 @@ class Solution {
108
112
public:
109
113
vector<string> divideString(string s, int k, char fill) {
110
114
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
+
}
113
118
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
+
}
115
122
return ans;
116
123
}
117
124
};
@@ -120,16 +127,27 @@ public:
120
127
#### Go
121
128
122
129
```go
123
-
func divideString(s string, k int, fill byte) []string {
130
+
func divideString(s string, k int, fill byte) (ans []string) {
124
131
n := len(s)
125
132
if n%k != 0 {
126
133
s += strings.Repeat(string(fill), k-n%k)
127
134
}
128
-
var ans []string
129
135
for i := 0; i < len(s)/k; i++ {
130
136
ans = append(ans, s[i*k:(i+1)*k])
131
137
}
132
-
return ans
138
+
return
139
+
}
140
+
```
141
+
142
+
#### TypeScript
143
+
144
+
```ts
145
+
function divideString(s:string, k:number, fill:string):string[] {
Copy file name to clipboardexpand all lines: solution/2100-2199/2140.Solving Questions With Brainpower/README_EN.md
+25-2
Original file line number
Diff line number
Diff line change
@@ -77,7 +77,18 @@ Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.
77
77
78
78
<!-- solution:start -->
79
79
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.
81
92
82
93
<!-- tabs:start -->
83
94
@@ -196,7 +207,19 @@ function mostPoints(questions: number[][]): number {
196
207
197
208
<!-- solution:start -->
198
209
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.
Copy file name to clipboardexpand all lines: solution/2100-2199/2141.Maximum Running Time of N Computers/README_EN.md
+9-1
Original file line number
Diff line number
Diff line change
@@ -70,7 +70,15 @@ We can run the two computers simultaneously for at most 2 minutes, so we return
70
70
71
71
<!-- solution:start -->
72
72
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)$.
Copy file name to clipboardexpand all lines: solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/README_EN.md
+5-1
Original file line number
Diff line number
Diff line change
@@ -78,7 +78,11 @@ Hence, the minimum cost to buy all candies is 5 + 5 = 10.
78
78
79
79
<!-- solution:start -->
80
80
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.
0 commit comments