Skip to content

Commit b93073d

Browse files
authored
Merge pull request #704 from jdy8739/main
[jdy8739] WEEK 02
2 parents 8f67e90 + 7cf288b commit b93073d

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

โ€Ž3sum/jdy8739.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
var threeSum = function(nums) {
6+
const answer = [];
7+
8+
const sorted = nums.sort((a, b) => a - b);
9+
10+
for (let i=0; i<sorted.length; i++) {
11+
if (i > 0 && sorted[i] === sorted[i - 1]) {
12+
// ์œ„ ์กฐ๊ฑด์œผ๋กœ ์ค‘๋ณต ์ˆซ์ž ํ•„ํ„ฐ
13+
continue;
14+
}
15+
16+
let l = i + 1;
17+
let h = sorted.length - 1;
18+
19+
while (l < h) {
20+
const sum = sorted[i] + sorted[l] + sorted[h];
21+
22+
if (sum === 0) {
23+
const arr = [sorted[i], sorted[l], sorted[h]];
24+
answer.push(arr);
25+
26+
// ์•„๋ž˜ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ค‘๋ณต ์ˆซ์ž ํ•„ํ„ฐ
27+
while (l < h && sorted[l] === sorted[l + 1]) l++;
28+
while (l < h && sorted[h] === sorted[h - 1]) h--;
29+
30+
h--;
31+
l++;
32+
}
33+
34+
if (sum > 0) h--;
35+
if (sum < 0) l++;
36+
}
37+
}
38+
39+
return answer;
40+
};
41+
42+
// TC: O(n2)
43+
// SC: O(1)
44+
45+

โ€Žclimbing-stairs/jdy8739.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var climbStairs = function(n) {
6+
let first = 1;
7+
let second = 2;
8+
9+
if (n <= 2) {
10+
return n;
11+
}
12+
13+
for (let i=2; i<n; i++) {
14+
let tmp = second;
15+
second = first + second;
16+
first = tmp;
17+
}
18+
19+
return second;
20+
};
21+
22+

โ€Žvalid-anagram/jdy8739.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const getDictionary = (s) => {
2+
const arr = s.split('');
3+
4+
const dict = {};
5+
6+
for (let i=0; i<arr.length; i++) {
7+
const key = arr[i];
8+
const value = dict[key];
9+
10+
if (value === undefined) {
11+
dict[key] = 1;
12+
} else {
13+
dict[key] = dict[key] + 1;
14+
}
15+
}
16+
17+
return dict;
18+
}
19+
20+
const checkSameLength = (dictA, dictB) => {
21+
return Object.keys(dictA).length === Object.keys(dictB).length
22+
}
23+
24+
const checkSameDict = (s, t) => {
25+
for (const key in s) {
26+
if (s[key] !== t[key]) {
27+
return false;
28+
}
29+
}
30+
31+
return true;
32+
}
33+
34+
/**
35+
* @param {string} s
36+
* @param {string} t
37+
* @return {boolean}
38+
*/
39+
var isAnagram = function(s, t) {
40+
const dictA = getDictionary(s);
41+
42+
const dictB = getDictionary(t);
43+
44+
return checkSameLength(dictA, dictB) && checkSameDict(dictA, dictB);
45+
};
46+
47+
// ๊ณต๊ฐ„๋ณต์žก๋„: ํ•ด์‹œ ํ…Œ์ด๋ธ”์— ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ €์žฅํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ O(n)
48+
// ์‹œ๊ฐ„๋ณต์žก๋„: ๋‘ ๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ํ•œ ๋ฒˆ์”ฉ ๋ฃจํ”„๋ฅผ ๋Œ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— 0(n)
49+

0 commit comments

Comments
ย (0)