knakul853

Untitled

Jul 17th, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string getPermutation(int n, int k) {
  4.        
  5.         string ans = "";
  6.         vector<int>num;
  7.        
  8.          int fact[n+1];
  9.         fact[0]=1;
  10.         for(int i=1;i<=n;i++)
  11.         {
  12.             fact[i] = i * fact[i-1];
  13.             num.push_back(i);
  14.         }k--;
  15.        
  16.         while(n)
  17.         {
  18.             int id = k/(fact[n-1]);
  19.            
  20.              ans+=to_string(num[id]);
  21.             num.erase(num.begin() + id);
  22.             k%=fact[n-1];
  23.             n--;
  24.         }
  25.         return ans;
  26.        
  27.     }
  28. };
Add Comment
Please, Sign In to add comment