"Leetcode" Russian Doll envelopes

Source: Internet
Author: User

Title Link: https://leetcode.com/problems/russian-doll-envelopes/

Topic:

You had 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 h Eight of the other envelope.

What is the maximum number of envelopes can 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 are 3 ([2,3] = [5,4] = [6,7]).

Ideas:

Similar to the longest growth substring, the difference is that the conditions for determining growth are different, and the array needs to be sorted first.

C[i] represents a size from 0 to I envelopes, the maximum number that can be contained, and the maximum number of envelopes that is recorded to I with a array.

State transition equation: c[i]=max{c[j]+1} envelopes[i][0]>envelopes[j][0]&&envelopes[i][1]> ENVELOPES[J][1] 0<=j<i

Sorting time complexity O (NLOGN), DP time complexity O (n^2), DP when in fact can be further using binary search optimization to O (NLOGN). However, because the following practice can also be too lazy to optimize the AC. = =


Algorithm:

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) {W Hile (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];//to I last set of baby's size int c[] = new int[envelopes.length];//C[i] represents from 0 to I envelopes, Maximum number of possible c[0] = 1;quicksort (envelopes, 0, envelopes.length-1); a[0] = Envelopes[0];int max = 1;for (int i = 1; i < env Elopes.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;}



"Leetcode" Russian Doll envelopes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.