mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
go: add «1105. Filling Bookcase Shelves»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
95c58e0ea3
commit
adcfcd4d91
1 changed files with 23 additions and 0 deletions
23
go/filling-bookcase-shelves.go
Normal file
23
go/filling-bookcase-shelves.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
package main
|
||||
|
||||
func minHeightShelves(books [][]int, shelfWidth int) int {
|
||||
dp := make([]int, len(books)+1)
|
||||
|
||||
dp[0], dp[1] = 0, books[0][1]
|
||||
|
||||
for i := 2; i <= len(books); i++ {
|
||||
remainingWidth := shelfWidth - books[i-1][0]
|
||||
maxHeight := books[i-1][1]
|
||||
|
||||
dp[i] = books[i-1][1] + dp[i-1]
|
||||
|
||||
for j := i - 1; j > 0 && remainingWidth-books[j-1][0] >= 0; j-- {
|
||||
maxHeight = max(maxHeight, books[j-1][1])
|
||||
remainingWidth -= books[j-1][0]
|
||||
|
||||
dp[i] = min(dp[i], maxHeight+dp[j-1])
|
||||
}
|
||||
}
|
||||
|
||||
return dp[len(books)]
|
||||
}
|
Loading…
Reference in a new issue