-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #818 from YeomChaeeun/main
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* 동전들로 금액을 만들때 필요한 최소 동전의 개수 찾기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(nxm) 동전의 개수 x 만들어야하는 금액의 크기 | ||
* - 공간 복잡도: O(m) 주어진 금액에 비례함 | ||
* @param coins | ||
* @param amount | ||
*/ | ||
function coinChange(coins: number[], amount: number): number { | ||
const dp = new Array(amount + 1).fill(amount + 1) | ||
dp[0] = 0 // 0원은 0개 | ||
|
||
for (const coin of coins) { | ||
for (let i = coin; i <= amount; i++) { | ||
dp[i] = Math.min(dp[i], dp[i - coin] + 1) | ||
} | ||
} | ||
|
||
return dp[amount] === amount + 1 ? -1 : dp[amount] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* class ListNode { | ||
* val: number | ||
* next: ListNode | null | ||
* constructor(val?: number, next?: ListNode | null) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
* } | ||
*/ | ||
/** | ||
* 두개의 리스트 정렬 - 재귀 알고리즘으로 접근 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n+m) - 모든 노드를 한 번씩 들르기 때문 | ||
* - 공간 복잡도: O(n+m) - 함수 호출 스택이 재귀 호출로 인해 사용하기 때문 | ||
* @param list1 | ||
* @param list2 | ||
*/ | ||
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null { | ||
if(!(list1 && list2)) return list1 || list2 | ||
if(list1.val < list2.val) { | ||
list1.next = mergeTwoLists(list1.next, list2); | ||
return list1 | ||
} else { | ||
list2.next = mergeTwoLists(list2.next, list1); | ||
return list2 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* 주어진 배열의 중간에 없는 숫자 찾기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(nlogn) | ||
* - 공간 복잡도: O(1) | ||
* @param nums | ||
*/ | ||
function missingNumber(nums: number[]): number { | ||
if(nums.length === 1) { | ||
return nums[0] === 0 ? 1 : 0 | ||
} | ||
|
||
nums.sort((a, b) => a - b) | ||
|
||
for(let i = 0; i < nums.length; i++) { | ||
if(nums[0] !== 0) return 0 | ||
if(nums[i] + 1 !== nums[i + 1]) | ||
return nums[i] + 1 | ||
} | ||
} |