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]; }
@barkaleamol93943 жыл бұрын
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整屋家具2 жыл бұрын
It is great. But Nick's solution has slightly better time complexity.
@iRockTheBeat12 жыл бұрын
The problem requires you to solve it in O(log n) time not O(n) time.
@iRockTheBeat12 жыл бұрын
@likhith chinnam My bad, I thought he wasn't using binary search. This solution is still O(logn).
@enigma85492 жыл бұрын
you are og
@mohitmotwani92564 жыл бұрын
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]; } };
@anuradhapandey21734 жыл бұрын
Can you please explain why you returned nums[high]??
@mohitmotwani92564 жыл бұрын
@@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.
@kaanatacan17595 жыл бұрын
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!
@MichaelSalo4 жыл бұрын
The loop condition (left < right) isn’t explained as different than a regular binary search.
@eugbyte18223 жыл бұрын
anyone can explain?
@mdsaifhussainiqbal22363 жыл бұрын
@@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 Жыл бұрын
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.
@AndrewCJXing3 жыл бұрын
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 Жыл бұрын
ig its for the case where midpoint can be same as the left index .
@blazer5115 жыл бұрын
Hi Nick, I liked this one, it's clean and 100 percent faster
@NickWhite5 жыл бұрын
Aaron Brown hell yeah man make sure to throw me a like so the algorithm promotes me
@blazer5115 жыл бұрын
@@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
@NickWhite5 жыл бұрын
You’re a good man
@f3-faithfitnessfinance5 жыл бұрын
@@NickWhite and you are a great man
@sgrpnwr3 жыл бұрын
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 Жыл бұрын
but if the array is not rotated at all then we will need the conditions
@poorpanda9033 Жыл бұрын
@@bhavyasharma4863 yes makes sense !
@irinasaghoian59202 жыл бұрын
Thank you for keeping it short in the intro we appreciate it!
@jaitehkaba87534 жыл бұрын
Nick, you are a star!!! I have my interview with Microsoft next week. Pray for me.
@saldanaswiz12913 жыл бұрын
how'd it go
@jaitehkaba87533 жыл бұрын
@@saldanaswiz1291 I just finished my internship at Microsoft today and got a return offer. I still come to this site just for fun.
@transam3513 жыл бұрын
@@jaitehkaba8753 What TC did they offer you?
@rupalitiwari50124 жыл бұрын
Code doesn't work for input [3,3,1,3]
@cwagnello4 жыл бұрын
How do you know the value will be at index "left"?
@dp4kallday3 жыл бұрын
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.
@prakanshmishra90043 жыл бұрын
@@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
@ZhouHaibo3 жыл бұрын
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-skills3 жыл бұрын
No need for line 3 to check `if (nums.length == 0) return -1` as the problem specifies 1
@doruwyl5 жыл бұрын
Hi Nick! Probably got unnoticed, but your LinkedIn URL is incomplete. (from video description)
@zishanshaikh30684 жыл бұрын
Hey Nick, Great explanation, Thank you for making these videos, they are really helpful, please keep making more videos.
@ALEEMKHAWAR12 жыл бұрын
pretty good explanation nick !!!
@putinscat12083 жыл бұрын
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-ku5xy3 жыл бұрын
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]
@gtacarlito4 жыл бұрын
Thanks for the algo, but does it work when our input is [2,1] ???
@KNukzzz4 жыл бұрын
No it does not
@ALifeExp4 жыл бұрын
This is a not a pivoted array, it's just a descending ordered array so it's not in the scope of this exercise ;)
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 Жыл бұрын
Why can't it be just Arrays.sort(nums); return nums[0]?
@xav8812 Жыл бұрын
it will be in O(nlogn), question asked to do in O(logn)
@raj_kundalia Жыл бұрын
thank you!
@transam3513 жыл бұрын
You need sleep man
@SayanKarmakar123 жыл бұрын
Believe me, it was a very good explanation!
@emmanuelmiramonvazquez95912 жыл бұрын
9:35 same thought here 😂
@bhavyasharma4863 Жыл бұрын
hey good explanation by the way you look like shown murfy from the good doctor
@usa54502 жыл бұрын
Best explanation of this problem No BS and so crisp 👍🏻
@rishikeshpuri7403 жыл бұрын
thnx bro for this
@kaaviyabaskaran10414 жыл бұрын
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_chatterjee2 жыл бұрын
hmm, nice one
@justiceessiel61232 жыл бұрын
so who as the thought of just doing min(nums) that was my brute force😂.. its more efficient than this
@sarveshzeke72553 жыл бұрын
not successfull in leetcode now . idk why !
@deepakanuraagsathyanarayan96664 жыл бұрын
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
@rudyyyxu4 жыл бұрын
left and right becomes the same thing, which is the last element left
@parthkumar10334 жыл бұрын
If the array is not rotated simply then nums[left] should be min.
@ayushsharma89503 жыл бұрын
Hey can't I use arrays.sort in it and print the element at 0 index???
@dossymzhankudaibergenov81933 жыл бұрын
It’s time complexity is O(nlogn), this problem requires O(logn)
@mujtabahussain70153 жыл бұрын
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.
@SandyRocks0074 жыл бұрын
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); } }
@NoWarForever4 жыл бұрын
Try to analyze space and time of your algorithm and you will know the reason :)
@bhavuks65544 жыл бұрын
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.