File tree Expand file tree Collapse file tree 6 files changed +243
-4
lines changed
solution/1500-1599/1578.Minimum Time to Make Rope Colorful Expand file tree Collapse file tree 6 files changed +243
-4
lines changed Original file line number Diff line number Diff line change @@ -55,27 +55,112 @@ Bob 可以移除下标 2 的蓝色气球。这将花费 3 秒。
55
55
56
56
<!-- 这里可写通用的实现逻辑 -->
57
57
58
+ ** 方法一:双指针 + 贪心**
59
+
60
+ 我们可以用双指针指向当前连续相同颜色的气球的首尾,然后计算出当前连续相同颜色的气球的总时间 $s$,以及最大的时间 $mx$。如果当前连续相同颜色的气球个数大于 $1$,那么我们可以贪心地选择保留时间最大的气球,然后移除其它相同颜色的气球,耗时 $s - mx$,累加到答案中。接下来继续遍历,直到遍历完所有气球。
61
+
62
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为气球的个数。
63
+
58
64
<!-- tabs:start -->
59
65
60
66
### ** Python3**
61
67
62
68
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
69
64
70
``` 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
66
87
```
67
88
68
89
### ** Java**
69
90
70
91
<!-- 这里可写当前语言的特殊实现逻辑 -->
71
92
72
93
``` 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
+ ```
73
114
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
+ };
74
138
```
75
139
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
+ ```
77
162
78
- <!-- 这里可写当前语言的特殊实现逻辑 -->
163
+ ### ** TypeScript **
79
164
80
165
``` ts
81
166
Original file line number Diff line number Diff line change @@ -54,13 +54,94 @@ There are no longer two consecutive balloons of the same color. Total time = 1 +
54
54
### ** Python3**
55
55
56
56
``` 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
58
73
```
59
74
60
75
### ** Java**
61
76
62
77
``` 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
+ ```
63
123
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
+ }
64
145
```
65
146
66
147
### ** TypeScript**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments