알고리즘 사이트 : https://app.codility.com/programmers/


안녕하세요.


오늘은 Lesson2의 1번 문제, CyclicRotation 를 풀어 보았습니다.




An array A consisting of N integers is given. Rotation of the array means that each element is shifted right by one index, and the last element of the array is moved to the first place. For example, the rotation of array A = [3, 8, 9, 7, 6] is [6, 3, 8, 9, 7] (elements are shifted right by one index and 6 is moved to the first place).

The goal is to rotate array A K times; that is, each element of A will be shifted to the right K times.

Write a function:

class Solution { public int[] solution(int[] A, int K); }

that, given an array A consisting of N integers and an integer K, returns the array A rotated K times.

For example, given

A = [3, 8, 9, 7, 6] K = 3

the function should return [9, 7, 6, 3, 8]. Three rotations were made:

[3, 8, 9, 7, 6] -> [6, 3, 8, 9, 7] [6, 3, 8, 9, 7] -> [7, 6, 3, 8, 9] [7, 6, 3, 8, 9] -> [9, 7, 6, 3, 8]

For another example, given

A = [0, 0, 0] K = 1

the function should return [0, 0, 0]

Given

A = [1, 2, 3, 4] K = 4

the function should return [1, 2, 3, 4]

Assume that:

  • N and K are integers within the range [0..100];
  • each element of array A is an integer within the range [−1,000..1,000].

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

Copyright 2009–2018 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.




CyclicRotation 문제는 A 배열 속에 있는 값을 오른쪽으로 K번 옮기는 문제 입니다.

예를 들어 A = {3, 5, 7} 이고 K = 2라면 2번이 옮겨지니 A = {5, 7, 3} 이 정답이 됩니다.



제가 풀이한 자바 알고리즘 (100%) 입니다.

다 풀고 나서 다른 분들의 풀이를 찾아보니 정말 다양한 방법으로 문제를 푸시더라구요.

제가 풀이한 답이 좋은 알고리즘이라고 할 순 없지만 100%로 통과했으니 참고 하시기 바랍니다!



제가 푼 방법입니다.


1. 새로운 배열 B를 생성

2. for문 속에 값을 저장해 놓을 temp 변수 선언

3. for문 속 순번(배열 번호)+K 값이 배열 마지막 숫자보다 큰 가를 비교.

4. 크다면 해당 값을 temp에 저장하고 배열번호-(배열크기-K값) 을 (배열크기-1)로 나눈 나머지 값을 B 배열 번호로 지정해 temp값 저장.

5. 작다면 배열번호-(배열크기-K값) 값을 B 배열 번호로 지정해 temp값 저장.

6. A=B



* 예외사항


1. 배열 크기는 0이상 100 이하이다.

2. K의 크기는 0이상 100 이하이다.

3. A배열 속에 있는 원소 값은 -1000이상 1000이하 이다.





 class Solution {

public int[] solution(int[] A, int K) { int B[] = new int[A.length]; if(A.length>=0 && A.length<=100 && K>=0 && K<=100) { for(int i = 0 ; i < A.length; i++) { if(A[i] > 1000 || A[i] < -1000) { System.out.println("ERROR : each element of array A is an integer within the range -1000< <1000"); }else { int temp; if( (i+K) > (A.length-1) ) { temp = A[i]; if( (i-(A.length-K)) > A.length-1 ) { B[(i-(A.length-K)) % A.length] = temp; }else { B[i-(A.length-K)] = temp; } }else { temp = A[i]; B[i+K] = temp; } } } A=B; }else { System.out.println("ERROR : N and K are integers within the range 0< <100"); } return A; } }





소스에 문제가 있거나, 더 나은 방향으로 제시해 주시면 너무나 감사하겠습니다.


이상 포스팅을 마치겠습니다.









'알고리즘 풀이' 카테고리의 다른 글

Codility ] Lesson1 : BinaryGap  (0) 2018.04.30
Level 1 수박수박수박수박수박수?  (0) 2017.03.30
Level 2 2016년  (0) 2017.03.30
Level 2 최솟값 만들기  (0) 2017.03.30
Level 1 행렬의 덧셈  (0) 2017.03.30

+ Recent posts