No video

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

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

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
@zishanshaikh3068
@zishanshaikh3068 4 жыл бұрын
Hey Nick, Great explanation, Thank you for making these videos, they are really helpful, please keep making more videos.
@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!
@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
@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
@irinasaghoian5920
@irinasaghoian5920 2 жыл бұрын
Thank you for keeping it short in the intro we appreciate it!
@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 .
@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
@doruwyl
@doruwyl 4 жыл бұрын
Hi Nick! Probably got unnoticed, but your LinkedIn URL is incomplete. (from video description)
@ALEEMKHAWAR1
@ALEEMKHAWAR1 2 жыл бұрын
pretty good explanation nick !!!
@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]
@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.
@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?
@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
@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?
@ayansrivastava731
@ayansrivastava731 2 жыл бұрын
def binary(A,st,en): while(True): mid=(st+en)//2 if(A[mid]
@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 !
@Nishant-qw7jk
@Nishant-qw7jk 4 ай бұрын
Hey that's a great explanation
@shaanyahoo
@shaanyahoo 3 жыл бұрын
last 10 seconds was the best :)
@dev-skills
@dev-skills 3 жыл бұрын
No need for line 3 to check `if (nums.length == 0) return -1` as the problem specifies 1
@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.
@shivanshupandey3637
@shivanshupandey3637 Жыл бұрын
very good explanation
@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 ;)
@usa5450
@usa5450 Жыл бұрын
Best explanation of this problem No BS and so crisp 👍🏻
@rishikeshpuri740
@rishikeshpuri740 3 жыл бұрын
thnx bro for this
@raj_kundalia
@raj_kundalia 10 ай бұрын
thank you!
@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?
@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)
@SayanKarmakar12
@SayanKarmakar12 3 жыл бұрын
Believe me, it was a very good explanation!
@jasonahn8658
@jasonahn8658 Жыл бұрын
Getting index -1 out of bounds for length 2
@bhavyasharma4863
@bhavyasharma4863 Жыл бұрын
hey good explanation by the way you look like shown murfy from the good doctor
@emmanuelmiramonvazquez9591
@emmanuelmiramonvazquez9591 2 жыл бұрын
9:35 same thought here 😂
@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]; }
@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.
@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
@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 !
@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.
@justiceessiel6123
@justiceessiel6123 2 жыл бұрын
so who as the thought of just doing min(nums) that was my brute force😂.. its more efficient than this
LeetCode 33. Search in Rotated Sorted Array
9:30
Nick White
Рет қаралды 97 М.
Why You Should AVOID Linked Lists
14:12
ThePrimeTime
Рет қаралды 273 М.
Mom's Unique Approach to Teaching Kids Hygiene #shorts
00:16
Fabiosa Stories
Рет қаралды 39 МЛН
Box jumping challenge, who stepped on the trap? #FunnyFamily #PartyGames
00:31
Family Games Media
Рет қаралды 23 МЛН
I'm Excited To see If Kelly Can Meet This Challenge!
00:16
Mini Katana
Рет қаралды 31 МЛН
BS-6. Minimum in Rotated Sorted Array
17:08
take U forward
Рет қаралды 148 М.
Binary Search Algorithm - Computerphile
18:34
Computerphile
Рет қаралды 158 М.
LeetCode 146. LRU Cache (Algorithm Explained)
18:00
Nick White
Рет қаралды 116 М.
Sorting Algorithms Explained Visually
9:01
Beyond Fireship
Рет қаралды 526 М.
Myth of the 10x Developer: Technical Interviews are Broken, (part 2 of n)
17:47
The Algorithm Behind Spell Checkers
13:02
b001
Рет қаралды 409 М.