-
Notifications
You must be signed in to change notification settings - Fork 0
/
All O`one Data Structure.java
175 lines (155 loc) · 5.19 KB
/
All O`one Data Structure.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Implement a data structure supporting the following operations:
Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is guaranteed to
be a non-empty string.
Dec(Key) - If Key's value is 1, remove it from the data structure. Otherwise decrements an existing
key by 1. If the key does not exist, this function does nothing. Key is guaranteed to be a non-empty
string.
GetMaxKey() - Returns one of the keys with maximal value. If no element exists, return an empty
string "".
GetMinKey() - Returns one of the keys with minimal value. If no element exists, return an empty
string "".
Challenge: Perform all these in O(1) time complexity.
Link: https://leetcode.com/problems/all-oone-data-structure/
Example: None
Solution: None
Source: https://discuss.leetcode.com/topic/67622/java-accepted-solution-using-hashmap-and-doublelinkedlist
*/
public class AllOne {
private HashMap<String, Node> hash = null;
private Node head = null, tail = null;
/** Initialize your data structure here. */
public AllOne() {
hash = new HashMap<String, Node>();
head = tail = null;
}
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
public void inc(String key) {
if (head == null) {
Node node = new Node(1);
node.keys.add(key);
head = tail = node;
hash.put(key, node);
return;
}
if (hash.containsKey(key)) {
Node node = hash.get(key);
if (node.next != null && node.next.val == node.val + 1) {
node.next.keys.add(key);
hash.put(key, node.next);
} else {
Node tmp = new Node(node.val + 1);
tmp.keys.add(key);
tmp.prev = node;
tmp.next = node.next;
if (node.next != null) {
node.next.prev = tmp;
}
node.next = tmp;
if (tmp.val > tail.val) {
tail = tmp;
}
hash.put(key, tmp);
}
node.keys.remove(key);
if (node.keys.size() == 0) {
remove(node);
}
} else {
if (head.val == 1) {
head.keys.add(key);
hash.put(key, head);
} else {
Node node = new Node(1);
node.keys.add(key);
node.next = head;
head.prev = node;
head = node;
hash.put(key, node);
}
}
}
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
public void dec(String key) {
if (hash.containsKey(key)) {
Node node = hash.get(key);
if (node.val != 1) {
if (node.prev != null && node.prev.val == node.val - 1) {
node.prev.keys.add(key);
hash.put(key, node.prev);
} else {
Node tmp = new Node(node.val - 1);
tmp.keys.add(key);
tmp.next = node;
tmp.prev = node.prev;
if (node.prev != null) {
node.prev.next = tmp;
}
node.prev = tmp;
if (tmp.val < head.val) {
head = tmp;
}
hash.put(key, tmp);
}
} else {
hash.remove(key);
}
node.keys.remove(key);
if (node.keys.size() == 0) {
remove(node);
}
}
}
private void remove(Node node) {
Node prev = node.prev;
Node next = node.next;
if (prev == null) {
head = next;
} else {
prev.next = next;
}
if (next == null) {
tail = node.prev;
} else {
next.prev = prev;
}
}
/** Returns one of the keys with maximal value. */
public String getMaxKey() {
if (tail != null && tail.keys.size() > 0) {
// System.out.println("max:"+tail.val);
for (String str: tail.keys) {
return str;
}
}
return "";
}
/** Returns one of the keys with Minimal value. */
public String getMinKey() {
if (head != null && head.keys.size() > 0) {
// System.out.println("min:"+head.val);
for (String str : head.keys) {
return str;
}
}
return "";
}
class Node {
public int val = 1;
public HashSet<String> keys = null;
public Node prev, next;
public Node(int val) {
this.val = val;
keys = new HashSet<String>();
prev = next = null;
}
}
}
/**
* Your AllOne object will be instantiated and called as such:
* AllOne obj = new AllOne();
* obj.inc(key);
* obj.dec(key);
* String param_3 = obj.getMaxKey();
* String param_4 = obj.getMinKey();
*/