diff --git a/find-minimum-in-rotated-sorted-array/hwanmini.js b/find-minimum-in-rotated-sorted-array/hwanmini.js new file mode 100644 index 000000000..c36f3dad2 --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/hwanmini.js @@ -0,0 +1,32 @@ +// 시간복잡도: O(log n) +// 공간복잡도: O(1) + +/** + * @param {number[]} nums + * @return {number} + */ +var findMin = function(nums) { + let leftIdx = 0; + let rightIdx = nums.length - 1; + + if (nums.length === 1) return nums[0] + + while (leftIdx <= rightIdx) { + if (nums[leftIdx] < nums[rightIdx]) return nums[leftIdx] + + let midIdx = Math.floor((leftIdx + rightIdx) / 2); + + if (nums[midIdx] > nums[midIdx+1]) { + return nums[midIdx+1] + } + + if (nums[leftIdx] < nums[midIdx] && nums[leftIdx] > nums[rightIdx]) { + leftIdx = midIdx + } else { + rightIdx = midIdx + } + } + + return nums[0] +}; + diff --git a/linked-list-cycle/hwanmini.js b/linked-list-cycle/hwanmini.js new file mode 100644 index 000000000..6cc49071e --- /dev/null +++ b/linked-list-cycle/hwanmini.js @@ -0,0 +1,29 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(1) + +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ + +/** + * @param {ListNode} head + * @return {boolean} + */ +var hasCycle = function(head) { + let fastPointer = head; + let slowPointer = head; + + while (fastPointer && fastPointer.next) { + slowPointer = slowPointer.next; + fastPointer = fastPointer.next.next + + if (fastPointer === slowPointer) return true + + } + + return false +}; diff --git a/maximum-subarray/hwanmini.js b/maximum-subarray/hwanmini.js new file mode 100644 index 000000000..4da2e8688 --- /dev/null +++ b/maximum-subarray/hwanmini.js @@ -0,0 +1,19 @@ +// 시간복잡도: O(n) +// 공간복잡도: O(1) + +/** + * @param {number[]} nums + * @return {number} + */ +var maxSubArray = function (nums) { + let maxNum = -Infinity; + let prevSum = -Infinity; + + for (let i = 0 ; i < nums.length; i++) { + prevSum = Math.max(prevSum + nums[i], nums[i]) + maxNum = Math.max(prevSum, maxNum) + } + + + return maxNum +};