This repository was archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDynamicArray.java
55 lines (45 loc) · 1.51 KB
/
DynamicArray.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
import java.util.ArrayList;
import java.util.List;
/**
* @author medany
*/
/*
*
* Create a list , seqList, of N empty sequences, where each sequence is indexed
* from 0 to N-1. The elements within each of the sequences also use 0-indexing.
* Create an integer, lastAnswer, and initialize it to 0. The types of queries
* that can be performed on your list of sequences (seqList) are described
* below: Query: 1 x y Find the sequence, seq, at index (x ^ lastAnswer) % N in
* seqList. Append integer y to sequence . Query: 2 x y Find the sequence, seq,
* at index (x ^ lastAnswer) % N in seqList. Find the value of element y % size
* in seq (where is the size of seq) and assign it to lastAnswer. Print the new
* value of on a new line
*
*/
public class DynamicArray {
public Integer[] solve(int n, String[] queries) {
List<Integer> output = new ArrayList<>();
List<Integer>[] seqList = new ArrayList[n];
for (int i = 0; i < n; i++) {
List<Integer> l = new ArrayList<>();
seqList[i] = l;
}
int lastAnswer = 0, size = 0;
for (String q : queries) {
String[] input = q.split(" ");
int type = Integer.parseInt(input[0]);
int x = Integer.parseInt(input[1]), y = Integer.parseInt(input[2]);
switch (type) {
case 1:
seqList[(x ^ lastAnswer) % n].add(y);
break;
case 2:
size = seqList[(x ^ lastAnswer) % n].size();
lastAnswer = seqList[(x ^ lastAnswer) % n].get(y % size);
output.add(lastAnswer);
break;
}
}
return output.toArray(new Integer[output.size()]);
}
}