Skip to content

Commit 672bf62

Browse files
authored
Merge pull request #721 from YeomChaeeun/main
[YeomChaeeun] Week 2
2 parents 1a83e1d + 0ae2dba commit 672bf62

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

3sum/YeomChaeeun.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 세 숫자의 합이 0이 되는 조합 찾기
3+
* 알고리즘 복잡도:
4+
* - 시간복잡도: O(n^2)
5+
* - 공간복잡도: O(1)
6+
* @param nums
7+
*/
8+
function threeSum(nums: number[]): number[][] {
9+
// 정렬
10+
nums.sort((a, b) => a - b)
11+
let result: number[][] = []
12+
13+
// 투포인터
14+
for (let i = 0; i < nums.length - 2; i++) {
15+
if (i > 0 && nums[i] === nums[i - 1]) continue;
16+
17+
let start = i + 1
18+
let end = nums.length - 1
19+
const target = -nums[i] // 고정 숫자를 이용
20+
// start + end + target === 0 이므로, start + end === -target
21+
22+
while (start < end) {
23+
const sum = nums[start] + nums[end]
24+
if (sum === target) {
25+
result.push([nums[i], nums[start], nums[end]])
26+
27+
// 배열 중복 값 건너뛰기
28+
while (start < end && nums[start] === nums[start + 1]) start++
29+
while (start < end && nums[start] === nums[end - 1]) end--
30+
31+
// 포인터 이동
32+
start++
33+
end--
34+
} else if (sum < target) {
35+
start++
36+
} else {
37+
end--
38+
}
39+
}
40+
}
41+
return result
42+
}

climbing-stairs/YeomChaeeun.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 계단 오르기
3+
* 알고리즘 복잡도:
4+
* - 시간복잡도: O(n) - 입력값 크기에 비례하는 단일 반복문
5+
* - 공간복잡도: O(1) - 상수 개의 변수만 사용
6+
* @param n
7+
*/
8+
function climbStairs(n: number): number {
9+
// 1 - 2 - 3 - 5 - 8 ... 규칙 발생
10+
if(n <= 3) return n
11+
12+
// 접근 (1) - 시간복잡도가 너무 큼
13+
// return climbStairs(n - 1) + climbStairs(n - 2) // 시간
14+
15+
// 접근 (2)
16+
// 피보나치 수열과 비슷한 앞의 두 숫자를 더해서 배열 구조를 만듬
17+
let current = 1; // 현재 방법
18+
let prev = 1; // 이전 단계의 방법
19+
20+
// n-1번 반복하여 계산
21+
for (let i = 1; i < n; i++) {
22+
[current, prev] = [current + prev, current];
23+
}
24+
25+
return current;
26+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
/**
15+
* 이진트리 만들기
16+
* 알고리즘 복잡도:
17+
* - 시간복잡도: O(n^2)
18+
* - 공간복잡도: O(n^2)
19+
*/
20+
function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
21+
// 전위 순회(preorder): 최상위 노드 -> 좌측 서브트리 -> 우측 서브트리
22+
// 중위 순회(inorder): 좌측 서브트리 -> 최상위 노드 -> 우측 서브트리
23+
24+
// 재귀적으로 호출하기 위해 배열이 비었을때 null을 반환하며 종료시킴
25+
if (preorder.length === 0 || inorder.length === 0) {
26+
return null
27+
}
28+
29+
let root = preorder[0]
30+
let mid = inorder.findIndex((value) => value === root)
31+
32+
let leftInorder = inorder.slice(0, mid)
33+
let rightInorder = inorder.slice(mid+1)
34+
35+
let leftPreorder = preorder.slice(1, 1 + leftInorder.length)
36+
let rightPreorder = preorder.slice(1 + leftInorder.length)
37+
38+
let left = buildTree(leftPreorder, leftInorder)
39+
let right = buildTree(rightPreorder, rightInorder)
40+
41+
return new TreeNode(root, left, right)
42+
}

valid-anagram/YeomChaeEun.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* 알고리즘 복잡도:
3+
* - 시간복잡도: O(n)
4+
* - 공간복잡도: O(k)
5+
* @param s
6+
* @param t
7+
*/
8+
function isAnagram(s: string, t: string): boolean {
9+
if(s.length !== t.length) return false
10+
11+
let counter = {}
12+
for(let sValue of s) {
13+
if(!counter[sValue]) { counter[sValue] = 1 }
14+
else { counter[sValue]++ }
15+
16+
}
17+
18+
for(let tValue of t) {
19+
if(!counter[tValue]) return false
20+
else counter[tValue]--
21+
}
22+
23+
return Object.values(counter).findIndex(value => value !== 0) < 0;
24+
}

0 commit comments

Comments
 (0)