I went through many rounds of Data Structure and Algorithm or Competitive Programming tests again.
In my scenario, I don't have internet access but if I use C++ I was allowed to import the STL library.
I found that <algorithm> and <vector> was pretty useful.
Where I can simply call a ready-made functions instead of memorizing the algo.
I review my past blog post about permutation and combination. Now, I realize that I find a better way to answer them. The full answer for permutation problem is available in Programiz online compiler. But, the only relevant code for the algo is just these following 9 lines:
template <typename T>
vector<vector<T>> permute(vector<T>& elems) {
vector<vector<T>> out;
sort(elems.begin(), elems.end());
do {
out.push_back(elems);
} while (next_permutation(elems.begin(), elems.end()));
return out;
}
It can even be shortened by simply defining the primitive type like int and char instead of generics <T>.
To be honest, this is my first time I see a positive return in coding lower level language with pointer like C++.
Maybe, I'll code more in lower level languages in the future.
Till then, peace out. ✌️
