77. Combinations - LeetCode Solution


Array Backtracking

Python Code:

class Solution:
    def combine(self, n: int, k: int) -> List[List[int]]:
        ans = []
        
        def trav(i, up):
            if i == n+1:
                return 
            if len(up) == k:
                ans.append(up)
                return 
            
            for j in range(i+1, n+1, 1):
                trav(j, up + [j])
        
        trav(0, [])
        return ans


Comments

Submit
0 Comments
More Questions

677C - Vanya and Label
1583B - Omkar and Heavenly Tree
1703C - Cypher
1511C - Yet Another Card Deck
1698A - XOR Mixup
1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon
1066A - Vova and Train
169B - Replacing Digits
171D - Broken checker
380C - Sereja and Brackets
1281B - Azamon Web Services
1702A - Round Down the Price
1681C - Double Sort
12A - Super Agent
1709A - Three Doors
1680C - Binary String
1684B - Z mod X = C
1003A - Polycarp's Pockets