-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3237.java
79 lines (72 loc) · 2.91 KB
/
_3237.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.fishercoder.solutions.fourththousand;
import java.util.HashMap;
import java.util.Map;
public class _3237 {
public static class Solution1 {
/*
* My completely original solution, very natural to think of doubly linked list + hashmap.
* Whenever a window is chosen (iterating on in the queries array), that window will be put onto the head of the list,
* all other windows will be pushed to the right by one position.
*/
public int[] simulationResult(int[] windows, int[] queries) {
Map<Integer, DoublyLinkedListNode> map = new HashMap<>();
DoublyLinkedListNode pre = buildList(windows, map);
for (int q : queries) {
moveToHead(q, pre, map);
}
return backToArray(pre, windows.length);
}
private int[] backToArray(DoublyLinkedListNode pre, int length) {
DoublyLinkedListNode tmp = pre;
int[] ans = new int[length];
for (int i = 0; i < length; i++) {
ans[i] = tmp.next.val;
tmp = tmp.next;
}
return ans;
}
private void moveToHead(
int q, DoublyLinkedListNode headPrev, Map<Integer, DoublyLinkedListNode> map) {
DoublyLinkedListNode node = map.get(q);
// if this window is already at the head, then we don't need to do anything
if (headPrev.next == node) {
return;
}
// get this node's next and prev pointers
DoublyLinkedListNode next = node.next;
DoublyLinkedListNode prev = node.prev;
// connect it's next to its previous' next, essentially cutting the current node out of
// the chain
prev.next = next;
// in case this is tail, we don't need to re-assign its next pointer
if (next != null) {
next.prev = prev;
}
DoublyLinkedListNode oldHead = headPrev.next;
headPrev.next = node;
node.next = oldHead;
oldHead.prev = node;
}
private DoublyLinkedListNode buildList(
int[] windows, Map<Integer, DoublyLinkedListNode> map) {
DoublyLinkedListNode pre = new DoublyLinkedListNode(-1);
DoublyLinkedListNode tmp = pre;
for (int i = 0; i < windows.length; i++) {
DoublyLinkedListNode next = new DoublyLinkedListNode(windows[i]);
next.prev = tmp;
tmp.next = next;
map.put(windows[i], next);
tmp = tmp.next;
}
return pre;
}
public static class DoublyLinkedListNode {
DoublyLinkedListNode prev;
DoublyLinkedListNode next;
int val;
public DoublyLinkedListNode(int val) {
this.val = val;
}
}
}
}