LeetCode: Combinations Solution

"Don't try" - Charles Bukowski

Implementation

1var combine = function (n, k) {
2 const res = []
3
4 const recusion = (arr, start, k) => {
5 if (k === 0) {
6 res.push([...arr])
7 return
8 }
9
10 for (let i = start; i <= n; i++) {
11 arr.push(i)
12 recusion(arr, i + 1, k - 1)
13 arr.pop()
14 }
15 }
16
17 recusion([], 1, k)
18
19 return res
20}

References

Original problem

Comments

Loading comments...

Tags

leetcode

backtracking

Next Post

LeetCode: Permutations

Backtracking with chosen state

Previous Post

LeetCode: Rotting Oranges

Simultaneously spread from rotten cells

HoningJS

Search Posts