返回介绍

solution / 1500-1599 / 1578.Minimum Time to Make Rope Colorful / README_EN

发布于 2024-06-17 01:03:17 字数 5018 浏览 0 评论 0 收藏 0

1578. Minimum Time to Make Rope Colorful

中文文档

Description

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.

Solutions

Solution 1

class Solution:
  def minCost(self, colors: str, neededTime: List[int]) -> int:
    ans = i = 0
    n = len(colors)
    while i < n:
      j = i
      s = mx = 0
      while j < n and colors[j] == colors[i]:
        s += neededTime[j]
        if mx < neededTime[j]:
          mx = neededTime[j]
        j += 1
      if j - i > 1:
        ans += s - mx
      i = j
    return ans
class Solution {
  public int minCost(String colors, int[] neededTime) {
    int ans = 0;
    int n = neededTime.length;
    for (int i = 0, j = 0; i < n; i = j) {
      j = i;
      int s = 0, mx = 0;
      while (j < n && colors.charAt(j) == colors.charAt(i)) {
        s += neededTime[j];
        mx = Math.max(mx, neededTime[j]);
        ++j;
      }
      if (j - i > 1) {
        ans += s - mx;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int minCost(string colors, vector<int>& neededTime) {
    int ans = 0;
    int n = colors.size();
    for (int i = 0, j = 0; i < n; i = j) {
      j = i;
      int s = 0, mx = 0;
      while (j < n && colors[j] == colors[i]) {
        s += neededTime[j];
        mx = max(mx, neededTime[j]);
        ++j;
      }
      if (j - i > 1) {
        ans += s - mx;
      }
    }
    return ans;
  }
};
func minCost(colors string, neededTime []int) (ans int) {
  n := len(colors)
  for i, j := 0, 0; i < n; i = j {
    j = i
    s, mx := 0, 0
    for j < n && colors[j] == colors[i] {
      s += neededTime[j]
      if mx < neededTime[j] {
        mx = neededTime[j]
      }
      j++
    }
    if j-i > 1 {
      ans += s - mx
    }
  }
  return
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文