본문 바로가기

DataStructure/Array

Dynamic Array

  • Create a list, , of  empty sequences, where each sequence is indexed from  to . The elements within each of the  sequences also use -indexing.
  • Create an integer, , and initialize it to .
  • The  types of queries that can be performed on your list of sequences () are described below:
    1. Query: 1 x y
      1. Find the sequence, , at index  in .
      2. Append integer  to sequence .
    2. Query: 2 x y
      1. Find the sequence, , at index  in .
      2. Find the value of element  in  (where  is the size of ) and assign it to .
      3. Print the new value of  on a new line

Task 
Given , and  queries, execute each query.

Note:  is the bitwise XOR operation, which corresponds to the ^ operator in most languages. Learn more about it on Wikipedia.

Input Format

The first line contains two space-separated integers,  (the number of sequences) and  (the number of queries), respectively. 
Each of the  subsequent lines contains a query in the format defined above.

Constraints

  • It is guaranteed that query type  will never query an empty sequence or index.

Output Format

For each type  query, print the updated value of  on a new line.

Sample Input

2 5
1 0 5
1 1 7
1 0 3
2 1 0
2 1 1

Sample Output

7
3

Explanation

Initial Values: 
 
 
 = [ ] 
 = [ ]

Query 0: Append  to sequence 
 
 = [5] 
 = [ ]

Query 1: Append  to sequence 
 = [5] 
 = [7]

Query 2: Append  to sequence 
 
 = [5, 3] 
 = [7]

Query 3: Assign the value at index  of sequence  to , print 
 
 = [5, 3] 
 = [7]

7

Query 4: Assign the value at index  of sequence  to , print 
 
 = [5, 3] 
 = [7]

3


풀이


솔직히 문제 이해가 잘 되지 않아서 애먹었다 -__-;;;;


^ 관련해서 검색해보면 설명이 잘 나와있는데 예를들어서


4 ^ 6 이라고 할 경우


4: 100

6: 110 

=  010 이라는 결과가 나오게 된다 


solution 이 예전과 달라졌는지 메소드에 n과 2차원 배열의 queries[][] 만 parameter 로 넘어온다.


정리해보면

- 처음 입력받는 n 에 따라서 배열의 크기가 설정된다.

- 처음 입력받는 q 로 2차원배열 행의 크키가 설정된다. (렬은 3으로 고정)

- 쿼리는 2종류가 있다 (1 or 2)

- 쿼리에 상관없이 index는  (x ^ lastAnswer) % N 을 계산한 값으로 한다.

- 쿼리가 1인 경우 y는 index에 해당하는 리스트 배열에 append 한다.
- 쿼리가 2인 경우 리스트 배열에서 index에 해당하는 리스트를 먼저 가져오고, 
- 그 리스트에서 y % 리스트배열[index].size 결과값에 해당하는 값을 가져와 lastAnswer로 지정한다.


static int[] dynamicArray(int n, int[][] queries) {
/*
* Write your code here.
*/
int lastAsw = 0;
int index = 0;
List<Integer>[] lists = new ArrayList[n];
List<Integer> resultList = new ArrayList<>();

// queries[i][1] = x;
// queries[i][2] = y;

for(int i = 0; i < queries.length; i++){
index = (queries[i][1]^lastAsw)%n;
if(queries[i][0] == 1){
if(lists[index] == null){
List<Integer> subList = new ArrayList<>();
subList.add(queries[i][2]);
lists[index] = subList;
} else {
lists[index].add(queries[i][2]);
}
} else if(queries[i][0] == 2){
lastAsw = lists[index].get(queries[i][2] % lists[index].size());
resultList.add(lastAsw);
}
}
int[] result = new int[resultList.size()];
for(int i = 0; i < result.length; i++){
result[i] = resultList.get(i);
}
return result;
}


'DataStructure > Array' 카테고리의 다른 글

Add One To Number  (0) 2018.10.15
Min Steps in Infinite Grid  (0) 2018.10.15
Left Rotation  (0) 2018.04.19
2D Array - DS  (0) 2018.04.13
Arrays - DS  (0) 2018.04.13