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

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11096572.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

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

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.

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

Example 1:

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:

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

附近的家居城促销,你买回了一直心仪的可调节书架,打算把自己的书都整理到新的书架上。

你把要摆放的书 books 都整理好,叠成一摞:从上往下,第 i 本书的厚度为 books[i][0],高度为 books[i][1]

按顺序 将这些书摆放到总宽度为 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_ Int) -> Int {
 3         return minHeightShelvesRecursionMain(books, shelf_width)
 4     }
 5     
 6     func minHeightShelvesRecursionMain(_ books: [[Int]], _ shelf_ 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_ 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_ 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_ 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_ Int) -> Int {
 3         return minHeightShelvesRecursionMain(books, shelf_width)
 4     }
 5     
 6     func minHeightShelvesRecursionMain(_ books: [[Int]], _ shelf_ 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_ 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_ 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_ 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 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 }
原文地址:https://www.cnblogs.com/strengthen/p/11096572.html