logseq_notes/pages/OJ notes/pages/Leetcode Combinations.md
2023-06-14 14:27:22 +08:00

2.3 KiB

Leetcode Combinations

2022-07-19 20:09

Algorithms:

#algorithm #backtrack

Difficulty:

#coding_problems #difficulty_medium

Additional tags:

#leetcode #CS_list_need_understanding

Revisions:

2022-09-21

Problem

Given two integers n and k, return all possible combinations of k numbers out of the range [1, n].

You may return the answer in any order.

Examples

Example 1:

**Input:** n = 4, k = 2
**Output:**
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]

Example 2:

**Input:** n = 1, k = 1
**Output:** [[1]]

Constraints

Constraints:

  • 1 <= n <= 20
  • 1 <= k <= n

Thoughts

[!summary] This is a #backtrack problem

The link I mentioned on the links section already have a good explanation on the topic.

In my eyes, backtracking means DFS-like recursion and searching in solving problems.

First, it add a option in the combinations, then backtrack it And when it's done, it gets poped out and try another solution, and backtrack.

Solution

Backtracking

class Solution {
void backtrack(vector<vector<int>> &ans, vector<int> &combs, int n, int nex,
               int k) {
  if (k == 0) {
    ans.push_back(combs);
  } else {
    // try different solutions
    for (int i = nex; i <= n; i++) {
      combs.push_back(i);

      backtrack(ans, combs, n, i + 1, k - 1);

      combs.pop_back();
    }
  }
}

public:
vector<vector<int>> combine(int n, int k) {
  vector<vector<int>> ans = {};
  vector<int> combs = {};

  backtrack(ans, combs, n, 1, k);

  return ans;
}
};

Alternative solution using private values

class Solution {
vector<vector<int>> ans;
vector<int> combs = {};
int N, K;
void backtrack(int cur) {
  if (combs.size() == K) {
    // we reached the end, should push to the answer array
    ans.push_back(combs);
    return;
  } else if (cur <= N) {
    combs.push_back(cur);
    backtrack(cur + 1);

    combs.pop_back();
    backtrack(cur + 1);
  }
}

public:
vector<vector<int>> combine(int n, int k) {
  N = n;
  K = k;
  backtrack(1);

  return ans;
}
};