-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
76 lines (62 loc) · 2.7 KB
/
Copy pathSolution.java
File metadata and controls
76 lines (62 loc) · 2.7 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package list;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("./data/list/input"));
//BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
List<String> stringArray = bufferedReader.lines().collect(Collectors.toList());
bufferedReader.close();
Solution solution = new Solution();
solution.solve(stringArray);
}
private void solve(List<String> stringArray) {
//Always defined
int lengthOfList = Integer.parseInt(stringArray.get(0)); //not needed
int numberOfQueries = Integer.parseInt(stringArray.get(2));
int startIndexForQueries = 3;
String[] valuesAsStringArray = stringArray.get(1).split(" ");
List<Integer> listOfValues = Arrays.stream(valuesAsStringArray)
.mapToInt(Integer::parseInt)
.boxed()
.collect(Collectors.toList());
List<Query> listOfQueries = new ArrayList<>();
List<String> queries = stringArray.subList(startIndexForQueries, startIndexForQueries + (numberOfQueries * 2));
for(int i = 0; i < queries.size(); i++) {
String name = queries.get(i);
if (name == null || !name.equalsIgnoreCase("insert") && !name.equalsIgnoreCase("delete")) {
continue;
}
int[] values = Arrays.stream(queries.get(i + 1).split(" ")).mapToInt(Integer::parseInt).toArray();
listOfQueries.add(new Query(name, values));
}
listOfQueries.forEach(query -> query.handle(listOfValues));
String finalOutput = listOfValues.stream().map(String::valueOf).collect(Collectors.joining(" "));
System.out.println(finalOutput);
}
static class Query {
private final String name;
private final int[] values;
public Query(String name, int[] values) {
this.name = name;
this.values = values;
}
public void handle(List<Integer> listOfValues) {
if(name.equalsIgnoreCase("insert")) {
if(values.length != 2) {
return;
}
int position = values[0];
int value = values[1];
listOfValues.add(position, value);
} else if(name.equalsIgnoreCase("delete")) {
Arrays.stream(values).forEach(listOfValues::remove);
}
}
}
}