题目如下:

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

解题思路:比起大神们的算法,我的dp算法多了一维,差距还是很大的。记dp[i][j] = v 表示第i本书是第j层的最后一本书时,书架第0层到第j层的高度的最小值是v。如果第j-1层的最后一本书是k,那么有dp[i][j] = min(dp[i][j],dp[k][j-1] + max(books[k+1] ~books[i])。有了这个状态转移方程,接下来就是求k的可能取值,每一层至少要有一本书,所以k的最大值只能是i-1,最小值则要通过计算得出,详见代码。

代码如下:

class Solution(object):
def minHeightShelves(self, books, shelf_width):
"""
:type books: List[List[int]]
:type shelf_width: int
:rtype: int
"""
dp = [[float('inf') for i in range(len(books))] for i in range(len(books))]
dp[0][0] = books[0][1] width = 0
level = 0
level_list_per_item = []
for i in range(len(books)):
if width + books[i][0] <= shelf_width:
level_list_per_item.append(level)
width += books[i][0]
else:
level += 1
level_list_per_item.append(level)
width = books[i][0] for i in range(1,len(books)):
for j in range(level_list_per_item[i],i+1):
if j > 0:
dp[i][j] = min(dp[i][j],dp[i-1][j-1] + books[i][1])
row_width = books[i][0]
row_max_height = books[i][1]
for k in range(i-1,j-2,-1):
if row_width + books[k][0] <= shelf_width:
row_width += books[k][0]
row_max_height = max(row_max_height,books[k][1])
if k == 0:
dp[i][j] = min(dp[i][0] ,row_max_height)
continue
dp[i][j] = min(dp[i][j],dp[k-1][j-1]+ row_max_height)
else:
break
#print dp
return min(dp[-1])

【leetcode】1105. Filling Bookcase Shelves的更多相关文章

  1. LeetCode 1105. Filling Bookcase Shelves

    原题链接在这里:https://leetcode.com/problems/filling-bookcase-shelves/ 题目: We have a sequence of books: the ...

  2. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  3. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  4. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  5. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  6. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  7. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  8. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  9. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

随机推荐

  1. postMessage——解决跨域、跨窗口消息传递

    参考资料1:[http://www.cnblogs.com/dolphinX/p/3464056.html] 参考资料2:[https://developer.mozilla.org/en-US/do ...

  2. 6.3.2巴特沃斯(butterworth)低通滤波器

    在本程序中,共有六个自定义函数,分别是: 1. myMagnitude(Mat & complexImg,Mat & magnitudeImage),在该函数中封装了Opencv中的 ...

  3. Java程序设计——反转字符串 & 找朋友 & 计算int型二进制1的个数 & 情报加密 & 计算日期 & 求近似数 & 输出较小数(练习1)

    作为刚刚入门Java的选手,其实C++的功底起到了很大的作用.但是,Java之于C++最大的不同,我个人认为,是其类的多样性.才入门的我,写着老师布置的简单的面对过程的题,如果是C++,可以算是简单了 ...

  4. CSRF token的原理

    参考: http://www.cnblogs.com/zhaof/p/6281482.html 简介 django为用户实现防止跨站请求伪造的功能,通过中间件 django.middleware.cs ...

  5. pip提示ModuleNotFoundError: No module named 'pkg_resources'

    卸载setuptools后,pip下载python包一直提示ModuleNotFoundError: No module named 'pkg_resources',如下图: 在网上找了很多贴了都无法 ...

  6. Elasticsearch-安装、日志解读

    ES-安装.日志解读 1. 准备tar包 https://www.elastic.co/cn/products/elasticsearch2. 解压 tar -zxvf elasticsearch-. ...

  7. [转帖]Linux下批量替换文件内容方法

    Linux下批量替换文件内容方法 https://www.cnblogs.com/fjping0606/p/4428850.html 刚才用到的命令 原作者写的挺好的记录一下 以后 用. 1:查找fi ...

  8. Spring(九)--通知

    Spring之Advice通知 Spring原生的经典模式  实现AOPadvice :通知 前置通知:在目标方法执行之前执行!不能改变方法的执行流程和结果!            实现MethodB ...

  9. 数位dp(不要62)

    http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间内满足以下条件的数量 1.数位不能出现4,2.任意两相邻数位不能是62. 解法:数位dp[po ...

  10. C++中的new/delete、构造/析构函数、dynamic_cast分析

    1,new 关键字和 malloc 函数区别(自己.功能.应用): 1,new 关键字是 C++ 的一部分: 1,如果是 C++ 编译器,则肯定可以用 new 申请堆空间内存: 2,malloc 是由 ...