Skip to content

Commit

Permalink
Merge pull request #541 from JEONGHWANMIN/main
Browse files Browse the repository at this point in the history
[ํ™˜๋ฏธ๋‹ˆ๋‹ˆ] Week10 Solutions
  • Loading branch information
JEONGHWANMIN authored Oct 21, 2024
2 parents b14e4d9 + dd3f08f commit f6c5877
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
30 changes: 30 additions & 0 deletions invert-binary-tree/hwanmini.js
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
};
20 changes: 20 additions & 0 deletions jump-game/hwanmini.js
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
};
34 changes: 34 additions & 0 deletions search-in-rotated-sorted-array/hwanmini.js
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
};

0 comments on commit f6c5877

Please sign in to comment.