Advertisement

Maximum Number of Robots Within Budget - LeetCode 2398 Solution

Maximum Number of Robots Within Budget - Complete Solution Guide

Maximum Number of Robots Within Budget is LeetCode problem 2398, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You have n robots. You are given two 0-indexed integer arrays, chargeTimes and runningCosts , both of length n . The i th 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 n

Detailed Explanation

The problem asks us to find the maximum number of *consecutive* robots we can run within a given budget. Each robot has a charge time and a running cost. The total cost for *k* consecutive robots is calculated as the maximum charge time among those *k* robots, plus *k* times the sum of their running costs. We need to find the largest possible *k* such that the total cost does not exceed the given budget.

Solution Approach

The solution uses a sliding window approach combined with a monotonic queue (deque). The sliding window expands from left to right, and the deque maintains the indices of the potential maximum charge times within the current window in descending order. The head of the deque always contains the index of the maximum charge time in the current window. If the cost exceeds the budget, the window shrinks from the left until the cost is within the budget.

Step-by-Step Algorithm

  1. Step 1: Initialize `left` pointer to 0 (start of the window), `current_sum_costs` to 0, `max_len` to 0, and a deque `dq`.
  2. Step 2: Iterate through the `chargeTimes` and `runningCosts` arrays using the `right` pointer (end of the window).
  3. Step 3: Add the running cost of the robot at index `right` to `current_sum_costs`.
  4. Step 4: Maintain the monotonic queue: Remove elements from the back of the deque if their corresponding charge times are less than or equal to the charge time of the current robot (at index `right`). Then, add `right` to the back of the deque.
  5. Step 5: Check if the cost (maximum charge time from the deque + (window size) * `current_sum_costs`) exceeds the budget.
  6. Step 6: If the cost exceeds the budget, shrink the window from the left. Remove the leftmost element from the deque if it's the leftmost index of the current window, subtract `runningCosts[left]` from `current_sum_costs`, and increment the `left` pointer.
  7. Step 7: Update `max_len` with the maximum window size seen so far (right - left + 1).
  8. Step 8: Return `max_len` after iterating through all the robots.

Key Insights

  • Insight 1: The problem requires finding the maximum length of a *consecutive* sub-array that satisfies a given condition (total cost <= budget). This strongly suggests using a sliding window approach.
  • Insight 2: Efficiently calculating the maximum charge time within the sliding window is crucial for performance. A monotonic queue (deque) can be used to maintain the maximum element in O(1) time per update.
  • Insight 3: The budget is large (up to 10^15), so we need to use `long` or `long long` data types to avoid integer overflow when calculating the total cost.
  • Insight 4: Realize the Monotonic Queue can maintain a sorted order of elements within the current window, enabling O(1) access to the maximum element. This is key to avoiding O(k) re-computation within each window.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Binary Search, Queue, Sliding Window, Heap (Priority Queue), Prefix Sum, Monotonic Queue.

Companies

Asked at: InMobi.