-
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 10 #1020
Merged
Merged
[YeomChaeeun] Week 10 #1020
Changes from all commits
Commits
Show all changes
3 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,45 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* class TreeNode { | ||
* val: number | ||
* left: TreeNode | null | ||
* right: TreeNode | null | ||
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
* } | ||
*/ | ||
|
||
/** | ||
* 이진트리 좌우 반전하기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n) | ||
* - 공간 복잡도: O(n) | ||
* @param root | ||
*/ | ||
function invertTree(root: TreeNode | null): TreeNode | null { | ||
|
||
// 1. 재귀 알고리즘 | ||
// if(root === null) return null | ||
// | ||
// const right = root.right; | ||
// root.right = invertTree(root.left) | ||
// root.left = invertTree(right) | ||
// | ||
// return root | ||
|
||
// 2. 스택을 사용하여 트리를 순회 - 반복 알고리즘 | ||
let stack : (TreeNode | null)[] = [root] | ||
while(stack.length > 0) { | ||
const node = stack.pop(); | ||
if(!node) continue | ||
|
||
[node.left, node.right] = [node.right, node.left]; | ||
|
||
stack.push(node.left) | ||
stack.push(node.right) | ||
} | ||
return root | ||
} |
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,27 @@ | ||
/** | ||
* 점프 게임 현재 인덱스에서 인덱스 값만큼 점프하여 마지막 인덱스까지 도달할 수 있는지 구하기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(n2) | ||
* - 공간 복잡도: O(n) | ||
* @param nums | ||
*/ | ||
function canJump(nums: number[]): boolean { | ||
const dp = new Array(nums.length).fill(false) | ||
|
||
dp[0] = true | ||
for(let i = 0; i < nums.length; i++) { | ||
if(!dp[i]) continue; | ||
|
||
for(let step = 1; step <= nums[i]; step++) { | ||
if(i + step < nums.length) { | ||
dp[i + step] = true; | ||
} | ||
} | ||
|
||
if(dp[nums.length -1]) { | ||
return true; | ||
} | ||
} | ||
|
||
return dp[nums.length - 1]; | ||
} |
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,38 @@ | ||
/** | ||
* 정렬된 배열에 target 문자의 인덱스 찾기 | ||
* 알고리즘 복잡도 | ||
* - 시간 복잡도: O(logn) | ||
* - 공간 복잡도: O(1) | ||
* @param nums | ||
* @param target | ||
*/ | ||
function search(nums: number[], target: number): number { | ||
// findIndex 는 시간복잡도 O(n) 임 | ||
// return nums.findIndex(value => value === target)?? -1 | ||
|
||
// 정렬되어 있는 특성을 이용한 풀이 | ||
let low = 0; | ||
let high = nums.length - 1; | ||
while(low <= high) { | ||
let mid = low + Math.floor((high - low) / 2); | ||
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[mid] === target) return mid | ||
|
||
if(nums[low] <= nums[mid]) { | ||
if(nums[low] <= target && target < nums[mid]) { | ||
high = mid - 1; | ||
} else { | ||
low = mid + 1; | ||
} | ||
} else { | ||
if(nums[mid] < target && target <= nums[high]) { | ||
low = mid + 1; | ||
} else { | ||
high = mid - 1; | ||
} | ||
} | ||
|
||
} | ||
|
||
return -1 | ||
} |
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.
재귀를 순회로 바꿔보신 것 정말 좋네요!