Skip to content

Commit 7017c79

Browse files
authored
Merge pull request #1230 from jeongwoo903/main
[jeongwoo903] Week 02 Solutions
2 parents d48dba6 + 72b3f50 commit 7017c79

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

3sum/jeongwoo903.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* 투 포인터 방식 사용
3+
* 시간 복잡도: O(n^2)
4+
* 공간 복잡도; O(n^2)
5+
*/
6+
7+
/**
8+
* @param {number[]} nums
9+
* @return {number[][]}
10+
*/
11+
const threeSum = (nums) => {
12+
const sorted = [...nums].sort((a, b) => a - b);
13+
const results = [];
14+
15+
sorted.forEach((val, i) => {
16+
if (i > 0 && val === sorted[i - 1]) return;
17+
18+
let left = i + 1;
19+
let right = sorted.length - 1;
20+
21+
while (left < right) {
22+
const sum = val + sorted[left] + sorted[right];
23+
24+
if (sum === 0) {
25+
results.push([val, sorted[left], sorted[right]]);
26+
27+
// 중복된 left/right 스킵
28+
const currentLeft = sorted[left];
29+
const currentRight = sorted[right];
30+
31+
while (left < right && sorted[left] === currentLeft) left++;
32+
while (left < right && sorted[right] === currentRight) right--;
33+
} else if (sum < 0) {
34+
left++;
35+
} else {
36+
right--;
37+
}
38+
}
39+
});
40+
41+
return results;
42+
};

climbing-stairs/jeongwoo903.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* 시간 복잡도: O(n)
3+
* 공간 복잡도: O(n)
4+
*/
5+
6+
/**
7+
* @param {number} n
8+
* @return {number}
9+
*/
10+
var climbStairs = function(n) {
11+
if (n === 1) return 1;
12+
13+
const dp = [1, 2]
14+
15+
for (let i = 2; i < n; i++) {
16+
dp[i] = dp[i-1] + dp[i-2]
17+
}
18+
19+
return dp[n-1]
20+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* 시간 복잡도: O(n)
3+
* 공간 복잡도: O(n)
4+
*/
5+
6+
/**
7+
* @param {number[]} nums
8+
* @return {number[]}
9+
*/
10+
11+
var productExceptSelf = function(nums) {
12+
const lefts = nums.reduce((products, num, i) => {
13+
products.push(i === 0 ? 1 : products[i - 1] * nums[i - 1]);
14+
return products;
15+
}, []
16+
);
17+
18+
return nums.reduceRight((result, num, i) => {
19+
const rights = i === nums.length - 1 ? 1 : result.rightProduct * nums[i + 1];
20+
result.products[i] *= rights;
21+
result.rightProduct = rights;
22+
return result;
23+
}, { products: lefts, rights: 1 }
24+
).products;
25+
};

valid-anagram/jeongwoo903.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* 시간 복잡도: O(n log n)
3+
* 공간 복잡도: O(n)
4+
*/
5+
6+
/**
7+
* @param {string} s
8+
* @param {string} t
9+
* @return {boolean}
10+
*/
11+
var isAnagram = function(s, t) {
12+
if(s.length !== t.length) return false;
13+
14+
const strA = [...s].sort().join('');
15+
const strB = [...t].sort().join('');
16+
17+
return strA === strB;
18+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* 시간 복잡도: O(n)
3+
* 공간 복잡도; O(h)
4+
* -> h는 트리의 높이
5+
*/
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* function TreeNode(val, left, right) {
10+
* this.val = (val===undefined ? 0 : val)
11+
* this.left = (left===undefined ? null : left)
12+
* this.right = (right===undefined ? null : right)
13+
* }
14+
*/
15+
/**
16+
* @param {TreeNode} root
17+
* @return {boolean}
18+
*/
19+
var isValidBST = function(root) {
20+
function bst(node , min, max) {
21+
if (!node) return true;
22+
23+
if (node.val <= min || node.val >= max) return false;
24+
25+
return (
26+
bst(node.left, min, node.val) && helper(node.right, node.val, max)
27+
);
28+
}
29+
30+
return bst(root, -Infinity, Infinity);
31+
};

0 commit comments

Comments
 (0)