From 4f1d3a882c81905375810f0099af7177637528e3 Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sat, 19 Oct 2024 17:41:44 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=EB=AC=B8=EC=A0=9C=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- invert-binary-tree/hwanmini.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 invert-binary-tree/hwanmini.js diff --git a/invert-binary-tree/hwanmini.js b/invert-binary-tree/hwanmini.js new file mode 100644 index 00000000..9d6880a2 --- /dev/null +++ b/invert-binary-tree/hwanmini.js @@ -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 +}; \ No newline at end of file From 65737c29b78e7e99d0550097a2b37a4a76ec9f53 Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sat, 19 Oct 2024 17:42:36 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=EB=AC=B8=EC=A0=9C=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- invert-binary-tree/hwanmini.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/invert-binary-tree/hwanmini.js b/invert-binary-tree/hwanmini.js index 9d6880a2..bc09c3a6 100644 --- a/invert-binary-tree/hwanmini.js +++ b/invert-binary-tree/hwanmini.js @@ -27,4 +27,4 @@ var invertTree = function(root) { invertTree(right) return root -}; \ No newline at end of file +}; From dd3f08ff7f8a52875167e0eb26ab6a70d375f6cf Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sun, 20 Oct 2024 01:24:00 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=EB=AC=B8=EC=A0=9C=ED=92=80?= =?UTF-8?q?=EC=9D=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jump-game/hwanmini.js | 20 +++++++++++++ search-in-rotated-sorted-array/hwanmini.js | 34 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 jump-game/hwanmini.js create mode 100644 search-in-rotated-sorted-array/hwanmini.js diff --git a/jump-game/hwanmini.js b/jump-game/hwanmini.js new file mode 100644 index 00000000..24be1fb4 --- /dev/null +++ b/jump-game/hwanmini.js @@ -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 +}; diff --git a/search-in-rotated-sorted-array/hwanmini.js b/search-in-rotated-sorted-array/hwanmini.js new file mode 100644 index 00000000..40a319d7 --- /dev/null +++ b/search-in-rotated-sorted-array/hwanmini.js @@ -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 +};