File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
longest-increasing-subsequence Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * <a href="https://leetcode.com/problems/longest-increasing-subsequence/">week06-4.longest-increasing-subsequence</a>
3+ * <li>Description: return the length of the longest strictly increasing subsequence</li>
4+ * <li>Topics: Array, Binary Search, Dynamic Programming </li>
5+ * <li>Time Complexity: O(NLogN), Runtime 6ms </li>
6+ * <li>Space Complexity: O(N), Memory 44.3MB </li>
7+ */
8+ class Solution {
9+ public int lengthOfLIS (int [] nums ) {
10+ List <Integer > dp = new ArrayList <>();
11+
12+ for (int num : nums ){
13+ int idx = Collections .binarySearch (dp , num );
14+
15+ if (idx < 0 ) {
16+ idx = -idx -1 ;
17+ }
18+
19+ if (idx == dp .size ()) {
20+ dp .add (num );
21+ } else {
22+ dp .set (idx , num );
23+ }
24+ }
25+
26+ return dp .size ();
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments