-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #997 from jdy8739/main
[jdy8739] Week 10
- Loading branch information
Showing
5 changed files
with
173 additions
and
1 deletion.
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์ค ๋ฃจํ๋ฅผ ๋๊ธฐ ๋๋ฌธ | ||
// ๊ณต๊ฐ๋ณต์ก๋ 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,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)๊ฐ ์๊ฐ๋ณต์ก๋๊ฐ ๋๋ค | ||
// ๊ณต๊ฐ๋ณต์ก๋ -> lists์ ๋ ธ๋ ์ ๋งํผ ๊ธธ์ด๊ฐ ๊ฒฐ์ ๋๋ฏ๋ก 0(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,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) -> ๊ณ ์ ๋ ๋ณ์ ์ฌ์ฉ ์ด์ธ์๋ ๋์ ์ธ ๋ฐฐ์ด, ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ฏ๋ก |