trying with monotonic queue - still no luck for No 2398
This commit is contained in:
@@ -1,88 +1,49 @@
|
|||||||
package main
|
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 {
|
func MaximumRobots(chargeTimes []int, runningCosts []int, budget int64) int {
|
||||||
l := len(chargeTimes)
|
l := len(chargeTimes)
|
||||||
if l != len(runningCosts) {
|
if l != len(runningCosts) {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
maxIntCache = map[CacheKey]int{}
|
totalCost := int64(0)
|
||||||
|
// Calculating running cost for the initial sliding window of size w == l
|
||||||
totalCost := sumInt(runningCosts, 0, l-1)
|
for i := 0; i < l; i++ {
|
||||||
runCost := totalCost
|
totalCost += int64(runningCosts[i])
|
||||||
|
|
||||||
for w := l; w > 0; w-- {
|
|
||||||
maxCharge := maxIntCached(chargeTimes, 0, w-1)
|
|
||||||
|
|
||||||
if w != l {
|
|
||||||
totalCost -= int64(runningCosts[w])
|
|
||||||
runCost = totalCost
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (int64(w)*runCost + int64(maxCharge)) <= budget {
|
for w := l; w > 0; w, totalCost = w-1, totalCost-int64(runningCosts[w-1]) {
|
||||||
return w
|
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++ {
|
for s := 1; (s + w) <= l; s++ {
|
||||||
runCost -= int64(runningCosts[s-1])
|
|
||||||
runCost += int64(runningCosts[s+w-1])
|
|
||||||
|
|
||||||
if chargeTimes[s-1] == maxCharge {
|
if int64(w)*runCost+int64(chargeTimes[monoQ[0]]) <= budget {
|
||||||
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 {
|
|
||||||
return w
|
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