With the first algorithm, i was like, damn, I built the exact same logic but it did not work on all cases. But then with the slightly different approach, it works. Thank you, keep up!
you earned a new subscriber....................your code writing skill is very nice, ..... it's clean and understandable............definitely will try to imitate you
impressive coding skills. The accent though helped me understand better. the same thing in java // [1,7,9,9,8,3] // index = 1 // sort from index 3 till end // swap 7 with just larger to right public void nextPermutation(int[] nums) { int n = nums.length; if(n0){ if(nums[i]>nums[i-1]) { index = i-1; break; } i--; } if(index == -1) reverseSort(nums, index+1, n-1); else{ reverseSort (nums, index+1, n-1); for(i = index+1; inums[index]) { swap(nums, index, i); break; } } } } void swap(int[] arr, int a, int b){ int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } void reverseSort(int[] arr, int start, int end){ while(start
very well explained, thank you so much for the solutions. I want to know how did you start learning DSA. I'm switching to software engineering from different background, if there is any good book that you followed while preparing can you let me know please
Great explanation, thank you! :) One thing I also found is that we don't necessarily need to check if next_num is less than len(nums) in line 28, since there must be a number larger than nums[dec], otherwise we would have a returned in line 26 earlier.
GREAt your second approach is on fire......the first approach has a problem in dealing with duplicates . If the next greater element of arr[index-1] is duplicated then we need to swap with the rightmost only because if we do not then after reversing ,smaller element will move furthur to the least significant positions which is wrong.......ABSolutely spot on
thanks a lot it helped me so much, but here's one suggestion though, i think you must have spent a little bit more time on cases where digits were repeating. i appreciate you taking the example - { 1 ,7 ,9 ,9 ,8 ,3} .I guess this example was really significant because this is one of the cases which people tend to forget ( that we might encounter digits repeating). Another great example could be taking - {1,7,9,9,8,7,3} and these two examples would cover both the = sign in comparisons. Anyways , great explanation. thankyou
Hey Jazab! tbh I did this question a while ago, but I'm almost certain I used hints for this problem - it's very nuanced and not immediately obvious - no worries at all if you struggled with it. I think just the fact that you tried a question you had not seen before and learned of the solution (whether on your own or using help/watching videos) is really helpful since you just learned something new! And going forward you now know how to solve this question and other similar ones:)
I tried to simplify code ``` class Solution(object): def nextPermutation(self, nums): i=len(nums)-2 while i>=0 and nums[i]>=nums[i+1]: i-=1 if i>=0: j=len(nums)-1 while j>=0 and nums[j]
@always sporty ofc!! So in the last lines, after we have finished reversing, we want to make the next permutation, and in order to do that we want to find the correct placement for our current number "next_num". @5:36 for example, we want to place the number so we maintain the ascending order for the numbers we just finished reversing. This will form the smallest next number possible, which would be what we are looking for - the next permutation. Looking from @1:03 might help with the walk-through of the different examples and why we want to do that:), but if you have any questions at all let me know and I'll be more then happy to answer Amisha!:))))
Hi Deepti , loved your video . Would love to collaborate and make a code review , sys design , algo solving -live videos . Let me know if you would like to collaborate sometime.
With the first algorithm, i was like, damn, I built the exact same logic but it did not work on all cases. But then with the slightly different approach, it works. Thank you, keep up!
Ofc thx sm Shubham!!:))
Another great video!
we can use this logic maybe for swapping:
nums[ind1] , nums[ind2] = nums[ind2] , nums[ind1]
For some reason, the first approach works on some cases but on others it doesn't. The second approach (5:11) worked great. Thanks!
you earned a new subscriber....................your code writing skill is very nice, ..... it's clean and understandable............definitely will try to imitate you
ahh thank you so much!!:))
impressive coding skills. The accent though helped me understand better.
the same thing in java
// [1,7,9,9,8,3]
// index = 1
// sort from index 3 till end
// swap 7 with just larger to right
public void nextPermutation(int[] nums) {
int n = nums.length;
if(n0){
if(nums[i]>nums[i-1]) {
index = i-1;
break;
}
i--;
}
if(index == -1) reverseSort(nums, index+1, n-1);
else{
reverseSort (nums, index+1, n-1);
for(i = index+1; inums[index]) {
swap(nums, index, i);
break;
}
}
}
}
void swap(int[] arr, int a, int b){
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
void reverseSort(int[] arr, int start, int end){
while(start
Straight to the point and easy to understand. Thanks!
Ty!:))
@@DEEPTITALESRA Please make more leetcode videos
@@nikhilmishra7572 yassss more coming up!!:)
@@DEEPTITALESRA can you please also explain Leetcode question 775. Global and Local Inversions
?
very well explained, thank you so much for the solutions. I want to know how did you start learning DSA. I'm switching to software engineering from different background, if there is any good book that you followed while preparing can you let me know please
Thanks, for the clear explanation.
you can assign a variable for lenght of array,which saves computational power
For swapping you could do it in one line in python: i1, i2 = i2, i1
Yes! The underlying logic is the same as using temp, but you're right it makes for cleaner code!!:)
can you explain the ancient indian method intuition behinfd the next_permutation stl in c++?
Great explanation, thank you! :) One thing I also found is that we don't necessarily need to check if next_num is less than len(nums) in line 28, since there must be a number larger than nums[dec], otherwise we would have a returned in line 26 earlier.
Ahh I totally missed that, you are absolutely correct! - line 28 should just be:
"while nums[next_num]
Really great!!! Thanks so much!!! Wow!!!
Awesome explanation! Finally understood this question! Thanks!
Thank you so much Jeremy!! Love to hear that:)
You are so good in explaining, please upload more post and more questions
Thanks so much Nooshin!! More on the way:))
amazing amazing, best ever
wow explanation
Very good explanation! Easy to understand! Thank you very much!
Thanks sm Ling!:))
I saw other videos on this question but i wasnt able to understand.then
Watched your video and understood in first go...great work ..keep it up
So glad to hear this helped!:) tysm!!
a lil confusion on how you found the next greater number after reversing the right part of the array.
just_great thanks you sharing. pls continue making more such videos
GREAt your second approach is on fire......the first approach has a problem in dealing with duplicates . If the next greater element of arr[index-1] is duplicated then we need to swap with the rightmost only because if we do not then after reversing ,smaller element will move furthur to the least significant positions which is wrong.......ABSolutely spot on
nice
we can swap values without using temp right?
a, b = b, a
Yes! It's the same underlying logic but you're right John - that would definitely make for cleaner code!
thanks a lot it helped me so much, but here's one suggestion though, i think you must have spent a little bit more time on cases where digits were repeating.
i appreciate you taking the example - { 1 ,7 ,9 ,9 ,8 ,3} .I guess this example was really significant because this is one of the cases which people tend to forget ( that we might encounter digits repeating).
Another great example could be taking - {1,7,9,9,8,7,3} and these two examples would cover both the = sign in comparisons.
Anyways , great explanation.
thankyou
great mam, wonderful explaination plz upload more videos in python
Thank you so much Rohit!! More videos coming soon:))
this is great, very clear to understand
Thank you sm!:)
@@DEEPTITALESRA i just get started with leetcode, please post more videos
朱超 more on the way!:)
Damn I really struggled with this question. Did you solve it on your first try without looking at hints/solution ?
Hey Jazab! tbh I did this question a while ago, but I'm almost certain I used hints for this problem - it's very nuanced and not immediately obvious - no worries at all if you struggled with it. I think just the fact that you tried a question you had not seen before and learned of the solution (whether on your own or using help/watching videos) is really helpful since you just learned something new! And going forward you now know how to solve this question and other similar ones:)
I tried to simplify code
```
class Solution(object):
def nextPermutation(self, nums):
i=len(nums)-2
while i>=0 and nums[i]>=nums[i+1]:
i-=1
if i>=0:
j=len(nums)-1
while j>=0 and nums[j]
Nice explanation! But don't you think we can remove those explicit conditions for length 1 & 2.
yes def! I had just included that to show base cases to consider, but you are absolutely correct - it'll make for much cleaner code!:)
pls provode the detailed explanation of what u have done in the last 4 lines
same im zoning out those 4 lines
@always sporty ofc!! So in the last lines, after we have finished reversing, we want to make the next permutation, and in order to do that we want to find the correct placement for our current number "next_num". @5:36 for example, we want to place the number so we maintain the ascending order for the numbers we just finished reversing. This will form the smallest next number possible, which would be what we are looking for - the next permutation. Looking from @1:03 might help with the walk-through of the different examples and why we want to do that:), but if you have any questions at all let me know and I'll be more then happy to answer Amisha!:))))
Thanks a lot, Great explanation!
:))
plzz make more vidoes they helped me a lot XD
awww thank you sm!! I'm really glad it helped:) ill def be making more videos soon (once classes dial down again haha)
thanks
I don't know why but I am getting a runtime error in Python2.
oo hmm I tried running it in Python2 rn and it seemed to work? Do you happen to know which part is timing out by any chance?
great explanation thank you
:))
very clear and nice explanation tq :)
Pavan Illa Thank you:))
Thanks a lot for this.
Ofc!!:)
@@DEEPTITALESRA Could you please upload a video on " First missing positive number in an array" in O(1) space and O(N) time.
Hi Deepti , loved your video . Would love to collaborate and make a code review , sys design , algo solving -live videos . Let me know if you would like to collaborate sometime.
Damnn! What an algorithm!
haha thank you Sujay!!:))
thanks didi
Ofc!!! thanks:))
you are my crush..❤️❤️❤️❤️
nice explanation...are u indian?
Thank you Abhijeet!! And yep I am!:)
Awesome Deepti