Skip to content

1700. Minimum Time To Make Rope Colorful

Difficulty: Medium

LeetCode Problem View on GitHub


1700. Minimum Time to Make Rope Colorful

Medium


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.

 

Example 1:

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.

Example 2:

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.

Example 3:

Input: colors = "aabaa", neededTime = [1,2,3,4,1]
Output: 2
Explanation: Bob will remove the balloons at indices 0 and 4. Each balloons takes 1 second to remove.
There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.

 

Constraints:

  • n == colors.length == neededTime.length
  • 1 <= n <= 105
  • 1 <= neededTime[i] <= 104
  • colors contains only lowercase English letters.

Solution

class Solution {
    public int minCost(String colors, int[] neededTime) {
        int n = neededTime.length;
        int pref[] = new int[n];
        pref[0] = neededTime[0];
        for (int i = 1; i < n; i++)
            pref[i] = pref[i - 1] + neededTime[i];

        SparseMax sp = new SparseMax(neededTime);

        int totalTime = 0, i = 0;
        while (i < n) {
            if (i == n - 1) break;
            if (colors.charAt(i) != colors.charAt(i + 1)) {
                i++;
                continue;
            }

            int j = i;
            while (j + 1 < n && colors.charAt(j + 1) == colors.charAt(i)) {
                j++;
            }

            /* From i --> j we have same characters */
            totalTime += pref[j];
            if (i - 1 >= 0) 
                totalTime -= pref[i - 1];
            totalTime -= sp.query(i, j);

            i = j + 1;
        }
        return totalTime;
    }

    static class SparseMax {
        int[][] st;      
        int[] log;     
        int n;
        public SparseMax(int[] arr) {
            n = arr.length;
            log = new int[n + 1];
            buildLog();

            int k = log[n] + 1;
            st = new int[n][k];

            for (int i = 0; i < n; i++) {
                st[i][0] = arr[i];
            }

            for (int j = 1; j < k; j++) {
                for (int i = 0; i + (1 << j) <= n; i++) {
                    st[i][j] = Math.max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
                }
            }
        }
        private void buildLog() {
            log[1] = 0;
            for (int i = 2; i <= n; i++) {
                log[i] = log[i / 2] + 1;
            }
        }
        public int query(int L, int R) {
            int j = log[R - L + 1];
            return Math.max(st[L][j], st[R - (1 << j) + 1][j]);
        }
    }
}

Complexity Analysis

  • Time Complexity: O(?)
  • Space Complexity: O(?)

Approach

Detailed explanation of the approach will be added here