Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 46 · Permutations

Permutations

LeetCode 46 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Fix position start, swap each choice into it.

நடுத்தரம்ArrayBacktracking
LeetCode இல் திற ↗

நான் Permutations (LeetCode 46) ஐ இப்படித் தீர்த்தேன் — Permutations பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.

கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.

Intuition

A permutation picks every element exactly once, in some order. Either build it by appending unused numbers, or generate in place by swapping each candidate into the current position and recursing on the rest.

Approach

  1. 1

    Version 1: append any number not yet in the list.

  2. 2

    Version 2: for i from start, swap nums[i] into position start.

  3. 3

    Recurse for start + 1, then swap back.

  4. 4

    Record when start reaches n.

நான் எப்படித் தீர்த்தேன் · முதல் யோசனையிலிருந்து இறுதித் தீர்வு வரை

1
Build with membership checkO(n²·n!) time

Append unused numbers.

2
Swap in placeAcceptedO(n·n!) time

No extra used-array.

Build with membership checkPythonO(n²·n!) time
class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        res=[]
        def dfs(index,ds):
            if index == len(nums):
                res.append(ds[:])
            for num in nums:
                if num not in ds:
                    ds.append(num)
                    dfs(index+1,ds)
                    ds.pop()
        dfs(0,[])
        return res
Swap in placePythonO(n·n!) time
class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        res=[]
        def dfs(start,ds):
            if start == len(nums):
                res.append(ds[:])
            for i in range(start,len(nums)):
                nums[i],nums[start]=nums[start],nums[i]
                dfs(start+1,nums)
                nums[start],nums[i]=nums[i],nums[start]
        dfs(0,[])
        return res
ஸ்டெப்-த்ரூ ஏற்றுகிறது…

Permutations — அடிக்கடி கேட்கப்படும் கேள்விகள்

Permutations பின்னாலுள்ள intuition என்ன?

A permutation picks every element exactly once, in some order. Either build it by appending unused numbers, or generate in place by swapping each candidate into the current position and recursing on the rest.

Permutations (LeetCode 46) ஐ எப்படித் தீர்ப்பது?

1. Version 1: append any number not yet in the list. 2. Version 2: for i from start, swap nums[i] into position start. 3. Recurse for start + 1, then swap back. 4. Record when start reaches n.

Permutations இன் time மற்றும் space complexity என்ன?

Time: O(n · n!). Space: O(n). (Accepted approach: Swap in place.)

Permutations இல் பொதுவான தவறுகள் என்ன?

`num not in ds` costs O(n) and breaks with duplicate values.

Permutations ஐத் தீர்க்க ஒன்றுக்கு மேற்பட்ட வழிகள் உள்ளனவா?

Build with membership check (O(n²·n!) time): Append unused numbers. Swap in place (O(n·n!) time): No extra used-array.