Skip to content

Commit 3921610

Browse files
authored
Merge pull request #1238 from uraflower/main
[uraflower] WEEK 02 solutions
2 parents 6398137 + a8ad6f0 commit 3921610

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

โ€Ž3sum/uraflower.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ ๋ฐฐ์—ด ์ค‘ ์„ธ ์›์†Œ์˜ ํ•ฉ์ด 0์ธ ๊ณ ์œ ํ•œ ๊ฒฝ์šฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {number[]} nums
4+
* @return {number[][]}
5+
*/
6+
const threeSum = function(nums) {
7+
const answer = [];
8+
const sorted = nums.toSorted((a, b) => Number(a) - Number(b));
9+
10+
for (let i = 0; i < sorted.length; i++) {
11+
if (i > 0 && sorted[i] === sorted[i - 1]) continue;
12+
13+
let left = i + 1;
14+
let right = sorted.length - 1;
15+
while (left < right) {
16+
const sum = sorted[i] + sorted[left] + sorted[right];
17+
18+
if (sum > 0) right--;
19+
else if (sum < 0) left++;
20+
else {
21+
answer.push([sorted[i], sorted[left], sorted[right]]);
22+
right--;
23+
24+
// ์ค‘๋ณต ๋ฐฉ์ง€
25+
while (sorted[left] === sorted[left + 1] && left < right) left++;
26+
while (sorted[right] === sorted[right + 1] && left < right) right--;
27+
}
28+
}
29+
}
30+
31+
return answer;
32+
};
33+
34+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n^2)
35+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)

โ€Žclimbing-stairs/uraflower.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* ํ•œ ์นธ ๋˜๋Š” ๋‘ ์นธ์”ฉ n๊ฐœ์˜ ๊ณ„๋‹จ์„ ์˜ค๋ฅด๋Š” ๋ฐฉ๋ฒ•์˜ ๊ฐ€์ง“์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {number} n
4+
* @return {number}
5+
*/
6+
const climbStairs = function(n) {
7+
const steps = Array.from({length: n + 1}).fill(0);
8+
9+
for (let i = 1; i <= n; i++) {
10+
if (i === 1) {
11+
steps[i] = 1;
12+
} else if (i === 2) {
13+
steps[i] = 2;
14+
} else {
15+
steps[i] = steps[i-1] + steps[i-2];
16+
}
17+
}
18+
19+
return steps[n];
20+
};
21+
22+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
23+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ ์ž๊ธฐ ์ž์‹ ์„ ์ œ์™ธํ•˜๊ณ  ๋‚˜๋จธ์ง€ ์›์†Œ๋ฅผ ๊ณฑํ•œ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ๋œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {number[]} nums
4+
* @return {number[]}
5+
*/
6+
const productExceptSelf = function(nums) {
7+
const dp1 = []; // left to right
8+
const dp2 = []; // right to left
9+
10+
for (let i = 0, j = nums.length - 1; i < nums.length; i++, j--) {
11+
dp1[i] = (dp1[i-1] ?? 1) * nums[i];
12+
dp2[j] = (dp2[j+1] ?? 1) * nums[j];
13+
}
14+
15+
return nums.map((_, i) => (dp1[i-1] ?? 1) * (dp2[i+1] ?? 1));
16+
};
17+
18+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
19+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n) --> dp ๋ฐฐ์—ด์„ ํ•˜๋‚˜๋งŒ ์“ฐ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์„ ๋“ฏ

โ€Žvalid-anagram/uraflower.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* ๋‘ ๋ฌธ์ž์—ด์ด ์• ๋„ˆ๊ทธ๋žจ์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {string} s
4+
* @param {string} t
5+
* @return {boolean}
6+
*/
7+
const isAnagram = function (s, t) {
8+
if (s.length !== t.length) return false;
9+
10+
const counter = Array.from(s).reduce((counter, char) => {
11+
counter[char] = counter[char] + 1 || 1;
12+
return counter;
13+
}, {});
14+
15+
for (let char of t) {
16+
if (!counter[char] || counter[char] === 0) {
17+
return false;
18+
}
19+
20+
counter[char] -= 1;
21+
}
22+
23+
return true;
24+
};
25+
26+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
27+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
* ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ(BST)์ธ์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
11+
* @param {TreeNode} root
12+
* @return {boolean}
13+
*/
14+
15+
///////////////////////// 1 ///////////////////////////
16+
// inorder traversalํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ธ์ง€ ํ™•์ธ
17+
const isValidBST = function(root) {
18+
const inorder = [];
19+
inorderTraversal(root, inorder);
20+
21+
return inorder.every((val, i, arr) => i === 0 || arr[i-1] < val);
22+
};
23+
24+
function inorderTraversal(current, result) {
25+
if (current.left) {
26+
inorderTraversal(current.left, result);
27+
}
28+
29+
result.push(current.val);
30+
31+
if (current.right) {
32+
inorderTraversal(current.right, result);
33+
}
34+
}
35+
36+
///////////////////////// 2 ///////////////////////////
37+
// inorder traversalํ•˜๋ฉด์„œ ๋ฐ”๋กœ ์ง์ „์— ์ˆœํšŒํ•œ ๊ฐ’์ด ํ˜„์žฌ ๊ฐ’๋ณด๋‹ค ์ž‘์€์ง€ ํ™•์ธ (=์˜ค๋ฆ„์ฐจ์ˆœ์ธ์ง€ ๋ฐ”๋กœ๋ฐ”๋กœ ํ™•์ธ)
38+
const isValidBST = function(root) {
39+
let prev = -Infinity;
40+
41+
function inorder(node) {
42+
if (!node) return true;
43+
44+
if (!inorder(node.left)) return false;
45+
46+
if (node.val <= prev) return false;
47+
prev = node.val;
48+
49+
return inorder(node.right);
50+
}
51+
52+
return inorder(root);
53+
};
54+
55+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
56+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n) (์žฌ๊ท€ ์Šคํƒ == ํŠธ๋ฆฌ ๋†’์ด. ์ตœ์•…์˜ ๊ฒฝ์šฐ ํŽธํ–ฅ ํŠธ๋ฆฌ์ผ ๋•Œ ๋†’์ด๋Š” n)

0 commit comments

Comments
ย (0)