Java实现 LeetCode 354 俄罗斯套娃信封问题
354. 俄罗斯套娃信封问题
给定一些标记了宽度和高度的信封,宽度和高度以整数对形式 (w, h) 出现。当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。
请计算最多能有多少个信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。
说明:
不允许旋转信封。
示例:
输入: envelopes = [[5,4],[6,4],[6,7],[2,3]]
输出: 3
解释: 最多信封的个数为 3, 组合为: [2,3] => [5,4] => [6,7]。
class Solution {
public int maxEnvelopes(int[][] envelopes) {
int maxL = 0;
int[] dp = new int[envelopes.length];
Arrays.sort(envelopes, (a, b) -> (a[0] == b[0] ? b[1]-a[1] : a[0]-b[0]));
for(int[] env : envelopes) {
int lo = 0, hi = maxL;
while(lo < hi) {
int mid = lo+(hi-lo)/2;
if(dp[mid] < env[1])
lo = mid+1;
else
hi = mid;
}
dp[lo] = env[1];
if(lo == maxL)
maxL++;
}
return maxL;
}
}
Java实现 LeetCode 354 俄罗斯套娃信封问题的更多相关文章
- leetcode 354. 俄罗斯套娃信封问题(二维排序有关)
题目描述 给定一些标记了宽度和高度的信封,宽度和高度以整数对形式 (w, h) 出现.当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样. 请计算最多能有 ...
- Leetcode 354.俄罗斯套娃信封问题
俄罗斯套娃信封问题 给定一些标记了宽度和高度的信封,宽度和高度以整数对形式 (w, h) 出现.当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样. 请计 ...
- 1、线性DP 354. 俄罗斯套娃信封问题
354. 俄罗斯套娃信封问题 https://leetcode-cn.com/problems/russian-doll-envelopes/ 算法分析 首先我们从两种情况来讨论这个问题: w无重复值 ...
- [Swift]LeetCode354. 俄罗斯套娃信封问题 | Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- Linux Kernel Makefiles Kbuild en
来自Linux kernel docs,顺便整理了一下排版 Linux Kernel Makefiles This document describes the Linux kernel Makefi ...
- JS字符串截取 “指定字符” 前面和后面的内容!
JS字符串截取 “指定字符” 前面和后面的内容! var string= "07/12" var before = string.split('/')[0] var after = ...
- CODING 敏捷实战系列课第四讲:从头搭建持续集成 DevOps 流水线
<从头搭建持续集成 DevOps 流水线>由资深敏捷教练.极限编程学院高级讲师.CODING 特邀敏捷顾问李小波老师主讲,将基于 CODING 展示如何编写 Jenkinsfile 搭建 ...
- An invalid domain [.test.com] was specified for this cookie 原因分析
java.lang.IllegalArgumentException: An invalid domain [.test.com] was specified for this cookie 以上博客 ...
- POI 导入excel数据自动封装成model对象--代码分析
上完代码后,对代码进行主要的分析: 1.主要使用反射api将数数据注入javabean对象 2.代码中的日志信息级别为debug级别 3.获取ExcelImport对象后需要调用init()方法初始化 ...
- RocketMQ Windows 搭建
一.rocketMQ 下载 官网:http://rocketmq.apache.org/ 本人使用是v4.3.0版本,百度网盘下载地址链接:https://pan.baidu.com/s/1qWewB ...
- js面试题(转)
https://segmentfault.com/a/1190000015288700 1 介绍JavaScript的基本数据类型 Number.String .Boolean .Null.Undef ...
- (mysql)数据库笔记
一.数据库的特点: a.实现数据共享 b.采用特定的数据类型. c.具有较高的数据独立性 d.具有统一的数据控制功能. 二.mysql的优势: a.速度:运行速度快 b.价格:mysql对多数个人来 ...
- VMware 安装 CentOS 7
下载并安装 VMware 访问 VMware 官方网站下载 VMware 安装包程序.博主使用的是 12.5.5 版本,下载完之后点击安装包程序进入 VMware 的安装向导,然后点击"下一 ...
- redis的参数解释
include /path/to/local.conf 当有公用配置时,可以采用独立出公共配置文件然后引入的方式达到公共配置unixsocket /tmp/redis.sock 通过socket文件进 ...