leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 1b6eb9811b03eb3366e679b374a8c87a4880a2cd
parent 12914f91e64c4550b90595ec1a23f7e489b376fd
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Fri, 24 Mar 2023 13:03:24 +0100

Random Problem

Diffstat:
AProblems/0060.cpp | 15+++++++++++++++
MREADME.md | 1+
2 files changed, 16 insertions(+), 0 deletions(-)

diff --git a/Problems/0060.cpp b/Problems/0060.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + string getPermutation(int n, int k) { + vector<char> avail = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; + int factorial = 1, pos; + string res; + for (int i = 1; i <= n; i++) factorial *= i; + for (k--; n; n--) { + factorial /= n; + res += avail[pos = k / factorial], k -= pos * factorial; + for (int i = pos; i < avail.size() - 1; i++) avail[i] = avail[i + 1]; + } + return res; + } +}; diff --git a/README.md b/README.md @@ -76,6 +76,7 @@ for solving problems. | 0057 | Medium | [Insert Interval](Problems/0057.cpp) | | 0058 | Easy | [Length of Last Word](Problems/0058.cpp) | | 0059 | Medium | [Spiral Matrix II](Problems/0059.cpp) | +| 0060 | Hard | [Permutation Sequence](Problems/0060.cpp) | | 0061 | Medium | [Rotate List](Problems/0061.cpp) | | 0062 | Medium | [Unique Paths](Problems/0062.cpp) | | 0063 | Medium | [Unique Paths II](Problems/0063.cpp) |