Skip to content

Commit 3aa9a18

Browse files
authored
Update 17-00153-find-minimum-in-rotated-sorted-array.py
1 parent 8092108 commit 3aa9a18

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

05-Binary Search/17-00153-find-minimum-in-rotated-sorted-array.py

+68
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,47 @@
1+
# Suppose an array of length n sorted in ascending order is rotated between 1 and n times.
2+
# For example, the array nums = [0,1,2,4,5,6,7] might become:
13

4+
# [4,5,6,7,0,1,2] if it was rotated 4 times.
5+
# [0,1,2,4,5,6,7] if it was rotated 7 times.
6+
# Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
7+
8+
# Given the sorted rotated array nums of unique elements, return the minimum element of this array.
9+
# You must write an algorithm that runs in O(log n) time.
10+
11+
# Example 1:
12+
# Input: nums = [3,4,5,1,2]
13+
# Output: 1
14+
# Explanation: The original array was [1,2,3,4,5] rotated 3 times.
15+
16+
# Example 2:
17+
# Input: nums = [4,5,6,7,0,1,2]
18+
# Output: 0
19+
# Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
20+
21+
# Example 3:
22+
# Input: nums = [11,13,15,17]
23+
# Output: 11
24+
# Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
25+
26+
27+
# Constraints:
28+
# n == nums.length
29+
# 1 <= n <= 5000
30+
# -5000 <= nums[i] <= 5000
31+
# All the integers of nums are unique.
32+
# nums is sorted and rotated between 1 and n times.
33+
34+
# --------------------------------------------------------------------------------------------------------------------------------------
35+
36+
# array is rotated but still sorted, so we can use the sorted property.
37+
# there are 2 sorted arrays- left and right
38+
# find m by taking the midpoint of l and m(floor division)
39+
# when we are in left array, try to search right as that might have smaller elements;
40+
# so when m >= l that means we are in left array; so l = m+1(we are trying to move right)
41+
# when we are in right array, try to search left as the left side in right array will have smaller elements
42+
# so when m < l that means we are in right array; so r = m-1(we are trying to move left)
43+
# when we are in an array that is sorted, this algorithm won't work, so search for the leftmost element and compare with res
44+
# if that l is smaller than res, that is the o/p and the loop will break
245

346
class Solution:
447
def findMin(self, nums: List[int]) -> int:
@@ -19,3 +62,28 @@ def findMin(self, nums: List[int]) -> int:
1962
r = m - 1
2063

2164
return res
65+
66+
Time Complexity = O(logn)
67+
Space Complexity = O(1)
68+
69+
70+
# Companies:
71+
# Meta- 4
72+
# Tiktok- 4
73+
# Google- 3
74+
# Apple- 2
75+
# Microsoft- 7
76+
# Amazon- 6
77+
# Goldman Sachs- 3
78+
# Adobe- 7
79+
# Bloomberg- 6
80+
# Yahoo- 4
81+
# Yandex- 3
82+
# Paypal- 3
83+
# eBay- 3
84+
# ServiceNow- 3
85+
# ByteDance- 2
86+
# Tesla- 2
87+
# Uber- 2
88+
89+

0 commit comments

Comments
 (0)