From dd3f08ff7f8a52875167e0eb26ab6a70d375f6cf Mon Sep 17 00:00:00 2001 From: JEONGHWANMIN Date: Sun, 20 Oct 2024 01:24:00 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=AC=B8=EC=A0=9C=ED=92=80=EC=9D=B4=20?= =?UTF-8?q?=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 000000000..24be1fb47 --- /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 000000000..40a319d7e --- /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 +};