-
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
[choidabom] Week 4 #837
Merged
Merged
[choidabom] Week 4 #837
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,48 @@ | ||
/** | ||
* Runtime: 0ms, Memory: 52.30MB | ||
* | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
* | ||
* 접근 | ||
* 핵심은 두 리스트를 비교하면서 작은 값부터 정렬되도록 리스트를 만드는 것이다. | ||
* 두 연결 리스트 중 하나가 null이 될 때까지 현재 노드 값을 비교하여 더 작은 값을 새로운 리스트트 추가하고, 남은 리스트를 추가한다. | ||
* | ||
* 평소 접하는 배열이 아닌 링크드 리스트로 풀어야 했기에 접근 방식이 와닿지 않았다. | ||
* 처음에는 list1, list2가 하나의 노드라고 생각하여 헷갈렸지만, 실제로는 각 노드가 next를 통해 연결된 연결 리스트임이 중요하다. | ||
*/ | ||
|
||
/** | ||
* 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) | ||
* } | ||
* } | ||
*/ | ||
|
||
function mergeTwoLists( | ||
list1: ListNode | null, | ||
list2: ListNode | null | ||
): ListNode | null { | ||
let dummy = new ListNode(-1); | ||
let current = dummy; | ||
|
||
while (list1 !== null && list2 !== null) { | ||
if (list1.val < list2.val) { | ||
current.next = list1; | ||
list1 = list1.next; | ||
} else { | ||
current.next = list2; | ||
list2 = list2.next; | ||
} | ||
current = current.next; | ||
} | ||
|
||
current.next = list1 || list2; | ||
|
||
return dummy.next; | ||
} |
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,40 @@ | ||
/** | ||
* Runtime: 19ms, Memory: 52.48MB | ||
* | ||
* 접근 | ||
* 직관적으로 생각했을 때, 0부터 n까지의 숫자 중에서 없는 숫자를 찾아야 한다. | ||
* 완전 탐색으로 정렬한 배열에서 순서대로 비교하면서 없는 숫자를 찾을 수 있다. | ||
* Time Complexity: O(nlogn) | ||
* Space Complexity: O(n) | ||
* | ||
* Follow up: Could you implement a solution using only O(1) extra space complexity and O(n) runtime complexity? | ||
*/ | ||
|
||
function missingNumber(nums: number[]): number { | ||
const numsLen = nums.length; | ||
const sortedNums = nums.sort((a, b) => a - b); // 오름차순 정렬 | ||
|
||
for (let i = 0; i <= numsLen; i++) { | ||
if (i !== sortedNums[i]) { | ||
return i; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Runtime: 1ms, Memory: 51.96MB | ||
* | ||
* 접근 | ||
* Follow up에 대한 해결 방법 | ||
* 0부터 n까지의 숫자의 합을 구한 뒤, 주어진 배열의 합을 빼면 없는 숫자를 찾을 수 있다. | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(1) | ||
*/ | ||
|
||
function missingNumber(nums: number[]): number { | ||
const size = nums.length; | ||
const sum = (size * (size + 1)) / 2; | ||
const accurate = nums.reduce((sum, num) => sum + num, 0); | ||
|
||
return sum - accurate; | ||
} |
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,53 @@ | ||
/** | ||
* Runtime: 239ms, Memory: 51.98MB | ||
* | ||
* Time Complexity: O(rows * cols * 4^L) L: 단어 길이 | ||
* Space Complexity: O(L) | ||
* | ||
*/ | ||
function exist(board: string[][], word: string): boolean { | ||
const ROWS = board.length; | ||
const COLUMNS = board[0].length; | ||
|
||
for (let r = 0; r < ROWS; r++) { | ||
for (let c = 0; c < COLUMNS; c++) { | ||
if (board[r][c] === word[0]) { | ||
if (check(r, c, 0, board, word)) return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function check( | ||
r: number, | ||
c: number, | ||
i: number, | ||
board: string[][], | ||
word: string | ||
): boolean { | ||
const ROWS = board.length; | ||
const COLUMNS = board[0].length; | ||
|
||
if (i === word.length) { | ||
return true; | ||
} | ||
|
||
if (r < 0 || r >= ROWS || c < 0 || c >= COLUMNS || board[r][c] !== word[i]) { | ||
return false; | ||
} | ||
|
||
const temp = board[r][c]; | ||
board[r][c] = "#"; | ||
|
||
const found = | ||
check(r - 1, c, i + 1, board, word) || // 위 | ||
check(r, c + 1, i + 1, board, word) || // 오른쪽 | ||
check(r + 1, c, i + 1, board, word) || // 아래 | ||
check(r, c - 1, i + 1, board, word); // 왼쪽 | ||
|
||
board[r][c] = temp; | ||
|
||
return found; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
안녕하세요 상단에 Big O 분석 추가해주시면 더 좋을 것 같습니다 :)