码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode1105 Filling Bookcase Shelves

时间:2019-11-20 22:02:38      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:elves   div   +=   i++   实现   shelve   cas   amp   shelf   

思路:

dp[i]表示摆放好前i本书所需要的最小代价。

实现:

 1 class Solution
 2 {
 3 public:
 4     int minHeightShelves(vector<vector<int>>& books, int shelf_width)
 5     {
 6         int n = books.size();
 7         vector<int> dp(n + 1, 0);
 8         dp[0] = 0; dp[1] = books[0][1];
 9         for (int i = 2; i <= n; i++)
10         {
11             dp[i] = dp[i - 1] + books[i - 1][1];
12             int sum = books[i - 1][0], maxn = books[i - 1][1];
13             for (int j = i - 1; j >= 1; j--)
14             {
15                 if (sum + books[j - 1][0] > shelf_width) break;
16                 maxn = max(maxn, books[j - 1][1]);
17                 dp[i] = min(dp[i], dp[j - 1] + maxn);
18                 sum += books[j - 1][0];
19             }
20         }
21         return dp[n];
22     }
23 }

leetcode1105 Filling Bookcase Shelves

标签:elves   div   +=   i++   实现   shelve   cas   amp   shelf   

原文地址:https://www.cnblogs.com/wangyiming/p/11900751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!