- Decide the key that represents the information needed later.
- Update its count or stored state while scanning the input.
- Use constant-time expected lookups to detect matches or assemble the result.
Code notes
- 116 lines of C++ from the credited upstream file design-most-recently-used-queue.cpp.
- The implementation visibly relies on sequence storage, hash lookup.
- 8 loop blocks detected.
Complexity
Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 4class MRUQueue {5public:6 MRUQueue(int n)7 : bit_{n}8 , curr_{n} {9 for (int i = 0; i < n; ++i) {10 lookup_[i] = i + 1;11 }12 }13 14 int fetch(int k) {15 int pos = bit_.binary_lift(k);16 int val = lookup_[pos];17 lookup_.erase(pos);18 bit_.add(pos, -1);19 bit_.add(curr_, 1);20 lookup_[curr_++] = val;21 return val;22 }23 24private:25 class BIT {26 public:27 static const int MAX_CALLS = 2000;28 29 BIT(int n) : bit_(n + MAX_CALLS + 1) { 30 for (int i = 1; i < size(bit_); ++i) {31 bit_[i] = ((i - 1 < n) ? 1 : 0) + bit_[i - 1];32 }33 for (int i = size(bit_) - 1; i >= 1; --i) {34 int last_i = i - lower_bit(i);35 bit_[i] -= bit_[last_i];36 }37 }38 39 void add(int i, int val) {40 ++i;41 for (; i < size(bit_); i += lower_bit(i)) {42 bit_[i] += val;43 }44 }45 46 int query(int i) const {47 ++i;48 int total = 0;49 for (; i > 0; i -= lower_bit(i)) {50 total += bit_[i];51 }52 return total;53 }54 55 int binary_lift(int k) const {56 int total = 0;57 int pos = 0;58 for (int i = floor_log2_x(size(bit_) - 1); i >= 0; --i) {59 if (pos + (1 << i) < size(bit_) && !(total + bit_[pos + (1 << i)] >= k)) {60 total += bit_[pos + (1 << i)];61 pos += (1 << i);62 }63 }64 return (pos + 1) - 1;65 }66 67 private:68 int lower_bit(int i) const {69 return i & -i;70 }71 72 int floor_log2_x(int x) const {73 return 8 * sizeof(int) - __builtin_clz(x) - 1;74 };75 76 vector<int> bit_;77 };78 79 BIT bit_;80 unordered_map<int, int> lookup_;81 int curr_;82};83 84858687class MRUQueue2 {88public:89 MRUQueue2(int n)90 : buckets_(ceil(sqrt(n))) {91 for (int i = 0; i < n; ++i) {92 buckets_[i / size(buckets_)].emplace_back(i + 1);93 }94 }95 96 int fetch(int k) {97 --k;98 int left = k / size(buckets_);99 int idx = k % size(buckets_);100 auto cit = cbegin(buckets_[left]);101 advance(cit, idx);102 int val = *cit;103 buckets_[left].erase(cit);104 buckets_.back().emplace_back(val);105 for (int i = size(buckets_) - 2; i >= left; --i) {106 int x = buckets_[i + 1].front();107 buckets_[i + 1].pop_front();108 buckets_[i].emplace_back(x);109 }110 return val;111 }112 113private:114 vector<list<int>> buckets_;115};116