52 lines
1.2 KiB
Go
52 lines
1.2 KiB
Go
package main
|
|
|
|
func MaximumRobots(chargeTimes []int, runningCosts []int, budget int64) int {
|
|
l := len(chargeTimes)
|
|
if l != len(runningCosts) {
|
|
return 0
|
|
}
|
|
|
|
totalCost := int64(0)
|
|
// Calculating running cost for the initial sliding window of size w == l
|
|
for i := 0; i < l; i++ {
|
|
totalCost += int64(runningCosts[i])
|
|
}
|
|
|
|
for w := l; w > 0; w, totalCost = w-1, totalCost-int64(runningCosts[w-1]) {
|
|
monoQ := []int{}
|
|
runCost := totalCost
|
|
|
|
for j := 0; j < w; j++ {
|
|
// Maintain the monotonic decreasing property
|
|
for len(monoQ) > 0 && chargeTimes[monoQ[len(monoQ)-1]] < chargeTimes[j] {
|
|
monoQ = monoQ[:len(monoQ)-1]
|
|
}
|
|
|
|
monoQ = append(monoQ, j)
|
|
}
|
|
|
|
for s := 1; (s + w) <= l; s++ {
|
|
|
|
if int64(w)*runCost+int64(chargeTimes[monoQ[0]]) <= budget {
|
|
return w
|
|
}
|
|
|
|
// Remove elements out of the current window
|
|
if len(monoQ) > 0 && monoQ[0] == s+w-1 {
|
|
monoQ = monoQ[1:]
|
|
}
|
|
// Maintain the monotonic decreasing property
|
|
for (s+w) < l && len(monoQ) > 0 && chargeTimes[monoQ[len(monoQ)-1]] < chargeTimes[s+w] {
|
|
monoQ = monoQ[:len(monoQ)-1]
|
|
}
|
|
|
|
monoQ = append(monoQ, s+w)
|
|
|
|
runCost -= int64(runningCosts[s-1])
|
|
runCost += int64(runningCosts[s+w-1])
|
|
}
|
|
}
|
|
|
|
return 0
|
|
}
|