-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jdy8739] Week 10 #997
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
Changes from 4 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,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) 깊이우선탐색으로 모든 노드를 순회하므로 |
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 배열 대신 변수 하나만 사용하는 방법을 찾아보실 수 있지 않을까요? 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. 넵넵 풀이를 봤는데 그런 방법도 있더라고요. 다음 문제 풀이 때 적용해보도록 하겠습니다. 코멘트 감사합니다! :) |
||
// 공간복잡도 0(n) -> nums의 길이에 따라 dp 배열의 길이가 결정되므로 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode[]} lists | ||
* @return {ListNode} | ||
*/ | ||
var mergeKLists = function (lists) { | ||
const arr = []; | ||
|
||
for (let i = 0; i < lists.length; i++) { | ||
let list = lists[i]; | ||
|
||
while (list) { | ||
arr.push(list.val); | ||
list = list.next; | ||
} | ||
} | ||
|
||
if (!arr.length) { | ||
return null; | ||
} | ||
|
||
arr.sort((a, b) => a - b); | ||
|
||
const first = new ListNode(arr[0]); | ||
|
||
let node = first; | ||
|
||
for (let j = 1; j < arr.length; j++) { | ||
const next = new ListNode(arr[j]); | ||
node.next = next; | ||
node = next; | ||
} | ||
|
||
return first; | ||
}; | ||
|
||
// 시간복잡도 -> for문 이후 sort 이후 for문을 수행하며 이는 O(n) + O(nlogn) + O(n)이므로 O(nlogn)가 시간복잡도가 된다 | ||
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. 중요한 내용은 아닙니다!
|
||
// 공간복잡도 -> lists의 노드 수 만큼 길이가 결정되므로 0(n) |
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) -> 고정된 변수 사용 이외에는 동적인 배열, 객체를 사용하지 않으므로 |
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.
공간 복잡도는 어떻게 될까요 :)
Uh oh!
There was an error while loading. Please reload this page.
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.
이후 커밋에 작성되어 있습니다. 코멘트 감사합니다! :)