From 59b940b001aed47353dc13fc0a15481064fb5ee5 Mon Sep 17 00:00:00 2001 From: Yeongrok Jeong Date: Thu, 4 May 2023 23:14:04 +0900 Subject: [PATCH 1/2] Create README - LeetHub --- .../README.md | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 yeongrok/0033-search-in-rotated-sorted-array/README.md diff --git a/yeongrok/0033-search-in-rotated-sorted-array/README.md b/yeongrok/0033-search-in-rotated-sorted-array/README.md new file mode 100644 index 0000000..f3ffefa --- /dev/null +++ b/yeongrok/0033-search-in-rotated-sorted-array/README.md @@ -0,0 +1,37 @@ +

33. Search in Rotated Sorted Array

Medium


Can you solve this real interview question? Search in Rotated Sorted Array - There is an integer array nums sorted in ascending order (with distinct values). + +Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. + +Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. + +You must write an algorithm with O(log n) runtime complexity. + +  + +Example 1: + +Input: nums = [4,5,6,7,0,1,2], target = 0 +Output: 4 + + +Example 2: + +Input: nums = [4,5,6,7,0,1,2], target = 3 +Output: -1 + + +Example 3: + +Input: nums = [1], target = 0 +Output: -1 + + +  + +Constraints: + + * 1 <= nums.length <= 5000 + * -104 <= nums[i] <= 104 + * All values of nums are unique. + * nums is an ascending array that is possibly rotated. + * -104 <= target <= 104 \ No newline at end of file From a9a078fdb9a149896cf64e86839d4411f37b53ca Mon Sep 17 00:00:00 2001 From: Yeongrok Jeong Date: Thu, 4 May 2023 23:14:06 +0900 Subject: [PATCH 2/2] Time: 70 ms (12.8%), Space: 43.7 MB (7.20%) - LeetHub --- .../0033-search-in-rotated-sorted-array.js | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 yeongrok/0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.js diff --git a/yeongrok/0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.js b/yeongrok/0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.js new file mode 100644 index 0000000..0ab43ae --- /dev/null +++ b/yeongrok/0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.js @@ -0,0 +1,35 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +var search = function(nums, target) { + if (nums.length === 1) return nums[0] === target ? 0 : -1; + + // 정렬 + let start = 0; + if (nums[0] > nums[nums.length - 1]) { + while (nums[start] < nums[start+1]) start++; + start++; + nums = [...nums.slice(start), ...nums.slice(0, start)]; + } + + // 절반씩 잘라가며 찾기 + var _search = (arr, startIndexOfOrigin) => { + const { length: l } = arr; + if (l === 0) return -1; + if (l === 1) return arr[0] === target ? startIndexOfOrigin : -1; + + const m = Math.floor(l / 2); + if (arr[m] < target) { + return _search(arr.slice(m + 1), startIndexOfOrigin + m + 1); + } else if (arr[m] === target) { + return startIndexOfOrigin + m; + } else { + return _search(arr.slice(0, m), startIndexOfOrigin); + } + } + + const v = _search(nums, 0); + return v === -1 ? -1 : start === 0 ? v : (v + start) % nums.length; +}; \ No newline at end of file