Skip to content

Commit da6f496

Browse files
update 300
1 parent 5390d9c commit da6f496

File tree

1 file changed

+9
-12
lines changed
  • src/main/java/com/fishercoder/solutions/firstthousand

1 file changed

+9
-12
lines changed

Diff for: src/main/java/com/fishercoder/solutions/firstthousand/_300.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
public class _300 {
66

7-
/**
8-
* credit: https://leetcode.com/problems/longest-increasing-subsequence/solution/
9-
*/
107
public static class Solution1 {
118
/**
129
* brute force:
@@ -71,7 +68,7 @@ private int recusionWithMemo(int[] nums, int prevIndex, int currIndex, int[][] m
7168

7269
public static class Solution3 {
7370
/**
74-
* DP solution
71+
* DP solution, credit: https://leetcode.com/problems/longest-increasing-subsequence/editorial/
7572
* Time: O(n^2)
7673
* Space: O(n)
7774
*/
@@ -80,19 +77,19 @@ public int lengthOfLIS(int[] nums) {
8077
return 0;
8178
}
8279
int[] dp = new int[nums.length];
83-
dp[0] = 1;
84-
int result = 1;
80+
Arrays.fill(dp, 1);
8581
for (int i = 1; i < nums.length; i++) {
86-
int maxVal = 0;
8782
for (int j = 0; j < i; j++) {
8883
if (nums[i] > nums[j]) {
89-
maxVal = Math.max(maxVal, dp[j]);
84+
dp[i] = Math.max(dp[i], dp[j] + 1);
9085
}
9186
}
92-
dp[i] = maxVal + 1;
93-
result = Math.max(result, dp[i]);
9487
}
95-
return result;
88+
int ans = 1;
89+
for (int val : dp) {
90+
ans = Math.max(ans, val);
91+
}
92+
return ans;
9693
}
9794
}
9895

@@ -102,7 +99,7 @@ public static class Solution4 {
10299
* Time: O(nlogn)
103100
* Space: O(n)
104101
* <p>
105-
* The reason we can use binary search here is because all numbers we put into dp array are sorted.
102+
* The reason we can use binary search here is all numbers we put into dp array are sorted.
106103
* Arrays.binarySearch() method returns index of the search key,
107104
* if it is contained in the array, else it returns (-(insertion point) - 1).
108105
* The insertion point is the point at which the key would be inserted into the array:

0 commit comments

Comments
 (0)