-
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 #541 from JEONGHWANMIN/main
[ํ๋ฏธ๋๋] Week10 Solutions
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
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,30 @@ | ||
// ์๊ฐ๋ณต์ก๋: O(n) | ||
// ๊ณต๊ฐ๋ณต์ก๋: O(n) | ||
|
||
/** | ||
* 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) { | ||
if (!root) return null; | ||
|
||
const left = root.left | ||
const right = root.right | ||
|
||
root.left = right | ||
root.right = left | ||
|
||
|
||
invertTree(left) | ||
invertTree(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,20 @@ | ||
// ์๊ฐ๋ณต์ก๋: O(n) | ||
// ๊ณต๊ฐ๋ณต์ก๋: O(1) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var canJump = function(nums) { | ||
let fast = 0; | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
if (i > 0 && i > fast) return false | ||
|
||
fast = Math.max(fast, i + nums[i]) | ||
|
||
if (fast >= nums.length -1) return true | ||
} | ||
|
||
return false | ||
}; |
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,34 @@ | ||
// ์๊ฐ๋ณต์ก๋: O(log n) | ||
// ๊ณต๊ฐ๋ณต์ก๋: O(1) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number} | ||
*/ | ||
var search = function(nums, target) { | ||
let leftIdx = 0; | ||
let rightIdx = nums.length - 1; | ||
|
||
while (leftIdx <= rightIdx) { | ||
const midIdx = Math.floor((leftIdx + rightIdx) / 2); | ||
|
||
if (nums[midIdx] === target) return midIdx; | ||
|
||
if (nums[leftIdx] <= nums[midIdx]) { | ||
if (nums[leftIdx] <= target && nums[midIdx] >= target) { | ||
rightIdx = midIdx - 1; | ||
} else { | ||
leftIdx = midIdx + 1; | ||
} | ||
} else { | ||
if (nums[rightIdx] >= target && nums[midIdx] <= target) { | ||
leftIdx = midIdx + 1; | ||
} else { | ||
rightIdx = midIdx - 1; | ||
} | ||
} | ||
} | ||
|
||
return -1 | ||
}; |