当前位置: 首页 > news >正文

LeetCode -- 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)


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


给定一个二维数组,对于[i,j]和[a,b]若满足i<a且j<b,则成为Russian doll。统计满足Russian doll的个数。


思路:


1. 使用类建模,对整个数组排序


2. 两层循环,排序后从第一个往后遍历(i:0->n);内层循环从后向前查找(j:i->0):
若a[i,0]>a[j,0] && a[0,i] > a[0,j]则取 Max(a[i]当前的结果,a[j]的结果+1)
因此可使用DP,用result[i]来存a[i]的Russian doll的数量,初始化为1。


实现代码:


public int MaxEnvelopes(int[,] envelopes)
    {
        if(envelopes == null || envelopes.Length == 0){
    		return 0;
    	}
    	
    	// 0. model the problem
    	var arr = new List<Doll>();
    	var rowCount = envelopes.Length / 2;
    
    	for (var i = 0;i < rowCount; i++){
    		arr.Add(new Doll(envelopes[i,0], envelopes[i,1]));
    	}
    	
    	// 1. sort the arrays 
    	arr = arr.OrderBy(x=>x).ToList();
    	
    	var result = new int[rowCount];
    	for (var i = 0;i < rowCount; i++){
    		// fetch the item from first to last
    		result[i] = 1;
    		for (var j = i-1;j >=0 ; j--){
    			if(arr[i].Width > arr[j].Width && arr[i].Height > arr[j].Height){
    				result[i] = Math.Max(result[i], result[j]+1);
    			}
    		}
    	}
    	//Console.WriteLine(result);
    	var max = result.Max();
    	
    	return max;
    	
    }
    
    public class Doll : IComparable
    {
    	public int Width;
    	public int Height;
    	public Doll(int width, int height){
    		this.Width = width;
    		this.Height = height;
    	}
    	
    	public int CompareTo(object obj){
    		var to = (Doll)obj;
    		if(Width!=to.Width){
    			return Width - to.Width;
    		}else{
    			return Height - to.Height;
    		}
    	}
    }


相关文章:

  • 查看sql server数据库的空间大小...
  • LeetCode -- Longest Palindrome
  • 有朋远方来-致力于java培训的张孝祥
  • LeetCode -- Range Sum Query 2D - Immutable
  • 从Oracle到DB2,问题集(一)
  • LeetCode -- Dungeon Game
  • 从Oracle到DB2,问题集(二)
  • LeetCode -- Contains Duplicate II
  • Sql union的反义词Minus
  • LeetCode -- Path Sum III
  • LeetCode -- Minimum Number of Arrows to Burst Balloons
  • 反醒反醒
  • LeetCode -- Arranging Coins
  • Bing在中国不会成功
  • LeetCode -- First Unique Character in a String
  • 2017前端实习生面试总结
  •  D - 粉碎叛乱F - 其他起义
  • Docker 笔记(2):Dockerfile
  • Java的Interrupt与线程中断
  • Java读取Properties文件的六种方法
  • js继承的实现方法
  • log4j2输出到kafka
  • node.js
  • OpenStack安装流程(juno版)- 添加网络服务(neutron)- controller节点
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • tweak 支持第三方库
  • 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
  • 关于字符编码你应该知道的事情
  • 记录一下第一次使用npm
  • 跨域
  • 前言-如何学习区块链
  • 如何合理的规划jvm性能调优
  • 入门级的git使用指北
  • 三栏布局总结
  • ​2021半年盘点,不想你错过的重磅新书
  • # 数论-逆元
  • $L^p$ 调和函数恒为零
  • $分析了六十多年间100万字的政府工作报告,我看到了这样的变迁
  • (13):Silverlight 2 数据与通信之WebRequest
  • (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
  • (C语言)输入自定义个数的整数,打印出最大值和最小值
  • (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
  • (solr系列:一)使用tomcat部署solr服务
  • (带教程)商业版SEO关键词按天计费系统:关键词排名优化、代理服务、手机自适应及搭建教程
  • (附源码)springboot“微印象”在线打印预约系统 毕业设计 061642
  • (实战篇)如何缓存数据
  • .gitignore文件设置了忽略但不生效
  • .locked1、locked勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET Micro Framework初体验
  • .php文件都打不开,打不开php文件怎么办
  • [] 与 [[]], -gt 与 > 的比较
  • [100天算法】-不同路径 III(day 73)
  • [AX]AX2012开发新特性-禁止表或者表字段
  • [bzoj1038][ZJOI2008]瞭望塔
  • [CISCN2019 华东南赛区]Web11