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
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
totalCost := int64(0)
|
prevCharges := make([]int, l)
|
||||||
// Calculating running cost for the initial sliding window of size w == l
|
prevCosts := make([]int, l)
|
||||||
for i := 0; i < l; i++ {
|
maxWndRobots := make([]int, l+1)
|
||||||
totalCost += int64(runningCosts[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
for w := l; w > 0; w, totalCost = w-1, totalCost-int64(runningCosts[w-1]) {
|
maxRobots := 0
|
||||||
monoQ := []int{}
|
|
||||||
runCost := totalCost
|
|
||||||
|
|
||||||
for j := 0; j < w; j++ {
|
for wnd := 1; wnd <= l; wnd++ {
|
||||||
// Maintain the monotonic decreasing property
|
windows := l - wnd + 1
|
||||||
for len(monoQ) > 0 && chargeTimes[monoQ[len(monoQ)-1]] < chargeTimes[j] {
|
|
||||||
monoQ = monoQ[:len(monoQ)-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++ {
|
maxWndRobots[wnd] = maxRobots
|
||||||
|
if maxWndRobots[wnd] == maxWndRobots[wnd-1] {
|
||||||
if int64(w)*runCost+int64(chargeTimes[monoQ[0]]) <= budget {
|
break
|
||||||
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
|
return maxRobots
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user