Skip to content

Commit 52d223f

Browse files
chore: add LeetCode daily solution
1 parent b12eb7a commit 52d223f

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Minimum Time to Make Rope Colorful (Medium)
2+
3+
**Problem ID:** 1578
4+
**Date:** 2025-11-03
5+
**Link:** https://leetcode.com/problems/minimum-time-to-make-rope-colorful/
6+
7+
## Approach
8+
9+
To solve the problem of making the rope colorful by removing the minimum time of balloons, we can utilize a greedy approach. The main idea is to iterate through the `colors` string and identify consecutive balloons of the same color. For each group of consecutive balloons, we will determine which ones to remove based on the time required to remove them, aiming to keep the balloon with the maximum removal time.
10+
11+
### Approach:
12+
13+
1. **Initialization**: Start with a variable to track the total time needed to remove balloons (`totalRemovalTime`) initialized to zero.
14+
15+
2. **Iterate through the colors**: Use a loop to traverse the `colors` string. For each balloon, check if it is the same color as the previous one.
16+
17+
3. **Group Consecutive Balloons**: If two consecutive balloons are of the same color, compare their removal times:
18+
- Keep the balloon with the maximum removal time and add the removal time of the other balloon to `totalRemovalTime`.
19+
- Continue this for the entire string.
20+
21+
4. **Edge Cases**: If there are no consecutive balloons of the same color, the total removal time remains zero.
22+
23+
### Data Structures:
24+
- We primarily use simple variables to track the current balloon and the total removal time, so no complex data structures are necessary.
25+
26+
### Complexity:
27+
- **Time Complexity**: O(n), where n is the length of the `colors` string. We make a single pass through the string.
28+
- **Space Complexity**: O(1), as we are using a constant amount of space for our variables.
29+
30+
This approach efficiently calculates the minimum time required to make the rope colorful by systematically removing the least costly balloons in groups of consecutive colors.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int minCost(String colors, int[] neededTime) {
3+
int totalCost = 0;
4+
int maxTime = 0;
5+
int n = colors.length();
6+
7+
for (int i = 0; i < n; i++) {
8+
if (i > 0 && colors.charAt(i) == colors.charAt(i - 1)) {
9+
totalCost += Math.min(maxTime, neededTime[i]);
10+
maxTime = Math.max(maxTime, neededTime[i]);
11+
} else {
12+
maxTime = neededTime[i];
13+
}
14+
}
15+
16+
return totalCost;
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var minCost = function(colors, neededTime) {
2+
let totalCost = 0;
3+
let maxTime = 0;
4+
5+
for (let i = 0; i < colors.length; i++) {
6+
if (i > 0 && colors[i] === colors[i - 1]) {
7+
totalCost += Math.min(maxTime, neededTime[i]);
8+
maxTime = Math.max(maxTime, neededTime[i]);
9+
} else {
10+
maxTime = neededTime[i];
11+
}
12+
}
13+
14+
return totalCost;
15+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def minCost(self, colors: str, neededTime: List[int]) -> int:
3+
total_time = 0
4+
max_time = 0
5+
6+
for i in range(len(colors)):
7+
if i > 0 and colors[i] == colors[i - 1]:
8+
total_time += min(max_time, neededTime[i])
9+
max_time = max(max_time, neededTime[i])
10+
else:
11+
max_time = neededTime[i]
12+
13+
return total_time

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
267267
- 2025-10-31 — [The Two Sneaky Numbers of Digitville](https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/) (Easy) → `Easy/2025-10-31-3289-The-Two-Sneaky-Numbers-of-Digitville`
268268
- 2025-11-01 — [Delete Nodes From Linked List Present in Array](https://leetcode.com/problems/delete-nodes-from-linked-list-present-in-array/) (Medium) → `Medium/2025-11-01-3217-Delete-Nodes-From-Linked-List-Present-in-Array`
269269
- 2025-11-02 — [Count Unguarded Cells in the Grid](https://leetcode.com/problems/count-unguarded-cells-in-the-grid/) (Medium) → `Medium/2025-11-02-2257-Count-Unguarded-Cells-in-the-Grid`
270+
- 2025-11-03 — [Minimum Time to Make Rope Colorful](https://leetcode.com/problems/minimum-time-to-make-rope-colorful/) (Medium) → `Medium/2025-11-03-1578-Minimum-Time-to-Make-Rope-Colorful`

0 commit comments

Comments
 (0)