reworked increasing sliding window - still no luck for No 2398
This commit is contained in:
@@ -6,46 +6,40 @@ func MaximumRobots(chargeTimes []int, runningCosts []int, budget int64) int {
|
||||
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])
|
||||
}
|
||||
prevCharges := make([]int, l)
|
||||
prevCosts := make([]int, l)
|
||||
maxWndRobots := make([]int, l+1)
|
||||
|
||||
for w := l; w > 0; w, totalCost = w-1, totalCost-int64(runningCosts[w-1]) {
|
||||
monoQ := []int{}
|
||||
runCost := totalCost
|
||||
maxRobots := 0
|
||||
|
||||
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]
|
||||
for wnd := 1; wnd <= l; wnd++ {
|
||||
windows := l - wnd + 1
|
||||
|
||||
begin := 0
|
||||
end := wnd - 1
|
||||
|
||||
for w := 0; w < windows; w++ {
|
||||
m := chargeTimes[end]
|
||||
|
||||
if m > prevCharges[w] {
|
||||
prevCharges[w] = m
|
||||
}
|
||||
prevCosts[w] += runningCosts[end]
|
||||
|
||||
totalCost := int64(wnd*prevCosts[w] + prevCharges[w])
|
||||
if totalCost <= budget {
|
||||
maxRobots = wnd
|
||||
}
|
||||
|
||||
monoQ = append(monoQ, j)
|
||||
begin++
|
||||
end++
|
||||
}
|
||||
|
||||
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])
|
||||
maxWndRobots[wnd] = maxRobots
|
||||
if maxWndRobots[wnd] == maxWndRobots[wnd-1] {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
return maxRobots
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user