Leetcode - Next Permutation (Python)

preview_player
Показать описание
January 2021 Leetcode Challenge
Leetcode - Next Permutation #31
Рекомендации по теме
Комментарии
Автор

Bro I love the way u explain . Understood the algorithm perfectly!!

💌

sailokesh
Автор

Like the way you walk through these problems. The mind process was logical and easy to follow.

chrisshao
Автор

Love the video and explanation! You're the GOAT! Quick question:
If you used reversed instead of sort on line 14 wouldn't that make it a little bit of a better solution since sort has O(nlogn) Time complexity and reversed has Linear Time Complexity?

moezabbes
Автор

Neat and concise explanation of the core idea, thanks!

oleksiimartens
Автор

Thanku so so much... love from Jaipur, India.🤗

dashrathsinghkaviya
Автор

You have a way of explanation that make me understand the problem right from the first time, and it's short, I'm so jealous anyway thank you a lot~

modreamer
Автор

Thank you for the great solution, only feedback was the reverse like the previous comments:D

mohfocus
Автор

How would I even know that's the algo to find next permutation. I just figure the next permutation by nature, not that complicated algo... lol

davidcheung
Автор

Hey Tim, find your videos very helpful. I may not be able to like or comment on all due to office laptop restrictions. 😅 But really love them.. keep making more and more

anjaliiyer
Автор

You dont need sorting for next permutation

carefree_ladka
Автор

Why did understanding the question took so long XD
Finally understood it. So easy

dARKfnXx
Автор

Here's my Python implementation:

class Solution(object):
def nextPermutation(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
i = len(nums) - 2
while i >= 0 and nums[i] >= nums[i+1]:
i -= 1

if i < 0:
nums.sort()
return

j = len(nums) - 1
while j >= 0 and nums[i] >= nums[j]:
j -= 1

nums[i], nums[j] = nums[j], nums[i]
nums[i+1:] = nums[i+1:][::-1]

edwardteach
Автор

There's always a better Asian
Now I don't have any doubt on it

abhishektiwari