Skip to content

Commit feae98b

Browse files
committed
leetcode
1 parent 008ad30 commit feae98b

File tree

4 files changed

+568
-0
lines changed

4 files changed

+568
-0
lines changed
Lines changed: 350 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,350 @@
1+
/*
2+
3+
-* Minimum Time to Make Rope Colorful *-
4+
5+
Alice has n balloons arranged on a rope. You are given a 0-indexed string colors where colors[i] is the color of the ith balloon.
6+
7+
Alice wants the rope to be colorful. She does not want two consecutive balloons to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it colorful. You are given a 0-indexed integer array neededTime where neededTime[i] is the time (in seconds) that Bob needs to remove the ith balloon from the rope.
8+
9+
Return the minimum time Bob needs to make the rope colorful.
10+
11+
12+
13+
Example 1:
14+
15+
16+
Input: colors = "abaac", neededTime = [1,2,3,4,5]
17+
Output: 3
18+
Explanation: In the above image, 'a' is blue, 'b' is red, and 'c' is green.
19+
Bob can remove the blue balloon at index 2. This takes 3 seconds.
20+
There are no longer two consecutive balloons of the same color. Total time = 3.
21+
Example 2:
22+
23+
24+
Input: colors = "abc", neededTime = [1,2,3]
25+
Output: 0
26+
Explanation: The rope is already colorful. Bob does not need to remove any balloons from the rope.
27+
Example 3:
28+
29+
30+
Input: colors = "aabaa", neededTime = [1,2,3,4,1]
31+
Output: 2
32+
Explanation: Bob will remove the balloons at indices 0 and 4. Each ballon takes 1 second to remove.
33+
There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.
34+
35+
36+
Constraints:
37+
38+
n == colors.length == neededTime.length
39+
1 <= n <= 105
40+
1 <= neededTime[i] <= 104
41+
colors contains only lowercase English letters.
42+
43+
44+
*/
45+
46+
import 'dart:collection';
47+
import 'dart:math';
48+
49+
class A {
50+
// Runtime: 833 ms, faster than 100.00% of Dart online submissions for Minimum Time to Make Rope Colorful.
51+
// Memory Usage: 195 MB, less than 100.00% of Dart online submissions for Minimum Time to Make Rope Colorful.
52+
int minCost(String colors, List<int> neededTime) {
53+
int sum = 0, maxSum = 0;
54+
for (int i = 0; i < colors.length; i++) {
55+
sum += neededTime[i];
56+
int max = neededTime[i], count = 0;
57+
for (int j = i + 1;
58+
j < colors.length && colors.codeUnitAt(i) == colors.codeUnitAt(j);
59+
j++) {
60+
sum += neededTime[j];
61+
count++;
62+
if (neededTime[j] > max) max = neededTime[j];
63+
i = j;
64+
}
65+
maxSum += max;
66+
}
67+
return sum - maxSum;
68+
}
69+
}
70+
71+
class B {
72+
// Runtime: 527 ms, faster than 100.00% of Dart online submissions for Minimum Time to Make Rope Colorful.
73+
// Memory Usage: 203.6 MB, less than 100.00% of Dart online submissions for Minimum Time to Make Rope Colorful.
74+
int minCost(String colors, List<int> neededTime) {
75+
if (colors.length == 0) return 0;
76+
77+
int prev = colors.codeUnitAt(0);
78+
int ans = 0;
79+
for (int i = 1; i < neededTime.length; i++) {
80+
if (colors.codeUnitAt(i) == prev) {
81+
int current = min(neededTime[i], neededTime[i - 1]);
82+
ans += current;
83+
neededTime[i] = max(neededTime[i], neededTime[i - 1]);
84+
} else {
85+
prev = colors.codeUnitAt(i);
86+
}
87+
}
88+
return ans;
89+
}
90+
}
91+
92+
class C {
93+
// Correct but TLC
94+
int grp(List<int> nT, int l, int r) {
95+
int maxi = 0, sum = 0;
96+
for (int i = l; i <= r; i++) {
97+
sum += nT[i];
98+
maxi = max(maxi, nT[i]);
99+
}
100+
return sum - maxi;
101+
}
102+
103+
int minCost(String colors, List<int> neededTime) {
104+
int n = colors.length;
105+
int cost = 0;
106+
for (int i = 0; i < n; i++) {
107+
int j = i + 1;
108+
109+
//while (j != n && colors[i] == colors[j]) j++;
110+
while (j != n && colors.codeUnitAt(i) == colors.codeUnitAt(j)) j++;
111+
if (j != i + 1) {
112+
cost += grp(neededTime, i, j - 1);
113+
i = j - 1;
114+
}
115+
}
116+
return cost;
117+
}
118+
}
119+
120+
class D {
121+
int minCost(String colors, List<int> neededTime) {
122+
int res = 0;
123+
int maxi = 0;
124+
for (int i = 0; i < colors.length; i++) {
125+
res += neededTime[i];
126+
maxi = max(maxi, neededTime[i]);
127+
// range error
128+
if (colors.codeUnitAt(i) != colors.codeUnitAt(i + 1)) {
129+
res -= maxi;
130+
maxi = 0;
131+
}
132+
}
133+
return res;
134+
}
135+
}
136+
137+
class E {
138+
/*
139+
Basically what you need to do is:
140+
141+
get the sum of needed time to remove the all the consecutive balloons of the same color
142+
while you are looping through them store the maximum value of needed time
143+
if there are two or more consecutive balloons of the same color :
144+
add their needed time sum to the result sum
145+
subtract the maximum value of needed time sum from the result sum
146+
return the sum after the loop finishes iteration
147+
*/
148+
149+
// Runtime: 646 ms, faster than 100.00% of Dart online submissions for Minimum Time to Make Rope Colorful.
150+
// Memory Usage: 203.2 MB, less than 100.00% of Dart online submissions for Minimum Time to Make Rope Colorful.
151+
152+
int minCost(String colors, List<int> neededTime) {
153+
int sum = 0;
154+
155+
for (int i = 1; i < neededTime.length; i++) {
156+
int tempSum = neededTime[i - 1];
157+
int max = neededTime[i - 1], count = 1;
158+
159+
while (colors[i] == colors[i - 1]) {
160+
tempSum += neededTime[i];
161+
if (max < neededTime[i]) max = neededTime[i];
162+
i++;
163+
count++;
164+
if (i == neededTime.length) break;
165+
}
166+
if (count == 1) continue;
167+
sum += tempSum - max;
168+
}
169+
170+
return sum;
171+
}
172+
}
173+
174+
class F {
175+
// Runtime: 706 ms, faster than 100.00% of Dart online submissions for Minimum Time to Make Rope Colorful.
176+
// Memory Usage: 193.9 MB, less than 100.00% of Dart online submissions for Minimum Time to Make Rope Colorful.
177+
178+
int minCost(String colors, List<int> neededTime) {
179+
int i = 1, sum = neededTime[0];
180+
int ans = 0, mx = sum;
181+
while (i < colors.length) {
182+
if (colors[i] == colors[i - 1]) {
183+
mx = max(mx, neededTime[i]);
184+
sum += neededTime[i];
185+
} else {
186+
sum -= mx;
187+
ans += sum;
188+
// cout<<sum<<endl;
189+
sum = neededTime[i];
190+
mx = sum;
191+
}
192+
i++;
193+
}
194+
if (sum != mx) {
195+
sum -= mx;
196+
ans += sum;
197+
}
198+
return ans;
199+
}
200+
}
201+
202+
// class G {
203+
// int minCost(String colors, List<int> neededTime) {
204+
// List<Map<String, int>> rope = [].map((e) => <String, int>{}).toList();
205+
// for (int i = 0; i < colors.length; i++)
206+
// rope.add(<String, int>{
207+
// // (colors.split("").elementAt(i), neededTime[i])
208+
// });
209+
210+
// List<Map<String, int>> colorfulRope =
211+
// [].map((e) => <String, int>{}).toList();
212+
// int time = 0;
213+
// for (Map<String, int> baloon in rope) {
214+
// if (colorfulRope.isEmpty)
215+
// colorfulRope.add(baloon);
216+
// else {
217+
// Map<String, int> last = colorfulRope.elementAt(colorfulRope.length - 1);
218+
// if (last.keys == baloon.keys) {
219+
// if (last.values > baloon.values)
220+
// time += baloon.values;
221+
// else {
222+
// //colorfulRope.set(colorfulRope.size()-1, baloon);
223+
// colorfulRope.forEach((key, value) {
224+
// colorfulRope.add(colorfulRope.length - 1);
225+
226+
// });
227+
228+
// time += last.values;
229+
// }
230+
// } else
231+
// colorfulRope.add(baloon);
232+
// }
233+
// }
234+
// return time;
235+
// }
236+
// }
237+
238+
/*
239+
240+
extension DefaultMap<K,V> on Map<K,V> {
241+
V getOrElse(K key, V defaultValue) {
242+
if (this.containsKey(key)) {
243+
return this[key];
244+
} else {
245+
return defaultValue;
246+
}
247+
}
248+
}
249+
250+
*/
251+
252+
// class Pair<T1, T2> {
253+
// final T1 first;
254+
// final T2 second;
255+
// Pair(this.first, this.second);
256+
257+
// bool operator ==(final Pair other) {
258+
// return first == other.first && second == other.second;
259+
// }
260+
261+
// int get hashCode => hash2(first.hashCode, second.hashCode);
262+
// }
263+
264+
// class H {
265+
// // outPut zero
266+
// int minCost(String colors, List<int> neededTime) {
267+
// int n = colors.length;
268+
// List<int> prefix = [n + 1];
269+
// for (int i = 1; i <= n; i++) {
270+
// prefix[i] = prefix[i - 1] + neededTime[i - 1];
271+
// }
272+
// int start = 0;
273+
// String prev = colors[0];
274+
// int end;
275+
// int maxi = neededTime[0];
276+
// Map<MapEntry<int, int>, int> vec = {}.map((key, value) => <int, int>{}.cast());
277+
// for (int i = 1; i < n; i++) {
278+
// if (colors[i] != prev) {
279+
// end = i - 1;
280+
// if (start != end) vec[{start, end}] = maxi;
281+
// start = i;
282+
// prev = colors[i];
283+
// maxi = neededTime[i];
284+
// } else {
285+
// maxi = max(maxi, neededTime[i]);
286+
// }
287+
// }
288+
// end = n - 1;
289+
// if (start != end) vec[{start, end}] = maxi;
290+
// int time = 0;
291+
// // must be iterable
292+
// for (var it in vec) {
293+
// int i = it.first.first;
294+
// int j = it.first.second;
295+
// int val = it.second;
296+
// time += (prefix[j + 1] - prefix[i] - val);
297+
// }
298+
// return time;
299+
// }
300+
// }
301+
302+
class I {
303+
int minCost(String colors, List<int> neededTime) {
304+
int sum = 0;
305+
HashMap<String, int> myMap = HashMap();
306+
for (int i = 0; i < colors.split("").length; i++) {
307+
// range error
308+
if (colors[i].codeUnitAt(0) == colors[i + 1].codeUnitAt(0)) {
309+
if (myMap.containsValue(colors.codeUnitAt(i))) {
310+
int value = myMap.values.elementAt(colors.codeUnitAt(i));
311+
312+
if (value <= neededTime[i]) {
313+
// myMap.set(colors[i],neededTime[i]);
314+
myMap.forEach((key, value) {
315+
key = colors[i];
316+
value = neededTime[i];
317+
});
318+
319+
sum += value;
320+
} else if (value > neededTime[i]) {
321+
sum += neededTime[i];
322+
}
323+
} else {
324+
//myMap.set(colors[i],neededTime[i]);
325+
myMap.forEach((key, value) {
326+
key = colors[i];
327+
value = neededTime[i];
328+
});
329+
}
330+
} else {
331+
if (myMap.containsValue(colors.codeUnitAt(i))) {
332+
int value = myMap.values.elementAt(colors.codeUnitAt(i));
333+
if (value <= neededTime[i]) {
334+
// myMap.set(colors[i],neededTime[i]);
335+
myMap.forEach((key, value) {
336+
key = colors[i];
337+
value = neededTime[i];
338+
});
339+
sum += value;
340+
} else if (value > neededTime[i]) {
341+
sum += neededTime[i];
342+
}
343+
}
344+
345+
myMap = HashMap();
346+
}
347+
}
348+
return sum;
349+
}
350+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import "sort"
4+
5+
func minCost(colors string, neededTime []int) int {
6+
res := 0
7+
r := 0
8+
for i := 1; i < len(colors); i++ {
9+
if colors[i-1] == colors[i] {
10+
r, i = findSame(i-1, colors, neededTime)
11+
res += r
12+
continue
13+
}
14+
15+
}
16+
return res
17+
}
18+
19+
func findSame(idx int, s string, neededTime []int) (rs int, ix int) {
20+
i := idx
21+
res := 0
22+
for i < len(s)-1 {
23+
if s[i+1] != s[i] {
24+
break
25+
}
26+
i++
27+
}
28+
29+
same := neededTime[idx : i+1]
30+
sort.Ints(same)
31+
32+
for i := 0; i < len(same)-1; i++ {
33+
res += same[i]
34+
}
35+
36+
return res, i + 1
37+
}

0 commit comments

Comments
 (0)