notes/OJ notes/pages/Leetcode Combinations.md

144 lines
2.4 KiB
Markdown
Raw Permalink Normal View History

2022-07-19 23:02:46 +08:00
# Leetcode Combinations
#### 2022-07-19 20:09
> ##### Algorithms:
2022-09-03 15:41:36 +08:00
>
2022-07-19 23:02:46 +08:00
> #algorithm #backtrack
2022-09-03 15:41:36 +08:00
>
2022-07-19 23:02:46 +08:00
> ##### Difficulty:
2022-09-03 15:41:36 +08:00
>
2022-09-06 20:22:48 +08:00
> #coding_problem #difficulty_medium
2022-09-03 15:41:36 +08:00
>
2022-07-19 23:02:46 +08:00
> ##### Additional tags:
2022-09-03 15:41:36 +08:00
>
> #leetcode #CS_list_need_understanding
>
2022-07-19 23:02:46 +08:00
> ##### Revisions:
2022-09-03 15:41:36 +08:00
>
2022-09-21 14:35:34 +08:00
> 2022-09-21
2022-07-19 23:02:46 +08:00
##### Related topics:
2022-09-03 15:41:36 +08:00
2022-07-19 23:02:46 +08:00
##### Links:
2022-09-03 15:41:36 +08:00
2022-07-19 23:02:46 +08:00
- [Link to problem](https://leetcode.com/problems/combinations/)
2022-07-19 23:12:37 +08:00
- [Explanation](https://leetcode.com/problems/combinations/discuss/844096/Backtracking-cheatsheet-%2B-simple-solution)
2022-09-03 15:41:36 +08:00
---
2022-07-19 23:02:46 +08:00
### Problem
2022-07-20 14:25:33 +08:00
Given two integers `n` and `k`, return _all possible combinations of_ `k` _numbers out of the range_ `[1, n]`.
2022-07-19 23:12:37 +08:00
2022-07-20 14:25:33 +08:00
You may return the answer in **any order**.
2022-07-19 23:12:37 +08:00
2022-07-19 23:02:46 +08:00
#### Examples
2022-07-20 14:25:33 +08:00
**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]]
```
2022-07-19 23:02:46 +08:00
#### Constraints
2022-07-20 14:25:33 +08:00
**Constraints:**
2022-09-03 15:41:36 +08:00
- `1 <= n <= 20`
- `1 <= k <= n`
2022-07-20 14:25:33 +08:00
2022-07-19 23:02:46 +08:00
### Thoughts
> [!summary]
2022-07-19 23:12:37 +08:00
> This is a #backtrack problem
The link I mentioned on the links section already have a good
explanation on the topic.
2022-07-19 23:02:46 +08:00
2022-07-19 23:20:25 +08:00
In my eyes, backtracking means DFS-like recursion and searching in solving problems.
2022-07-20 14:25:33 +08:00
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.
2022-07-19 23:02:46 +08:00
### Solution
2022-07-20 14:25:33 +08:00
Backtracking
2022-09-03 15:41:36 +08:00
2022-07-20 14:25:33 +08:00
```cpp
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;
}
};
```
2022-09-21 14:35:34 +08:00
Alternative solution using private values
```cpp
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;
}
};
```