time limit exceeded solution for No 2398
This commit is contained in:
130
2398-max-num-robots-on-budget.go
Normal file
130
2398-max-num-robots-on-budget.go
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
key.end++
|
||||||
|
value, ok = maxIntCache[key]
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
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
|
||||||
|
//}
|
||||||
|
|
||||||
|
totalCost := sumInt(runningCosts, 0, l-1)
|
||||||
|
runCost := totalCost
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
20
2398-max-num-robots-on-budget_test.go
Normal file
20
2398-max-num-robots-on-budget_test.go
Normal file
File diff suppressed because one or more lines are too long
1
go.mod
1
go.mod
@@ -7,6 +7,5 @@ require github.com/stretchr/testify v1.7.0
|
|||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.0 // indirect
|
github.com/davecgh/go-spew v1.1.0 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
github.com/stretchr/objx v0.1.0 // indirect
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
1
go.sum
1
go.sum
@@ -2,7 +2,6 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
|
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
|||||||
Reference in New Issue
Block a user