LeetCode 153. Find Minimum in Rotated Sorted Array (Algorithm Explained)

  Рет қаралды 44,752

Nick White

Nick White

4 жыл бұрын

The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/software-develo...
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.dev/courses/nick
AlgoCademy - algocademy.com/?referral=nick...
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/nickwhite
Follow Me on X/Twitter - x.com/nickwhitereal
Follow My Instagram - / nickwwhite
Other Social Media
----------------------------------------------
Discord - / discord
Twitch - / nickwhitettv
TikTok - / nickwhitetiktok
LinkedIn - / nicholas-w-white
Show Support
------------------------------------------------------------------------------
Patreon - / nick_white
PayPal - paypal.me/nickwwhite?locale.x...
Become A Member - / @nickwhite
#coding #programming #softwareengineering

Пікірлер: 84
@mehroosali
@mehroosali 3 жыл бұрын
More simpler code - public static int findMin(int[] nums) { int left = 0, right = nums.length - 1; while (left < right) { int mid = left + (right - left) / 2; if(nums[mid] < nums[right]) { right = mid; } else { left = mid+1; } } return nums[left]; }
@barkaleamol9394
@barkaleamol9394 2 жыл бұрын
OMG!!!!!!!!!!!!!! Bro, you are too brilliant, I spent a full day and tried with so many types. how you did it. I am a new coder but I loved your approach man thankssssss!
@西雅图UrbanEdge整屋家具
@西雅图UrbanEdge整屋家具 2 жыл бұрын
It is great. But Nick's solution has slightly better time complexity.
@iRockTheBeat1
@iRockTheBeat1 2 жыл бұрын
The problem requires you to solve it in O(log n) time not O(n) time.
@iRockTheBeat1
@iRockTheBeat1 2 жыл бұрын
@likhith chinnam My bad, I thought he wasn't using binary search. This solution is still O(logn).
@enigma8549
@enigma8549 Жыл бұрын
you are og
@kaanatacan1759
@kaanatacan1759 4 жыл бұрын
The explanation was pretty good, just don't worry and keep going to make videos. Normally, I don't write comments but thank you man, really!
@zishanshaikh3068
@zishanshaikh3068 4 жыл бұрын
Hey Nick, Great explanation, Thank you for making these videos, they are really helpful, please keep making more videos.
@AndrewCJXing
@AndrewCJXing 3 жыл бұрын
Hey Nick thanks for the vid. The logic of binary search is pretty straightforward but it is the small details that sometimes throw things off, like why nums[left]
@kshitizsharma2923
@kshitizsharma2923 Жыл бұрын
ig its for the case where midpoint can be same as the left index .
@irinasaghoian5920
@irinasaghoian5920 2 жыл бұрын
Thank you for keeping it short in the intro we appreciate it!
@MichaelSalo
@MichaelSalo 3 жыл бұрын
The loop condition (left < right) isn’t explained as different than a regular binary search.
@eugbyte1822
@eugbyte1822 2 жыл бұрын
anyone can explain?
@mdsaifhussainiqbal2236
@mdsaifhussainiqbal2236 2 жыл бұрын
@@eugbyte1822 the whole point is to make l=r because when l=r then while loop break and the single element left and that is your minimum number
@doruwyl
@doruwyl 4 жыл бұрын
Hi Nick! Probably got unnoticed, but your LinkedIn URL is incomplete. (from video description)
@aaronbrown3820
@aaronbrown3820 4 жыл бұрын
Hi Nick, I liked this one, it's clean and 100 percent faster
@NickWhite
@NickWhite 4 жыл бұрын
Aaron Brown hell yeah man make sure to throw me a like so the algorithm promotes me
@aaronbrown3820
@aaronbrown3820 4 жыл бұрын
@@NickWhite I did haha! I made sure to before the video ended. I noticed that there were zero likes and it would be wrong for me to comment and not hit that thumbs up
@NickWhite
@NickWhite 4 жыл бұрын
You’re a good man
@f3-faithfitnessfinance
@f3-faithfitnessfinance 4 жыл бұрын
@@NickWhite and you are a great man
@mohitmotwani9256
@mohitmotwani9256 4 жыл бұрын
Nice work!! More staright forward code (c++) class Solution { public: int findMin(vector& nums) { int n = nums.size(); if(n == 0) return -1; int mid; int low = 0; int high = n -1 ; while(low < high) { mid = low + (high - low)/2; if(nums[mid] > nums[high]) low = mid+1; else high = mid; } return nums[high]; } };
@anuradhapandey2173
@anuradhapandey2173 3 жыл бұрын
Can you please explain why you returned nums[high]??
@mohitmotwani9256
@mohitmotwani9256 3 жыл бұрын
@@anuradhapandey2173 because if you see in the else part, we assign high as mid. Also, if you dry run this algo you will get to know why high.
@sgrpnwr
@sgrpnwr 2 жыл бұрын
There are too many unnecessary conditions here... It is understood that if the left part of the array is sorted, that will mean that right part of the array is unsorted and vice versa. And rather than checking the adjacent variable, we can just keep moving the left index towards unsorted array and return the low at the end. Check the code below: var findMin = function(arr) { let low=0; let high=arr.length-1; while(lowarr[high]){ low=mid+1 } else{ high=mid } } return arr[low] };
@bhavyasharma4863
@bhavyasharma4863 Жыл бұрын
but if the array is not rotated at all then we will need the conditions
@poorpanda9033
@poorpanda9033 11 ай бұрын
@@bhavyasharma4863 yes makes sense !
@GauravSingh-ku5xy
@GauravSingh-ku5xy 2 жыл бұрын
Need a little help. I have a similar solution based on binary search. Until the commented line (Main program starts here), I have taken care of all the edge cases. After that, it's just a normal binary search program modified for this problem. But in the test case [3, 4, 5, 1, 2] it says that the function does not return any value. But if I dry run it, it does. What am I doing wrong? int findMin(vector& nums) { int n = nums.size(), left, right, mid; if(nums.size()==1){ return nums[0]; } if(nums[0]
@xav8812
@xav8812 10 ай бұрын
This is how we need to really think about when solving the question on leetcode by specific solution on specific questions, not just using the general solving method to submit all of similar questions.
@ALEEMKHAWAR1
@ALEEMKHAWAR1 2 жыл бұрын
pretty good explanation nick !!!
@rupalitiwari5012
@rupalitiwari5012 4 жыл бұрын
Code doesn't work for input [3,3,1,3]
@ayansrivastava731
@ayansrivastava731 2 жыл бұрын
def binary(A,st,en): while(True): mid=(st+en)//2 if(A[mid]
@jaitehkaba8753
@jaitehkaba8753 3 жыл бұрын
Nick, you are a star!!! I have my interview with Microsoft next week. Pray for me.
@saldanaswiz1291
@saldanaswiz1291 3 жыл бұрын
how'd it go
@jaitehkaba8753
@jaitehkaba8753 3 жыл бұрын
@@saldanaswiz1291 I just finished my internship at Microsoft today and got a return offer. I still come to this site just for fun.
@transam351
@transam351 2 жыл бұрын
@@jaitehkaba8753 What TC did they offer you?
@cwagnello
@cwagnello 4 жыл бұрын
How do you know the value will be at index "left"?
@dp4kallday
@dp4kallday 3 жыл бұрын
As you keep dividing the array in half, you eventually get an array with a single element which would be nums[0] and left is originally set to that value.
@prakanshmishra9004
@prakanshmishra9004 3 жыл бұрын
@@dp4kallday Just wanted to point that what you stated is wrong. The array after getting divided by two and becoming smaller doesn't end at nums[0], but actually at any nums[i], where 0
@putinscat1208
@putinscat1208 2 жыл бұрын
Personally, if I got down to 2 or 3 elements in an array, I would just find the min in that array. This would solve having some complex code to determine the middle on a 2 element array. A couple of important assumptions with this question, 0 is the lowest int allowed, and the original array is sorted low to high before rotation. I would love to know what the point of this is in real life?
@Nishant-qw7jk
@Nishant-qw7jk 4 ай бұрын
Hey that's a great explanation
@dev-skills
@dev-skills 3 жыл бұрын
No need for line 3 to check `if (nums.length == 0) return -1` as the problem specifies 1
@shaanyahoo
@shaanyahoo 3 жыл бұрын
last 10 seconds was the best :)
@raj_kundalia
@raj_kundalia 10 ай бұрын
thank you!
@shivanshupandey3637
@shivanshupandey3637 Жыл бұрын
very good explanation
@kaaviyabaskaran1041
@kaaviyabaskaran1041 4 жыл бұрын
Hi, first of all thanks for this video, it really made me understand the concept well. However, when I executed this on Leetcode, I am getting an error that the time limit exceeded. Could you explain why?
@gtacarlito
@gtacarlito 4 жыл бұрын
Thanks for the algo, but does it work when our input is [2,1] ???
@KNukzzz
@KNukzzz 4 жыл бұрын
No it does not
@ALifeExp
@ALifeExp 4 жыл бұрын
This is a not a pivoted array, it's just a descending ordered array so it's not in the scope of this exercise ;)
@rishikeshpuri740
@rishikeshpuri740 3 жыл бұрын
thnx bro for this
@ZhouHaibo
@ZhouHaibo 3 жыл бұрын
A simper version. --------------------------------------------------- public int findMin(int[] nums) { int left = 0; int right = nums.length - 1; while (left < right) { int mid = left + (right-left) / 2; if (nums[mid] < nums[right]) { right = mid; } else { left = mid + 1; } } return nums[left]; }
@emmanuelmiramonvazquez9591
@emmanuelmiramonvazquez9591 2 жыл бұрын
9:35 same thought here 😂
@usa5450
@usa5450 Жыл бұрын
Best explanation of this problem No BS and so crisp 👍🏻
@jasonahn8658
@jasonahn8658 Жыл бұрын
Getting index -1 out of bounds for length 2
@SayanKarmakar12
@SayanKarmakar12 3 жыл бұрын
Believe me, it was a very good explanation!
@bayareamountainbiker
@bayareamountainbiker Жыл бұрын
Why can't it be just Arrays.sort(nums); return nums[0]?
@xav8812
@xav8812 10 ай бұрын
it will be in O(nlogn), question asked to do in O(logn)
@bhavyasharma4863
@bhavyasharma4863 Жыл бұрын
hey good explanation by the way you look like shown murfy from the good doctor
@tanson86
@tanson86 Жыл бұрын
Why don't you just identify the pivot and return the number at the pivot. Finding pivot is just like how you did it for leetcode 33.
@transam351
@transam351 2 жыл бұрын
You need sleep man
@siddharth_chatterjee
@siddharth_chatterjee 2 жыл бұрын
hmm, nice one
@deepakanuraagsathyanarayan9666
@deepakanuraagsathyanarayan9666 4 жыл бұрын
i feel binary search is too hard to simulate and write the edge cases did u see that he returned nums[left] why? i am figuring it out right now
@rudyyyxu
@rudyyyxu 4 жыл бұрын
left and right becomes the same thing, which is the last element left
@parthkumar1033
@parthkumar1033 4 жыл бұрын
If the array is not rotated simply then nums[left] should be min.
@ayushsharma8950
@ayushsharma8950 3 жыл бұрын
Hey can't I use arrays.sort in it and print the element at 0 index???
@dossymzhankudaibergenov8193
@dossymzhankudaibergenov8193 3 жыл бұрын
It’s time complexity is O(nlogn), this problem requires O(logn)
@mujtabahussain7015
@mujtabahussain7015 2 жыл бұрын
if it weren't for O(log(N)), then ideal solution should have been linear search, sorting and then searching is very slow and inefficient.
@sarveshzeke7255
@sarveshzeke7255 2 жыл бұрын
not successfull in leetcode now . idk why !
@justiceessiel6123
@justiceessiel6123 2 жыл бұрын
so who as the thought of just doing min(nums) that was my brute force😂.. its more efficient than this
@SandyRocks007
@SandyRocks007 4 жыл бұрын
Why not use HashSet ? I am new to programming import java.util.*; class Solution { public int findMin(int[] nums) { HashSet h = new HashSet(); for(int x : nums){ h.add(x); } System.out.println(h); return (Integer)Collections.min(h); } }
@NoWarForever
@NoWarForever 4 жыл бұрын
Try to analyze space and time of your algorithm and you will know the reason :)
@bhavuks6554
@bhavuks6554 4 жыл бұрын
HashSet will have O(N) worst case time complexity {where N = number of elements in the array} whereas Binary search will do it in O(LogN). Hope that helps.
LeetCode 56. Merge Intervals (Algorithm Explained)
12:57
Nick White
Рет қаралды 89 М.
لااا! هذه البرتقالة مزعجة جدًا #قصير
00:15
One More Arabic
Рет қаралды 15 МЛН
IQ Level: 10000
00:10
Younes Zarou
Рет қаралды 11 МЛН
Пранк пошел не по плану…🥲
00:59
Саша Квашеная
Рет қаралды 7 МЛН
EVOLUTION OF ICE CREAM 😱 #shorts
00:11
Savage Vlogs
Рет қаралды 12 МЛН
LeetCode 36. Valid Sudoku (Algorithm Explained)
11:33
Nick White
Рет қаралды 98 М.
LeetCode 48. Rotate Image (Solution Explained)
10:18
Nick White
Рет қаралды 84 М.
LeetCode 33. Search in Rotated Sorted Array
9:30
Nick White
Рет қаралды 97 М.
LeetCode 146. LRU Cache (Algorithm Explained)
18:00
Nick White
Рет қаралды 116 М.
BS-6. Minimum in Rotated Sorted Array
17:08
take U forward
Рет қаралды 147 М.
Medium Google Coding Interview With Ben Awad
51:27
Clément Mihailescu
Рет қаралды 1,2 МЛН
Binary Search Algorithm - Computerphile
18:34
Computerphile
Рет қаралды 158 М.
#samsung #retrophone #nostalgia #x100
0:14
mobijunk
Рет қаралды 14 МЛН
iPhone socket cleaning #Fixit
0:30
Tamar DB (mt)
Рет қаралды 18 МЛН
Мой новый мега монитор!🤯
1:00
Корнеич
Рет қаралды 907 М.
Bluetooth connected successfully 💯💯
0:16
Blue ice Comedy
Рет қаралды 1,7 МЛН
Tag him😳💕 #miniphone #iphone #samsung #smartphone #fy
0:11
Pockify™
Рет қаралды 4,6 МЛН