Skip to content

Commit ff0976c

Browse files
committed
feat: add solutions to lc problem: No.1578
1 parent 2cf001b commit ff0976c

File tree

5 files changed

+167
-11
lines changed

5 files changed

+167
-11
lines changed

solution/1500-1599/1578.Minimum Time to Make Rope Colorful/README.md

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ func minCost(colors string, neededTime []int) (ans int) {
160160
s, mx := 0, 0
161161
for j < n && colors[j] == colors[i] {
162162
s += neededTime[j]
163-
if mx < neededTime[j] {
164-
mx = neededTime[j]
165-
}
163+
mx = max(mx, neededTime[j])
166164
j++
167165
}
168166
if j-i > 1 {
@@ -173,6 +171,62 @@ func minCost(colors string, neededTime []int) (ans int) {
173171
}
174172
```
175173

174+
#### TypeScript
175+
176+
```ts
177+
function minCost(colors: string, neededTime: number[]): number {
178+
let ans = 0;
179+
const n = neededTime.length;
180+
181+
for (let i = 0, j = 0; i < n; i = j) {
182+
j = i;
183+
let [s, mx] = [0, 0];
184+
while (j < n && colors[j] === colors[i]) {
185+
s += neededTime[j];
186+
mx = Math.max(mx, neededTime[j]);
187+
++j;
188+
}
189+
if (j - i > 1) {
190+
ans += s - mx;
191+
}
192+
}
193+
194+
return ans;
195+
}
196+
```
197+
198+
#### Rust
199+
200+
```rust
201+
impl Solution {
202+
pub fn min_cost(colors: String, needed_time: Vec<i32>) -> i32 {
203+
let n = needed_time.len();
204+
let mut ans = 0;
205+
let bytes = colors.as_bytes();
206+
let mut i = 0;
207+
208+
while i < n {
209+
let mut j = i;
210+
let mut s = 0;
211+
let mut mx = 0;
212+
213+
while j < n && bytes[j] == bytes[i] {
214+
s += needed_time[j];
215+
mx = mx.max(needed_time[j]);
216+
j += 1;
217+
}
218+
219+
if j - i > 1 {
220+
ans += s - mx;
221+
}
222+
i = j;
223+
}
224+
225+
ans
226+
}
227+
}
228+
```
229+
176230
<!-- tabs:end -->
177231

178232
<!-- solution:end -->

solution/1500-1599/1578.Minimum Time to Make Rope Colorful/README_EN.md

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ There are no longer two consecutive balloons of the same color. Total time = 1 +
7070

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

73-
### Solution 1
73+
### Solution 1: Two Pointers + Greedy
74+
75+
We can use two pointers to point to the beginning and end of the current consecutive balloons with the same color, then calculate the total time $s$ of these consecutive balloons with the same color, as well as the maximum time $mx$. If the number of consecutive balloons with the same color is greater than $1$, we can greedily choose to keep the balloon with the maximum time and remove the other balloons with the same color, which takes time $s - mx$, and add it to the answer. Then we continue to traverse until all balloons are traversed.
76+
77+
The time complexity is $O(n)$ and the space complexity is $O(1)$, where $n$ is the number of balloons.
7478

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

@@ -154,9 +158,7 @@ func minCost(colors string, neededTime []int) (ans int) {
154158
s, mx := 0, 0
155159
for j < n && colors[j] == colors[i] {
156160
s += neededTime[j]
157-
if mx < neededTime[j] {
158-
mx = neededTime[j]
159-
}
161+
mx = max(mx, neededTime[j])
160162
j++
161163
}
162164
if j-i > 1 {
@@ -167,6 +169,62 @@ func minCost(colors string, neededTime []int) (ans int) {
167169
}
168170
```
169171

172+
#### TypeScript
173+
174+
```ts
175+
function minCost(colors: string, neededTime: number[]): number {
176+
let ans = 0;
177+
const n = neededTime.length;
178+
179+
for (let i = 0, j = 0; i < n; i = j) {
180+
j = i;
181+
let [s, mx] = [0, 0];
182+
while (j < n && colors[j] === colors[i]) {
183+
s += neededTime[j];
184+
mx = Math.max(mx, neededTime[j]);
185+
++j;
186+
}
187+
if (j - i > 1) {
188+
ans += s - mx;
189+
}
190+
}
191+
192+
return ans;
193+
}
194+
```
195+
196+
#### Rust
197+
198+
```rust
199+
impl Solution {
200+
pub fn min_cost(colors: String, needed_time: Vec<i32>) -> i32 {
201+
let n = needed_time.len();
202+
let mut ans = 0;
203+
let bytes = colors.as_bytes();
204+
let mut i = 0;
205+
206+
while i < n {
207+
let mut j = i;
208+
let mut s = 0;
209+
let mut mx = 0;
210+
211+
while j < n && bytes[j] == bytes[i] {
212+
s += needed_time[j];
213+
mx = mx.max(needed_time[j]);
214+
j += 1;
215+
}
216+
217+
if j - i > 1 {
218+
ans += s - mx;
219+
}
220+
i = j;
221+
}
222+
223+
ans
224+
}
225+
}
226+
```
227+
170228
<!-- tabs:end -->
171229

172230
<!-- solution:end -->

solution/1500-1599/1578.Minimum Time to Make Rope Colorful/Solution.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ func minCost(colors string, neededTime []int) (ans int) {
55
s, mx := 0, 0
66
for j < n && colors[j] == colors[i] {
77
s += neededTime[j]
8-
if mx < neededTime[j] {
9-
mx = neededTime[j]
10-
}
8+
mx = max(mx, neededTime[j])
119
j++
1210
}
1311
if j-i > 1 {
1412
ans += s - mx
1513
}
1614
}
1715
return
18-
}
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
impl Solution {
2+
pub fn min_cost(colors: String, needed_time: Vec<i32>) -> i32 {
3+
let n = needed_time.len();
4+
let mut ans = 0;
5+
let bytes = colors.as_bytes();
6+
let mut i = 0;
7+
8+
while i < n {
9+
let mut j = i;
10+
let mut s = 0;
11+
let mut mx = 0;
12+
13+
while j < n && bytes[j] == bytes[i] {
14+
s += needed_time[j];
15+
mx = mx.max(needed_time[j]);
16+
j += 1;
17+
}
18+
19+
if j - i > 1 {
20+
ans += s - mx;
21+
}
22+
i = j;
23+
}
24+
25+
ans
26+
}
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function minCost(colors: string, neededTime: number[]): number {
2+
let ans = 0;
3+
const n = neededTime.length;
4+
5+
for (let i = 0, j = 0; i < n; i = j) {
6+
j = i;
7+
let [s, mx] = [0, 0];
8+
while (j < n && colors[j] === colors[i]) {
9+
s += neededTime[j];
10+
mx = Math.max(mx, neededTime[j]);
11+
++j;
12+
}
13+
if (j - i > 1) {
14+
ans += s - mx;
15+
}
16+
}
17+
18+
return ans;
19+
}

0 commit comments

Comments
 (0)