[Swift]LeetCode1105. 填充書架 | Filling Bookcase Shelves

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-xissikfo-kz.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

We have a sequence of books: the i-th book has thickness books[i][0] and height books[i][1].git

We want to place these books in order onto bookcase shelves that have total width shelf_width.github

We choose some of the books to place on this shelf (such that the sum of their thickness is <= shelf_width), then build another level of shelf of the bookcase so that the total height of the bookcase has increased by the maximum height of the books we just put down.  We repeat this process until there are no more books to place.微信

Note again that at each step of the above process, the order of the books we place is the same order as the given sequence of books.  For example, if we have an ordered list of 5 books, we might place the first and second book onto the first shelf, the third book on the second shelf, and the fourth and fifth book on the last shelf.ide

Return the minimum possible height that the total bookshelf can be after placing shelves in this manner.oop

Example 1:ui

Input: books = [[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]], shelf_width = 4
Output: 6
Explanation:
The sum of the heights of the 3 shelves are 1 + 3 + 2 = 6.
Notice that book number 2 does not have to be on the first shelf.

Constraints:this

  • 1 <= books.length <= 1000
  • 1 <= books[i][0] <= shelf_width <= 1000
  • 1 <= books[i][1] <= 1000

附近的家居城促銷,你買回了一直心儀的可調節書架,打算把本身的書都整理到新的書架上。spa

你把要擺放的書 books 都整理好,疊成一摞:從上往下,第 i 本書的厚度爲 books[i][0],高度爲 books[i][1]code

按順序 將這些書擺放到總寬度爲 shelf_width 的書架上。

先選幾本書放在書架上(它們的厚度之和小於等於書架的寬度 shelf_width),而後再建一層書架。重複這個過程,直到把全部的書都放在書架上。

須要注意的是,在上述過程的每一個步驟中,擺放書的順序與你整理好的順序相同。 例如,若是這裏有 5 本書,那麼可能的一種擺放狀況是:第一和第二本書放在第一層書架上,第三本書放在第二層書架上,第四和第五本書放在最後一層書架上。

每一層所擺放的書的最大高度就是這一層書架的層高,書架總體的高度爲各層高之和。

以這種方式佈置書架,返回書架總體可能的最小高度。 

示例:

輸入:books = [[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]], shelf_width = 4
輸出:6
解釋:
3 層書架的高度和爲 1 + 3 + 2 = 6 。
第 2 本書沒必要放在第一層書架上。 

提示:

  • 1 <= books.length <= 1000
  • 1 <= books[i][0] <= shelf_width <= 1000
  • 1 <= books[i][1] <= 1000

20ms
 1 class Solution {
 2     func minHeightShelves(_ books: [[Int]], _ shelf_width: Int) -> Int {
 3         return minHeightShelvesRecursionMain(books, shelf_width)
 4     }
 5     
 6     func minHeightShelvesRecursionMain(_ books: [[Int]], _ shelf_width: Int) -> Int {
 7         guard !books.isEmpty else { return 0 }
 8         
 9         var hash = [Int:Int]()
10         return minHeightShelvesRecursion(books, shelf_width, 0, &hash)
11     }
12     
13     func minHeightShelvesRecursion(_ books: [[Int]], 
14                                    _ shelf_width: Int,
15                                    _ bookIndex: Int,
16                                    _ hash: inout [Int:Int]) -> Int {
17         guard bookIndex < books.count else { return 0 }
18         
19         if let result = hash[bookIndex] { return result }
20         
21         var i = bookIndex
22         var width = 0
23         var height = 0
24         var result = Int.max
25         
26         while i < books.count, width + books[i][0] <= shelf_width {
27             width += books[i][0]
28             height = max(height, books[i][1])
29             
30             let next = minHeightShelvesRecursion(books, shelf_width, i+1, &hash)
31             result = min(result, height + next)   
32             
33             i += 1
34         }
35         
36         hash[bookIndex] = result
37         return result
38     }
39 }

24ms

 1 class Solution {
 2     func minHeightShelves(_ books: [[Int]], _ shelf_width: Int) -> Int {
 3         // Initialize array of solutions for each book
 4         let n = books.count
 5         var leastHeight = [Int](repeating: 0, count: n + 1)
 6         for i in (0 ..< n).reversed() {
 7             var rowThickness = books[i][0]
 8             var rowHeight = books[i][1]
 9             leastHeight[i] = rowHeight + leastHeight[i + 1]
10             // Loop through all possible shelfmates
11             for k in i + 1 ..< n {
12                 rowThickness += books[k][0]
13                 if rowThickness > shelf_width {
14                     break
15                 }
16                 rowHeight = max(rowHeight, books[k][1])
17                 leastHeight[i] = min(leastHeight[i], rowHeight + leastHeight[k + 1])
18             }
19         }
20         return leastHeight[0]
21     }
22 }

28ms

 1 class Solution {
 2     func minHeightShelves(_ books: [[Int]], _ shelf_width: Int) -> Int {
 3         var dp = [Int](repeating: 1000 * 1000, count: books.count+1)
 4         dp[0] = 0
 5         for i in 1...books.count {
 6             var b = books[i-1]
 7             var w = b[0]
 8             var h = b[1]
 9             dp[i] = dp[i-1] + h
10             for j in stride(from:i-1, to:0, by: -1) {
11                 w += books[j-1][0]
12                 h = max(h, books[j-1][1])
13                 if w > shelf_width { break }
14                 dp[i] = min(dp[i], dp[j-1] + h)
15             }
16         }
17         return dp[books.count]
18     }
19 }

36ms

 1 class Solution {
 2     func minHeightShelves(_ books: [[Int]], _ shelf_width: Int) -> Int {
 3         return minHeightShelvesRecursionMain(books, shelf_width)
 4     }
 5     
 6     func minHeightShelvesRecursionMain(_ books: [[Int]], _ shelf_width: Int) -> Int {
 7         guard !books.isEmpty else { return 0 }
 8         
 9         var hash = [Int:Int]()
10         return minHeightShelvesRecursion(books, shelf_width, 0, &hash)
11     }
12     
13     func minHeightShelvesRecursion(_ books: [[Int]], 
14                                    _ shelf_width: Int,
15                                    _ bookIndex: Int,
16                                    _ hash: inout [Int:Int]) -> Int {
17         guard bookIndex < books.count else { return 0 }
18         
19         if let result = hash[bookIndex] { return result }
20         
21         var i = bookIndex
22         var width = 0
23         var height = 0
24         var result = Int.max
25         
26         while i < books.count, width + books[i][0] <= shelf_width {
27             width += books[i][0]
28             height = max(height, books[i][1])
29             
30             let next = minHeightShelvesRecursion(books, shelf_width, i+1, &hash)
31             
32             if next != Int.max {
33                 result = min(result, height + next)   
34             }
35             
36             i += 1
37         }
38         
39         hash[bookIndex] = result
40         return result
41     }
42 }

44ms 
 1 class Solution {
 2     func minHeightShelves(_ books: [[Int]], _ shelf_width: Int) -> Int {
 3         // Initialize array of solutions for each book
 4         let n = books.count
 5         var leastHeight = [Int](repeating: 0, count: n + 1)
 6         for i in (0 ..< n).reversed() {
 7             var rowThickness = books[i][0]
 8             var rowHeight = books[i][1]
 9             leastHeight[i] = rowHeight + leastHeight[i + 1]
10             // Loop through all possible shelfmates
11             for k in i + 1 ..< n {
12                 rowThickness += books[k][0]
13                 guard rowThickness <= shelf_width else { break }
14                 rowHeight = max(rowHeight, books[k][1])
15                 leastHeight[i] = min(leastHeight[i], rowHeight + leastHeight[k + 1])
16             }
17         }
18         return leastHeight[0]
19     }
20 }

Runtime: 64 ms
Memory Usage: 20.8 MB
 1 class Solution {
 2     let INF:Int = Int(1e9 + 5)
 3     func minHeightShelves(_ books: [[Int]], _ shelf_width: Int) -> Int {
 4         let n:Int = books.count
 5         var dp:[Int] = [Int](repeating:INF,count:n + 1)
 6         dp[0] = 0
 7         
 8         for i in 0..<n
 9         {
10             var width:Int = 0
11             var height:Int = 0
12             
13             for j in i..<n
14             {
15                 width += books[j][0]
16                 height = max(height, books[j][1])
17                 
18                 if width <= shelf_width
19                 {
20                     dp[j + 1] = min(dp[j + 1], dp[i] + height)
21                 }
22             }
23         }
24         return dp[n]
25     }
26 }
相關文章
相關標籤/搜索