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 ...
随机推荐
- STM32 标准库V3.5启动文件startup_stm32f10xxx.s分析
layout: post tags: [STM32] comments: true 文章目录 layout: post tags: [STM32] comments: true 前言 分析startu ...
- Quartus II 与modelsim连接不上的问题
在Quartus II 中tools>options>General>EDA Tool Options 设置modelsim 路径 说明:不管是Quartus II 与modelsi ...
- C#枚举高级战术
文章开头先给大家出一道面试题: 在设计某小型项目的数据库(假设用的是 MySQL)时,如果给用户表(User)添加一个字段(Roles)用来存储用户的角色,你会给这个字段设置什么类型?提示:要考虑到角 ...
- JUC(3)---CountDownLatch、CyclicBarrier和AQS
CountDownLatch可以让一个线程等待其他线程完成了各自的工作之后再执行.比如说一个切菜,一个人切肉,都准备完毕之后才能炒肉. 构造方法: public CountDownLatch(int ...
- MyEclipse安装后的配置
一.Window-->Preferences-->General --> Workspace --> UTF-8 作用:从此以后,你创建的任何项目编码都是UTF-8,一次解决所 ...
- 51Nod1127 最小包含字符串
51Nod1127 #include <iostream> #include <string> using namespace std; const int inf = 0x3 ...
- MySQL 5.7 基于GTID创建运行主库的从库-xtrabackup+mysqldump
一.GTID innobackupex备份实现主从同步 1)master备份 innobackupex --defaults-file=/etc/my.cnf --user=root --passwo ...
- Jquery学习2---倒计时
以下代码是mvc4.0代码,其功能是让页面上的数字3,变2,变1 然后跳转页面 @{ ViewBag.Title = "LoginOut"; } <html> < ...
- mysql 赋权语句
grant all privileges on phplampDB.* to phplamp@localhost identified by '1234';
- C#正则表达式基础
namespace ---> System.Text.RegularExpressions. static void Main(string[] args) { // if (IsInputMa ...