【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 ...
随机推荐
- python的一些总结4
这篇继续水 但是在水的的基础上 让搭建能正常使用flask 搭建一个站 上篇讲到在 模板view中 输入{{xx }} 可以打印 后台传的值. 这篇讲一下 循环控制 条件控制等 修改后台代码: @ap ...
- ubuntu 15.04开放mysql远程连接
首先查看端口是否打开 netstat -an|grep 3306 此图为开启3306端口的截图,之前显示为. . . 127.0.0.1:3306 . . . 打开mysql配置文件vi /etc/m ...
- hihocoder #1223 : 不等式 水题
#1223 : 不等式 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/1223 ...
- Codeforces Gym 100338B Geometry Problem 计算几何
Problem B. Geometry ProblemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...
- 在Linux里设置环境变量的方法(export PATH)
一般来说,配置交叉编译工具链的时候需要指定编译工具的路径,此时就需要设置环境变量.例如我的mips-linux-gcc编译器在“/opt/au1200_rm/build_tools/bin”目录下,b ...
- 设置EXCEL2010打开多个独立窗口
最近发现一个奇怪的问题,发现office中的word和ppt在我使用笔记本分屏幕(双屏)的时候都可以将2份文档分别在2个窗口打开,但是在使用excel的时候却发现不行,最后研究发现原因 ...
- Python学习 之 包和模块
1.rpm -ql python #查看python在计算机中安装了哪些文件 2.模块是一个可以导入的Python脚本文件 包是一堆按目录组织的模块和子包,目录下的__init__.py文件存放了包的 ...
- 使用ReactiveCocoa开发RSS阅读器
目前已经完成的功能有对RSS的解析和Atom解析,RSS内容本地数据库存储和读取,抓取中状态进度展示,标记阅读状态,标记全部已读等.这些功能里我对一些异步操作产生的数据采用了ReactiveCocoa ...
- IOS横竖屏控制与事件处理
公司App里面有个需求,即所有界面都是竖屏,且不允许横屏切换,唯独有一个图表界面允许横屏.那么,根据此需求处理如下: 首先,确保App本身应该允许转屏切换: 再次,我的App里面都是走UINaviga ...
- 《Entity Framework 6 Recipes》中文翻译——第十章EntityFramework存储过程处理(八)
将插入.更新和删除操作映射到存储过程 问题 您想在存储过程中映射插入.更新和删除操作. 解决方案 下图所示的运动员Athlete实体模型.底层数据库有一张运动员Athlete表.您想使用存储过程进行实 ...