Skip to content

Commit 81ca178

Browse files
authored
Merge pull request #2398 from Cyjin-jani/main
[Cyjin-jani] WEEK 02 solutions
2 parents 114d7cb + a104fa5 commit 81ca178

5 files changed

Lines changed: 145 additions & 0 deletions

File tree

3sum/Cyjin-jani.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// ! 풀다가 막혀서 AI의 도움(힌트)을 받아 완성한 코드입니다..
2+
3+
// tc: o(n^2)
4+
// sc: o(n)
5+
const threeSum = function (nums) {
6+
const answer = [];
7+
8+
// 먼저 오름차순 정렬하기
9+
const sortedArr = nums.sort((a, b) => a - b);
10+
11+
for (let i = 0; i < sortedArr.length; i++) {
12+
if (i > 0 && sortedArr[i] === sortedArr[i - 1]) continue;
13+
14+
let left = i + 1;
15+
let right = sortedArr.length - 1;
16+
17+
while (left < right) {
18+
const sum = sortedArr[i] + sortedArr[left] + sortedArr[right];
19+
20+
if (sum === 0) {
21+
answer.push([sortedArr[i], sortedArr[left], sortedArr[right]]);
22+
23+
// 중복 처리
24+
// left가 가리키는 숫자가 이전 숫자와 똑같다면 계속 패스
25+
while (left < right && sortedArr[left] === sortedArr[left + 1]) {
26+
left++;
27+
}
28+
// right가 가리키는 숫자가 이전 숫자와 똑같다면 계속 패스
29+
while (left < right && sortedArr[right] === sortedArr[right - 1]) {
30+
right--;
31+
}
32+
// 똑같은 숫자들을 다 건너뛰었으니, 새로운 숫자로 넘어감
33+
left++;
34+
right--;
35+
} else if (sum < 0) {
36+
// 0보다 작으면 left를 키움 (오름차순 정렬이기 때문)
37+
left++;
38+
} else {
39+
right--;
40+
}
41+
}
42+
}
43+
44+
return answer;
45+
};

climbing-stairs/Cyjin-jani.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// 1트
2+
// tc: O(n)
3+
// sc: O(n)
4+
const climbStairs_before = function (n) {
5+
// 1걸음 혹은 2걸음만 이동 가능하니까 전 step까지의 결과 + 전전 step까지의 결과
6+
// D(n-1) + D(n-2)
7+
const data = [0, 1, 2];
8+
9+
for (let i = 1; i <= n; i++) {
10+
if (!data[i]) {
11+
data[i] = data[i - 1] + data[i - 2];
12+
}
13+
}
14+
15+
return data[n];
16+
17+
// 이렇게 풀면 sc가 O(n)...
18+
// 더 나은 방법을 고민하는 것이 필요..
19+
};
20+
21+
// 2트
22+
// tc: O(n)
23+
// sc: O(1)
24+
const climbStairs = function (n) {
25+
if (n < 3) return n;
26+
27+
let d1 = 1;
28+
let d2 = 2;
29+
30+
for (let i = 3; i <= n; i++) {
31+
let sum = d1 + d2;
32+
d1 = d2;
33+
d2 = sum;
34+
}
35+
36+
return d2;
37+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// tc : O(n).
2+
// sc : O(n)이지만 출력 배열(result)은 카운트 되지 않는다고 했으므로 O(1).
3+
const productExceptSelf = function (nums) {
4+
const length = nums.length;
5+
const result = Array.from({ length }).fill(1);
6+
7+
let leftSide = 1;
8+
let rightSide = 1;
9+
10+
for (let i = 0; i < length; i++) {
11+
result[i] = leftSide;
12+
leftSide *= nums[i];
13+
}
14+
15+
for (let i = length - 1; i >= 0; i--) {
16+
result[i] *= rightSide;
17+
rightSide *= nums[i];
18+
}
19+
20+
return result;
21+
};

valid-anagram/Cyjin-jani.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// tc: O(n)
2+
// sc: 제약조건에 따르면 O(1). 그러나 모든 유니코드 포함될 경우 O(n)
3+
const isAnagram = function (s, t) {
4+
// 글자수가 다르면 anangram이 성립될 수 없으므로 빠른 return;
5+
if (s.length !== t.length) return false;
6+
7+
const data = new Map();
8+
9+
for (char of s) {
10+
data.set(char, (data.get(char) || 0) + 1);
11+
}
12+
13+
for (let char of t) {
14+
if (!data.get(char)) return false;
15+
data.set(char, data.get(char) - 1);
16+
}
17+
18+
return true;
19+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const isValidBST = function (root) {
2+
let answer = true;
3+
4+
function validateRecursion(node, min, max) {
5+
if (!node) return;
6+
7+
if (node.val >= max || node.val <= min) {
8+
answer = false;
9+
return;
10+
}
11+
if (node.val <= min || node.val >= max) {
12+
answer = false;
13+
return;
14+
}
15+
16+
validateRecursion(node.left, min, node.val);
17+
validateRecursion(node.right, node.val, max);
18+
}
19+
20+
validateRecursion(root, -Infinity, Infinity);
21+
22+
return answer;
23+
};

0 commit comments

Comments
 (0)