返回介绍

solution / 2100-2199 / 2167.Minimum Time to Remove All Cars Containing Illegal Goods / README_EN

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

2167. Minimum Time to Remove All Cars Containing Illegal Goods

中文文档

Description

You are given a 0-indexed binary string s which represents a sequence of train cars. s[i] = '0' denotes that the ith car does not contain illegal goods and s[i] = '1' denotes that the ith car does contain illegal goods.

As the train conductor, you would like to get rid of all the cars containing illegal goods. You can do any of the following three operations any number of times:

  1. Remove a train car from the left end (i.e., remove s[0]) which takes 1 unit of time.
  2. Remove a train car from the right end (i.e., remove s[s.length - 1]) which takes 1 unit of time.
  3. Remove a train car from anywhere in the sequence which takes 2 units of time.

Return _the minimum time to remove all the cars containing illegal goods_.

Note that an empty sequence of cars is considered to have no cars containing illegal goods.

 

Example 1:

Input: s = "1100101"
Output: 5
Explanation: 
One way to remove all the cars containing illegal goods from the sequence is to
- remove a car from the left end 2 times. Time taken is 2 * 1 = 2.
- remove a car from the right end. Time taken is 1.
- remove the car containing illegal goods found in the middle. Time taken is 2.
This obtains a total time of 2 + 1 + 2 = 5. 

An alternative way is to
- remove a car from the left end 2 times. Time taken is 2 * 1 = 2.
- remove a car from the right end 3 times. Time taken is 3 * 1 = 3.
This also obtains a total time of 2 + 3 = 5.

5 is the minimum time taken to remove all the cars containing illegal goods. 
There are no other ways to remove them with less time.

Example 2:

Input: s = "0010"
Output: 2
Explanation:
One way to remove all the cars containing illegal goods from the sequence is to
- remove a car from the left end 3 times. Time taken is 3 * 1 = 3.
This obtains a total time of 3.

Another way to remove all the cars containing illegal goods from the sequence is to
- remove the car containing illegal goods found in the middle. Time taken is 2.
This obtains a total time of 2.

Another way to remove all the cars containing illegal goods from the sequence is to 
- remove a car from the right end 2 times. Time taken is 2 * 1 = 2. 
This obtains a total time of 2.

2 is the minimum time taken to remove all the cars containing illegal goods. 
There are no other ways to remove them with less time.

 

Constraints:

  • 1 <= s.length <= 2 * 105
  • s[i] is either '0' or '1'.

Solutions

Solution 1

class Solution:
  def minimumTime(self, s: str) -> int:
    n = len(s)
    pre = [0] * (n + 1)
    suf = [0] * (n + 1)
    for i, c in enumerate(s):
      pre[i + 1] = pre[i] if c == '0' else min(pre[i] + 2, i + 1)
    for i in range(n - 1, -1, -1):
      suf[i] = suf[i + 1] if s[i] == '0' else min(suf[i + 1] + 2, n - i)
    return min(a + b for a, b in zip(pre[1:], suf[1:]))
class Solution {
  public int minimumTime(String s) {
    int n = s.length();
    int[] pre = new int[n + 1];
    int[] suf = new int[n + 1];
    for (int i = 0; i < n; ++i) {
      pre[i + 1] = s.charAt(i) == '0' ? pre[i] : Math.min(pre[i] + 2, i + 1);
    }
    for (int i = n - 1; i >= 0; --i) {
      suf[i] = s.charAt(i) == '0' ? suf[i + 1] : Math.min(suf[i + 1] + 2, n - i);
    }
    int ans = Integer.MAX_VALUE;
    for (int i = 1; i <= n; ++i) {
      ans = Math.min(ans, pre[i] + suf[i]);
    }
    return ans;
  }
}
class Solution {
public:
  int minimumTime(string s) {
    int n = s.size();
    vector<int> pre(n + 1);
    vector<int> suf(n + 1);
    for (int i = 0; i < n; ++i) pre[i + 1] = s[i] == '0' ? pre[i] : min(pre[i] + 2, i + 1);
    for (int i = n - 1; ~i; --i) suf[i] = s[i] == '0' ? suf[i + 1] : min(suf[i + 1] + 2, n - i);
    int ans = INT_MAX;
    for (int i = 1; i <= n; ++i) ans = min(ans, pre[i] + suf[i]);
    return ans;
  }
};
func minimumTime(s string) int {
  n := len(s)
  pre := make([]int, n+1)
  suf := make([]int, n+1)
  for i, c := range s {
    pre[i+1] = pre[i]
    if c == '1' {
      pre[i+1] = min(pre[i]+2, i+1)
    }
  }
  for i := n - 1; i >= 0; i-- {
    suf[i] = suf[i+1]
    if s[i] == '1' {
      suf[i] = min(suf[i+1]+2, n-i)
    }
  }
  ans := 0x3f3f3f3f
  for i := 1; i <= n; i++ {
    ans = min(ans, pre[i]+suf[i])
  }
  return ans
}

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

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

发布评论

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