Skip to content

Instantly share code, notes, and snippets.

@shiywang
Created July 17, 2022 19:31
Show Gist options
  • Save shiywang/56fd031ea9d3f60c11c88dafee3ef44a to your computer and use it in GitHub Desktop.
Save shiywang/56fd031ea9d3f60c11c88dafee3ef44a to your computer and use it in GitHub Desktop.
class Solution {
int N, K;
public:
int dfs(int index, vector<int> &add, int k, vector<bool> &visited) {
if(index == N && k == K) {
for(auto &a : add) {
cout << a << " ";
}
cout << endl;
return 1;
}
if(k == K) {
return 0;
}
for(int i = 1; i <= N; i++) {
if(!visited[i]) {
visited[i] = true;
add.push_back(i);
dfs(index+1, add, visited);
visited[i] = false;
add.pop_back();
}
}
}
int kInversePairs(int n, int k) {
N = n; K = k;
vector<bool> visited(N+1, 0);
vector<int> add;
dfs(0, add, 0, visited);
return 0;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment