Skip to content

Commit 75697a8

Browse files
committed
Sync LeetCode submission Runtime - 178 ms (17.07%), Memory - 26.4 MB (63.18%)
1 parent 66c0920 commit 75697a8

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<p>Alice has <code>n</code> balloons arranged on a rope. You are given a <strong>0-indexed</strong> string <code>colors</code> where <code>colors[i]</code> is the color of the <code>i<sup>th</sup></code> balloon.</p>
2+
3+
<p>Alice wants the rope to be <strong>colorful</strong>. She does not want <strong>two consecutive balloons</strong> to be of the same color, so she asks Bob for help. Bob can remove some balloons from the rope to make it <strong>colorful</strong>. You are given a <strong>0-indexed</strong> integer array <code>neededTime</code> where <code>neededTime[i]</code> is the time (in seconds) that Bob needs to remove the <code>i<sup>th</sup></code> balloon from the rope.</p>
4+
5+
<p>Return <em>the <strong>minimum time</strong> Bob needs to make the rope <strong>colorful</strong></em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/ballon1.jpg" style="width: 404px; height: 243px;" />
10+
<pre>
11+
<strong>Input:</strong> colors = &quot;abaac&quot;, neededTime = [1,2,3,4,5]
12+
<strong>Output:</strong> 3
13+
<strong>Explanation:</strong> In the above image, &#39;a&#39; is blue, &#39;b&#39; is red, and &#39;c&#39; is green.
14+
Bob can remove the blue balloon at index 2. This takes 3 seconds.
15+
There are no longer two consecutive balloons of the same color. Total time = 3.</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/balloon2.jpg" style="width: 244px; height: 243px;" />
19+
<pre>
20+
<strong>Input:</strong> colors = &quot;abc&quot;, neededTime = [1,2,3]
21+
<strong>Output:</strong> 0
22+
<strong>Explanation:</strong> The rope is already colorful. Bob does not need to remove any balloons from the rope.
23+
</pre>
24+
25+
<p><strong class="example">Example 3:</strong></p>
26+
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/13/balloon3.jpg" style="width: 404px; height: 243px;" />
27+
<pre>
28+
<strong>Input:</strong> colors = &quot;aabaa&quot;, neededTime = [1,2,3,4,1]
29+
<strong>Output:</strong> 2
30+
<strong>Explanation:</strong> Bob will remove the balloons at indices 0 and 4. Each balloons takes 1 second to remove.
31+
There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>n == colors.length == neededTime.length</code></li>
39+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
40+
<li><code>1 &lt;= neededTime[i] &lt;= 10<sup>4</sup></code></li>
41+
<li><code>colors</code> contains only lowercase English letters.</li>
42+
</ul>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Approach 2: Advanced 1-Pass
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def minCost(self, colors: str, neededTime: List[int]) -> int:
8+
total_time = 0
9+
curr_max_time = 0
10+
11+
for i in range(len(colors)):
12+
if i > 0 and colors[i] != colors[i - 1]:
13+
curr_max_time = 0
14+
15+
total_time += min(curr_max_time, neededTime[i])
16+
curr_max_time = max(curr_max_time, neededTime[i])
17+
18+
return total_time
19+

0 commit comments

Comments
 (0)