原题链接在这里:https://leetcode.com/problems/filling-bookcase-shelves/

题目:

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

题解:

Let dp[i] denotes up to index i-1, the minimum height.

For the new book i. It could be on the next row. It could be just itself or with previous books.

When book i and previous i-1, i-2 ... j, stay on the same row, its total width could not be larger than shelf_width.

And coming to j, it could be like j to i-1 is already on this row, i is just added to the same row.

For each j < i, util total width <= shelf_width, update dp[i] with dp[j-1] + max(book j, j+1, j+2, ... i). When update it uses dp[j-1], since j is already on the next row.

Time Complexity: O(n^2). n = books.length.

Space: O(n).

AC Java:

 class Solution {
public int minHeightShelves(int[][] books, int shelf_width) {
if(books == null || books.length == 0 || shelf_width <= 0){
return 0;
} int n = books.length;
int [] dp = new int[n+1]; for(int i = 1; i<=n; i++){
int w = books[i-1][0];
int h = books[i-1][1];
dp[i] = dp[i-1] + h;
for(int j = i-1; j>0 && w+books[j-1][0]<=shelf_width; j--){
h = Math.max(h, books[j-1][1]);
w += books[j-1][0];
dp[i] = Math.min(dp[i], dp[j-1]+h);
}
} return dp[n];
}
}

LeetCode 1105. Filling Bookcase Shelves的更多相关文章

  1. 【leetcode】1105. Filling Bookcase Shelves

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

  2. leetcode1105 Filling Bookcase Shelves

    思路: dp[i]表示摆放好前i本书所需要的最小代价. 实现: class Solution { public: int minHeightShelves(vector<vector<in ...

  3. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  4. [LeetCode] Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  5. Leetcode: Sudoku Solver

    July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...

  6. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

  7. [LeetCode]题解(python):037-Sudoku Solver

    题目来源 https://leetcode.com/problems/sudoku-solver/ Write a program to solve a Sudoku puzzle by fillin ...

  8. Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  9. Codeforces Round #368 (Div. 2) D. Persistent Bookcase

    Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...

随机推荐

  1. C++ 定义一个指针类型

    #include <iostream>using namespace std; int main(){ int a= 10;  //定义变量a int * p ; //定义个指针P p = ...

  2. Mesh网格简化

    Mesh简化算法: 1. 通过mesh简化,可以将一个多边形的网格A转化成另一个网格B 网格B相比A,有更少的三角形面.边.顶点. 2. 简化的过程是受到一定的约束的.会有一系列自定义的质量标准来控制 ...

  3. golang开发:环境篇(六) Go运行监控Supervisord的使用

    为什么要使用Supervisord 17年第一次写Go项目的时候,用Go开发项目倒没没费多大劲,很快就开发完成了.到了在测试环境部署的时候,由于不知道有 Supervisord 这个软件,着实花了些功 ...

  4. Java字节码扩展

    异常表 代码一: public class Test03 { public void test() { try { InputStream is = new FileInputStream(" ...

  5. node-red inject节点 debug节点 switch节点

    inject节点: https://blog.csdn.net/geek_monkey/article/details/80737818 debug节点: https://blog.csdn.net/ ...

  6. Spring-Cloud之Config配置中心-7

    一.我们前面基本上都是讲解的Spring Cloud Netflix的组件,下面我们会重点说Spring Cloud Config分布式配置中心.为什么需要这个组件来管理配置呢?在分布式应用开发过程中 ...

  7. Linq实现分组后取最大(小)值

    var beatles = (new[] { new { inst = "张三" , age="50" }, new { inst = "张三&quo ...

  8. Windows7平台下gitblit服务器安装

    在日常开发工作中,我们通常使用版本控制软件管理团队的源代码,常用的SVN.Git.与SVN相比,Git有分支的概念,可以从主分支创建开发分支,在开发分支测试没有问题之后,再合并到主分支上去,从而避免了 ...

  9. Java操作JSON数据(2)--Gson操作JSON数据

    Gson是Google公司发布的一个开发源码的Java库,可用于将Java对象转换为JSON字符串,也可用于将JSON字符串转换为对应的Java对象.本介绍下Gson的基本使用方法,包括序列化和反序列 ...

  10. Java调用Http/Https接口(7,end)--WebClient调用Http/Https接口

    WebClient是Spring提供的非阻塞.响应式的Http客户端,提供同步及异步的API,将会代替RestTemplate及AsyncRestTemplate.文中所使用到的软件版本:Java 1 ...