From df2eb7aa15fd73b0fb2aae7859d9ff37299fbbda Mon Sep 17 00:00:00 2001 From: "rostislavs.kuracs" Date: Mon, 27 Nov 2023 19:14:04 +0200 Subject: [PATCH] trying with monotonic queue - still no luck for No 2398 --- 2398-max-num-robots-on-budget.go | 97 ++++++++++---------------------- 1 file changed, 29 insertions(+), 68 deletions(-) diff --git a/2398-max-num-robots-on-budget.go b/2398-max-num-robots-on-budget.go index bcdefd5..e71eba7 100644 --- a/2398-max-num-robots-on-budget.go +++ b/2398-max-num-robots-on-budget.go @@ -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]) } }