trying with monotonic queue - still no luck for No 2398
This commit is contained in:
@@ -1,88 +1,49 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type CacheKey struct {
|
||||
start int
|
||||
end int
|
||||
}
|
||||
|
||||
var maxIntCache map[CacheKey]int
|
||||
|
||||
func maxIntCached(in []int, start int, end int) int {
|
||||
key := CacheKey{start: start, end: end}
|
||||
value, ok := maxIntCache[key]
|
||||
if ok {
|
||||
return value
|
||||
}
|
||||
fmt.Println(start, " - ", end)
|
||||
|
||||
key.start = start
|
||||
value = in[start]
|
||||
|
||||
for j := start + 1; j <= end; j++ {
|
||||
key.end = j
|
||||
if in[j] > value {
|
||||
value = in[j]
|
||||
}
|
||||
maxIntCache[key] = value
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func sumInt(in []int, start int, end int) int64 {
|
||||
r := int64(0)
|
||||
for i := start; i <= end; i++ {
|
||||
r += int64(in[i])
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func MaximumRobots(chargeTimes []int, runningCosts []int, budget int64) int {
|
||||
l := len(chargeTimes)
|
||||
if l != len(runningCosts) {
|
||||
return 0
|
||||
}
|
||||
|
||||
maxIntCache = map[CacheKey]int{}
|
||||
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])
|
||||
}
|
||||
|
||||
totalCost := sumInt(runningCosts, 0, l-1)
|
||||
runCost := totalCost
|
||||
for w := l; w > 0; w, totalCost = w-1, totalCost-int64(runningCosts[w-1]) {
|
||||
monoQ := []int{}
|
||||
runCost := totalCost
|
||||
|
||||
for w := l; w > 0; w-- {
|
||||
maxCharge := maxIntCached(chargeTimes, 0, w-1)
|
||||
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]
|
||||
}
|
||||
|
||||
if w != l {
|
||||
totalCost -= int64(runningCosts[w])
|
||||
runCost = totalCost
|
||||
}
|
||||
|
||||
if (int64(w)*runCost + int64(maxCharge)) <= budget {
|
||||
return w
|
||||
monoQ = append(monoQ, j)
|
||||
}
|
||||
|
||||
for s := 1; (s + w) <= l; s++ {
|
||||
runCost -= int64(runningCosts[s-1])
|
||||
runCost += int64(runningCosts[s+w-1])
|
||||
|
||||
if chargeTimes[s-1] == maxCharge {
|
||||
if chargeTimes[s] != maxCharge {
|
||||
maxCharge = maxIntCached(chargeTimes, s, s+w-1)
|
||||
} else {
|
||||
if chargeTimes[s+w-1] > maxCharge {
|
||||
maxCharge = chargeTimes[s+w-1]
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if chargeTimes[s+w-1] > maxCharge {
|
||||
maxCharge = chargeTimes[s+w-1]
|
||||
}
|
||||
}
|
||||
|
||||
if (int64(w)*runCost + int64(maxCharge)) <= budget {
|
||||
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])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user