Search in Rotated Sorted Array - Leetcode 33 - Binary Search (Python)

preview_player
Показать описание


Please check my playlists for free DSA problem solutions:

Best Courses for Analytics:
---------------------------------------------------------------------------------------------------------

Best Courses for Programming:
---------------------------------------------------------------------------------------------------------

Best Courses for Machine Learning:
---------------------------------------------------------------------------------------------------------

Best Courses for Statistics:
---------------------------------------------------------------------------------------------------------

Best Courses for Big Data:
---------------------------------------------------------------------------------------------------------

More Courses:
---------------------------------------------------------------------------------------------------------

Full Disclosure:
Please note that I may earn a commission for purchases made at the above sites! I strongly believe in the material provided; I only recommend what I truly think is great. If you do choose to make purchases through these links; thank you for supporting the channel, it helps me make more free content like this!
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

this was a very tricky problem for me, but your explanation is incredibly clear, thanks!

christianjt
Автор

Isn't the 3rd case m, n-1 identical to edge case where m is position 0 ? 0, n-1 ? So why is 0, n-1 an edge case?

KurtSchwind
Автор

To avoid all the extra conditions, you can also set left = min_index and right = min_index + nums.length, ofcourse while accessing those indices will be out of bounds of the array, so while accessing real item indices you can just do "nums[<left or right or mid>] mod nums.length" and when its time to update the left or right bounds, use the "fake" indices.

saurabhbhagat
Автор

first = 0
last = len(nums) - 1
while first <= last:
middle = (first + last) // 2
if nums[middle] == target:
return middle
if nums[first] <= nums[middle]:
if nums[first] <= target < nums[middle]:
last = middle - 1
else:
first = middle + 1

else:
if nums[middle] < target <= nums[last]:
first = middle + 1
else:
last = middle - 1
return -1

Instead of finding the minimum value, we can also use the same method of finding the middle value and continue searching for the target on either sides of the middle value.

Chathur
Автор

Hi greg,

I am your new subscriber from India. Love your content.

I need a suggestion, Is DSA mandatory to learn if we want to get into data science field?

mohammedyousufbaba
Автор

you should create a separate playlist for all your leetcode videos

EchoesOfSurrealism