LeetCode 2398. 预算内的最多机器人数目

2398. 预算内的最多机器人数目

你有 n 个机器人,给你两个下标从 0 开始的整数数组 chargeTimes 和 runningCosts ,两者长度都为 n 。第 i 个机器人充电时间为 chargeTimes[i] 单位时间,花费 runningCosts[i] 单位时间运行。再给你一个整数 budget 。

运行 k 个机器人 总开销 是 max(chargeTimes) + k * sum(runningCosts) ,其中 max(chargeTimes) 是这 k 个机器人中最大充电时间,sum(runningCosts) 是这 k 个机器人的运行时间之和。

请你返回在 不超过 budget 的前提下,你 最多 可以 连续 运行的机器人数目为多少。

示例 1:

输入:chargeTimes = [3,6,1,3,4], runningCosts = [2,1,3,4,5], budget = 25
输出:3
解释:
可以在 budget 以内运行所有单个机器人或者连续运行 2 个机器人。
选择前 3 个机器人,可以得到答案最大值 3 。总开销是 max(3,6,1) + 3 * sum(2,1,3) = 6 + 3 * 6 = 24 ,小于 25 。
可以看出无法在 budget 以内连续运行超过 3 个机器人,所以我们返回 3 。

示例 2:

输入:chargeTimes = [11,12,19], runningCosts = [10,8,7], budget = 19
输出:0
解释:即使运行任何一个单个机器人,还是会超出 budget,所以我们返回 0 。

提示:

  • chargeTimes.length == runningCosts.length == n
  • 1 <= n <= 5 * 10^4
  • 1 <= chargeTimes[i], runningCosts[i] <= 10^5
  • 1 <= budget <= 10^15

提示 1

Use binary search to convert the problem into checking if we can find a specific number of consecutive robots within the budget.


提示 2

Maintain a sliding window of the consecutive robots being considered.


提示 3

Use either a map, deque, or heap to find the maximum charge times in the window efficiently.

解法1:滑动窗口 + 单调队列

同类题型详解:LeetCode 239. 滑动窗口最大值-CSDN博客

Java版:

class Solution {
    public int maximumRobots(int[] chargeTimes, int[] runningCosts, long budget) {
        Deque<Integer> queue = new ArrayDeque<>();
        int n = chargeTimes.length;
        int cnt = 0;
        long costs = 0;
        int l = 0;
        int r = 0;
        while (r < n) {
            while (!queue.isEmpty() && chargeTimes[r] > queue.peekLast()) {
                queue.pollLast();
            }
            queue.offer(chargeTimes[r]);
            costs += runningCosts[r];
            while (l <= r && queue.peek() + (r - l + 1) * costs > budget) {
                if (chargeTimes[l] == queue.peek()) {
                    queue.poll();
                }
                costs -= runningCosts[l];
                l++;
            }
            cnt = Math.max(cnt, r - l + 1);
            r++;
        }
        return cnt;
    }
}

Python3版:

class Solution:
    def maximumRobots(self, chargeTimes: List[int], runningCosts: List[int], budget: int) -> int:
        queMax = deque()
        cnt = 0
        costs = 0
        l = 0
        r = 0
        while r < len(chargeTimes):
            while queMax and chargeTimes[r] > queMax[-1]:
                queMax.pop()
            queMax.append(chargeTimes[r])
            costs += runningCosts[r]
            if l <= r and queMax[0] + (r - l + 1) * costs > budget:
                if chargeTimes[l] == queMax[0]:
                    queMax.popleft()
                costs -= runningCosts[l]
                l += 1
            cnt = max(cnt, r - l + 1)
            r += 1
        return cnt
复杂度分析
  • 时间复杂度:O(n),其中 n 为 chargeTimes 的长度。
  • 空间复杂度:O(n)。
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值