-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathHashMapApplications.java
152 lines (128 loc) · 4.49 KB
/
HashMapApplications.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
import java.util.*;
public class HashMapApplications {
// Implementation of Topological Sort using hashmap.
public boolean topologicalSort(int numCourses, int[][] prerequisites) {
int count = 0;
HashMap<Integer, List<Integer>> map = new HashMap<>();
int[] indegree = new int[numCourses];
for(int i = 0 ;i < prerequisites.length; i++) {
if(!map.containsKey(prerequisites[i][1])) {
map.put(prerequisites[i][1], new ArrayList<>());
}
indegree[prerequisites[i][0]]++;
map.get(prerequisites[i][1]).add(prerequisites[i][0]);
}
Queue<Integer> q = new LinkedList<>();
for(int i = 0; i < numCourses; i++) {
if(indegree[i] == 0) {
q.offer(i);
}
}
if(q.isEmpty()) {
return false;
}
while(!q.isEmpty()) {
int size = q.size();
for(int i = 0; i < size; i++) {
int temp = q.poll();
count++;
List<Integer> list = map.get(temp);
if(list != null) {
for(int l: list) {
indegree[l]--;
if(indegree[l] == 0) {
q.offer(l);
}
}
}
}
}
return count == numCourses;
}
// Implementation of Prefix tree (Prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings)
// using hashmap.
class Trie {
TrieNode root = new TrieNode();
public Trie() {
}
public void insert(String word) {
TrieNode current = root;
for(char ch : word.toCharArray()){
if(!current.children.containsKey(ch)){
current.children.put(ch, new TrieNode());
}
current = current.children.get(ch);
}
current.children.put('*', null);
}
public boolean search(String word) {
TrieNode node = root;
for(char ch : word.toCharArray()){
if(!node.children.containsKey(ch)){
return false;
}
node = node.children.get(ch);
}
return node.children.containsKey('*');
}
public boolean startsWith(String prefix) {
TrieNode node = root;
for(char ch : prefix.toCharArray()){
if(!node.children.containsKey(ch)){
return false;
}
node = node.children.get(ch);
}
return true;
}
static class TrieNode{
Map<Character, TrieNode> children = new HashMap<>();
}
}
//LFU Cache Implementation
class LFUCache {
int cap;
Map<Integer,Integer> cache = new HashMap<>();
Map<Integer,Integer> keycount = new HashMap<>();
Map<Integer,LinkedHashSet<Integer>> freqmap = new HashMap<>();
int min = -1;
public LFUCache(int capacity) {
cap = capacity;
freqmap.put(1,new LinkedHashSet<>());
}
public int get(int key) {
if(cache.containsKey(key)==false)
return -1;
int count = keycount.get(key);
keycount.put(key,count+1);
freqmap.get(count).remove(key);
if(count==min&&freqmap.get(count).size()==0)
min++;
if(freqmap.containsKey(count+1)==false)
freqmap.put(count+1,new LinkedHashSet<>());
freqmap.get(count+1).add(key);
return cache.get(key);
}
public void put(int key, int value) {
if(cap<=0)
return;
if(cache.containsKey(key))
{
cache.put(key,value);
get(key);
return;
}
if(cache.size()>=cap)
{
int evict = freqmap.get(min).iterator().next();
freqmap.get(min).remove(evict);
cache.remove(evict);
keycount.remove(evict);
}
cache.put(key,value);
keycount.put(key,1);
min = 1;
freqmap.get(1).add(key);
}
}
}