File tree Expand file tree Collapse file tree 5 files changed +115
-0
lines changed
product-of-array-except-self
validate-binary-search-tree Expand file tree Collapse file tree 5 files changed +115
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ time complexity : O(n^2)
3+ space complexity : O(1)
4+ */
5+ function threeSum ( nums : number [ ] ) : number [ ] [ ] {
6+ const result : number [ ] [ ] = [ ]
7+ const sortedNums = nums . sort ( ( a , b ) => a - b )
8+
9+ for ( let i = 0 ; i < sortedNums . length - 2 ; i ++ ) {
10+ if ( i > 0 && sortedNums [ i ] === sortedNums [ i - 1 ] ) continue
11+ let low = i + 1
12+ let high = sortedNums . length - 1
13+ while ( low < high ) {
14+ const threeSum = sortedNums [ i ] + sortedNums [ low ] + sortedNums [ high ]
15+ if ( threeSum < 0 ) {
16+ low += 1
17+ } else if ( threeSum > 0 ) {
18+ high -= 1
19+ } else {
20+ result . push ( [ sortedNums [ i ] , sortedNums [ low ] , sortedNums [ high ] ] )
21+ while ( low < high && sortedNums [ low ] === sortedNums [ low + 1 ] ) low ++
22+ while ( low < high && sortedNums [ high ] === sortedNums [ high - 1 ] ) high --
23+
24+ low += 1
25+ high -= 1
26+ }
27+ }
28+ }
29+ return result
30+ }
Original file line number Diff line number Diff line change 1+ /*
2+ 시간복잡도 : O(n)
3+ 공간복잡도 : O(1)
4+ */
5+ function climbStairs ( n : number ) : number {
6+ if ( n < 3 ) return n
7+ let prev = 1
8+ let curr = 2
9+ for ( let i = 0 ; i < n - 2 ; i ++ ) {
10+ const tempPrev = prev
11+ prev = curr
12+ curr = tempPrev + curr
13+ }
14+ return curr
15+ }
Original file line number Diff line number Diff line change 1+ /*
2+ time complexity : O(n)
3+ space complexity : O(1)
4+ */
5+ function productExceptSelf ( nums : number [ ] ) : number [ ] {
6+ const results = new Array ( nums . length ) . fill ( 1 )
7+ let before = 1
8+ let after = 1
9+ for ( let i = 0 ; i < nums . length - 1 ; i ++ ) {
10+ before *= nums [ i ]
11+ results [ i + 1 ] *= before
12+ }
13+ for ( let i = nums . length - 1 ; i > 0 ; i -- ) {
14+ after *= nums [ i ]
15+ results [ i - 1 ] *= after
16+ }
17+
18+ return results
19+ }
Original file line number Diff line number Diff line change 1+ function isAnagram ( s : string , t : string ) : boolean {
2+ // 시간복잡도 O(nlogn), 공간복잡도 O(n)
3+ // const sSorted = s.split('').sort().join(',')
4+ // const tSorted = t.split('').sort().join(',')
5+ // return sSorted === tSorted
6+
7+ /*
8+ 시간복잡도 O(n), 공간복잡도 O(1)
9+ */
10+ if ( s . length != t . length ) return false
11+ const count = new Array ( 26 ) . fill ( 0 )
12+ for ( let i = 0 ; i < s . length ; i ++ ) {
13+ count [ s . charCodeAt ( i ) - 97 ] ++
14+ count [ t . charCodeAt ( i ) - 97 ] --
15+ }
16+ return count . every ( c => c === 0 )
17+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * class TreeNode {
4+ * val: number
5+ * left: TreeNode | null
6+ * right: TreeNode | null
7+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+ * this.val = (val===undefined ? 0 : val)
9+ * this.left = (left===undefined ? null : left)
10+ * this.right = (right===undefined ? null : right)
11+ * }
12+ * }
13+ */
14+ /*
15+ time complexity : O(n)
16+ space complexity : O(n)
17+ */
18+ function isValidBST ( root : TreeNode | null ) : boolean {
19+ let prev = - Infinity
20+ let isValid = true
21+ const inOrder = ( node : TreeNode | null ) : void => {
22+ if ( ! isValid || ! node ) return
23+ inOrder ( node . left )
24+
25+ if ( prev >= node . val ) {
26+ isValid = false
27+ return
28+ }
29+ prev = node . val
30+ inOrder ( node . right )
31+ }
32+ inOrder ( root )
33+ return isValid
34+ }
You can’t perform that action at this time.
0 commit comments