|
| 1 | +package org.gd.leetcode.p0347; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +/** |
| 6 | + * @author Horkhover Dmytro |
| 7 | + * @see SortSolution |
| 8 | + * @since 2020-08-10 |
| 9 | + */ |
| 10 | +class LinkedListSolution implements Solution { |
| 11 | + |
| 12 | + private CountNode head, tail; |
| 13 | + |
| 14 | + public int[] topKFrequent(final int[] nums, final int k) { |
| 15 | + |
| 16 | + if (nums == null || nums.length == 0) |
| 17 | + return nums; |
| 18 | + |
| 19 | + head = tail = new CountNode(-1); |
| 20 | + CountNode.connect(head, tail); |
| 21 | + head.insertRight(new CountNode(0)); |
| 22 | + |
| 23 | + Map<Integer, Node> nodeMap = new HashMap<>(nums.length); |
| 24 | + |
| 25 | + for (int num : nums) { |
| 26 | + |
| 27 | + Node node = nodeMap.get(num); |
| 28 | + if (node == null) { |
| 29 | + |
| 30 | + node = new Node(num); |
| 31 | + head.next(0).add(node); |
| 32 | + nodeMap.putIfAbsent(num, node); |
| 33 | + |
| 34 | + } else { |
| 35 | + |
| 36 | + CountNode parent = node.parent; |
| 37 | + node.unlink(); |
| 38 | + parent.next().add(node); |
| 39 | + if (parent.isEmpty()) |
| 40 | + parent.unlink(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + int i = 0; |
| 45 | + int[] result = new int[Math.min(k, nodeMap.size())]; |
| 46 | + Iterator<Node> iterator = new DownIterator(tail); |
| 47 | + while (iterator.hasNext() && i < result.length) |
| 48 | + result[i++] = iterator.next().value; |
| 49 | + |
| 50 | + return result; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public String toString() { |
| 55 | + StringJoiner sj = new StringJoiner(", ", "[", "]"); |
| 56 | + CountNode next = head.next; |
| 57 | + while (next != tail) { |
| 58 | + sj.add(next.toString()); |
| 59 | + next = next.next; |
| 60 | + } |
| 61 | + return sj.toString(); |
| 62 | + } |
| 63 | + |
| 64 | + static class CountNode { |
| 65 | + final int count; |
| 66 | + CountNode prev, next; |
| 67 | + Node head, tail; |
| 68 | + |
| 69 | + CountNode(int count) { |
| 70 | + this.count = count; |
| 71 | + head = tail = new Node(-1, this); |
| 72 | + Node.connect(head, tail); |
| 73 | + } |
| 74 | + |
| 75 | + static void connect(CountNode prev, CountNode next) { |
| 76 | + if (prev != null) |
| 77 | + prev.next = next; |
| 78 | + if (next != null) |
| 79 | + next.prev = prev; |
| 80 | + } |
| 81 | + |
| 82 | + boolean isEmpty() { |
| 83 | + return head.next == tail; |
| 84 | + } |
| 85 | + |
| 86 | + CountNode next() { |
| 87 | + return next(count + 1); |
| 88 | + } |
| 89 | + |
| 90 | + CountNode next(int count) { |
| 91 | + if (next.count == count) |
| 92 | + return next; |
| 93 | + |
| 94 | + CountNode next = new CountNode(count); |
| 95 | + insertRight(next); |
| 96 | + return next; |
| 97 | + } |
| 98 | + |
| 99 | + void insertRight(CountNode node) { |
| 100 | + connect(node, next); |
| 101 | + connect(this, node); |
| 102 | + } |
| 103 | + |
| 104 | + void add(Node node) { |
| 105 | + node.parent = this; |
| 106 | + head.insertRight(node); |
| 107 | + } |
| 108 | + |
| 109 | + void unlink() { |
| 110 | + connect(prev, next); |
| 111 | + prev = null; |
| 112 | + next = null; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public String toString() { |
| 117 | + StringJoiner sj = new StringJoiner(", ", count + " -> [", "]"); |
| 118 | + Node next = head.next; |
| 119 | + while (next != tail) { |
| 120 | + sj.add("" + next.value); |
| 121 | + next = next.next; |
| 122 | + } |
| 123 | + return sj.toString(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + static class Node { |
| 128 | + |
| 129 | + final int value; |
| 130 | + CountNode parent; |
| 131 | + Node prev, next; |
| 132 | + |
| 133 | + Node(int value) { |
| 134 | + this.value = value; |
| 135 | + } |
| 136 | + |
| 137 | + Node(int value, CountNode parent) { |
| 138 | + this.value = value; |
| 139 | + this.parent = parent; |
| 140 | + } |
| 141 | + |
| 142 | + static void connect(Node prev, Node next) { |
| 143 | + if (prev != null) |
| 144 | + prev.next = next; |
| 145 | + if (next != null) |
| 146 | + next.prev = prev; |
| 147 | + } |
| 148 | + |
| 149 | + void insertRight(Node node) { |
| 150 | + node.parent = parent; |
| 151 | + Node next = this.next; |
| 152 | + connect(this, node); |
| 153 | + connect(node, next); |
| 154 | + } |
| 155 | + |
| 156 | + void unlink() { |
| 157 | + connect(prev, next); |
| 158 | + prev = null; |
| 159 | + next = null; |
| 160 | + parent = null; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + static class DownIterator implements Iterator<Node> { |
| 165 | + |
| 166 | + CountNode countNode; |
| 167 | + Node node = null; |
| 168 | + |
| 169 | + DownIterator(CountNode countNode) { |
| 170 | + this.countNode = countNode; |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public boolean hasNext() { |
| 175 | + if (countNode == null) |
| 176 | + return false; |
| 177 | + |
| 178 | + if (node == null) |
| 179 | + node = countNode.tail.prev; |
| 180 | + |
| 181 | + if (node != countNode.head) |
| 182 | + return true; |
| 183 | + |
| 184 | + node = null; |
| 185 | + countNode = countNode.prev; |
| 186 | + return hasNext(); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public Node next() { |
| 191 | + if (!hasNext()) |
| 192 | + throw new NoSuchElementException(); |
| 193 | + |
| 194 | + Node value = this.node; |
| 195 | + this.node = this.node.prev; |
| 196 | + return value; |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments