-
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
[jdy8739] Week 10 #997
Open
jdy8739
wants to merge
4
commits into
DaleStudy:main
Choose a base branch
from
jdy8739:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[jdy8739] Week 10 #997
Changes from 3 commits
Commits
Show all changes
4 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,32 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* function TreeNode(val, left, right) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
*/ | ||
/** | ||
* @param {TreeNode} root | ||
* @return {TreeNode} | ||
*/ | ||
var invertTree = function (root) { | ||
const dfs = (node) => { | ||
if (!node) { | ||
return null; | ||
} | ||
|
||
const temp = node.left; | ||
node.left = node.right; | ||
node.right = temp; | ||
|
||
dfs(node.left); | ||
dfs(node.right); | ||
} | ||
|
||
dfs(root); | ||
|
||
return root; | ||
}; | ||
|
||
// 시간복잡도 O(n) 깊이우선탐색으로 모든 노드를 순회하므로 |
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,28 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var canJump = function (nums) { | ||
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 j = 1; j < nums[i] + 1; j++) { | ||
if (i + j >= nums.length) { | ||
break; | ||
} | ||
|
||
dp[i + j] = true; | ||
} | ||
} | ||
|
||
return dp[dp.length - 1]; | ||
}; | ||
|
||
// 시간복잡도 O(n2) -> nums의 길이대로 순회하면서 1부터 nums[j]의 값까지 순회하는 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. '최대로 어디까지 점프할수 있을까?'라는 관점에서, dp 배열 대신 변수 하나만 사용하는 방법을 찾아보실 수 있지 않을까요? |
||
// 공간복잡도 0(n) -> nums의 길이에 따라 dp 배열의 길이가 결정되므로 |
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
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,67 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number} | ||
*/ | ||
var search = function (nums, target) { | ||
const pivotIndex = findPivot(nums); | ||
|
||
const firstHalfResult = findIndex(nums, 0, pivotIndex - 1, target); | ||
|
||
return firstHalfResult === -1 ? findIndex(nums, pivotIndex, nums.length - 1, target) : firstHalfResult; | ||
}; | ||
|
||
function findIndex(nums, start, end, target) { | ||
while (start <= end) { | ||
// console.log(start, end, target) | ||
const mid = start + Math.floor((end - start) / 2); | ||
|
||
const midValue = nums[mid]; | ||
|
||
if (midValue === target) { | ||
return mid; | ||
} | ||
|
||
if (nums[start] === target) { | ||
return start; | ||
} | ||
|
||
if (nums[end] === target) { | ||
return end; | ||
} | ||
|
||
if (nums[mid] < target) { | ||
start = mid + 1; | ||
} else if (nums[mid] > target) { | ||
end = mid - 1; | ||
} | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
|
||
function findPivot(nums) { | ||
let low = 0; | ||
let high = nums.length - 1; | ||
|
||
while (low <= high) { | ||
const mid = low + Math.floor((high - low) / 2); | ||
|
||
if (0 < mid && nums[mid - 1] > nums[mid]) { | ||
return mid; | ||
} | ||
|
||
if (nums[0] <= nums[mid]) { | ||
low = mid + 1; | ||
} else { | ||
high = mid - 1; | ||
} | ||
|
||
} | ||
|
||
return 0; | ||
} | ||
|
||
// 시간복잡도 O(logn) -> 피봇, 최소값을 찾을 때 이진탐색을 사용하므로 | ||
// 공간복잡도 O(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.
공간 복잡도는 어떻게 될까요 :)