Problem
John Watson knows of an operation called a right circular rotation on an array of integers. One rotation operation moves the last array element to the first position and shifts all remaining elements right one. To test Sherlock's abilities, Watson provides Sherlock with an array of integers. Sherlock is to perform the rotation operation a number of times then determine the value of the element at a given position.
For each array, perform a number of right circular rotations and return the value of the element at a given index.
For example, array , number of rotations, and indices to check, .
First we perform the two rotations:
Now return the values from the zero-based indices and as indicated in the array.
Function Description
Complete the circularArrayRotation function in the editor below. It should return an array of integers representing the values at the specified indices.
circularArrayRotation has the following parameter(s):
- a: an array of integers to rotate
- k: an integer, the rotation count
- queries: an array of integers, the indices to report
Input Format
The first line contains space-separated integers, , , and , the number of elements in the integer array, the rotation count and the number of queries.
The second line contains space-separated integers, where each integer describes array element (where ).
Each of the subsequent lines contains a single integer denoting , the index of the element to return from .
Constraints
Output Format
For each query, print the value of the element at index of the rotated array on a new line.
Sample Input 0
3 2 3
1 2 3
0
1
2
Sample Output 0
2
3
1
Explanation 0
After the first rotation, the array becomes .
After the second (and final) rotation, the array becomes .
Let's refer to the array's final state as array . For each query, we just have to print the value of on a new line:
- , .
- , .
- , .
How I solved the problem
# Math.abs(aCount - k + queryValue) % aCount 이런식으로 할 때 로테이션이 돈 후에 몇 번째 인덱스에 어떤 값이 있는지 알 수 있음.
# 만약 rotation의 횟수가 주어진 array의 개수보다 클 때, rotation의 횟수를 array의 개수로 한 번 나눠서 그 나머지의 값으로 넣어주면 됨.
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 | import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.regex.*; public class Solution { // Complete the circularArrayRotation function below. static int[] circularArrayRotation(int[] a, int k, int[] queries) { //a (given array) k (rotation) queries(queries' value is index) int aCount = a.length; int queryCount = queries.length; int[] result = new int[queryCount]; for (int i = 0; i < queryCount; i++) { int queryValue = queries[i]; if (k > aCount) { k = k % aCount; } result[i] = a[Math.abs(aCount - k + queryValue) % aCount]; } return result; } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); String[] nkq = scanner.nextLine().split(" "); int n = Integer.parseInt(nkq[0]); int k = Integer.parseInt(nkq[1]); int q = Integer.parseInt(nkq[2]); int[] a = new int[n]; String[] aItems = scanner.nextLine().split(" "); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); for (int i = 0; i < n; i++) { int aItem = Integer.parseInt(aItems[i]); a[i] = aItem; } int[] queries = new int[q]; for (int i = 0; i < q; i++) { int queriesItem = scanner.nextInt(); scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); queries[i] = queriesItem; } int[] result = circularArrayRotation(a, k, queries); for (int i = 0; i < result.length; i++) { bufferedWriter.write(String.valueOf(result[i])); if (i != result.length - 1) { bufferedWriter.write("\n"); } } bufferedWriter.newLine(); bufferedWriter.close(); scanner.close(); } } | cs |
[출처 : https://www.hackerrank.com ]
'1 Day 1 Algorithms' 카테고리의 다른 글
[2019.03.13] Jumping on the Clouds: Revisited (0) | 2019.03.13 |
---|---|
[2019.03.12] Sequence Equation (0) | 2019.03.12 |
[2019.03.07] Save the Prisoner! (0) | 2019.03.07 |
[2019.03.06] Viral Advertisement (0) | 2019.03.06 |
[2019.03.05] Beautiful Days at the Movies (0) | 2019.03.05 |
댓글