|
| 1 | +package com.igorwojda.cache.lru |
| 2 | + |
| 3 | +// Implementation is using combination of HashMap and LinkedList. |
| 4 | +// Time Complexity: O(1) |
| 5 | +private object Solution1 { |
| 6 | + class LRUCache(private val capacity: Int) { |
| 7 | + private val map = HashMap<Int, Node>() |
| 8 | + |
| 9 | + private var head: Node? = null |
| 10 | + private var tail: Node? = null |
| 11 | + |
| 12 | + val size get() = map.size |
| 13 | + |
| 14 | + fun put(key: Int, value: String) { |
| 15 | + // 1. Check if node exicts |
| 16 | + val existingNode = map[key] |
| 17 | + |
| 18 | + if (existingNode == null) { |
| 19 | + // 2. Check Map capacity |
| 20 | + if (map.size >= capacity) { |
| 21 | + val removedNode = removeHead() |
| 22 | + map.remove(removedNode?.key) |
| 23 | + } |
| 24 | + |
| 25 | + // 3. Add a new node |
| 26 | + val newNode = Node(key, value) |
| 27 | + |
| 28 | + map[key] = newNode |
| 29 | + addTail(newNode) |
| 30 | + } else { |
| 31 | + existingNode.value = value |
| 32 | + moveToTail(existingNode) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private fun addTail(node: Node) { |
| 37 | + // 1. If list is empty |
| 38 | + if (head == null) { |
| 39 | + head = node |
| 40 | + } else { |
| 41 | + node.prev = tail |
| 42 | + tail?.next = node |
| 43 | + } |
| 44 | + |
| 45 | + tail = node |
| 46 | + } |
| 47 | + |
| 48 | + private fun removeHead(): Node? { |
| 49 | + // 1. Head exists |
| 50 | + if (head != null) { |
| 51 | + // 2. Store current head to return |
| 52 | + val node = head |
| 53 | + |
| 54 | + // 3. Remove head |
| 55 | + head = head?.next |
| 56 | + head?.prev = null |
| 57 | + |
| 58 | + // 4. Remove tail if head is tail |
| 59 | + if (node == tail) tail = null |
| 60 | + |
| 61 | + return node |
| 62 | + } |
| 63 | + |
| 64 | + return null |
| 65 | + } |
| 66 | + |
| 67 | + fun get(key: Int): String? { |
| 68 | + // 1. get the node |
| 69 | + val node = map[key] |
| 70 | + |
| 71 | + // 2. Move to tail if exists |
| 72 | + if (node != null) { |
| 73 | + moveToTail(node) |
| 74 | + } |
| 75 | + |
| 76 | + // 3. Return value |
| 77 | + return node?.value |
| 78 | + } |
| 79 | + |
| 80 | + private fun moveToTail(node: Node) { |
| 81 | + // 1. Check if node is tail |
| 82 | + if (node != tail) { |
| 83 | + // 2. Remove node from list |
| 84 | + if (node == head) { |
| 85 | + head = node.next |
| 86 | + } else { |
| 87 | + node.prev?.next = node.next |
| 88 | + node.next?.prev = node.prev |
| 89 | + } |
| 90 | + |
| 91 | + // 3. Add node to tail |
| 92 | + addTail(node) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private data class Node( |
| 97 | + val key: Int, |
| 98 | + var value: String, |
| 99 | + var prev: Node? = null, |
| 100 | + var next: Node? = null, |
| 101 | + ) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// Implementation using LinkedHashMap |
| 106 | +// Time Complexity: O(1) |
| 107 | +private object Solution2 { |
| 108 | + class LRUCache(private val capacity: Int) { |
| 109 | + val size get() = linkedHashMap.size |
| 110 | + |
| 111 | + private val linkedHashMap = object : |
| 112 | + LinkedHashMap<Int, String>(capacity, 0.75f, true) { |
| 113 | + override fun removeEldestEntry(eldest: MutableMap.MutableEntry<Int, String>?): Boolean { |
| 114 | + return size > capacity |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + fun put(key: Int, value: String) { |
| 119 | + linkedHashMap[key] = value |
| 120 | + } |
| 121 | + |
| 122 | + fun get(key: Int): String? { |
| 123 | + return linkedHashMap[key]?.also { |
| 124 | + linkedHashMap.remove(key) |
| 125 | + linkedHashMap[key] = it |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// Implementation using List |
| 132 | +// Time Complexity: O(n) |
| 133 | +private object Solution3 { |
| 134 | + class LRUCache(private val capacity: Int) { |
| 135 | + private val list = mutableListOf<Pair<Int, String>>() |
| 136 | + |
| 137 | + val size get() = list.size |
| 138 | + |
| 139 | + fun get(key: Int): String? { |
| 140 | + val value = list.firstOrNull { it.first == key }?.second |
| 141 | + |
| 142 | + if (value != null) { |
| 143 | + val pair = Pair(key, value) |
| 144 | + list.remove(pair) |
| 145 | + list.add(pair) |
| 146 | + } |
| 147 | + |
| 148 | + return list.firstOrNull { it.first == key }?.second |
| 149 | + } |
| 150 | + |
| 151 | + fun put(key: Int, value: String) { |
| 152 | + list.removeIf { it.first == key } |
| 153 | + list.add(Pair(key, value)) |
| 154 | + |
| 155 | + if (list.size > capacity) { |
| 156 | + list.removeFirst() |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments