返回介绍

solution / 2400-2499 / 2483.Minimum Penalty for a Shop / README_EN

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

2483. Minimum Penalty for a Shop

中文文档

Description

You are given the customer visit log of a shop represented by a 0-indexed string customers consisting only of characters 'N' and 'Y':

  • if the ith character is 'Y', it means that customers come at the ith hour
  • whereas 'N' indicates that no customers come at the ith hour.

If the shop closes at the jth hour (0 <= j <= n), the penalty is calculated as follows:

  • For every hour when the shop is open and no customers come, the penalty increases by 1.
  • For every hour when the shop is closed and customers come, the penalty increases by 1.

Return_ the earliest hour at which the shop must be closed to incur a minimum penalty._

Note that if a shop closes at the jth hour, it means the shop is closed at the hour j.

 

Example 1:

Input: customers = "YYNY"
Output: 2
Explanation: 
- Closing the shop at the 0th hour incurs in 1+1+0+1 = 3 penalty.
- Closing the shop at the 1st hour incurs in 0+1+0+1 = 2 penalty.
- Closing the shop at the 2nd hour incurs in 0+0+0+1 = 1 penalty.
- Closing the shop at the 3rd hour incurs in 0+0+1+1 = 2 penalty.
- Closing the shop at the 4th hour incurs in 0+0+1+0 = 1 penalty.
Closing the shop at 2nd or 4th hour gives a minimum penalty. Since 2 is earlier, the optimal closing time is 2.

Example 2:

Input: customers = "NNNNN"
Output: 0
Explanation: It is best to close the shop at the 0th hour as no customers arrive.

Example 3:

Input: customers = "YYYY"
Output: 4
Explanation: It is best to close the shop at the 4th hour as customers arrive at each hour.

 

Constraints:

  • 1 <= customers.length <= 105
  • customers consists only of characters 'Y' and 'N'.

Solutions

Solution 1: Prefix Sum + Enumeration

First, we calculate how many customers arrive in the first $i$ hours and record it in the prefix sum array $s$.

Then we enumerate the closing time $j$ of the shop, calculate the cost, and take the earliest closing time with the smallest cost.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the string $customers$.

class Solution:
  def bestClosingTime(self, customers: str) -> int:
    n = len(customers)
    s = [0] * (n + 1)
    for i, c in enumerate(customers):
      s[i + 1] = s[i] + int(c == 'Y')
    ans, cost = 0, inf
    for j in range(n + 1):
      t = j - s[j] + s[-1] - s[j]
      if cost > t:
        ans, cost = j, t
    return ans
class Solution {
  public int bestClosingTime(String customers) {
    int n = customers.length();
    int[] s = new int[n + 1];
    for (int i = 0; i < n; ++i) {
      s[i + 1] = s[i] + (customers.charAt(i) == 'Y' ? 1 : 0);
    }
    int ans = 0, cost = 1 << 30;
    for (int j = 0; j <= n; ++j) {
      int t = j - s[j] + s[n] - s[j];
      if (cost > t) {
        ans = j;
        cost = t;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int bestClosingTime(string customers) {
    int n = customers.size();
    vector<int> s(n + 1);
    for (int i = 0; i < n; ++i) {
      s[i + 1] = s[i] + (customers[i] == 'Y');
    }
    int ans = 0, cost = 1 << 30;
    for (int j = 0; j <= n; ++j) {
      int t = j - s[j] + s[n] - s[j];
      if (cost > t) {
        ans = j;
        cost = t;
      }
    }
    return ans;
  }
};
func bestClosingTime(customers string) (ans int) {
  n := len(customers)
  s := make([]int, n+1)
  for i, c := range customers {
    s[i+1] = s[i]
    if c == 'Y' {
      s[i+1]++
    }
  }
  cost := 1 << 30
  for j := 0; j <= n; j++ {
    t := j - s[j] + s[n] - s[j]
    if cost > t {
      ans, cost = j, t
    }
  }
  return
}
impl Solution {
  #[allow(dead_code)]
  pub fn best_closing_time(customers: String) -> i32 {
    let n = customers.len();
    let mut penalty = i32::MAX;
    let mut ret = -1;
    let mut prefix_sum = vec![0; n + 1];

    // Initialize the vector
    for (i, c) in customers.chars().enumerate() {
      prefix_sum[i + 1] = prefix_sum[i] + (if c == 'Y' { 1 } else { 0 });
    }

    // Calculate the answer
    for i in 0..=n {
      if penalty > ((prefix_sum[n] - prefix_sum[i]) as i32) + ((i - prefix_sum[i]) as i32) {
        penalty = ((prefix_sum[n] - prefix_sum[i]) as i32) + ((i - prefix_sum[i]) as i32);
        ret = i as i32;
      }
    }

    ret
  }
}

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

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

发布评论

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