File tree Expand file tree Collapse file tree 3 files changed +116
-0
lines changed
container-with-most-water
longest-increasing-subsequence Expand file tree Collapse file tree 3 files changed +116
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } height
3+ * @return {number }
4+ *
5+ * complexity
6+ * time: O(n)
7+ * space: O(1)
8+ */
9+
10+ var maxArea = function ( height ) {
11+ let answer = 0 ;
12+ let l = 0 , r = height . length - 1 ;
13+
14+ while ( l < r ) {
15+ const area = ( r - l ) * Math . min ( height [ l ] , height [ r ] ) ;
16+
17+ answer = area > answer ? area : answer ;
18+
19+ if ( height [ l ] < height [ r ] ) {
20+ l += 1 ;
21+ } else {
22+ r -= 1 ;
23+ }
24+ }
25+
26+ return answer ;
27+ } ;
28+
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @return {number }
4+ *
5+ * complexity
6+ * time: O(nlogn) : lower_bound : O(logn), for문 : O(n)
7+ * space: O(n)
8+ */
9+ var lengthOfLIS = function ( nums ) {
10+ const lower_bound = ( arr , value ) => {
11+ let ret = - 1 ;
12+ let l = 0 ;
13+ let r = arr . length - 1 ;
14+
15+ while ( l <= r ) {
16+ const mid = Math . floor ( ( l + r ) / 2 ) ;
17+ if ( arr [ mid ] >= value ) {
18+ ret = mid ;
19+ r = mid - 1 ;
20+ } else {
21+ l = mid + 1 ;
22+ }
23+ }
24+
25+ return ret ;
26+ }
27+
28+ let sequence = [ ] ;
29+
30+ for ( let i = 0 ; i < nums . length ; i ++ ) {
31+ const ret = lower_bound ( sequence , nums [ i ] )
32+ if ( ret === - 1 ) {
33+ sequence . push ( nums [ i ] ) ;
34+ } else {
35+ sequence [ ret ] = nums [ i ]
36+ }
37+ }
38+
39+ return sequence . length ;
40+ } ;
41+
Original file line number Diff line number Diff line change 1+ class Stack {
2+ constructor ( ) {
3+ this . container = [ ] ;
4+ }
5+
6+ push ( item ) {
7+ this . container . push ( item ) ;
8+ }
9+
10+ pop ( ) {
11+ return this . container . pop ( ) ;
12+ }
13+
14+ top ( ) {
15+ if ( this . container . length === 0 ) return null ;
16+ return this . container [ this . container . length - 1 ] ;
17+ }
18+
19+ isEmpty ( ) {
20+ return this . container . length === 0 ;
21+ }
22+ }
23+
24+ /**
25+ * @param {string } s
26+ * @return {boolean }
27+ *
28+ * complexity
29+ * time: O(n)
30+ * space: O(n)
31+ */
32+ var isValid = function ( s ) {
33+ const stack = new Stack ( ) ;
34+ for ( let x of s ) {
35+ if ( x === '(' || x === '[' || x === '{' ) {
36+ stack . push ( x ) ;
37+ } else {
38+ if ( stack . isEmpty ( ) ) return false ;
39+ if ( x === ')' && stack . top ( ) !== '(' ) return false ;
40+ if ( x === ']' && stack . top ( ) !== '[' ) return false ;
41+ if ( x === '}' && stack . top ( ) !== '{' ) return false ;
42+ stack . pop ( ) ;
43+ }
44+ }
45+ return stack . isEmpty ( ) ;
46+ } ;
47+
You can’t perform that action at this time.
0 commit comments