From 37c001014c9b3659bc3eb0b428c8460042d38c5d Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 24 Jun 2024 19:59:28 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problems: No.2138,2140,2141 * No.2138.Divide a String Into Groups of Size k * No.2140.Solving Questions With Brainpower * No.2141.Maximum Running Time of N Computers --- .../README.md | 32 +++++++++++++++---- .../README_EN.md | 32 +++++++++++++++---- .../Solution.cpp | 9 ++++-- .../Solution.go | 5 ++- .../Solution.ts | 7 ++++ .../README_EN.md | 27 ++++++++++++++-- .../README_EN.md | 10 +++++- 7 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.ts diff --git a/solution/2100-2199/2138.Divide a String Into Groups of Size k/README.md b/solution/2100-2199/2138.Divide a String Into Groups of Size k/README.md index e1bf9c3c09a54..e6b38a5adcbe0 100644 --- a/solution/2100-2199/2138.Divide a String Into Groups of Size k/README.md +++ b/solution/2100-2199/2138.Divide a String Into Groups of Size k/README.md @@ -71,7 +71,11 @@ tags: -### 方法一 +### 方法一:模拟 + +我们可以直接模拟题目描述的过程,将字符串 $s$ 按照长度 $k$ 进行分组,然后对于最后一组不足 $k$ 个字符的情况,使用字符 $\text{fill}$ 进行填充。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。 @@ -108,10 +112,13 @@ class Solution { public: vector divideString(string s, int k, char fill) { int n = s.size(); - if (n % k) - for (int i = 0; i < k - n % k; ++i) s.push_back(fill); + if (n % k) { + s += string(k - n % k, fill); + } vector ans; - for (int i = 0; i < s.size() / k; ++i) ans.push_back(s.substr(i * k, k)); + for (int i = 0; i < s.size() / k; ++i) { + ans.push_back(s.substr(i * k, k)); + } return ans; } }; @@ -120,16 +127,27 @@ public: #### Go ```go -func divideString(s string, k int, fill byte) []string { +func divideString(s string, k int, fill byte) (ans []string) { n := len(s) if n%k != 0 { s += strings.Repeat(string(fill), k-n%k) } - var ans []string for i := 0; i < len(s)/k; i++ { ans = append(ans, s[i*k:(i+1)*k]) } - return ans + return +} +``` + +#### TypeScript + +```ts +function divideString(s: string, k: number, fill: string): string[] { + const ans: string[] = []; + for (let i = 0; i < s.length; i += k) { + ans.push(s.slice(i, i + k).padEnd(k, fill)); + } + return ans; } ``` diff --git a/solution/2100-2199/2138.Divide a String Into Groups of Size k/README_EN.md b/solution/2100-2199/2138.Divide a String Into Groups of Size k/README_EN.md index 4013d205e724d..93ce54ac04d5d 100644 --- a/solution/2100-2199/2138.Divide a String Into Groups of Size k/README_EN.md +++ b/solution/2100-2199/2138.Divide a String Into Groups of Size k/README_EN.md @@ -71,7 +71,11 @@ Thus, the 4 groups formed are "abc", "def", "ghi", -### Solution 1 +### Solution 1: Simulation + +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. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $s$. @@ -108,10 +112,13 @@ class Solution { public: vector divideString(string s, int k, char fill) { int n = s.size(); - if (n % k) - for (int i = 0; i < k - n % k; ++i) s.push_back(fill); + if (n % k) { + s += string(k - n % k, fill); + } vector ans; - for (int i = 0; i < s.size() / k; ++i) ans.push_back(s.substr(i * k, k)); + for (int i = 0; i < s.size() / k; ++i) { + ans.push_back(s.substr(i * k, k)); + } return ans; } }; @@ -120,16 +127,27 @@ public: #### Go ```go -func divideString(s string, k int, fill byte) []string { +func divideString(s string, k int, fill byte) (ans []string) { n := len(s) if n%k != 0 { s += strings.Repeat(string(fill), k-n%k) } - var ans []string for i := 0; i < len(s)/k; i++ { ans = append(ans, s[i*k:(i+1)*k]) } - return ans + return +} +``` + +#### TypeScript + +```ts +function divideString(s: string, k: number, fill: string): string[] { + const ans: string[] = []; + for (let i = 0; i < s.length; i += k) { + ans.push(s.slice(i, i + k).padEnd(k, fill)); + } + return ans; } ``` diff --git a/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.cpp b/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.cpp index 6d342d85e61c9..2e3031d31d387 100644 --- a/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.cpp +++ b/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.cpp @@ -2,10 +2,13 @@ class Solution { public: vector divideString(string s, int k, char fill) { int n = s.size(); - if (n % k) - for (int i = 0; i < k - n % k; ++i) s.push_back(fill); + if (n % k) { + s += string(k - n % k, fill); + } vector ans; - for (int i = 0; i < s.size() / k; ++i) ans.push_back(s.substr(i * k, k)); + for (int i = 0; i < s.size() / k; ++i) { + ans.push_back(s.substr(i * k, k)); + } return ans; } }; \ No newline at end of file diff --git a/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.go b/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.go index 47aa8be718223..9c2fb3c69d8cd 100644 --- a/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.go +++ b/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.go @@ -1,11 +1,10 @@ -func divideString(s string, k int, fill byte) []string { +func divideString(s string, k int, fill byte) (ans []string) { n := len(s) if n%k != 0 { s += strings.Repeat(string(fill), k-n%k) } - var ans []string for i := 0; i < len(s)/k; i++ { ans = append(ans, s[i*k:(i+1)*k]) } - return ans + return } \ No newline at end of file diff --git a/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.ts b/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.ts new file mode 100644 index 0000000000000..13d01addea380 --- /dev/null +++ b/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.ts @@ -0,0 +1,7 @@ +function divideString(s: string, k: number, fill: string): string[] { + const ans: string[] = []; + for (let i = 0; i < s.length; i += k) { + ans.push(s.slice(i, i + k).padEnd(k, fill)); + } + return ans; +} diff --git a/solution/2100-2199/2140.Solving Questions With Brainpower/README_EN.md b/solution/2100-2199/2140.Solving Questions With Brainpower/README_EN.md index 8c526726d15bc..fc240c1ce5ef0 100644 --- a/solution/2100-2199/2140.Solving Questions With Brainpower/README_EN.md +++ b/solution/2100-2199/2140.Solving Questions With Brainpower/README_EN.md @@ -77,7 +77,18 @@ Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points. -### Solution 1 +### Solution 1: Memoization Search + +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)$. + +The calculation method of the function $dfs(i)$ is as follows: + +- If $i \geq n$, it means that all problems have been solved, return $0$; +- 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))$. + +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)$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of problems. @@ -196,7 +207,19 @@ function mostPoints(questions: number[][]): number { -### Solution 2 +### Solution 2: Dynamic Programming + +We define $f[i]$ as the maximum score that can be obtained starting from the $i$-th problem. Therefore, the answer is $f[0]$. + +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: + +$$ +f[i] = \max(p + f[i + b + 1], f[i + 1]) +$$ + +We calculate the values of $f$ from back to front, and finally return $f[0]$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of problems. diff --git a/solution/2100-2199/2141.Maximum Running Time of N Computers/README_EN.md b/solution/2100-2199/2141.Maximum Running Time of N Computers/README_EN.md index 933ad30b85b94..9e461165829d9 100644 --- a/solution/2100-2199/2141.Maximum Running Time of N Computers/README_EN.md +++ b/solution/2100-2199/2141.Maximum Running Time of N Computers/README_EN.md @@ -70,7 +70,15 @@ We can run the two computers simultaneously for at most 2 minutes, so we return -### Solution 1 +### Solution 1: Binary Search + +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$. + +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. + +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. + +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)$. From 167ecfda95aa096ee19ce9686e0077c73367abd7 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 24 Jun 2024 20:04:46 +0800 Subject: [PATCH 2/2] docs: update --- .../README_EN.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/README_EN.md b/solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/README_EN.md index bdeef31a1f61a..651bd23235e38 100644 --- a/solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/README_EN.md +++ b/solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/README_EN.md @@ -78,7 +78,11 @@ Hence, the minimum cost to buy all candies is 5 + 5 = 10. -### Solution 1 +### Solution 1: Greedy Algorithm + +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. + +The time complexity is $O(n \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the number of candies.