【Leetcode】Russian Doll Envelopes

题目链接:https://leetcode.com/problems/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]).

思路:

类似于最长增长子串,不同的在于判断增长的条件不同,以及需要先将数组排序。

c[i]表示从0到i个envelopes,最大可以包含的数目,用a数组记录到i的时候,最大包含数目的最大的envelopes的size。

状态转移方程:c[i]=max{c[j]+1}  envelopes[i][0]>envelopes[j][0]&&envelopes[i][1]>envelopes[j][1]
   0<=j<i

排序时间复杂度O(nlogn),dp时间复杂度O(n^2),dp的时候其实可以进一步用二分搜索优化成O(nlogn)的。不过因为下面的做法也能ac就懒得优化 了。= =

算法:

	private void quickSort(int[][] array, int beg, int end) {
		if (beg >= end || array == null)
			return;
		int p = partition(array, beg, end);
		quickSort(array, beg, p - 1);
		quickSort(array, p + 1, end);
	}

	private int partition(int[][] array, int beg, int end) {
		int first = array[beg][0];
		int i = beg, j = end;
		while (i < j) {
			while (array[i][0] <= first && i < end) {
				i++;
			}
			while (array[j][0] > first && j >= beg) {
				j--;
			}
			if (i < j) {
				int tmp[] = array[i];
				array[i] = array[j];
				array[j] = tmp;
			}
		}
		if (j != beg) {
			int tmp[] = array[j];
			array[j] = array[beg];
			array[beg] = tmp;
		}
		return j;
	}

	public int maxEnvelopes(int[][] envelopes) {
		if (envelopes.length == 0) {
			return 0;
		}

		int a[][] = new int[envelopes.length][2];// 到i最后一个套娃的大小
		int c[] = new int[envelopes.length];// c[i]表示从0到i个envelopes,最大可以包含的数目
		c[0] = 1;

		quickSort(envelopes, 0, envelopes.length - 1);
		a[0] = envelopes[0];
		int max = 1;

		for (int i = 1; i < envelopes.length; i++) {
			int m = 1;
			boolean flag = true;
			for (int j = i - 1; j >= 0; j--) {
				if (envelopes[i][0] > a[j][0] && envelopes[i][1] > a[j][1]) {
					flag = false;
					if (m <= c[j] + 1) {
						m = c[j] + 1;
						a[i] = envelopes[i];
					}
				}
			}
			if (flag) {
				a[i] = envelopes[i];
			}
			c[i] = m;
			max = Math.max(max, m);
		}
		return max;
	}
时间: 2024-11-05 06:24:39

【Leetcode】Russian Doll Envelopes的相关文章

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

[动态规划] leetcode 354 Russian Doll Envelopes

problem:https://leetcode.com/problems/russian-doll-envelopes/ 最长连续子序列类型问题.先排序,dp[i]记录使用第i个套娃的最大数量. bool cmp(const vector<int>& x, const vector<int>& y) { return x[0] == y[0] ? x[1] < y[1] : x[0] < y[0]; } class Solution { public:

第十二周 Leetcode 354. Russian Doll Envelopes(HARD) LIS问题

Leetcode354 暴力的方法是显而易见的 O(n^2)构造一个DAG找最长链即可. 也有办法优化到O(nlogn) 注意 信封的方向是不能转换的. 对第一维从小到大排序,第一维相同第二维从大到小排序. 维护一个符合题意的队列,当队列中的第二维均比当前信封的第二维小时,必然可以增加到队尾. 如果不然,可以让当前信封作为"替补",它可以在恰当的时候代替恰好比它大的信封. 当替补们足够替换所有已有信封时,就可以增加新的信封了. 比较抽象,不过这个技巧很有趣. 看代码吧,很清晰. cla

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

【LeetCode】二分 binary_search(共58题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [4]Median of Two Sorted Arrays [29]Divide Two Integers [33]Search in Rotated Sorted Array [34]Find First and Last Position of Element in Sorted Array [35]Search Insert Position [50]Pow(

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<