{"id":5264,"date":"2019-06-29T21:40:29","date_gmt":"2019-06-30T04:40:29","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5264"},"modified":"2019-06-30T23:30:31","modified_gmt":"2019-07-01T06:30:31","slug":"leetcode-1105-filling-bookcase-shelves","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1105-filling-bookcase-shelves\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1105. Filling Bookcase Shelves"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1105. Filling Bookcase Shelves - \u5237\u9898\u627e\u5de5\u4f5c EP253\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/a7TLEVdqg0Q?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>We have a sequence of&nbsp;<code>books<\/code>: the&nbsp;<code>i<\/code>-th book has thickness&nbsp;<code>books[i][0]<\/code>&nbsp;and height&nbsp;<code>books[i][1]<\/code>.<\/p>\n\n\n\n<p>We want to place these books&nbsp;<strong>in order<\/strong>&nbsp;onto bookcase shelves that have total width&nbsp;<code>shelf_width<\/code>.<\/p>\n\n\n\n<p>We choose&nbsp;some of the books to place on this shelf (such that the sum of their thickness is&nbsp;<code>&lt;= shelf_width<\/code>), 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.&nbsp; We repeat this process until there are no more books to place.<\/p>\n\n\n\n<p>Note again that at each step of the above&nbsp;process,&nbsp;the order of the books we place is the same order as the given sequence of books.&nbsp; For example, if we have an ordered list of 5&nbsp;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.<\/p>\n\n\n\n<p>Return the minimum possible height that the total bookshelf can be after placing shelves in this manner.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/06\/24\/shelves.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong> books = [[1,1],[2,3],[2,3],[1,1],[1,1],[1,1],[1,2]], shelf_width = 4\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong>\nThe sum of the heights of the 3 shelves are 1 + 3 + 2 = 6.\nNotice that book number 2 does not have to be on the first shelf.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= books.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= books[i][0] &lt;= shelf_width &lt;= 1000<\/code><\/li><li><code>1 &lt;= books[i][1] &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/06\/1105-ep253.png\" alt=\"\" class=\"wp-image-5268\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/06\/1105-ep253.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/06\/1105-ep253-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/06\/1105-ep253-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>dp[i] := min height of placing books[0] ~ books[i]<br>dp[-1] = 0<br>dp[j] = min{dp[i-1] + max(h[i] ~ h[j])}, 0 &lt; i &lt;= j, sum(w[i] ~ w[j]) &lt;= shelf_width<\/p>\n\n\n\n<p>Time complexity: O(n^2)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n\/\/ Author: Huahua, running time: 0 ms\nclass Solution {\npublic:\n  int minHeightShelves(vector<vector<int>>& books, int sw) {\n    int n = books.size();      \n    vector<int> dp(n, INT_MAX \/ 2);\n    for (int i = 0; i < n; ++i) {\n      int w = 0;\n      int h = 0;\n      for (int j = i; j < n; ++j) {        \n        if ((w += books[j][0]) > sw) break;\n        h = max(h, books[j][1]);\n        dp[j] = min(dp[j], (i == 0 ? 0 : dp[i - 1]) + h);\n      }\n    }\n    return dp.back();\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We have a sequence of&nbsp;books: the&nbsp;i-th book has thickness&nbsp;books[i][0]&nbsp;and height&nbsp;books[i][1]. We want to place these books&nbsp;in order&nbsp;onto bookcase shelves that have total width&nbsp;shelf_width. We choose&nbsp;some&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[20,18],"class_list":["post-5264","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-array","tag-dp","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5264"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5264\/revisions"}],"predecessor-version":[{"id":5271,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5264\/revisions\/5271"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}