Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
98 lines (87 loc) · 2.35 KB

File metadata and controls

98 lines (87 loc) · 2.35 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*******************************************************************************
* Copyright (c) 2017 ejpark.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU General Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl-3.0.html
*
* Contributors:
* eungjun11@gmail.com - initial API and implementation
******************************************************************************/
package algorithm.li;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author ejpark
*
*/
public class A1004 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
boolean isOperation = true;
StringBuilder output = new StringBuilder();
Storage storage = new Storage();
while (isOperation) {
String input = br.readLine();
String[] commands = input.split(" ");
String command = commands[0];
switch (command) {
case "evict":
storage.evict();
break;
case "add":
storage.add(Integer.parseInt(commands[1]), Integer.parseInt(commands[2]));
break;
case "get":
output.append(storage.get(Integer.parseInt(commands[1]))).append("\n");
break;
case "remove":
output.append(storage.remove(Integer.parseInt(commands[1]))).append("\n");
break;
case "exit":
isOperation = false;
break;
default:
}
}
System.out.println(output.toString());
}
public static class Storage {
private List<Integer> list = new ArrayList<Integer>();
private Map<Integer, Integer> map = new HashMap<Integer, Integer>();
public void evict() {
if (!list.isEmpty()) {
map.remove(list.get(0));
}
}
public void add(int key, int value) {
map.put(key, value);
orderKey(key);
}
public int get(int key) {
if (map.containsKey(key)) {
orderKey(key);
return map.get(key);
}
return -1;
}
public int remove(int key) {
if (map.containsKey(key)) {
list.remove(list.indexOf(key));
return map.remove(key);
}
return -1;
}
private void orderKey(int key) {
int index = list.indexOf(key);
if (index > -1) {
list.remove(index);
}
list.add(key);
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.