File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
longest-increasing-subsequence Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * [Problem]: [300] Longest Increasing Subsequence
3+ * (https://leetcode.com/problems/longest-increasing-subsequence/)
4+ */
5+
6+ function lengthOfLIS ( nums : number [ ] ) : number {
7+ //시간복잡도 O(n^2)
8+ //공간복잡도 O(n)
9+ function dpFunc ( nums : number [ ] ) : number {
10+ const dp = new Array ( nums . length + 1 ) . fill ( 1 ) ;
11+
12+ for ( let i = 0 ; i < nums . length ; i ++ ) {
13+ for ( let j = 0 ; j < i ; j ++ ) {
14+ if ( nums [ j ] < nums [ i ] ) {
15+ dp [ i ] = Math . max ( dp [ i ] , dp [ j ] + 1 ) ;
16+ }
17+ }
18+ }
19+ return Math . max ( ...dp ) ;
20+ }
21+
22+ //시간복잡도 O(nlogn)
23+ //공간복잡도 O(n)
24+ function binearySearchFunc ( nums : number [ ] ) : number {
25+ const results : number [ ] = [ ] ;
26+
27+ for ( const num of nums ) {
28+ let left = 0 ;
29+ let right = results . length ;
30+
31+ while ( left < right ) {
32+ const mid = Math . floor ( ( left + right ) / 2 ) ;
33+ if ( results [ mid ] < num ) {
34+ left = mid + 1 ;
35+ } else {
36+ right = mid ;
37+ }
38+ }
39+
40+ if ( left < results . length ) {
41+ results [ left ] = num ;
42+ } else {
43+ results . push ( num ) ;
44+ }
45+ }
46+
47+ return results . length ;
48+ }
49+
50+ return binearySearchFunc ( nums ) ;
51+ }
You can’t perform that action at this time.
0 commit comments