原题链接在这里: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语言开发中常见报错的解决方案

    C语言开发中常见报错的解决方案 整理来源于网络,侵权请通知删除.*禁止转载 ---- fatal error C1003: error count exceeds number; stopping c ...

  2. Android Studio代码错误提示无效(not available in Power Save mode)

    针对一位博友提的问题,我这边写出来,估计还是很多人会碰到这个问题,但是不知道如何解决的. 就是在设置了代码自动提示功能后,发现不生效的,如何设置代码自动提示请戳这:Android Studio如何设置 ...

  3. 部署.Net Core APi+Vue 到 linux centos 服务器(一)

    部署.Net Core APi+Vue 到 linux centos 服务器(一) 前言:项目采用的是 .net core 作为接口,vue作为前端. 此时需要把整个项目架设到linux centos ...

  4. Codeforces Round #426 (Div. 1) (ABCDE)

    1. 833A The Meaningless Game 大意: 初始分数为$1$, 每轮选一个$k$, 赢的人乘$k^2$, 输的人乘$k$, 给定最终分数, 求判断是否成立. 判断一下$a\cdo ...

  5. .net core webapi通过中间件获取请求和响应内容

    本文主要根据中间件来实现对.net core webapi中产生的请求和响应数据进行获取并存入日志文件中: 这里不详细介绍日志文件的使用.你可以自己接入NLog,log4net,Exceptionle ...

  6. python入门基础思维导图

  7. kafka汇总

    Kafka 1. kafka概念 kafka是一个高吞吐亮的.分布式.基于发布/订阅(也就是一对多)的消息系统,最初由Linkedln公司开发的,使用Scala语言编写的,目前是Apache的开源项目 ...

  8. 【GLG Toolkit 行业案例】Baltic Information Systems, Ltd.公司选择使用GLG Toolkit

    BIS (Baltic Information Systems, Ltd.)是一家专门为核电厂开发安全和过程监控系统的软件公司. BIS的新产品 BISMARC 是一个通用过程控制和监控的SCADA系 ...

  9. OC中并发编程的相关API和面临的挑战

    OC中并发编程的相关API和面临的挑战(1) 小引 http://www.objc.io/站点主要以杂志的形式,深入挖掘在OC中的最佳编程实践和高级技术,每个月探讨一个主题,每个主题都会有几篇相关的文 ...

  10. python+java全栈工程师 转 向前端的路

    python的优点 简单 简单 简单 我目前在公司用python 1. 增加odoo的各种业务,成本核算.自动跑单.自动备份数据库之类的 ----odoo是国外大佬做的一个开源erp 用的python ...