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

  Рет қаралды 45,685

Nick White

Nick White

Күн бұрын

Пікірлер: 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 3 жыл бұрын
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 2 жыл бұрын
you are og
@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 4 жыл бұрын
Can you please explain why you returned nums[high]??
@mohitmotwani9256
@mohitmotwani9256 4 жыл бұрын
@@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.
@kaanatacan1759
@kaanatacan1759 5 жыл бұрын
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!
@MichaelSalo
@MichaelSalo 4 жыл бұрын
The loop condition (left < right) isn’t explained as different than a regular binary search.
@eugbyte1822
@eugbyte1822 3 жыл бұрын
anyone can explain?
@mdsaifhussainiqbal2236
@mdsaifhussainiqbal2236 3 жыл бұрын
@@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
@xav8812
@xav8812 Жыл бұрын
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.
@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 .
@blazer511
@blazer511 5 жыл бұрын
Hi Nick, I liked this one, it's clean and 100 percent faster
@NickWhite
@NickWhite 5 жыл бұрын
Aaron Brown hell yeah man make sure to throw me a like so the algorithm promotes me
@blazer511
@blazer511 5 жыл бұрын
@@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 5 жыл бұрын
You’re a good man
@f3-faithfitnessfinance
@f3-faithfitnessfinance 5 жыл бұрын
@@NickWhite and you are a great man
@sgrpnwr
@sgrpnwr 3 жыл бұрын
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 Жыл бұрын
@@bhavyasharma4863 yes makes sense !
@irinasaghoian5920
@irinasaghoian5920 2 жыл бұрын
Thank you for keeping it short in the intro we appreciate it!
@jaitehkaba8753
@jaitehkaba8753 4 жыл бұрын
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 3 жыл бұрын
@@jaitehkaba8753 What TC did they offer you?
@rupalitiwari5012
@rupalitiwari5012 4 жыл бұрын
Code doesn't work for input [3,3,1,3]
@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
@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]; }
@dev-skills
@dev-skills 3 жыл бұрын
No need for line 3 to check `if (nums.length == 0) return -1` as the problem specifies 1
@doruwyl
@doruwyl 5 жыл бұрын
Hi Nick! Probably got unnoticed, but your LinkedIn URL is incomplete. (from video description)
@zishanshaikh3068
@zishanshaikh3068 4 жыл бұрын
Hey Nick, Great explanation, Thank you for making these videos, they are really helpful, please keep making more videos.
@ALEEMKHAWAR1
@ALEEMKHAWAR1 2 жыл бұрын
pretty good explanation nick !!!
@putinscat1208
@putinscat1208 3 жыл бұрын
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?
@GauravSingh-ku5xy
@GauravSingh-ku5xy 3 жыл бұрын
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]
@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 ;)
@ayansrivastava731
@ayansrivastava731 2 жыл бұрын
def binary(A,st,en): while(True): mid=(st+en)//2 if(A[mid]
@shaanyahoo
@shaanyahoo 3 жыл бұрын
last 10 seconds was the best :)
@Nishant-qw7jk
@Nishant-qw7jk 7 ай бұрын
Hey that's a great explanation
@jasonahn8658
@jasonahn8658 Жыл бұрын
Getting index -1 out of bounds for length 2
@shivanshupandey3637
@shivanshupandey3637 2 жыл бұрын
very good explanation
@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.
@bayareamountainbiker
@bayareamountainbiker Жыл бұрын
Why can't it be just Arrays.sort(nums); return nums[0]?
@xav8812
@xav8812 Жыл бұрын
it will be in O(nlogn), question asked to do in O(logn)
@raj_kundalia
@raj_kundalia Жыл бұрын
thank you!
@transam351
@transam351 3 жыл бұрын
You need sleep man
@SayanKarmakar12
@SayanKarmakar12 3 жыл бұрын
Believe me, it was a very good explanation!
@emmanuelmiramonvazquez9591
@emmanuelmiramonvazquez9591 2 жыл бұрын
9:35 same thought here 😂
@bhavyasharma4863
@bhavyasharma4863 Жыл бұрын
hey good explanation by the way you look like shown murfy from the good doctor
@usa5450
@usa5450 2 жыл бұрын
Best explanation of this problem No BS and so crisp 👍🏻
@rishikeshpuri740
@rishikeshpuri740 3 жыл бұрын
thnx bro for this
@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?
@siddharth_chatterjee
@siddharth_chatterjee 2 жыл бұрын
hmm, nice one
@justiceessiel6123
@justiceessiel6123 2 жыл бұрын
so who as the thought of just doing min(nums) that was my brute force😂.. its more efficient than this
@sarveshzeke7255
@sarveshzeke7255 3 жыл бұрын
not successfull in leetcode now . idk why !
@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 3 жыл бұрын
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.
@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.
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 117 МЛН
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 21 МЛН
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 22 МЛН
Как Я Брата ОБМАНУЛ (смешное видео, прикол, юмор, поржать)
00:59
LeetCode 48. Rotate Image (Solution Explained)
10:18
Nick White
Рет қаралды 86 М.
BS-6. Minimum in Rotated Sorted Array
17:08
take U forward
Рет қаралды 193 М.
LeetCode 33. Search in Rotated Sorted Array
9:30
Nick White
Рет қаралды 99 М.
Search in rotated sorted array - Leetcode 33 - Python
13:28
NeetCode
Рет қаралды 364 М.
LeetCode 36. Valid Sudoku (Algorithm Explained)
11:33
Nick White
Рет қаралды 101 М.
Binary Search Algorithm - Computerphile
18:34
Computerphile
Рет қаралды 164 М.
LeetCode 238. Product of Array Except Self (Solution Explained)
14:49
BS-4. Search Element in Rotated Sorted Array - I
16:38
take U forward
Рет қаралды 292 М.
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 117 МЛН