Next Permutation | Leetcode #31

  Рет қаралды 109,957

Techdose

Techdose

Күн бұрын

Пікірлер: 164
@life_of_anjali
@life_of_anjali 3 жыл бұрын
Great explanation! I think this approach can be modified to make the complexity O(n) by avoiding sorting. After swapping is done, it can be observed that all elements after the left index selected for swapping till the end of the array will be reverse sorted. So, we only need to reverse array after the left swapped index.
@techdose4u
@techdose4u 3 жыл бұрын
Great :)
@splinkerp2222
@splinkerp2222 3 жыл бұрын
Yes this is true
@glenfernandes253
@glenfernandes253 3 жыл бұрын
pin this comment. :)
@niteshkhetan9757
@niteshkhetan9757 3 жыл бұрын
Pin this comment plz
@prakharsankrityayan1300
@prakharsankrityayan1300 3 жыл бұрын
For this to work, nums[i]
@Cocoblu2
@Cocoblu2 3 жыл бұрын
At first I was confused by what the question was actually asking, but watching the whole explanation it became clearer and the approach really made sense. Thanks!
@techdose4u
@techdose4u 3 жыл бұрын
:)
@srikantd6054
@srikantd6054 3 жыл бұрын
So far the best content I have ever seen and best channel for DS and Algo.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@darshansimha2166
@darshansimha2166 3 жыл бұрын
Great explanation, how the hell do we come up with a solution with so many edge cases in an interview where you have 30 minutes at best? 😩
@BiplabSutradhar-c8e
@BiplabSutradhar-c8e 6 ай бұрын
after 2 years how it feels
@bthulasikrishna9597
@bthulasikrishna9597 3 ай бұрын
After 3 years ?
@johnnychang3456
@johnnychang3456 2 ай бұрын
You don't. You simply memorize it.
@SaurabhKumar-qc6og
@SaurabhKumar-qc6og Ай бұрын
That's why you spend hundreds of hours practicing DSA problems
@vcfirefox
@vcfirefox 3 жыл бұрын
This is absolutely the best explanation I found after almost getting paralyzed by the question. Thanks!
@ShubhamYadav-dd4ej
@ShubhamYadav-dd4ej 2 ай бұрын
YES! THIS QUESTION IS VERY CONFUSING
@parthokr
@parthokr 3 жыл бұрын
That graph representation just blew my mind.
@art4eigen93
@art4eigen93 2 жыл бұрын
wow! reduction at work guys. The whole world is running upon two things 1. reduction 2. approximation.
@chiragsahu5468
@chiragsahu5468 3 жыл бұрын
Best explanation i have seen. I was unable to understand what is next permutations. U taught me that. Thank you 😊
@techdose4u
@techdose4u 3 жыл бұрын
Welcome 😀
@payalshekhawat7563
@payalshekhawat7563 3 жыл бұрын
I was struggling with this question. You made it easier ..thank you so much for this amazing explanation!!
@afridimajeed5897
@afridimajeed5897 3 жыл бұрын
I no longer dread this problem, great explanation!
@dikshantboora3764
@dikshantboora3764 2 жыл бұрын
very much better than striver
@jsuryakt
@jsuryakt Ай бұрын
I really understood this after watching 3 videos, the graph and peak explanation with decimal breakdown made it clear
@techdose4u
@techdose4u Ай бұрын
nice :)
@abhishekjadoun7562
@abhishekjadoun7562 3 жыл бұрын
was looking for this Question and then you just uploaded this video. Best explanation for this Question !!!!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@satheeshshanmugam5940
@satheeshshanmugam5940 3 жыл бұрын
Great Great .. Explained well with different cases... All of my queries are resolved .. thank you so much ..
@shobhanksharma65
@shobhanksharma65 3 жыл бұрын
O(n) code: public void nextPermutation(int[] nums) { int peak = nums.length-1; // find peak from right while(peak>0) { if(nums[peak] > nums[peak-1]) { break; } peak--; } if(peak==0) { // all numbers are descending, so reverse reverse(nums, 0, nums.length-1); } else { int i = peak-1; int j = nums.length-1; // find a number greater than that at i and has as low weight as possible // this is to find lowest number greater than the number to be swapped so that we can get next higher number while(j>i && nums[i] >= nums[j]) j--; swap(nums, i, j); // i+1 till end of array is descending ordered if(i
@aditisinghh
@aditisinghh 3 жыл бұрын
i am always looking for this channel if i want explaination of any questions . saves me a lot of time !!! 💛
@techdose4u
@techdose4u 3 жыл бұрын
Great ❤️
@rajatgoyal8155
@rajatgoyal8155 3 жыл бұрын
Best explanation for this problem....👍👍 Thank you so Much for such a detailed explanation...🙂
@techdose4u
@techdose4u 3 жыл бұрын
Welcome : )
@gouthamr8214
@gouthamr8214 2 жыл бұрын
This is O(n) , we don't require the last sorting part as the elements are already sorted but in reverse order. So simply swapping that would works.
@dhanashreegodase4445
@dhanashreegodase4445 3 жыл бұрын
you deserve more likes yar... great video
@TheSridharraj
@TheSridharraj 3 жыл бұрын
Best explanation i have ever seen
@tanishhhhhhh
@tanishhhhhhh 2 жыл бұрын
great explaination of the question
@beaglesnlove580
@beaglesnlove580 3 жыл бұрын
This was amazing description thank you
@pratosh
@pratosh 2 жыл бұрын
More simpler approach: public void nextPermutation(int[] nums) { int n=nums.length; if(n=0 && nums[i]>=nums[i+1]) i--; if(i>=0){ int j=n-1; while(nums[j]
@minatokuns1810
@minatokuns1810 2 жыл бұрын
best explanation so far.. there was some part which should have explanied more with the code though..
@radhasingh3549
@radhasingh3549 2 жыл бұрын
Amazing explanation😍
@krentwhite2668
@krentwhite2668 3 жыл бұрын
If you get this qn in an interview... u can easily pass time by explaining this approach... btw crystal clear explanation sir🙏
@techdose4u
@techdose4u 3 жыл бұрын
Thanks 😊
@brijvaghani6212
@brijvaghani6212 2 жыл бұрын
Very well explained 🤜
@selvaprasanths2202
@selvaprasanths2202 3 жыл бұрын
The best of all 🎉🔥please keep onposting
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@shriyasonam2716
@shriyasonam2716 2 жыл бұрын
beautifully explained!!!
@manojs157
@manojs157 2 жыл бұрын
Great explanation thank you
@techdose4u
@techdose4u 2 жыл бұрын
Welcome 😊
@harpercfc_
@harpercfc_ 2 жыл бұрын
Very useful one. Thank you sir
@Ajeet-Yadav-IIITD
@Ajeet-Yadav-IIITD 3 жыл бұрын
OMG!!! I'm feeling lucky that KZbin recommend me your channel🥰, You are amazing!!....Thank you for the good work sir!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@大盗江南
@大盗江南 8 ай бұрын
Thanks, i like ur videos the most among all.
@FOODASMRFAMILY
@FOODASMRFAMILY 3 жыл бұрын
amazing explanation with diagram approach
@sekharsamanta6266
@sekharsamanta6266 Жыл бұрын
Wow! such a clear explanation...thank you
@stoicbro1635
@stoicbro1635 2 жыл бұрын
beautiful explanation
@ninhbinhhuynh7619
@ninhbinhhuynh7619 3 жыл бұрын
This is the best explaination about this problem, thank you so much 🙏
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@LalitPandeyontube
@LalitPandeyontube 2 жыл бұрын
such a great explanation
@SHUBHAMYADAV-bs8vl
@SHUBHAMYADAV-bs8vl 3 жыл бұрын
Great explanation sir!, I find this problem difficult to solve, but after watching your video, it makes me easy to solve.
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@maksymmoroz
@maksymmoroz 11 ай бұрын
Thank you man, you helped me! Very clear
@kaushikramabhotla4635
@kaushikramabhotla4635 2 жыл бұрын
Maja aagya sir 🙌
@techdose4u
@techdose4u 2 жыл бұрын
Thanks 😊
@TheBaljitSingh
@TheBaljitSingh 5 ай бұрын
it was a great explanations video. i finally understand the #concept
@ullasbc728
@ullasbc728 Жыл бұрын
Good Explanation! Thank You
@gauravraj2604
@gauravraj2604 3 жыл бұрын
Great explanation. Thanks a lot
@johncenakiwi
@johncenakiwi 3 жыл бұрын
It is almost impossible to come up with a solution to this in an interview setting, if you haven't already solved this question before. It is one of those questions, if you know, you know.
@mahipalsingh-yo4jt
@mahipalsingh-yo4jt 3 жыл бұрын
This channel saves me everytime:)
@techdose4u
@techdose4u 3 жыл бұрын
❤️
@madhavanr8996
@madhavanr8996 3 жыл бұрын
Can u pls explain the last line of code that starting from sort(nums.begin()......
@paragroy5359
@paragroy5359 3 жыл бұрын
Sir in all your previous vides I used to get the logic pretty easily....but in this video I struggled a lot....I think the question is quite tricky..
@techdose4u
@techdose4u 3 жыл бұрын
Yes it is :)
@ganavin3423
@ganavin3423 2 жыл бұрын
Thanks .it helped me understood
@tridoan7383
@tridoan7383 3 жыл бұрын
Great explanation
@elmalkiahmed-v7o
@elmalkiahmed-v7o 2 ай бұрын
thanks dude, this is a very helpful vedio
@techdose4u
@techdose4u 2 ай бұрын
welcome :)
@gokulnaathbaskar9808
@gokulnaathbaskar9808 2 жыл бұрын
Thank you, Sir, this was nice
@rushikeshkulkarni1784
@rushikeshkulkarni1784 2 жыл бұрын
What if we get peak at 0th index but other half is not sorted.. eg. 3 1 2
@codingnightmares3407
@codingnightmares3407 3 жыл бұрын
Thanks a lot! Best explanation 🧡
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@abhijit-sarkar
@abhijit-sarkar Жыл бұрын
Here's the logic in simple terms without the confusion of multiple special cases. Since a descending sequence is already at its largest value, we can't get the next larger sequence from it. We therefore find the first ascending pair a[i] > a[i-1] from the end, and swap a[i-1] with the smallest possible value on the right that is larger than it. Since a[i-1] has been increased in value, the sequence a[i:] must be set to its smallest value to give the smallest next larger sequence, which is given by the ascending order. Furthermore, since we swapped a[i-1] with the smallest possible value on the right, say a[j], all elements in sequence a[j+1:] are smaller than a[i-1], and all elements in the sequence a[i:j] are larger than a[i-1]. Thus, the sequence a[i:] is in descending order, and can be made ascending by using two pointers to swap elements from both ends.
@alwinsanthosh6878
@alwinsanthosh6878 3 ай бұрын
My goodness, this is an absolute banger of an explaination. tysm
@handlerhandle123
@handlerhandle123 3 жыл бұрын
Graph helped thank you
@techdose4u
@techdose4u 3 жыл бұрын
Great ❤️
@hridamchakraborty6818
@hridamchakraborty6818 3 жыл бұрын
thank you so sooo much.. the explanation is great ..
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@kasturisanyal6649
@kasturisanyal6649 3 жыл бұрын
The explanation is so good and every detail is covered throughly. A hard problem made easy. Thank You so much:)
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@ishwaripednekar5164
@ishwaripednekar5164 3 жыл бұрын
Really appreciated ❤️
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@anubhavsethi7728
@anubhavsethi7728 3 жыл бұрын
Well explained 👌🏻👌🏻💯💯
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@amitmohapatra292
@amitmohapatra292 2 жыл бұрын
can anyone tell me what kind of questions is this i mean under which concept is it coming ?
@sujalhansda8285
@sujalhansda8285 3 жыл бұрын
Brilliant !
@jlecampana
@jlecampana 2 жыл бұрын
Great Explanation, but I still think this is an Awful Question for an Interview.
@gokulnaathbaskar9808
@gokulnaathbaskar9808 2 жыл бұрын
This was beautiful, thank you
@quirkyquester
@quirkyquester 3 ай бұрын
you're the man broo! Thx!
@techdose4u
@techdose4u 3 ай бұрын
Thanks 🙏🏼
@waliullahsiam7999
@waliullahsiam7999 2 жыл бұрын
thank you so much for such a beautiful explanation may god bless you
@outsider9633
@outsider9633 3 жыл бұрын
Cant we replace the last for loop by : int index = lstpeak; if (arr[lstpeak + 1] > arr[lstpeak - 1]) index = lstpeak + 1; EDIT: no we cant , we need to replace lst-1 with the minimum element in the right of lstindex
@thinkverse94
@thinkverse94 3 жыл бұрын
Hi Suriya bro, I've one doubt. Can you please clarify for me? Last 3 years I'm working as a manual and automation tester in Embedded Networking, somehow I entered into this domain, but I've been self-interested in coding & want to be a developer. but, in the developer interview, I don't have an experience of dev to show. I'm not getting interested to work on testing, my minds want to be a developer but not able to do. It's internally hurting me a lot. what to do? Need your suggestion, please.
@techdose4u
@techdose4u 3 жыл бұрын
Replied
@praharshsingh2095
@praharshsingh2095 3 жыл бұрын
at 16:12 u said that , if u have to swap the last n-1 elements the time complexity will be nlogn, but is there is a possibility that , last n-1 will be sorted in decreasing order , so we can use two pointers to make it sorted in increasing order,resulting in optimized time complexity of O(n/2) or O(n) ?
@BMEARJUNSINGH
@BMEARJUNSINGH 2 жыл бұрын
Thanks
@shubhamchavan1387
@shubhamchavan1387 3 жыл бұрын
Thank you!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@praveengautam4689
@praveengautam4689 2 жыл бұрын
genius...:)
@gautam4696
@gautam4696 3 жыл бұрын
Bhai you are the best
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@stark3.148
@stark3.148 3 ай бұрын
Sorting is not required, we can just reverse that decreasing sequence in the end. It will automatically sort the array
@ganeshjaggineni4097
@ganeshjaggineni4097 Ай бұрын
NICE SUPER EXCELLENT MOTIVATED
@techdose4u
@techdose4u Ай бұрын
nice :)
@MrChetan54
@MrChetan54 2 жыл бұрын
Too many edge cases in the explaination, but code doesn't have many. Ayushi Sharma has also provided with good explaination, incase you feel this video is li'l tough
@KarthikNandam-xs4qn
@KarthikNandam-xs4qn 2 ай бұрын
Amazing 🤩
@techdose4u
@techdose4u 2 ай бұрын
thanks :)
@ShivamGupta-cx3hy
@ShivamGupta-cx3hy 3 жыл бұрын
Awesome Teacher
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@Purubro
@Purubro Жыл бұрын
Level ka observation h vaiya
@mohdfarhanahmed9416
@mohdfarhanahmed9416 3 жыл бұрын
Bhai can you also provide the code in java in the upcoming videos. It would be very helpful for many of java geeks.
@ghanishsingla8270
@ghanishsingla8270 9 ай бұрын
Code link doesn't work, getting 404.
@chiragPatel22c
@chiragPatel22c 2 жыл бұрын
very appreciate your hard work, seen many videos regarding the same topic, Found your video the best explanation and clearing all blurry clouds. Thanks again😊😊. All the best 👍👍
@shivamagrawal3542
@shivamagrawal3542 3 жыл бұрын
What is the time complexity here?
@avert_
@avert_ 3 жыл бұрын
nlogn
@coder1015
@coder1015 3 жыл бұрын
Amazing ;)
@techdose4u
@techdose4u 3 жыл бұрын
:)
@themountains1701
@themountains1701 2 жыл бұрын
It's not working.
@satyanarayanagokavarapu3011
@satyanarayanagokavarapu3011 3 жыл бұрын
Can you explain Integer to English words leetcode 273 problem ?
@techdose4u
@techdose4u 3 жыл бұрын
Sure. It's very important for FAANG :)
@satyanarayanagokavarapu3011
@satyanarayanagokavarapu3011 3 жыл бұрын
@@techdose4u Word break and word break 2 problems . Expecting these two from you long back very important for all FAANG . Can you please take these two into your list ? Thank you for helping us.
@techdose4u
@techdose4u 3 жыл бұрын
Sure
@formulaire.8379
@formulaire.8379 5 ай бұрын
why we dont just use next_permutation ?
@takumisugita936
@takumisugita936 2 жыл бұрын
medium btw
@andriidanylov9453
@andriidanylov9453 Жыл бұрын
Appreciate
@qazaqempire3828
@qazaqempire3828 2 жыл бұрын
what a horrible question , the only way is to memorize this - it needs a trick to know. read the comments under the official solution on leetcode! they all complain and justfiedly
@samakshtandon1190
@samakshtandon1190 3 жыл бұрын
excellent explanation. just one thing. instead of sorting the last part we can just reverse it in o(n/2) time since it will always be in descending order. but in this case we just have to make sure if there are two places where i can make a change then i select the further one.eg:[2,3,1,3,3].
@Ayon-Karmakar
@Ayon-Karmakar 2 жыл бұрын
Found a mistake 12:51 [2,4,3,2] -> ngs is not [2,4,2,3]
@ECBBHAVYASREESUNDURU
@ECBBHAVYASREESUNDURU Жыл бұрын
The peak element here is 4 . ig we have to swap 4 with 2.
@GeetainSaar
@GeetainSaar 6 ай бұрын
Better than striver
@Karanyadav-cd4sq
@Karanyadav-cd4sq 3 жыл бұрын
rather than sorting we can reverse it, so making the complexity go from o(nlogn) to o(n)
@nishant5249
@nishant5249 2 жыл бұрын
At 4:30 your explanation is not correct... If you check the striever video explanation then you can spot the mistake
@user-jp9jd6el1f
@user-jp9jd6el1f Жыл бұрын
Sir plz apni facevalue bhi banaaiye
@tech_wizard9315
@tech_wizard9315 3 жыл бұрын
Plz make a detailed video on how to develop LinkedIn profile so strong that recruiters Of top companies can't resist to give job opening opportunities and internship opportunities specially for 2020grads (unplaced)
@iamparitosh
@iamparitosh 3 жыл бұрын
The only secret to nailing Linkedin is to be successful in real life. Do extraordinary things only when that will happen; No shortcuts whatsoever
@tech_wizard9315
@tech_wizard9315 3 жыл бұрын
@@iamparitosh along with good work, if you customize your account to make it look more presentable, it can help too!
@PujaKumari-rp5sg
@PujaKumari-rp5sg 3 сағат бұрын
💗
@techdose4u
@techdose4u 3 сағат бұрын
:)
@rhs1078
@rhs1078 10 ай бұрын
Worst explanation ever
@saikatpaul6576
@saikatpaul6576 Жыл бұрын
how did you rearrange the next greater sequence, its confusing as I am not understanding whats the next greatern sequence means
@manojjain3501
@manojjain3501 10 ай бұрын
Minimum of all greater permutations of given number. Ex 1386 -> Next minimum should be 1638 ( It is lowest of any combination of greater permutations )
@bhuvneshsingh3347
@bhuvneshsingh3347 3 жыл бұрын
Thanks!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome 😀
Next Permutation - Intuition in Detail 🔥 | Brute to Optimal
28:15
take U forward
Рет қаралды 498 М.
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 56 МЛН
NVIDIA's New 575W 5090 looks mental.
8:00
optimum
Рет қаралды 344 М.
Winning Google Kickstart Round A 2020 + Facecam
17:10
William Lin (tmwilliamlin168)
Рет қаралды 10 МЛН
Coding Interview | Software Engineer @ Bloomberg (Part 1)
30:05
Keep On Coding
Рет қаралды 4,8 МЛН
The Biggest Mistake Intermediate React Developers Make
18:32
Cosden Solutions
Рет қаралды 33 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 190 М.
NEXT PERMUTATION | LEETCODE # 31 | PYTHON OPTIMAL SOLUTION
18:50
Cracking FAANG
Рет қаралды 14 М.
Transformers (how LLMs work) explained visually | DL5
27:14
3Blue1Brown
Рет қаралды 4,2 МЛН
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 761 М.
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 56 МЛН