返回介绍

solution / 2300-2399 / 2398.Maximum Number of Robots Within Budget / README_EN

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

2398. Maximum Number of Robots Within Budget

中文文档

Description

You have n robots. You are given two 0-indexed integer arrays, chargeTimes and runningCosts, both of length n. The ith robot costs chargeTimes[i] units to charge and costs runningCosts[i] units to run. You are also given an integer budget.

The total cost of running k chosen robots is equal to max(chargeTimes) + k * sum(runningCosts), where max(chargeTimes) is the largest charge cost among the k robots and sum(runningCosts) is the sum of running costs among the k robots.

Return_ the maximum number of consecutive robots you can run such that the total cost does not exceed _budget.

 

Example 1:

Input: chargeTimes = [3,6,1,3,4], runningCosts = [2,1,3,4,5], budget = 25
Output: 3
Explanation: 
It is possible to run all individual and consecutive pairs of robots within budget.
To obtain answer 3, consider the first 3 robots. The total cost will be max(3,6,1) + 3 * sum(2,1,3) = 6 + 3 * 6 = 24 which is less than 25.
It can be shown that it is not possible to run more than 3 consecutive robots within budget, so we return 3.

Example 2:

Input: chargeTimes = [11,12,19], runningCosts = [10,8,7], budget = 19
Output: 0
Explanation: No robot can be run that does not exceed the budget, so we return 0.

 

Constraints:

  • chargeTimes.length == runningCosts.length == n
  • 1 <= n <= 5 * 104
  • 1 <= chargeTimes[i], runningCosts[i] <= 105
  • 1 <= budget <= 1015

Solutions

Solution 1

class Solution:
  def maximumRobots(
    self, chargeTimes: List[int], runningCosts: List[int], budget: int
  ) -> int:
    q = deque()
    ans = j = s = 0
    for i, (a, b) in enumerate(zip(chargeTimes, runningCosts)):
      while q and chargeTimes[q[-1]] <= a:
        q.pop()
      q.append(i)
      s += b
      while q and chargeTimes[q[0]] + (i - j + 1) * s > budget:
        if q[0] == j:
          q.popleft()
        s -= runningCosts[j]
        j += 1
      ans = max(ans, i - j + 1)
    return ans
class Solution {
  public int maximumRobots(int[] chargeTimes, int[] runningCosts, long budget) {
    Deque<Integer> q = new ArrayDeque<>();
    int n = chargeTimes.length;
    long s = 0;
    int j = 0;
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      int a = chargeTimes[i], b = runningCosts[i];
      while (!q.isEmpty() && chargeTimes[q.getLast()] <= a) {
        q.pollLast();
      }
      q.offer(i);
      s += b;
      while (!q.isEmpty() && chargeTimes[q.getFirst()] + (i - j + 1) * s > budget) {
        if (q.getFirst() == j) {
          q.pollFirst();
        }
        s -= runningCosts[j++];
      }
      ans = Math.max(ans, i - j + 1);
    }
    return ans;
  }
}
class Solution {
public:
  int maximumRobots(vector<int>& chargeTimes, vector<int>& runningCosts, long long budget) {
    deque<int> q;
    long long s = 0;
    int ans = 0, j = 0, n = chargeTimes.size();
    for (int i = 0; i < n; ++i) {
      int a = chargeTimes[i], b = runningCosts[i];
      while (!q.empty() && chargeTimes[q.back()] <= a) q.pop_back();
      q.push_back(i);
      s += b;
      while (!q.empty() && chargeTimes[q.front()] + (i - j + 1) * s > budget) {
        if (q.front() == j) {
          q.pop_front();
        }
        s -= runningCosts[j++];
      }
      ans = max(ans, i - j + 1);
    }
    return ans;
  }
};
func maximumRobots(chargeTimes []int, runningCosts []int, budget int64) int {
  s := int64(0)
  ans, j := 0, 0
  q := []int{}
  for i, a := range chargeTimes {
    for len(q) > 0 && chargeTimes[q[len(q)-1]] <= a {
      q = q[:len(q)-1]
    }
    q = append(q, i)
    s += int64(runningCosts[i])
    for len(q) > 0 && int64(chargeTimes[q[0]])+int64(i-j+1)*s > budget {
      if q[0] == j {
        q = q[1:]
      }
      s -= int64(runningCosts[j])
      j++
    }
    ans = max(ans, i-j+1)
  }
  return ans
}

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

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

发布评论

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