Skip to content

feat: add solutions to lc problems: No.2138,2140,2141 #3158

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

Merged
merged 2 commits into from
Jun 24, 2024
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 @@ -71,7 +71,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:模拟

我们可以直接模拟题目描述的过程,将字符串 $s$ 按照长度 $k$ 进行分组,然后对于最后一组不足 $k$ 个字符的情况,使用字符 $\text{fill}$ 进行填充。

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

<!-- tabs:start -->

Expand Down Expand Up @@ -108,10 +112,13 @@ class Solution {
public:
vector<string> 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<string> 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;
}
};
Expand All @@ -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;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ Thus, the 4 groups formed are &quot;abc&quot;, &quot;def&quot;, &quot;ghi&quot;,

<!-- solution:start -->

### 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$.

<!-- tabs:start -->

Expand Down Expand Up @@ -108,10 +112,13 @@ class Solution {
public:
vector<string> 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<string> 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;
}
};
Expand All @@ -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;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ class Solution {
public:
vector<string> 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<string> 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;
}
};
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,18 @@ Total points earned: 2 + 5 = 7. There is no other way to earn 7 or more points.

<!-- solution:start -->

### 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.

<!-- tabs:start -->

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

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ We can run the two computers simultaneously for at most 2 minutes, so we return

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@ Hence, the minimum cost to buy all candies is 5 + 5 = 10.

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down
Loading