forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllOne.java
125 lines (106 loc) · 3.42 KB
/
AllOne.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
package com.leetcode.design;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Level: Hard
* Link: https://leetcode.com/problems/all-oone-data-structure/
* Description:
* 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 "".
* <p>
* Challenge: Perform all these in O(1) time complexity.
*
* @author rampatra
* @since 2019-08-11
*/
public class AllOne {
Map<String, Integer> keyToValMap;
Map<Integer, Set<String>> valToKeyMap;
/**
* Initialize your data structure here.
*/
public AllOne() {
keyToValMap = new HashMap<>();
valToKeyMap = new HashMap<>();
}
/**
* Inserts a new key <Key> with value 1. Or increments an existing key by 1.
*/
public void inc(String key) {
}
/**
* Decrements an existing key by 1. If Key's value is 1, remove it from the data structure.
*/
public void dec(String key) {
}
/**
* Returns one of the keys with maximal value.
*/
public String getMaxKey() {
return null;
}
/**
* Returns one of the keys with Minimal value.
*/
public String getMinKey() {
return null;
}
public static void main(String[] args) {
AllOne allOne = new AllOne();
allOne.inc("r");
allOne.inc("r");
allOne.dec("r");
allOne.inc("a");
allOne.inc("b");
allOne.inc("b");
assertEquals("b", allOne.getMaxKey());
assertEquals("a", allOne.getMinKey());
allOne = new AllOne();
allOne.dec("hello");
assertEquals("", allOne.getMaxKey());
allOne = new AllOne();
allOne.inc("a");
allOne.inc("b");
allOne.inc("b");
allOne.inc("b");
allOne.inc("b");
allOne.dec("b");
allOne.dec("b");
assertEquals("b", allOne.getMaxKey());
assertEquals("a", allOne.getMinKey());
allOne = new AllOne();
allOne.inc("hello");
allOne.inc("hello");
assertEquals("hello", allOne.getMaxKey());
assertEquals("hello", allOne.getMinKey());
allOne.inc("leet");
assertEquals("hello", allOne.getMaxKey());
assertEquals("leet", allOne.getMinKey());
allOne = new AllOne();
allOne.inc("a");
allOne.inc("b");
allOne.inc("b");
allOne.inc("c");
allOne.inc("c");
allOne.inc("c");
allOne.dec("b");
allOne.dec("b");
assertEquals("a", allOne.getMinKey());
allOne.dec("a");
assertEquals("c", allOne.getMaxKey());
//assertEquals("c", allOne.getMinKey());
allOne = new AllOne();
allOne.inc("hello");
allOne.dec("hello");
assertEquals("", allOne.getMaxKey());
assertEquals("", allOne.getMinKey());
}
}