预算内的最多机器人数目
难度:
标签:
题目描述
代码结果
运行时间: 166 ms, 内存: 21.9 MB
/*
题目思路:
使用Java Stream API实现最大连续机器人数的计算。主要思路与Java版本相同,只是使用Stream来计算总运行成本和最大充电时间。
*/
import java.util.*;
import java.util.stream.*;
public int maxRobotsStream(int[] chargeTimes, int[] runningCosts, long budget) {
int n = chargeTimes.length;
int left = 0, right = 0;
long sumRunningCosts = 0;
int maxRobots = 0;
while (right < n) {
// 计算总运行成本
sumRunningCosts += runningCosts[right];
// 计算当前窗口中的最大充电时间
int finalRight = right;
int maxChargeTime = IntStream.range(left, right + 1)
.map(i -> chargeTimes[i])
.max()
.getAsInt();
// 计算当前窗口的总成本
long totalCost = maxChargeTime + (right - left + 1) * sumRunningCosts;
// 如果总成本超出预算,移动左指针,缩小窗口
while (totalCost > budget && left <= right) {
sumRunningCosts -= runningCosts[left];
left++;
maxChargeTime = IntStream.range(left, right + 1)
.map(i -> chargeTimes[i])
.max()
.getAsInt();
totalCost = maxChargeTime + (right - left + 1) * sumRunningCosts;
}
// 更新最大连续运行的机器人数
maxRobots = Math.max(maxRobots, right - left + 1);
right++;
}
return maxRobots;
}
解释
方法:
The solution uses a sliding window approach combined with a deque to maintain the maximum charge time within the current window. As we iterate through the robots using a right pointer, we add the current robot's charge time to the deque in a way that ensures the deque is always sorted in descending order. This allows the largest element (maximum charge time) to always be at the front of the deque. We also keep a running sum of the running costs. At each step, we check if the sum of the maximum charge time and the product of the number of robots and the sum of running costs fits within the budget. If it does, we update our answer. If it doesn't, we adjust the window by moving the left pointer to the right, ensuring the properties of the deque and updating the running costs sum accordingly.
时间复杂度:
O(n)
空间复杂度:
O(n)
代码细节讲解
🦆
在滑动窗口调整左指针时,直接检查deque的首元素与chargeTimes[left]是否相等来决定是否出队,这种做法是否总是正确的?存在哪些潜在风险?
▷🦆
如果数组chargeTimes中存在重复的最大值,当前的解决方案如何处理?解决方案是否能正确地从deque中移除对应元素?
▷🦆
在计算当前窗口是否符合预算条件时,公式直接使用了`(right - left + 1) * costs`来计算总运行成本,这里的costs变量是否正确地表示了窗口内机器人的运行成本之和?
▷