-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[YeomChaeeun] Week 4 #818
[YeomChaeeun] Week 4 #818
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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] | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 타입스크립트는 논리 연산자를 활용하여 간결하게 값 선택 및 조건 처리를 할 수 있는게 좋은 것 같네요! |
||
if(list1.val < list2.val) { | ||
list1.next = mergeTwoLists(list1.next, list2); | ||
return list1 | ||
} else { | ||
list2.next = mergeTwoLists(list2.next, list1); | ||
return list2 | ||
} | ||
Comment on lines
+22
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 객체 값을 직접 비교하는 방법을 썼는데 재귀를 쓰니까 새로운 객체를 생성할 필요가 없다는게 좋은 것 같습니다! |
||
} |
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++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오,, 처음엔 순회 범위가 잘못된줄 알았는데요, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 맞습니다! |
||
if(nums[0] !== 0) return 0 | ||
if(nums[i] + 1 !== nums[i + 1]) | ||
return nums[i] + 1 | ||
} | ||
Comment on lines
+15
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nums[i]를 swap하는 방식 혹은 bit연산을 활용해서 O(N)의 시간복잡도로 줄여보는 것도 좋을 것 같아요 |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 이중 for문에서 바깥쪽 for문을 amount로 했는데,
바깥쪽 for문을 coin으로 하면 더 간결한 코드로 같은 결과를 도출할 수 있어서 좋은 것 같습니다!