Skip to content

Commit 6b1b9b9

Browse files
committed
feat: add solutions to lc problem: No.2052
No.2052.Minimum Cost to Separate Sentence Into Rows
1 parent 8e98826 commit 6b1b9b9

File tree

6 files changed

+401
-2
lines changed

6 files changed

+401
-2
lines changed

solution/2000-2099/2052.Minimum Cost to Separate Sentence Into Rows/README.md

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,159 @@
7373

7474
<!-- 这里可写通用的实现逻辑 -->
7575

76+
**方法一:记忆化搜索**
77+
7678
<!-- tabs:start -->
7779

7880
### **Python3**
7981

8082
<!-- 这里可写当前语言的特殊实现逻辑 -->
8183

8284
```python
83-
85+
class Solution:
86+
def minimumCost(self, sentence: str, k: int) -> int:
87+
@lru_cache(None)
88+
def dfs(i):
89+
if s[-1] - s[i] + n - i - 1 <= k:
90+
return 0
91+
ans, j = inf, i + 1
92+
while j < n and (t := s[j] - s[i] + j - i - 1) <= k:
93+
ans = min(ans, (k - t) ** 2 + dfs(j))
94+
j += 1
95+
return ans
96+
97+
t = [len(w) for w in sentence.split()]
98+
n = len(t)
99+
s = list(accumulate(t, initial=0))
100+
return dfs(0)
84101
```
85102

86103
### **Java**
87104

88105
<!-- 这里可写当前语言的特殊实现逻辑 -->
89106

90107
```java
108+
class Solution {
109+
private static final int INF = Integer.MAX_VALUE;
110+
private int[] memo;
111+
private int[] s;
112+
private int n;
113+
114+
public int minimumCost(String sentence, int k) {
115+
String[] words = sentence.split(" ");
116+
n = words.length;
117+
s = new int[n + 1];
118+
for (int i = 0; i < n; ++i) {
119+
s[i + 1] = s[i] + words[i].length();
120+
}
121+
memo = new int[n];
122+
Arrays.fill(memo, INF);
123+
return dfs(0, k);
124+
}
125+
126+
private int dfs(int i, int k) {
127+
if (memo[i] != INF) {
128+
return memo[i];
129+
}
130+
if (s[n] - s[i] + n - i - 1 <= k) {
131+
memo[i] = 0;
132+
return 0;
133+
}
134+
int ans = INF;
135+
for (int j = i + 1; j < n; ++j) {
136+
int t = s[j] - s[i] + j - i - 1;
137+
if (t <= k) {
138+
ans = Math.min(ans, (k - t) * (k - t) + dfs(j, k));
139+
}
140+
}
141+
memo[i] = ans;
142+
return ans;
143+
}
144+
}
145+
```
146+
147+
### **C++**
148+
149+
```cpp
150+
class Solution {
151+
public:
152+
const int inf = INT_MAX;
153+
int n;
154+
155+
int minimumCost(string sentence, int k) {
156+
istringstream is(sentence);
157+
vector<string> words;
158+
string word;
159+
while (is >> word) words.push_back(word);
160+
n = words.size();
161+
vector<int> s(n + 1);
162+
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + words[i].size();
163+
vector<int> memo(n, inf);
164+
return dfs(0, k, s, memo);
165+
}
166+
167+
int dfs(int i, int k, vector<int>& s, vector<int>& memo) {
168+
if (memo[i] != inf) return memo[i];
169+
if (s[n] - s[i] + n - i - 1 <= k)
170+
{
171+
memo[i] = 0;
172+
return 0;
173+
}
174+
int ans = inf;
175+
for (int j = i + 1; j < n; ++j)
176+
{
177+
int t = s[j] - s[i] + j - i - 1;
178+
if (t <= k) ans = min(ans, (k - t) * (k - t) + dfs(j, k, s, memo));
179+
}
180+
memo[i] = ans;
181+
return ans;
182+
}
183+
};
184+
```
91185
186+
### **Go**
187+
188+
```go
189+
func minimumCost(sentence string, k int) int {
190+
words := strings.Split(sentence, " ")
191+
n := len(words)
192+
inf := math.MaxInt32
193+
s := make([]int, n+1)
194+
for i, word := range words {
195+
s[i+1] = s[i] + len(word)
196+
}
197+
memo := make([]int, n)
198+
for i := range memo {
199+
memo[i] = inf
200+
}
201+
var dfs func(int) int
202+
dfs = func(i int) int {
203+
if memo[i] != inf {
204+
return memo[i]
205+
}
206+
if s[n]-s[i]+n-i-1 <= k {
207+
memo[i] = 0
208+
return 0
209+
}
210+
ans := inf
211+
for j := i + 1; j < n; j++ {
212+
t := s[j] - s[i] + j - i - 1
213+
if t <= k {
214+
ans = min(ans, (k-t)*(k-t)+dfs(j))
215+
}
216+
}
217+
memo[i] = ans
218+
return ans
219+
}
220+
return dfs(0)
221+
}
222+
223+
func min(a, b int) int {
224+
if a < b {
225+
return a
226+
}
227+
return b
228+
}
92229
```
93230

94231
### **...**

solution/2000-2099/2052.Minimum Cost to Separate Sentence Into Rows/README_EN.md

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,148 @@ The cost of the last row is not included in the total cost, and since there is o
7373
### **Python3**
7474

7575
```python
76-
76+
class Solution:
77+
def minimumCost(self, sentence: str, k: int) -> int:
78+
@lru_cache(None)
79+
def dfs(i):
80+
if s[-1] - s[i] + n - i - 1 <= k:
81+
return 0
82+
ans, j = inf, i + 1
83+
while j < n and (t := s[j] - s[i] + j - i - 1) <= k:
84+
ans = min(ans, (k - t) ** 2 + dfs(j))
85+
j += 1
86+
return ans
87+
88+
t = [len(w) for w in sentence.split()]
89+
n = len(t)
90+
s = list(accumulate(t, initial=0))
91+
return dfs(0)
7792
```
7893

7994
### **Java**
8095

8196
```java
97+
class Solution {
98+
private static final int INF = Integer.MAX_VALUE;
99+
private int[] memo;
100+
private int[] s;
101+
private int n;
102+
103+
public int minimumCost(String sentence, int k) {
104+
String[] words = sentence.split(" ");
105+
n = words.length;
106+
s = new int[n + 1];
107+
for (int i = 0; i < n; ++i) {
108+
s[i + 1] = s[i] + words[i].length();
109+
}
110+
memo = new int[n];
111+
Arrays.fill(memo, INF);
112+
return dfs(0, k);
113+
}
114+
115+
private int dfs(int i, int k) {
116+
if (memo[i] != INF) {
117+
return memo[i];
118+
}
119+
if (s[n] - s[i] + n - i - 1 <= k) {
120+
memo[i] = 0;
121+
return 0;
122+
}
123+
int ans = INF;
124+
for (int j = i + 1; j < n; ++j) {
125+
int t = s[j] - s[i] + j - i - 1;
126+
if (t <= k) {
127+
ans = Math.min(ans, (k - t) * (k - t) + dfs(j, k));
128+
}
129+
}
130+
memo[i] = ans;
131+
return ans;
132+
}
133+
}
134+
```
135+
136+
### **C++**
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
const int inf = INT_MAX;
142+
int n;
143+
144+
int minimumCost(string sentence, int k) {
145+
istringstream is(sentence);
146+
vector<string> words;
147+
string word;
148+
while (is >> word) words.push_back(word);
149+
n = words.size();
150+
vector<int> s(n + 1);
151+
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + words[i].size();
152+
vector<int> memo(n, inf);
153+
return dfs(0, k, s, memo);
154+
}
155+
156+
int dfs(int i, int k, vector<int>& s, vector<int>& memo) {
157+
if (memo[i] != inf) return memo[i];
158+
if (s[n] - s[i] + n - i - 1 <= k)
159+
{
160+
memo[i] = 0;
161+
return 0;
162+
}
163+
int ans = inf;
164+
for (int j = i + 1; j < n; ++j)
165+
{
166+
int t = s[j] - s[i] + j - i - 1;
167+
if (t <= k) ans = min(ans, (k - t) * (k - t) + dfs(j, k, s, memo));
168+
}
169+
memo[i] = ans;
170+
return ans;
171+
}
172+
};
173+
```
82174
175+
### **Go**
176+
177+
```go
178+
func minimumCost(sentence string, k int) int {
179+
words := strings.Split(sentence, " ")
180+
n := len(words)
181+
inf := math.MaxInt32
182+
s := make([]int, n+1)
183+
for i, word := range words {
184+
s[i+1] = s[i] + len(word)
185+
}
186+
memo := make([]int, n)
187+
for i := range memo {
188+
memo[i] = inf
189+
}
190+
var dfs func(int) int
191+
dfs = func(i int) int {
192+
if memo[i] != inf {
193+
return memo[i]
194+
}
195+
if s[n]-s[i]+n-i-1 <= k {
196+
memo[i] = 0
197+
return 0
198+
}
199+
ans := inf
200+
for j := i + 1; j < n; j++ {
201+
t := s[j] - s[i] + j - i - 1
202+
if t <= k {
203+
ans = min(ans, (k-t)*(k-t)+dfs(j))
204+
}
205+
}
206+
memo[i] = ans
207+
return ans
208+
}
209+
return dfs(0)
210+
}
211+
212+
func min(a, b int) int {
213+
if a < b {
214+
return a
215+
}
216+
return b
217+
}
83218
```
84219

85220
### **...**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
const int inf = INT_MAX;
4+
int n;
5+
6+
int minimumCost(string sentence, int k) {
7+
istringstream is(sentence);
8+
vector<string> words;
9+
string word;
10+
while (is >> word) words.push_back(word);
11+
n = words.size();
12+
vector<int> s(n + 1);
13+
for (int i = 0; i < n; ++i) s[i + 1] = s[i] + words[i].size();
14+
vector<int> memo(n, inf);
15+
return dfs(0, k, s, memo);
16+
}
17+
18+
int dfs(int i, int k, vector<int>& s, vector<int>& memo) {
19+
if (memo[i] != inf) return memo[i];
20+
if (s[n] - s[i] + n - i - 1 <= k)
21+
{
22+
memo[i] = 0;
23+
return 0;
24+
}
25+
int ans = inf;
26+
for (int j = i + 1; j < n; ++j)
27+
{
28+
int t = s[j] - s[i] + j - i - 1;
29+
if (t <= k) ans = min(ans, (k - t) * (k - t) + dfs(j, k, s, memo));
30+
}
31+
memo[i] = ans;
32+
return ans;
33+
}
34+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
func minimumCost(sentence string, k int) int {
2+
words := strings.Split(sentence, " ")
3+
n := len(words)
4+
inf := math.MaxInt32
5+
s := make([]int, n+1)
6+
for i, word := range words {
7+
s[i+1] = s[i] + len(word)
8+
}
9+
memo := make([]int, n)
10+
for i := range memo {
11+
memo[i] = inf
12+
}
13+
var dfs func(int) int
14+
dfs = func(i int) int {
15+
if memo[i] != inf {
16+
return memo[i]
17+
}
18+
if s[n]-s[i]+n-i-1 <= k {
19+
memo[i] = 0
20+
return 0
21+
}
22+
ans := inf
23+
for j := i + 1; j < n; j++ {
24+
t := s[j] - s[i] + j - i - 1
25+
if t <= k {
26+
ans = min(ans, (k-t)*(k-t)+dfs(j))
27+
}
28+
}
29+
memo[i] = ans
30+
return ans
31+
}
32+
return dfs(0)
33+
}
34+
35+
func min(a, b int) int {
36+
if a < b {
37+
return a
38+
}
39+
return b
40+
}

0 commit comments

Comments
 (0)