原题链接在这里: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. grpc Unary模式下客户端创建insecure channel的主要流程

    (原创)C/C/1.25.0-dev grpc-c/8.0.0, 使用的例子是自带的例子GreeterClient grpc Unary模式下客户端创建insecure channel的主要流程 gr ...

  2. DS AVL树详解

    先说说二叉搜索树: 是有序的二叉树,根值>左节点值,右节点值>根值. 如果要查找某个值,二叉搜索树和二分查找一样,每进行一次值比较,就会减少一半的遍历区间. 但是,如果树插入的值一直递增/ ...

  3. android studio下 library打包文件(.aar)和本地引用

    关键点: 利用Gradle发布本地maven库支持android library 打包文件(*.aar) 的本地引用 开发环境: windows7 64位操作系统 android studio0.5. ...

  4. Vue 公众号开发 (菜鸡前段的血泪史)

    首先vue-cli就不说了 接下来要说我们需要注意什么 公众号的每个页面都有一个title 所以我们在开发过程中 需要插件 安装vue-wechat-title 安装vue-js-sdk

  5. aop 打印请求信息

    项目中使用 AOP 打印请求信息,打印响应信息.package com.example.aspect; import com.alibaba.fastjson.JSON;import com.goog ...

  6. Java数据结构-ArrayList最细致的解析笔记

    ArrayList是一个类,这个类有一个数组参数elementData,ArrayList集合中的元素正是保存在这个数组中,它继承了数组查询的高性能,参考第3篇.ArrayList还封装了很多方法,便 ...

  7. Typora基础

    Typora下载网址https://typora.io 一级标题 :# 空格 编写内容 二级标题 2*# 空格 内容 typora快捷键 ctrl+1 =一级标题 有序内容 1.+tab (Q旁边的t ...

  8. activiti用户手册

    http://www.mossle.com/docs/activiti/index.html

  9. Function.prototype.apply.call 理解分析

    首先需要了解apply,call的基本用法,其目的是改变调用方法中的this指向,将其指向为传入的对象,改变this的指向,两种方法接收参数的方式不同. 代码:console.log var cons ...

  10. string 转stream和stream转string

    string test = “Testing 1-2-3″; // convert string to stream MemoryStream stream = new MemoryStream(); ...