File tree Expand file tree Collapse file tree 5 files changed +158
-0
lines changed
product-of-array-except-self
validate-binary-search-tree Expand file tree Collapse file tree 5 files changed +158
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 시간복잡도: O(n²)
3
+ * 공간복잡도: O(1) (결과 배열 제외)
4
+ * 풀이 방법: 정렬 후 투 포인터 방식
5
+ * @param {number[] } nums
6
+ * @return {number[][] }
7
+ */
8
+ const threeSum = function ( nums ) {
9
+ const sortedNums = nums . sort ( ( a , b ) => a - b ) ;
10
+ const result = [ ] ;
11
+
12
+ for ( let i = 0 ; i < sortedNums . length ; i += 1 ) {
13
+ // 첫 번째 요소의 중복 제거
14
+ if ( i > 0 && sortedNums [ i ] === sortedNums [ i - 1 ] ) {
15
+ continue ;
16
+ }
17
+
18
+ let left = i + 1 ;
19
+ let right = sortedNums . length - 1 ;
20
+
21
+ while ( left < right ) {
22
+ const threeSum = sortedNums [ i ] + sortedNums [ left ] + sortedNums [ right ] ;
23
+
24
+ if ( threeSum > 0 ) {
25
+ right -= 1 ;
26
+ } else if ( threeSum < 0 ) {
27
+ left += 1 ;
28
+ } else {
29
+ result . push ( [ sortedNums [ i ] , sortedNums [ left ] , sortedNums [ right ] ] ) ;
30
+
31
+ // 중복 제거
32
+ while ( left < right && sortedNums [ left ] === sortedNums [ left + 1 ] ) {
33
+ left += 1 ;
34
+ }
35
+ while ( left < right && sortedNums [ right ] === sortedNums [ right - 1 ] ) {
36
+ right -= 1 ;
37
+ }
38
+
39
+ left += 1 ;
40
+ right -= 1 ;
41
+ }
42
+ }
43
+ }
44
+
45
+ return result ;
46
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @description
3
+ * time complexity: O(n)
4
+ * space complexity: O(n)
5
+ * runtime: 0ms
6
+ * 풀이 방법: 기본적인 DP 풀이 방법
7
+ * @param {number } n
8
+ * @return {number }
9
+ */
10
+ const climbStairs = function ( n ) {
11
+ const dp = [ 1 , 2 ] ;
12
+
13
+ for ( let i = 2 ; i < n ; i += 1 ) {
14
+ dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ;
15
+ }
16
+
17
+ return dp [ n - 1 ] ;
18
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @description
3
+ * time complexity: O(n)
4
+ * space complexity: O(n)
5
+ * runtime: 5ms
6
+ * 풀이 방법 : 40분고민하다가 다른사람 풀이보고 누적합을 이용하면 된다해서 구현함
7
+ * @param {number[] } nums
8
+ * @return {number[] }
9
+ */
10
+ var productExceptSelf = function ( nums ) {
11
+ const result = new Array ( nums . length ) . fill ( 1 ) ;
12
+
13
+ let prefix = 1 ;
14
+
15
+ for ( let i = 0 ; i < nums . length ; i += 1 ) {
16
+ result [ i ] = prefix ;
17
+ prefix *= nums [ i ] ;
18
+ }
19
+
20
+ let postfix = 1 ;
21
+
22
+ for ( let i = nums . length - 1 ; i >= 0 ; i -= 1 ) {
23
+ result [ i ] *= postfix ;
24
+ postfix *= nums [ i ] ;
25
+ }
26
+
27
+ return result ;
28
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @description
3
+ * time complexity: O(nlogn) split 시 새로운 배열을 생성하고 sort 시 nlogn 시간 소요
4
+ * space complexity: O(n) split 시 새로운 배열을 생성함
5
+ * runtime: 32ms
6
+ * 풀이 방법: 두 문자열을 정렬하여 비교하는 방법
7
+ * @param {string } s
8
+ * @param {string } t
9
+ * @return {boolean }
10
+ */
11
+ const isAnagram = function ( s , t ) {
12
+ return s . split ( "" ) . sort ( ) . join ( "" ) === t . split ( "" ) . sort ( ) . join ( "" ) ;
13
+ } ;
14
+
15
+ /**
16
+ * @description
17
+ * time complexity: O(n)
18
+ * space complexity: O(n)
19
+ * runtime: 15ms
20
+ * 풀이 방법: 해쉬맵을 통해 카운트를 추가하거나 제거하는 방식, 유니코드도 대응가능
21
+ * @param {string } s
22
+ * @param {string } t
23
+ * @return {boolean }
24
+ */
25
+ const isAnagramSolution2 = function ( s , t ) {
26
+ if ( s . length !== t . length ) return false ;
27
+
28
+ const map = new Map ( ) ;
29
+
30
+ for ( let i = 0 ; i < s . length ; i += 1 ) {
31
+ map . set ( s [ i ] , ( map . get ( s [ i ] ) || 0 ) + 1 ) ;
32
+ map . set ( t [ i ] , ( map . get ( t [ i ] ) || 0 ) - 1 ) ;
33
+ }
34
+
35
+ for ( const value of map . values ( ) ) {
36
+ if ( value !== 0 ) return false ;
37
+ }
38
+
39
+ return true ;
40
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val, left, right) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.left = (left===undefined ? null : left)
6
+ * this.right = (right===undefined ? null : right)
7
+ * }
8
+ */
9
+ /**
10
+ * @param {TreeNode } root
11
+ * @return {boolean }
12
+ */
13
+ var isValidBST = function ( root , low = - Infinity , high = Infinity ) {
14
+ if ( ! root ) {
15
+ return true ;
16
+ }
17
+
18
+ if ( root . val <= low || root . val >= high ) {
19
+ return false ;
20
+ }
21
+
22
+ return (
23
+ isValidBST ( root . left , low , root . val ) &&
24
+ isValidBST ( root . right , root . val , high )
25
+ ) ;
26
+ } ;
You can’t perform that action at this time.
0 commit comments