We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f3b689d commit 621834dCopy full SHA for 621834d
longest-increasing-subsequence/njngwn.java
@@ -0,0 +1,23 @@
1
+// time complexity: O(nlogn), n: nums.length (logn because of binary search)
2
+// space complexity: O(n), n: nums.length
3
+class Solution {
4
+ public int lengthOfLIS(int[] nums) {
5
+ ArrayList<Integer> incSeqList = new ArrayList<Integer>(); // dp
6
+ incSeqList.add(nums[0]);
7
+
8
+ for (int num : nums) {
9
+ if (num > incSeqList.get(incSeqList.size()-1)) {
10
+ // add element to incSeqLit
11
+ incSeqList.add(num);
12
+ } else {
13
+ int idx = Collections.binarySearch(incSeqList, num);
14
+ if (idx < 0) { // idx returns -(insertedPos + 1)
15
+ int insertedIdx = -(idx + 1);
16
+ incSeqList.set(insertedIdx, num);
17
+ }
18
19
20
21
+ return incSeqList.size();
22
23
+}
0 commit comments