From fc1a1ed95f19a3106c5b9b6fd203981e14ce04ba Mon Sep 17 00:00:00 2001 From: Yeongrok Jeong Date: Tue, 9 May 2023 23:12:41 +0900 Subject: [PATCH 1/2] Create README - LeetHub --- .../README.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 yeongrok/0034-find-first-and-last-position-of-element-in-sorted-array/README.md diff --git a/yeongrok/0034-find-first-and-last-position-of-element-in-sorted-array/README.md b/yeongrok/0034-find-first-and-last-position-of-element-in-sorted-array/README.md new file mode 100644 index 0000000..5e21a9e --- /dev/null +++ b/yeongrok/0034-find-first-and-last-position-of-element-in-sorted-array/README.md @@ -0,0 +1,34 @@ +

34. Find First and Last Position of Element in Sorted Array

Medium


Can you solve this real interview question? Find First and Last Position of Element in Sorted Array - Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value. + +If target is not found in the array, return [-1, -1]. + +You must write an algorithm with O(log n) runtime complexity. + +  + +Example 1: + +Input: nums = [5,7,7,8,8,10], target = 8 +Output: [3,4] + + +Example 2: + +Input: nums = [5,7,7,8,8,10], target = 6 +Output: [-1,-1] + + +Example 3: + +Input: nums = [], target = 0 +Output: [-1,-1] + + +  + +Constraints: + + * 0 <= nums.length <= 105 + * -109 <= nums[i] <= 109 + * nums is a non-decreasing array. + * -109 <= target <= 109 \ No newline at end of file From fcc93393ea39b90103241480b6e23320d9c596a4 Mon Sep 17 00:00:00 2001 From: Yeongrok Jeong Date: Tue, 9 May 2023 23:12:43 +0900 Subject: [PATCH 2/2] Time: 70 ms (11.4%), Space: 42.4 MB (66.35%) - LeetHub --- ...ast-position-of-element-in-sorted-array.js | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 yeongrok/0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.js diff --git a/yeongrok/0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.js b/yeongrok/0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.js new file mode 100644 index 0000000..c946616 --- /dev/null +++ b/yeongrok/0034-find-first-and-last-position-of-element-in-sorted-array/0034-find-first-and-last-position-of-element-in-sorted-array.js @@ -0,0 +1,34 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var searchRange = function(nums, target) { + + var _searchRange = function(nums, target, originStartIndex) { + const l = nums.length; + if (l === 0) return [-1, -1]; + if (l === 1) { + return nums[0] === target + ? [originStartIndex, originStartIndex] + : [-1, -1]; + } + + const mid = Math.floor((l-1)/2); + if (nums[mid] === target) { + let min = mid; + let max = mid; + + while (min > 0 && nums[min] === nums[min-1]) min--; + while (max < l - 1 && nums[max] === nums[max+1]) max++; + + return [originStartIndex + min, originStartIndex + max]; + } + + return nums[mid] > target + ? _searchRange(nums.slice(0, mid), target, originStartIndex) + : _searchRange(nums.slice(mid + 1), target, originStartIndex + mid + 1) + }; + + return _searchRange(nums, target, 0); +}; \ No newline at end of file