-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet-code-q-68.java
More file actions
26 lines (23 loc) · 1016 Bytes
/
leet-code-q-68.java
File metadata and controls
26 lines (23 loc) · 1016 Bytes
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
class Solution
{
public int minCost(String colors, int[] neededTime)
{
// Step 1: Initialize totalTime to store total minimum time required
int totalTime = 0;
// Step 2: Traverse through each balloon starting from index 1
for (int i = 1; i < colors.length(); i++)
{
// Step 3: Compare current balloon color with the previous one
if (colors.charAt(i) == colors.charAt(i - 1))
{
// Step 4: If colors are the same, remove the one with smaller neededTime
totalTime += Math.min(neededTime[i], neededTime[i - 1]);
// Keep the balloon with higher removal time for next comparison
neededTime[i] = Math.max(neededTime[i], neededTime[i - 1]);
}
// Step 5: If colors differ, continue to the next balloon
}
// Step 6: Return the total time required to make the rope colorful
return totalTime;
}
}