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 package main
import ( import "fmt"
"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
}
type CacheKey struct { type CacheKey struct {
start int start int
@@ -42,24 +15,17 @@ func maxIntCached(in []int, start int, end int) int {
if ok { if ok {
return value return value
} }
fmt.Println(start, " - ", end)
key.end++ key.start = start
value, ok = maxIntCache[key] value = in[start]
if !ok { for j := start + 1; j <= end; j++ {
maxValue, _ := maxInt(in, start, end) key.end = j
value = maxValue if in[j] > value {
key.start = start value = in[j]
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
} }
maxIntCache[key] = value
} }
return 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 { func MaximumRobots(chargeTimes []int, runningCosts []int, budget int64) int {
maxIntCache = map[CacheKey]int{}
l := len(chargeTimes) l := len(chargeTimes)
if l != len(runningCosts) { if l != len(runningCosts) {
return 0 return 0
} }
maxCharge := maxIntCached(chargeTimes, 0, l-1) maxIntCache = map[CacheKey]int{}
//minCosts := minInt(runningCosts)
//if int64(maxCharge + minCosts) > budget {
//return 0
//}
totalCost := sumInt(runningCosts, 0, l-1) totalCost := sumInt(runningCosts, 0, l-1)
runCost := totalCost runCost := totalCost
for w := l; w > 0; w-- { for w := l; w > 0; w-- {
maxCharge = maxIntCached(chargeTimes, 0, w-1) maxCharge := maxIntCached(chargeTimes, 0, w-1)
if w != l { if w != l {
totalCost -= int64(runningCosts[w]) totalCost -= int64(runningCosts[w])