Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite07e906

Browse files
length of increasing subsequence
1 parent99f729a commite07e906

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

‎MEDIUM/src/medium/LengthIncreasingSubsequence.java

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,53 @@ Your algorithm should run in O(n2) complexity.
1919
Credits:
2020
Special thanks to @pbrother for adding this problem and creating all test cases.*/
2121
publicclassLengthIncreasingSubsequence {
22+
publicstaticvoidmain(String...strings){
23+
LengthIncreasingSubsequencetest =newLengthIncreasingSubsequence();
24+
int[]nums =newint[]{10,9,2,5,3,7,101,18};
25+
// int[] nums = new int[]{10,9,2,5,3,4};
26+
// int[] nums = new int[]{1,3,6,7,9,4,10,5,6};
27+
// int[] nums = new int[]{18,55,66,2,3,54};
28+
System.out.println(test.lengthOfLIS(nums));
29+
}
30+
31+
/**This is the closest I got, passed all normal cases, made it to 22/23 test cases, but got TLE, as I expected,
32+
* since this algorithm runs in O(n^3) time.
33+
* My idea: compute a 2D tabular: n*n.
34+
*
35+
* Then miracle happens, I was about to turn to Discuss, before that, I clicked the Show Tags button, it says:
36+
* Binary Search, this hints me to take a second look at my code, then I added this line:
37+
* if(nums.length-i < max) return max;
38+
* then it got AC'ed!
39+
* This is the power of pruning! So Cool!
40+
*
41+
* Also, another key was that let j start from i, not 0, we don't need to initialize the bottom left part to 1, just
42+
* leave them as zero, that's fine, since we don't need to touch that part at all!
43+
* This also saves time! Cool!
44+
* */
2245
publicintlengthOfLIS(int[]nums) {
23-
if(nums ==null ||nums.length ==0)return0;
24-
46+
47+
if (nums ==null ||nums.length ==0)
48+
return0;
49+
2550
int[][]dp =newint[nums.length][nums.length];
2651
intmax =0;
27-
for(inti =0;i <nums.length;i++){
28-
intcurrentMaxForThisRow =nums[i];
29-
for(intj =0;j <nums.length;j++){
30-
if(j <=i)dp[i][j] =1;
31-
else {
32-
if(nums[j] >nums[i]) {
33-
if(nums[j] >currentMaxForThisRow) {
34-
dp[i][j] =dp[i][j-1]+1;
35-
currentMaxForThisRow =nums[j];
36-
}else {
37-
dp[i][j] =dp[i][j-1];
38-
//in this case, we need to figure out when should we update currentMaxForThisRow?
39-
for(intk =j-1;k >=0;k--){
40-
if(nums[k] <nums[j]){
41-
if(dp[i][k]+1 ==dp[i][j] &&nums[j-1] >nums[j]){
42-
currentMaxForThisRow =nums[j];
43-
}
44-
break;
45-
}
46-
}
52+
for (inti =0;i <nums.length;i++) {
53+
if(nums.length-i <max)returnmax;
54+
for (intj =i;j <nums.length;j++) {
55+
if (nums[j] >nums[i]) {
56+
for (intk =j -1;k >=i;k--) {
57+
if (nums[k] <nums[j]) {
58+
dp[i][j] =Math.max(dp[i][k] +1,dp[i][j]);
4759
}
4860
}
49-
elsedp[i][j] =dp[i][j-1];
50-
}
61+
}else
62+
dp[i][j] =1;
5163
max =Math.max(max,dp[i][j]);
5264
}
5365
}
5466
CommonUtils.printMatrix(dp);
5567
returnmax;
68+
5669
}
5770

58-
publicstaticvoidmain(String...strings){
59-
LengthIncreasingSubsequencetest =newLengthIncreasingSubsequence();
60-
// int[] nums = new int[]{10, 9, 2, 5, 3, 7, 101, 18};
61-
// int[] nums = new int[]{10,9,2,5,3,4};
62-
int[]nums =newint[]{1,3,6,7,9,4,10,5,6};
63-
System.out.println(test.lengthOfLIS(nums));
64-
}
6571
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp