Skip to content

Commit 8a7472d

Browse files
authored
Merge pull request #818 from YeomChaeeun/main
2 parents 4c091c8 + 9e3a817 commit 8a7472d

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

coin-change/YeomChaeeun.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 동전들로 금액을 만들때 필요한 최소 동전의 개수 찾기
3+
* 알고리즘 복잡도
4+
* - 시간 복잡도: O(nxm) 동전의 개수 x 만들어야하는 금액의 크기
5+
* - 공간 복잡도: O(m) 주어진 금액에 비례함
6+
* @param coins
7+
* @param amount
8+
*/
9+
function coinChange(coins: number[], amount: number): number {
10+
const dp = new Array(amount + 1).fill(amount + 1)
11+
dp[0] = 0 // 0원은 0개
12+
13+
for (const coin of coins) {
14+
for (let i = coin; i <= amount; i++) {
15+
dp[i] = Math.min(dp[i], dp[i - coin] + 1)
16+
}
17+
}
18+
19+
return dp[amount] === amount + 1 ? -1 : dp[amount]
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
/**
13+
* 두개의 리스트 정렬 - 재귀 알고리즘으로 접근
14+
* 알고리즘 복잡도
15+
* - 시간 복잡도: O(n+m) - 모든 노드를 한 번씩 들르기 때문
16+
* - 공간 복잡도: O(n+m) - 함수 호출 스택이 재귀 호출로 인해 사용하기 때문
17+
* @param list1
18+
* @param list2
19+
*/
20+
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
21+
if(!(list1 && list2)) return list1 || list2
22+
if(list1.val < list2.val) {
23+
list1.next = mergeTwoLists(list1.next, list2);
24+
return list1
25+
} else {
26+
list2.next = mergeTwoLists(list2.next, list1);
27+
return list2
28+
}
29+
}

missing-number/YeomChaeeun.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 주어진 배열의 중간에 없는 숫자 찾기
3+
* 알고리즘 복잡도
4+
* - 시간 복잡도: O(nlogn)
5+
* - 공간 복잡도: O(1)
6+
* @param nums
7+
*/
8+
function missingNumber(nums: number[]): number {
9+
if(nums.length === 1) {
10+
return nums[0] === 0 ? 1 : 0
11+
}
12+
13+
nums.sort((a, b) => a - b)
14+
15+
for(let i = 0; i < nums.length; i++) {
16+
if(nums[0] !== 0) return 0
17+
if(nums[i] + 1 !== nums[i + 1])
18+
return nums[i] + 1
19+
}
20+
}

0 commit comments

Comments
 (0)