-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
62 lines (52 loc) · 1.44 KB
/
Solution.java
File metadata and controls
62 lines (52 loc) · 1.44 KB
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
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;
public class Solution {
Map<Integer,Integer> map ;
Deque<Integer> dq;
int capacity;
public Solution(int capacity) {
map = new HashMap<Integer,Integer>();
dq = new LinkedList<Integer>();
this.capacity = capacity;
}
public int get(int key) {
if(map.containsKey(key))
return map.get(key);
return -1;
}
public void set(int key, int value) {
if(!map.containsKey(key))
{
if(dq.size()==capacity)
{
dq.remove(key);
map.remove(key);
}
}
else
{
map.remove(dq.removeLast());
}
dq.addFirst(key);
map.put(key, value);
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int n = s.nextInt();
Solution solution = new Solution( n );
for(int i=0;i<n;i++)
{
int key = s.nextInt();
int value = s.nextInt();
solution.set( key, value );
}
System.out.println( solution.get( 6 ) );
System.out.println( solution.get( 5 ) );
System.out.println( solution.get( 1 ) );
System.out.println( solution.get( 2 ) );
}
}