improved but still - time limit exceeded solution for No 2398

This commit is contained in:
rostislavs.kuracs
2023-01-05 19:59:59 +02:00
parent 9cbf3325ed
commit d6e27db0bc

View File

@@ -1,33 +1,6 @@
package main
import (
"fmt"
"math"
)
func minInt(in []int) int {
m := math.MaxInt
for _, v := range in {
if v < m {
m = v
}
}
return m
}
func maxInt(in []int, start int, end int) (int, int) {
fmt.Println(start, " - ", end)
m := math.MinInt
place := -1
for i := start; i <= end; i++ {
v := in[i]
if v > m {
place = i
m = v
}
}
return m, place
}
import "fmt"
type CacheKey struct {
start int
@@ -42,24 +15,17 @@ func maxIntCached(in []int, start int, end int) int {
if ok {
return value
}
fmt.Println(start, " - ", end)
key.end++
value, ok = maxIntCache[key]
key.start = start
value = in[start]
if !ok {
maxValue, _ := maxInt(in, start, end)
value = maxValue
key.start = start
key.end = end
maxIntCache[key] = value
} else {
if value == in[end+1] {
maxValue, _ := maxInt(in, start, end)
value = maxValue
key.start = start
key.end = end
maxIntCache[key] = value
for j := start + 1; j <= end; j++ {
key.end = j
if in[j] > value {
value = in[j]
}
maxIntCache[key] = value
}
return value
@@ -74,24 +40,18 @@ func sumInt(in []int, start int, end int) int64 {
}
func MaximumRobots(chargeTimes []int, runningCosts []int, budget int64) int {
maxIntCache = map[CacheKey]int{}
l := len(chargeTimes)
if l != len(runningCosts) {
return 0
}
maxCharge := maxIntCached(chargeTimes, 0, l-1)
//minCosts := minInt(runningCosts)
//if int64(maxCharge + minCosts) > budget {
//return 0
//}
maxIntCache = map[CacheKey]int{}
totalCost := sumInt(runningCosts, 0, l-1)
runCost := totalCost
for w := l; w > 0; w-- {
maxCharge = maxIntCached(chargeTimes, 0, w-1)
maxCharge := maxIntCached(chargeTimes, 0, w-1)
if w != l {
totalCost -= int64(runningCosts[w])