Created
July 17, 2022 19:31
-
-
Save shiywang/56fd031ea9d3f60c11c88dafee3ef44a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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