From the time I spent watching, I was in a tangle sort of long. Pay attention to the comparator of the writing, later will be good ~
The algorithm is to sort the size of the envolope, then for each envelope after sorting, it can be loaded into the maximum number of small envelopes, is the length and width of the envelope is smaller than it in a maximum of +1. And 368 of what was done before. Largest divisible subset idea is the same, even more simple.
Dynamic regulation.
1 Static classMysortImplementscomparator<int[]> {2 Public intCompareint[] O1,int[] O2) {3 for(inti = 0; i < o1.length; i++) {4 if(O1[i]! =O2[i]) {5 returnO1[i]-O2[i];6 }7 }8 return0;9 }Ten } One A Public intMaxenvelopes (int[] envelopes) { - if(Envelopes.length = = 0 | | envelopes[0].length = = 0) { - return0; the } -Mysort Mysort =NewMysort (); - Arrays.sort (envelopes, mysort); - intLen =envelopes.length; + int[] res =New int[Len]; - intmax = 1; + for(inti = 0; i < Len; i++) { ARes[i] = 1; at } - for(inti = 1; i < envelopes.length; i++) { - for(intj = i-1; J >= 0; j--) { - if(Envelopes[i][0] > Envelopes[j][0] && envelopes[i][1] > envelopes[j][1]) { -Res[i] = Math.max (Res[j] + 1, Res[i]); -Max =Math.max (Max, res[i]); in } - } to } + returnMax; -}
354. Russian Doll Envelopes