博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 354. Russian Doll Envelopes
阅读量:5738 次
发布时间:2019-06-18

本文共 1663 字,大约阅读时间需要 5 分钟。

Problem

You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.

What is the maximum number of envelopes can you Russian doll? (put one inside other)

Note:

Rotation is not allowed.

Example:

Input: [[5,4],[6,4],[6,7],[2,3]]

Output: 3
Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).

Solution

class Solution {    public int maxEnvelopes(int[][] envelopes) {        if (envelopes == null || envelopes.length == 0 ||           envelopes[0] == null || envelopes[0].length != 2) return 0;                Arrays.sort(envelopes, new Comparator
(){ public int compare(int[] a, int[] b) { if (a[0] != b[0]) return a[0]-b[0]; else return b[1]-a[1]; } }); int[] dp = new int[envelopes.length]; int len = 0; //find the position to be inserted //if the height is smaller than current high, just update smaller index //if the height is larger than current high: // it will be the length 'len' (and happen to be the // highest index+1 ==> next index), so we insert it into // the next index in dp[] and update effective length 'len' for (int[] envelope: envelopes) { int index = Arrays.binarySearch(dp, 0, len, envelope[1]); if (index < 0) index = -index-1; dp[index] = envelope[1]; if (index == len) len++; } return len; }}

转载地址:http://bafzx.baihongyu.com/

你可能感兴趣的文章
NotePad++ - 安装和配置C/C++开发插件 | NotePad++ - Install and Configure plugins for develop C/C++...
查看>>
安卓之页面跳转与传值和按钮事件
查看>>
PHP if...else...elseif 语句
查看>>
黄聪:wordpress3.4修复WP No Category Base插件无法去掉category的Bug
查看>>
HDU 2824 The Euler function --------欧拉模板
查看>>
django实例(4):一对多外键关联
查看>>
SVN: 在Ecplise管理SVN资源库
查看>>
浅谈SpringMVC执行过程
查看>>
关于IIS的IUSER和IWAM帐户
查看>>
加载节点
查看>>
编程基础知识学习———C语言中可变参数的用法
查看>>
【Java实现随机验证码功能实例】
查看>>
SELECT INTO 和 INSERT INTO SELECT
查看>>
C#.ToString()格式大全
查看>>
关于boostrap的thead固定tbody滚动
查看>>
Mac: mac git 的安装 及实现自动补全
查看>>
读取xml格式的字符串和上下文中的xml数据
查看>>
深入比较选择 Angular 还是 React
查看>>
web之javascript BOM语句
查看>>
企业级应用和互联网应用的异同
查看>>