标签: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