【leetcode】354. Russian Doll Envelopes
题目描述:
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)
解题思路:
这道题个人觉得没有别的hard难度的题复杂。难点可能在于对数据的操作。为此我新建一个”信封“类,把二维数组的每一行,即每一个“信封”封装成了一个对象,存在数组中。还新建了一个MyCompatator类方便为信封数组进行排序。经过这样的抽象化步骤,这个问题就变成了很简单的有序数组中二次循环找最大值的问题。这就十分容易解决了。
具体代码:
public class Solution {
public static int maxEnvelopes(int[][] envelopes) {
//特殊情况的判断
if(envelopes==null||envelopes.length==0)
return 0;
if(envelopes.length==1)
return 1;
A[] array = new A[envelopes.length];
for(int i=0;i<envelopes.length;i++){
array[i] =new A(envelopes[i][0],envelopes[i][1]);
}
//为所有信封按“大小”排序
MyCompatator m = new MyCompatator();
Arrays.sort(array,m);
int[] result = new int[envelopes.length];
int max =1;
//很简单的循环操作找最大值,不在细讲
for(int i=0;i<envelopes.length;i++){
result[i]=1;
for(int j=0;j<i;j++){
if( (array[j].w<array[i].w) && (array[j].h<array[i].h) ){
result[i] = Math.max(result[i], 1+result[j]);
}
}
if(max<result[i]){
max=result[i];
}
}
return max;
}
}
//比较器,用来为数组排序
class MyCompatator implements Comparator<A>{
@Override
public int compare(A o1, A o2) {
if(o1.w>o2.w){
return 1;
}
else if(o1.w<o2.w){
return -1;
}
else{
if(o1.h>o2.h){
return 1;
}
else if(o1.h<o2.h){
return -1;
}
else
return 0;
}
}
}
//信封类
class A{
int w;
int h;
public A(int w,int h){
this.w=w;
this.h=h;
}
}
【leetcode】354. Russian Doll Envelopes的更多相关文章
- leetcode@ [354] Russian Doll Envelopes (Dynamic Programming)
https://leetcode.com/problems/russian-doll-envelopes/ You have a number of envelopes with widths and ...
- [LeetCode] 354. Russian Doll Envelopes 俄罗斯套娃信封
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- leetCode 354. Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- 354 Russian Doll Envelopes 俄罗斯娃娃信封
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- 354. Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- 第十二周 Leetcode 354. Russian Doll Envelopes(HARD) LIS问题
Leetcode354 暴力的方法是显而易见的 O(n^2)构造一个DAG找最长链即可. 也有办法优化到O(nlogn) 注意 信封的方向是不能转换的. 对第一维从小到大排序,第一维相同第二维从大到小 ...
- 【LeetCode】二分 binary_search(共58题)
[4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find F ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- 直接下载Google Play市场的APK
传送门在这里:http://apps.evozi.com/apk-downloader/ 似乎很方便.很迅速的样子,忍不住在这里记录一下.
- JS 操作 radio input(cc问卷管理)
1.选中特定的单选按钮 function showDetail(content){ $("input[name^='radio']").removeAttr("check ...
- centos 配置NFS服务器
转载:http://boloveyo.blog.163.com/blog/static/203926187201232383956558/ 系统是CentOS5.6,假设NFS Server IP为1 ...
- Asp.Net 之 WebService部署到服务器后出现" The test form is only available for requests from the local machine "
最近由于任务需要开发了一个WebService, 部署到服务器以后,出现上述问题,网上查找到如下解决方案: 问题原因: 从 NET Framework 1.1 起定义了一个名为 HttpPostLoc ...
- The First
好吧,今天是第一次,写博客,虽然有点不明白,博客的作用.以后就写点感想,与每天的收获.既然,在博客园注册,自然也与IT相关了. hello,world!
- Virtualbox - Fatal: Could not read from the boot medium; system halted!
刚装好的虚拟机系统,重新打开Virtualbox时出现个什么提示没认真看,顺势点了下去......结果虚拟机的xp系统打不开了,启动后出现:Fatal: Could not read from the ...
- 【转】所需即所获:像 IDE 一样使用 vim
转自: https://github.com/yangyangwithgnu/use_vim_as_ide 所需即所获:像 IDE 一样使用 vim yangyangwithgnu@yeah.net ...
- asp.net连接oracle无法进行中文字符查询的问题
用 select * from nls_database_parameters where parameter = 'NLS_CHARACTERSET' 查询服务器字符集为 AL32UTF8 数据库连 ...
- Delphi 中的MD5实现方法(转)
在Delphi自带的Indy控件中其实是提供了MD2,MD4,MD5对象的,我们可以直接使用它们来完成MD5的签名算法.而不需要再去找其它的DLL或是Pas了. 在Uses单元中引用 IdHashMe ...
- Centos系统使用代理上网时 yum的代理设置
yum的存在使centos上软件的安装.配置.升级.卸载变得十分的方便,但是当安装centos的机器是通过代理服务器访问外网的话,yum的 使用就变得无从下手了,以下介绍以下怎样为yum配置代理地址及 ...