Permutations - Leetcode 46 - Python

Sdílet
Vložit
  • čas přidán 24. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
    🐦 Twitter: / neetcode1
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    Problem Link: neetcode.io/problems/permutat...
    0:00 - Read the problem
    0:30 - Drawing Explanation
    6:08 - Coding Recursive
    9:35 - Coding Iterative
    leetcode 46
    #neetcode #leetcode #python
  • Věda a technologie

Komentáře • 17

  • @riddle-master-ruben
    @riddle-master-ruben Před 27 dny +3

    This is a way better explanation than the old video. I went months with this problem unsolved because I just couldn't understand, but this explanation helped me tremendously. Thanks!

  • @hasferrr
    @hasferrr Před 12 dny +2

    i think the backtracking solution with decision tree is more intuitive. For each recursion call, you can store the number that has already added to the Answer list into the HashSet, and for each recursion call you can skip the number that already in the HashSet
    function permute(nums: number[]): number[][] {
    const result = []
    const dfs = (set: Set, pm: number[]): void => {
    if (pm.length === nums.length) {
    result.push([...pm])
    return
    }
    for (let i = 0; i < nums.length; i++) {
    if (set.has(nums[i])) {
    continue
    }
    pm.push(nums[i])
    set.add(nums[i])
    dfs(set, pm)
    pm.pop()
    set.delete(nums[i])
    }
    }
    dfs(new Set(), [])
    return result
    }

  • @bandarupawankumar7549
    @bandarupawankumar7549 Před 27 dny +1

    Thank you neetcode for again working on this problem :)

  • @pastori2672
    @pastori2672 Před 27 dny +3

    i feel like a time traveler

  • @MyPodie
    @MyPodie Před 27 dny +1

    Morning Neetcode!

  • @AVGVSTVSivlivs
    @AVGVSTVSivlivs Před 23 dny

    Swapping indices method for this question is probably more clear and you should also review Longest Palindromic Substring question. Solutions in the other questions are quite optimal.
    Thanks a lot!

  • @user-vu4ng4rb8k
    @user-vu4ng4rb8k Před 27 dny +1

    hey also do video for cherry pickup 1 and bst to sorted DLL

  • @johnveracruz2102
    @johnveracruz2102 Před 26 dny

    @NeetcodeIO PLS add a functionality on your site to not show the topics for the questions. want to do them in order without knowing the topic. thanks

  • @kanjurer
    @kanjurer Před 27 dny

    I actually just solved the problem yesterday lmao

  • @Gomeroff
    @Gomeroff Před 27 dny +9

    I live in Russia, a new day has not yet begun, and you are already solving a new problem)

  • @ijavd
    @ijavd Před 27 dny

    Morning

  • @mukeshrawat1304
    @mukeshrawat1304 Před 27 dny

    Is it possible for you to have some sort of poll or something where we could ask for a video solution of a leetcode problem which you haven't already solved. Because there are quite a few problems which don't have a proper solution on CZcams and God knows when they will appear as POTD.

    • @DeathSugar
      @DeathSugar Před 27 dny

      I guess thats what his site is for.

  • @pushkarsaini2
    @pushkarsaini2 Před 27 dny

    Now Solve 31 Next Permutation

  • @m_jdm357
    @m_jdm357 Před 13 dny

    The worst solution I've seen.

    • @NeetCodeIO
      @NeetCodeIO  Před 13 dny +1

      anything specific about it that you did not like?

    • @m_jdm357
      @m_jdm357 Před 13 dny

      @@NeetCodeIO I tried to understand it but it's really bad when debugging or writing down on paper. Really hard to understand. I found this solution:
      def permute(self, nums: List[int]) -> List[List[int]]:
      res = []
      def backtrack(arr):
      if len(arr) == len(nums):
      res.append(arr.copy())
      return

      for i in range(len(nums)):
      if nums[i] in arr:
      continue
      arr.append(nums[i])
      backtrack(arr)
      arr.pop()

      backtrack([])
      return res
      This I got. If no one supports me and everyone thinks your solution is good. Then I'm wrong.