Skip to content
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

[환미니니] Week10 Solutions #541

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
};