Skip to content

Commit 109a279

Browse files
authored
Merge pull request DaleStudy#748 from anniemon/main
[anniemon78] Week 2
2 parents 8f52fc5 + 3274855 commit 109a279

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

โ€Ž3sum/anniemon.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„:
3+
* nums์˜ ๊ธธ์ด๋งŒํผ for๋ฌธ์„ ์ˆœํšŒํ•˜๊ณ , ๋‚ด๋ถ€์—์„œ ํˆฌ ํฌ์ธํ„ฐ๋กœ ๋˜ ํ•œ ๋ฒˆ ์ˆœํšŒํ•˜๋ฏ€๋กœ, O(nยฒ)
4+
* ๊ณต๊ฐ„ ๋ณต์žก๋„:
5+
* ์ •๋ ฌ์€ ์ถ”๊ฐ€ ๊ณต๊ฐ„ ์‚ฌ์šฉ์ด ์—†์Œ.
6+
* res ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” ๊ณ ์œ ํ•œ ์„ธ ์ˆซ์ž ์กฐํ•ฉ์˜ ๊ฐฏ์ˆ˜.
7+
* ์ด๋ฅผ k๋ผ๊ณ  ํ•˜๋ฉด, ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(k)
8+
*/
9+
/**
10+
* @param {number[]} nums
11+
* @return {number[][]}
12+
*/
13+
var threeSum = function (nums) {
14+
nums.sort((a, b) => a - b);
15+
16+
const res = [];
17+
for (let i = 0; i < nums.length; i++) {
18+
if (i > 0 && nums[i] === nums[i - 1]) {
19+
continue;
20+
}
21+
22+
let l = i + 1;
23+
let r = nums.length - 1;
24+
while (l < r) {
25+
const sum = nums[i] + nums[l] + nums[r];
26+
if (sum > 0) {
27+
r--;
28+
} else if (sum < 0) {
29+
l++;
30+
} else if (sum === 0) {
31+
res.push([nums[i], nums[l], nums[r]]);
32+
l++;
33+
r--;
34+
while (l < r && nums[l] === nums[l - 1]) {
35+
l++;
36+
}
37+
}
38+
}
39+
}
40+
return res;
41+
};

โ€Žclimbing-stairs/anniemon.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„:
3+
* ๋ฉ”๋ชจ์ด์ œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ n๊นŒ์ง€ ๊ฐ€๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ์ €์žฅ.
4+
* ์žฌ๊ท€ ํ•จ์ˆ˜๋Š” ์ตœ๋Œ€ n๋งŒํผ ํ˜ธ์ถœ๋จ.
5+
* ๋”ฐ๋ผ์„œ, ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)
6+
* ๊ณต๊ฐ„ ๋ณต์žก๋„:
7+
* ๋ฉ”๋ชจ ๊ฐ์ฒด์˜ ํฌ๊ธฐ๋Š” n์˜ ํฌ๊ธฐ์™€ ๊ฐ™์Œ.
8+
* ์žฌ๊ท€ ํ•จ์ˆ˜๋Š” ์ตœ๋Œ€ n๋งŒํผ ํ˜ธ์ถœ๋จ.
9+
* ๋”ฐ๋ผ์„œ, ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(n)
10+
*/
11+
/**
12+
* @param {number} n
13+
* @return {number}
14+
*/
15+
var climbStairs = function(n) {
16+
const memo = [0, 1, 2];
17+
const recurse = (n) => {
18+
if(memo[n]) {
19+
return memo[n];
20+
}
21+
memo[n] = recurse(n - 1) + recurse(n - 2);
22+
return memo[n];
23+
}
24+
return recurse(n);
25+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„:
3+
* preIdx๋ฅผ ์ด์šฉํ•˜์—ฌ ์ค‘์œ„ ์ˆœํšŒ ๋ฐฐ์—ด์—์„œ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์™ผ์ชฝ ์„œ๋ธŒ ํŠธ๋ฆฌ์™€ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒ ํŠธ๋ฆฌ๋ฅผ ํƒ์ƒ‰.
4+
* ๊ฐ ์„œ๋ธŒ ํŠธ๋ฆฌ๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋ฉฐ ๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์ˆœํšŒํ•˜๋ฏ€๋กœ, ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)
5+
* ๊ณต๊ฐ„ ๋ณต์žก๋„:
6+
* ์ค‘์œ„ ์ˆœํšŒ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ๋งต์„ ์ƒ์„ฑํ•˜๋ฏ€๋กœ, ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(n)
7+
*/
8+
/**
9+
* Definition for a binary tree node.
10+
* function TreeNode(val, left, right) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.left = (left===undefined ? null : left)
13+
* this.right = (right===undefined ? null : right)
14+
* }
15+
*/
16+
/**
17+
* @param {number[]} preorder
18+
* @param {number[]} inorder
19+
* @return {TreeNode}
20+
*/
21+
var buildTree = function(preorder, inorder) {
22+
let preIdx = 0;
23+
const inorderMap = new Map(inorder.map((e, i) => [e, i]))
24+
25+
const dfs = (l, r) => {
26+
if(l > r) {
27+
return null;
28+
}
29+
let root = preorder[preIdx];
30+
preIdx++;
31+
32+
let rootIdx = inorderMap.get(root);
33+
34+
const node = new TreeNode(root);
35+
node.left = dfs(l, rootIdx - 1);
36+
node.right = dfs(rootIdx + 1, r);
37+
return node;
38+
}
39+
return dfs(0, inorder.length - 1)
40+
};

โ€Žvalid-anagram/anniemon.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„:
3+
* s์™€ t์˜ ๊ธธ์ด๋งŒํผ ๊ฐ ๋ฌธ์ž์˜ ์นด์šดํŠธ๋ฅผ ๊ธฐ๋กํ•˜๊ณ  ์ด๋ฅผ ํ™•์ธํ•˜๋ฏ€๋กœ, ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)
4+
* ๊ณต๊ฐ„ ๋ณต์žก๋„:
5+
* ์นด์šดํŠธ ๊ฐ์ฒด๋Š” ์ตœ๋Œ€ s์™€ t์˜ ๊ธธ์ด๋งŒํผ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜๋ฏ€๋กœ, ๊ณต๊ฐ„ ๋ณต์žก๋„๋Š” O(n)
6+
*/
7+
/**
8+
* @param {string} s
9+
* @param {string} t
10+
* @return {boolean}
11+
*/
12+
var isAnagram = function (s, t) {
13+
if (s.length !== t.length) {
14+
return false;
15+
}
16+
17+
const count = {};
18+
for (let i = 0; i < s.length; i++) {
19+
count[s[i]] = (count[s[i]] || 0) + 1;
20+
count[t[i]] = (count[t[i]] || 0) - 1;
21+
}
22+
23+
for (const key in count) {
24+
if (count[key] !== 0) {
25+
return false;
26+
}
27+
}
28+
return true;
29+
};

0 commit comments

Comments
ย (0)