Skip to content

Commit 7828ead

Browse files
committed
feat: add solutions to lc problem: No.1578
No.1578.Minimum Time to Make Rope Colorful
1 parent 8b35f78 commit 7828ead

File tree

6 files changed

+243
-4
lines changed

6 files changed

+243
-4
lines changed

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

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,112 @@ Bob 可以移除下标 2 的蓝色气球。这将花费 3 秒。
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:双指针 + 贪心**
59+
60+
我们可以用双指针指向当前连续相同颜色的气球的首尾,然后计算出当前连续相同颜色的气球的总时间 $s$,以及最大的时间 $mx$。如果当前连续相同颜色的气球个数大于 $1$,那么我们可以贪心地选择保留时间最大的气球,然后移除其它相同颜色的气球,耗时 $s - mx$,累加到答案中。接下来继续遍历,直到遍历完所有气球。
61+
62+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为气球的个数。
63+
5864
<!-- tabs:start -->
5965

6066
### **Python3**
6167

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

6470
```python
65-
71+
class Solution:
72+
def minCost(self, colors: str, neededTime: List[int]) -> int:
73+
ans = i = 0
74+
n = len(colors)
75+
while i < n:
76+
j = i
77+
s = mx = 0
78+
while j < n and colors[j] == colors[i]:
79+
s += neededTime[j]
80+
if mx < neededTime[j]:
81+
mx = neededTime[j]
82+
j += 1
83+
if j - i > 1:
84+
ans += s - mx
85+
i = j
86+
return ans
6687
```
6788

6889
### **Java**
6990

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

7293
```java
94+
class Solution {
95+
public int minCost(String colors, int[] neededTime) {
96+
int ans = 0;
97+
int n = neededTime.length;
98+
for (int i = 0, j = 0; i < n; i = j) {
99+
j = i;
100+
int s = 0, mx = 0;
101+
while (j < n && colors.charAt(j) == colors.charAt(i)) {
102+
s += neededTime[j];
103+
mx = Math.max(mx, neededTime[j]);
104+
++j;
105+
}
106+
if (j - i > 1) {
107+
ans += s - mx;
108+
}
109+
}
110+
return ans;
111+
}
112+
}
113+
```
73114

115+
### **C++**
116+
117+
```cpp
118+
class Solution {
119+
public:
120+
int minCost(string colors, vector<int>& neededTime) {
121+
int ans = 0;
122+
int n = colors.size();
123+
for (int i = 0, j = 0; i < n; i = j) {
124+
j = i;
125+
int s = 0, mx = 0;
126+
while (j < n && colors[j] == colors[i]) {
127+
s += neededTime[j];
128+
mx = max(mx, neededTime[j]);
129+
++j;
130+
}
131+
if (j - i > 1) {
132+
ans += s - mx;
133+
}
134+
}
135+
return ans;
136+
}
137+
};
74138
```
75139
76-
### **TypeScript**
140+
### **Go**
141+
142+
```go
143+
func minCost(colors string, neededTime []int) (ans int) {
144+
n := len(colors)
145+
for i, j := 0, 0; i < n; i = j {
146+
j = i
147+
s, mx := 0, 0
148+
for j < n && colors[j] == colors[i] {
149+
s += neededTime[j]
150+
if mx < neededTime[j] {
151+
mx = neededTime[j]
152+
}
153+
j++
154+
}
155+
if j-i > 1 {
156+
ans += s - mx
157+
}
158+
}
159+
return
160+
}
161+
```
77162

78-
<!-- 这里可写当前语言的特殊实现逻辑 -->
163+
### **TypeScript**
79164

80165
```ts
81166

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

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,94 @@ There are no longer two consecutive balloons of the same color. Total time = 1 +
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def minCost(self, colors: str, neededTime: List[int]) -> int:
59+
ans = i = 0
60+
n = len(colors)
61+
while i < n:
62+
j = i
63+
s = mx = 0
64+
while j < n and colors[j] == colors[i]:
65+
s += neededTime[j]
66+
if mx < neededTime[j]:
67+
mx = neededTime[j]
68+
j += 1
69+
if j - i > 1:
70+
ans += s - mx
71+
i = j
72+
return ans
5873
```
5974

6075
### **Java**
6176

6277
```java
78+
class Solution {
79+
public int minCost(String colors, int[] neededTime) {
80+
int ans = 0;
81+
int n = neededTime.length;
82+
for (int i = 0, j = 0; i < n; i = j) {
83+
j = i;
84+
int s = 0, mx = 0;
85+
while (j < n && colors.charAt(j) == colors.charAt(i)) {
86+
s += neededTime[j];
87+
mx = Math.max(mx, neededTime[j]);
88+
++j;
89+
}
90+
if (j - i > 1) {
91+
ans += s - mx;
92+
}
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int minCost(string colors, vector<int>& neededTime) {
105+
int ans = 0;
106+
int n = colors.size();
107+
for (int i = 0, j = 0; i < n; i = j) {
108+
j = i;
109+
int s = 0, mx = 0;
110+
while (j < n && colors[j] == colors[i]) {
111+
s += neededTime[j];
112+
mx = max(mx, neededTime[j]);
113+
++j;
114+
}
115+
if (j - i > 1) {
116+
ans += s - mx;
117+
}
118+
}
119+
return ans;
120+
}
121+
};
122+
```
63123
124+
### **Go**
125+
126+
```go
127+
func minCost(colors string, neededTime []int) (ans int) {
128+
n := len(colors)
129+
for i, j := 0, 0; i < n; i = j {
130+
j = i
131+
s, mx := 0, 0
132+
for j < n && colors[j] == colors[i] {
133+
s += neededTime[j]
134+
if mx < neededTime[j] {
135+
mx = neededTime[j]
136+
}
137+
j++
138+
}
139+
if j-i > 1 {
140+
ans += s - mx
141+
}
142+
}
143+
return
144+
}
64145
```
65146

66147
### **TypeScript**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int minCost(string colors, vector<int>& neededTime) {
4+
int ans = 0;
5+
int n = colors.size();
6+
for (int i = 0, j = 0; i < n; i = j) {
7+
j = i;
8+
int s = 0, mx = 0;
9+
while (j < n && colors[j] == colors[i]) {
10+
s += neededTime[j];
11+
mx = max(mx, neededTime[j]);
12+
++j;
13+
}
14+
if (j - i > 1) {
15+
ans += s - mx;
16+
}
17+
}
18+
return ans;
19+
}
20+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func minCost(colors string, neededTime []int) (ans int) {
2+
n := len(colors)
3+
for i, j := 0, 0; i < n; i = j {
4+
j = i
5+
s, mx := 0, 0
6+
for j < n && colors[j] == colors[i] {
7+
s += neededTime[j]
8+
if mx < neededTime[j] {
9+
mx = neededTime[j]
10+
}
11+
j++
12+
}
13+
if j-i > 1 {
14+
ans += s - mx
15+
}
16+
}
17+
return
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int minCost(String colors, int[] neededTime) {
3+
int ans = 0;
4+
int n = neededTime.length;
5+
for (int i = 0, j = 0; i < n; i = j) {
6+
j = i;
7+
int s = 0, mx = 0;
8+
while (j < n && colors.charAt(j) == colors.charAt(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+
return ans;
18+
}
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def minCost(self, colors: str, neededTime: List[int]) -> int:
3+
ans = i = 0
4+
n = len(colors)
5+
while i < n:
6+
j = i
7+
s = mx = 0
8+
while j < n and colors[j] == colors[i]:
9+
s += neededTime[j]
10+
if mx < neededTime[j]:
11+
mx = neededTime[j]
12+
j += 1
13+
if j - i > 1:
14+
ans += s - mx
15+
i = j
16+
return ans

0 commit comments

Comments
 (0)