LeetCode 1105. Filling Bookcase Shelves
原题链接在这里: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 <= 10001 <= books[i][0] <= shelf_width <= 10001 <= 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的更多相关文章
- 【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 ...
- leetcode1105 Filling Bookcase Shelves
思路: dp[i]表示摆放好前i本书所需要的最小代价. 实现: class Solution { public: int minHeightShelves(vector<vector<in ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- Leetcode: Sudoku Solver
July 19, 2015 Problem statement: Write a program to solve a Sudoku puzzle by filling the empty cells ...
- 【Codeforces-707D】Persistent Bookcase DFS + 线段树
D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...
- [LeetCode]题解(python):037-Sudoku Solver
题目来源 https://leetcode.com/problems/sudoku-solver/ Write a program to solve a Sudoku puzzle by fillin ...
- 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 ...
- Codeforces Round #368 (Div. 2) D. Persistent Bookcase
Persistent Bookcase Problem Description: Recently in school Alina has learned what are the persisten ...
随机推荐
- PB 点击标题行排序和双击打开编辑页面共存不冲突的方法
根据doubleclicked() 事件的参数 row 进行判断 大于0才进入编辑页面(不能用getrow()事件获取行id,双击标题行获取的是1) if row>0 then event ue ...
- MMKV 多进程K-V组件 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- DevExpress中GridColumnCollection实现父子表数据绑定
绑定数据: 父表: DataTable _parent = _dvFlt.ToTable().Copy(); 子表: DataTable _child = _dvLog.ToTable().Copy( ...
- 勒索病毒,华为/H3C三层交换机/路由器用ACL访问控制实现端口禁用
前不久勒索病毒横行,很多人都纷纷中招,从公司到个人,损失相当惨重.有些公司在互联网入口上做了控制,但是这样并非完全,万一有人把中了毒的U盘插入网内设备上呢?那我们的内网中很有可能集体中招(打过相关补丁 ...
- 练习bloc , 动画
有点意思, import 'package:flutter/material.dart'; import 'package:rxdart/rxdart.dart'; main()=>runApp ...
- 【转载】WPS通过设置密码的方式对Excel文件加密
有时候Excel文件中可能包含一些敏感数据,此时希望对Excel文件进行加入密码的形式进行加密保护,在WPS软件和Office Excel软件中都支持对Excel文件进行密码保护,设置了密码保护的Ex ...
- python day 16: FTP脚本作业用例图,类图,活动图与代码重写
目录 python day 16 1. FTP脚本的用例图 python day 16 2019/10/22 - 2019/10/26 学习资料来自老男孩教育 1. FTP脚本的用例图 老师的讲解视频 ...
- JAVA基础之设置随机成语验证码
package com.oracle; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import ja ...
- 使用 audioqueue 播放PCM数据
// // MainViewController.h // RawAudioDataPlayer // // Created by SamYou on 12-8-18. // Copyright (c ...
- JS基础 浏览器弹出的三种提示框(提示信息框、确认框、输入文本框)
浏览器的三种提示框 alert() //提示信息框 confirm() //提示确认框 prompt() //提示输入文本框 1.alert( ) 提示信息框 <script> alert ...