-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1578_colourful_rope.ts
96 lines (85 loc) · 3.12 KB
/
1578_colourful_rope.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
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.
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.
Return the minimum time Bob needs to make the rope colorful.
Input: colors = "abaac", neededTime = [1,2,3,4,5]
Output: 3
Explanation: In the above image, 'a' is blue, 'b' is red, and 'c' is green.
Bob can remove the blue balloon at index 2. This takes 3 seconds.
There are no longer two consecutive balloons of the same color. Total time = 3.
Input: colors = "abc", neededTime = [1,2,3]
Output: 0
Explanation: The rope is already colorful. Bob does not need to remove any balloons from the rope.
Input: colors = "aabaa", neededTime = [1,2,3,4,1]
Output: 2
Explanation: Bob will remove the ballons at indices 0 and 4. Each ballon takes 1 second to remove.
There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.
*/
/*
Approach :
Use sliding window to traverse sequences
Since we want to break all balloons in a sequence except one, we need to break all balloons except the one with most time needed in a sequence
Find total sum of the sequence
Find the largest number in the sequence
Desired time of breaking a sequence = sum of a sequence - largest number in the sequence
Total time needed = sum of breaking times of all such sequences
*/
/*
function minCost(colors: string, neededTime: number[]): number {
let i: number = 0;
let j: number = -1;
let minCostInSeq: number = Infinity;
let minCostSum: number = 0;
while (i < colors.length) {
if (colors[i] == colors[i + 1]) {
if (j == -1) {
j = i + 1;
}
if (colors[i] == colors[j]) {
minCostInSeq = Math.min(neededTime[i], neededTime[j], minCostInSeq);
j++;
} else {
minCostSum += minCostInSeq;
i = j;
j = -1;
}
} else {
i++;
}
}
return minCostSum;
}
*/
function minCost(colors: string, neededTime: number[]): number {
let i: number = 0;
let j: number = -1;
let maxCostInSeq: number = 0;
let sumOfSequence: number = 0;
let minCostSum: number = 0;
while (i < colors.length) {
if (colors[i] == colors[i + 1]) {
if (j == -1) {
j = i;
}
if (colors[i] == colors[j]) {
maxCostInSeq = Math.max(neededTime[j], maxCostInSeq);
sumOfSequence += neededTime[j];
j++;
} else {
minCostSum += sumOfSequence - maxCostInSeq;
sumOfSequence = 0;
maxCostInSeq = 0;
i = j;
j = -1;
}
} else {
i++;
}
}
return minCostSum;
}
console.log(minCost("rgbbbbr", [7, 9, 15, 7, 3, 8, 4])); // 18
console.log(minCost("abaac", [1, 2, 3, 4, 5])); // 3
console.log(minCost("abc", [1, 2, 3])); // 0
console.log(minCost("aabaa", [1, 2, 3, 4, 1])); // 2
console.log(minCost("bbbaaa", [4, 9, 3, 8, 8, 9])); // 23