Skip to content

Commit 1ffd5f4

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 18.3 MB (7.20%)
1 parent dd85464 commit 1ffd5f4

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<p>There is an integer array <code>nums</code> sorted in ascending order (with <strong>distinct</strong> values).</p>
2+
3+
<p>Prior to being passed to your function, <code>nums</code> is <strong>possibly rotated</strong> at an unknown pivot index <code>k</code> (<code>1 &lt;= k &lt; nums.length</code>) such that the resulting array is <code>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]</code> (<strong>0-indexed</strong>). For example, <code>[0,1,2,4,5,6,7]</code> might be rotated at pivot index <code>3</code> and become <code>[4,5,6,7,0,1,2]</code>.</p>
4+
5+
<p>Given the array <code>nums</code> <strong>after</strong> the possible rotation and an integer <code>target</code>, return <em>the index of </em><code>target</code><em> if it is in </em><code>nums</code><em>, or </em><code>-1</code><em> if it is not in </em><code>nums</code>.</p>
6+
7+
<p>You must write an algorithm with <code>O(log n)</code> runtime complexity.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
<pre><strong>Input:</strong> nums = [4,5,6,7,0,1,2], target = 0
12+
<strong>Output:</strong> 4
13+
</pre><p><strong class="example">Example 2:</strong></p>
14+
<pre><strong>Input:</strong> nums = [4,5,6,7,0,1,2], target = 3
15+
<strong>Output:</strong> -1
16+
</pre><p><strong class="example">Example 3:</strong></p>
17+
<pre><strong>Input:</strong> nums = [1], target = 0
18+
<strong>Output:</strong> -1
19+
</pre>
20+
<p>&nbsp;</p>
21+
<p><strong>Constraints:</strong></p>
22+
23+
<ul>
24+
<li><code>1 &lt;= nums.length &lt;= 5000</code></li>
25+
<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
26+
<li>All values of <code>nums</code> are <strong>unique</strong>.</li>
27+
<li><code>nums</code> is an ascending array that is possibly rotated.</li>
28+
<li><code>-10<sup>4</sup> &lt;= target &lt;= 10<sup>4</sup></code></li>
29+
</ul>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Approach 1: Find Pivot Index + Binary Search
2+
3+
# Time: O(log n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def search(self, nums: List[int], target: int) -> int:
8+
n = len(nums)
9+
left, right = 0, n - 1
10+
11+
# Find the index of the pivot element (the smallest element)
12+
while left <= right:
13+
mid = (left + right) // 2
14+
if nums[mid] > nums[-1]:
15+
left = mid + 1
16+
else:
17+
right = mid - 1
18+
19+
# Binary search over an inclusive range [left_boundary ~ right_boundary]
20+
def binary_search(left_boundary, right_boundary, target):
21+
left, right = left_boundary, right_boundary
22+
while left <= right:
23+
mid = (left + right) // 2
24+
if nums[mid] == target:
25+
return mid
26+
elif nums[mid] > target:
27+
right = mid - 1
28+
else:
29+
left = mid + 1
30+
return -1
31+
32+
# Binary search over elements on the pivot element's left
33+
if (answer := binary_search(0, left - 1, target)) != -1:
34+
return answer
35+
36+
# Binary search over elements on the pivot element's right
37+
return binary_search(left, n - 1, target)
38+

0 commit comments

Comments
 (0)