diff --git a/LeetCode/1409.queries-on-a-permutation-with-key.cpp b/LeetCode/1409.queries-on-a-permutation-with-key.cpp index 4c5b09e..8d1b69f 100644 --- a/LeetCode/1409.queries-on-a-permutation-with-key.cpp +++ b/LeetCode/1409.queries-on-a-permutation-with-key.cpp @@ -1,28 +1,15 @@ class Solution { -public: - vector processQueries(vector& queries, int m) { - unordered_map pos, rev_pos; // pos[3] - 10 => 3 occurs at index 10 - for(int i = 1; i <= m; i++) { - pos[i] = i-1, rev_pos[i-1] = i; - } - - vector ans = {}; - for(int q: queries) { - ans.push_back(pos[q]); - // increment the pos of all nos from 0 to pos[q]-1 - for (int i = pos[q]-1 ; i >= 0; i--) { - int cur = rev_pos[i]; - pos[cur] = i+1; // shift to right side - rev_pos[i+1] = cur; - } - pos[q] = 0; - rev_pos[0] = q; + public: + vector processQueries(vector& queries, int m) + { + vector ans, P(m); + int n = queries.size(); + iota(P.begin(), P.end(), 1); // initialize P + for (int i = 0; i < n; i++) { + auto it = find(P.begin(), P.end(), queries[i]); // find location of query in P + ans.push_back(it - P.begin()); // push index of the query in ans vector + rotate(P.begin(), it, it + 1); // move the query at the beginning of P } return ans; } -}; -// 1,2,3,4,5 -// 3,1,2,4,5 - -// _ _ _ _ _ _ _ _ _ _ _ - // ^ \ No newline at end of file +}; \ No newline at end of file