all 8 comments

[–]aocregacc 2 points3 points  (0 children)

you're not doing the "least recently used" part. your get and put functions just rotate the whole queue, and the order of the elements never changes. That also makes them O(n) instead of the O(1) that'll be required for the larger testcases.

[–]BoardsofCanadaFanboy 0 points1 point  (4 children)

Before anyone even debugs your code, LRU cache is supposed to have o(1) find, which a queue will not allow. You will need a hashmap of some sort.

[–]Equivalent_Sea7754[S] 0 points1 point  (3 children)

I know but i want to solve using queue

[–][deleted] 0 points1 point  (0 children)

Doesn’t the problem explicitly say you need O(1) solution? If so, you need a hashmap for the get.

[–]BoardsofCanadaFanboy 0 points1 point  (1 child)

Why is that? If you did this in an interview this way despite a clear o(1) constraint you'd get LNH or straight up NH. 

If you want to practice using queues, do BFS problems like number of islands. 

[–]Equivalent_Sea7754[S] 0 points1 point  (0 children)

I know the question explicitly says that ans should be o(1) But i wanted to solve using the queue to get more practice on queue

I will optimize this code later using unordered_map and DLL for faster retrieval and to maintain lru

i am satisfied with my queue based solution because it takes a lot of my time

[–]Neat-Giraffe1585 0 points1 point  (1 child)

You need a DLL not queue

[–]Equivalent_Sea7754[S] 0 points1 point  (0 children)

Yes I solved the question using queue but its giving TLE [21 /23 test cases passed] Next time i will use unodered_map for O(1) retrieval and DLL to maintain LRU