|
1 | 1 | import java.util.ArrayList;
|
2 | 2 | import java.util.List;
|
3 |
| -import java.util.Scanner; |
4 | 3 |
|
5 | 4 | /**
|
6 | 5 | * @author medany
|
7 | 6 | */
|
8 | 7 |
|
| 8 | +/* |
| 9 | + * |
| 10 | + * Create a list , seqList, of N empty sequences, where each sequence is indexed |
| 11 | + * from 0 to N-1. The elements within each of the sequences also use 0-indexing. |
| 12 | + * Create an integer, lastAnswer, and initialize it to 0. The types of queries |
| 13 | + * that can be performed on your list of sequences (seqList) are described |
| 14 | + * below: Query: 1 x y Find the sequence, seq, at index (x ^ lastAnswer) % N in |
| 15 | + * seqList. Append integer y to sequence . Query: 2 x y Find the sequence, seq, |
| 16 | + * at index (x ^ lastAnswer) % N in seqList. Find the value of element y % size |
| 17 | + * in seq (where is the size of seq) and assign it to lastAnswer. Print the new |
| 18 | + * value of on a new line |
| 19 | + * |
| 20 | + */ |
| 21 | + |
9 | 22 | public class DynamicArray {
|
10 | 23 |
|
11 | 24 | public Integer[] solve(int n, String[] queries) {
|
12 | 25 |
|
13 | 26 | List<Integer> output = new ArrayList<>();
|
14 | 27 |
|
15 |
| - List<List<Integer>> seqList = new ArrayList<>(n); |
16 |
| - int N = n; |
17 |
| - for (int i = 0; i < N; N--) { |
18 |
| - List<Integer> l = new ArrayList<>(n); |
19 |
| - seqList.add(l); |
| 28 | + List<Integer>[] seqList = new ArrayList[n]; |
| 29 | + for (int i = 0; i < n; i++) { |
| 30 | + List<Integer> l = new ArrayList<>(); |
| 31 | + seqList[i] = l; |
20 | 32 | }
|
21 | 33 |
|
22 |
| - int lastAnswer = 0; |
| 34 | + int lastAnswer = 0, size = 0; |
23 | 35 |
|
24 | 36 | for (String q : queries) {
|
25 |
| - int type = Integer.parseInt(q.split(" ")[0]); |
26 |
| - int x = Integer.parseInt(q.split(" ")[1]), y = Integer.parseInt(q.split(" ")[2]); |
| 37 | + String[] input = q.split(" "); |
| 38 | + int type = Integer.parseInt(input[0]); |
| 39 | + int x = Integer.parseInt(input[1]), y = Integer.parseInt(input[2]); |
27 | 40 |
|
28 | 41 | switch (type) {
|
29 | 42 | case 1:
|
30 |
| - seqList.get((x ^ lastAnswer) % n).add(y); |
| 43 | + seqList[(x ^ lastAnswer) % n].add(y); |
31 | 44 | break;
|
32 | 45 | case 2:
|
33 |
| - int size = seqList.get((x ^ lastAnswer) % n).size(); |
34 |
| - lastAnswer = seqList.get((x ^ lastAnswer) % n).get(y % size); |
| 46 | + size = seqList[(x ^ lastAnswer) % n].size(); |
| 47 | + lastAnswer = seqList[(x ^ lastAnswer) % n].get(y % size); |
35 | 48 | output.add(lastAnswer);
|
36 | 49 | break;
|
37 | 50 | }
|
38 | 51 | }
|
39 | 52 |
|
40 | 53 | return output.toArray(new Integer[output.size()]);
|
41 | 54 | }
|
42 |
| - |
43 |
| - public static void main(String[] args) { |
44 |
| - Scanner sc = new Scanner(System.in); |
45 |
| - |
46 |
| - int N = sc.nextInt(); |
47 |
| - int Q = sc.nextInt(); |
48 |
| - |
49 |
| - List<List<Integer>> seqList = new ArrayList<>(N); |
50 |
| - int n = N; |
51 |
| - for (int i = 0; i < n; n--) { |
52 |
| - List<Integer> l = new ArrayList<>(N); |
53 |
| - seqList.add(l); |
54 |
| - } |
55 |
| - |
56 |
| - int lastAnswer = 0; |
57 |
| - |
58 |
| - while (Q > 0) { |
59 |
| - int type = sc.nextInt(); |
60 |
| - int x, y; |
61 |
| - |
62 |
| - switch (type) { |
63 |
| - case 1: |
64 |
| - x = sc.nextInt(); |
65 |
| - y = sc.nextInt(); |
66 |
| - seqList.get((x ^ lastAnswer) % N).add(y); |
67 |
| - break; |
68 |
| - case 2: |
69 |
| - x = sc.nextInt(); |
70 |
| - y = sc.nextInt(); |
71 |
| - int size = seqList.get((x ^ lastAnswer) % N).size(); |
72 |
| - lastAnswer = seqList.get((x ^ lastAnswer) % N).get(y % size); |
73 |
| - System.out.println(lastAnswer); |
74 |
| - break; |
75 |
| - } |
76 |
| - |
77 |
| - Q--; |
78 |
| - } |
79 |
| - |
80 |
| - sc.close(); |
81 |
| - } |
82 | 55 | }
|
0 commit comments