LeetCode每日一题(2100. Find Good Days to Rob the Bank)

这篇博客讨论了一种算法问题,涉及到在给定银行警卫数量的时间序列中,找出适合抢劫银行的‘好日子’。好日子定义为在该日之前和之后的指定时间内,警卫人数呈非递减趋势。通过维护一个最小值队列,可以高效地计算左右两侧满足条件的天数,从而找出所有符合条件的‘好日子’。示例展示了如何在不同情况下应用这种方法。

You and a gang of thieves are planning on robbing a bank. You are given a 0-indexed integer array security, where security[i] is the number of guards on duty on the ith day. The days are numbered starting from 0. You are also given an integer time.

The ith day is a good day to rob the bank if:

There are at least time days before and after the ith day,
The number of guards at the bank for the time days before i are non-increasing, and
The number of guards at the bank for the time days after i are non-decreasing.
More formally, this means day i is a good day to rob the bank if and only if security[i - time] >= security[i - time + 1] >= … >= security[i] <= … <= security[i + time - 1] <= security[i + time].

Return a list of all days (0-indexed) that are good days to rob the bank. The order that the days are returned in does not matter.

Example 1:

Input: security = [5,3,3,3,5,6,2], time = 2
Output: [2,3]

Explanation:
On day 2, we have security[0] >= security[1] >= security[2] <= security[3] <= security[4].
On day 3, we have security[1] >= security[2] >= security[3] <= security[4] <= security[5].
No other days satisfy this condition, so days 2 and 3 are the only good days to rob the bank.

Example 2:

Input: security = [1,1,1,1,1], time = 0
Output: [0,1,2,3,4]

Explanation:
Since time equals 0, every day is a good day to rob the bank, so return every day.

Example 3:

Input: security = [1,2,3,4,5,6], time = 2
Output: []

Explanation:
No day has 2 days before it that have a non-increasing number of guards.
Thus, no day is a good day to rob the bank, so return an empty list.

Constraints:

  • 1 <= security.length <= 105
  • 0 <= security[i], time <= 105

计算 security[i]左边连续小于 security[i]的天数 left[i]与右边连续小于 security[i]的天数 right[i], 这样只要 left[i] >= time 并且 right[i] >= time, 那 security[i]就是适合打劫的好日子。

现在突然发现做复杂了, 其实根本不用队列来维护前面的连续值, 我们只需要维护一个最小值和计数就可以了。



impl Solution {
    pub fn good_days_to_rob_bank(security: Vec<i32>, time: i32) -> Vec<i32> {
        let mut queue = Vec::new();
        let mut left = vec![0; security.len()];
        for (i, &s) in security.iter().enumerate() {
            if let Some(&p) = queue.last() {
                if s <= p {
                    left[i] = queue.len() as i32;
                    queue.push(s);
                    continue;
                }
            }
            left[i] = 0;
            queue.clear();
            queue.push(s);
        }
        queue.clear();
        let mut right = Vec::new();
        for &s in security.iter().rev() {
            if let Some(&p) = queue.last() {
                if s <= p {
                    right.push(queue.len() as i32);
                    queue.push(s);
                    continue;
                }
            }
            right.push(0);
            queue.clear();
            queue.push(s);
        }
        right.reverse();
        let mut ans = Vec::new();
        for (i, (l, r)) in left.into_iter().zip(right).enumerate() {
            if l >= time && r >= time {
                ans.push(i as i32)
            }
        }
        ans
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值