문제 난이도 : Easy
문제 유형 : Data Structures - Arrays
문제 설명 간략 :
배열을 주어진 int만큼 왼쪽으로 돌려라.
idea
돌린 위치부터 담고 나머지를 처음부터 순서대로 담는다.
제약사항
- 1 <= n <= 10^5
- 1 <= d <= n
- 1 <= a[i] <= 10^6
자바 풀이
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'rotateLeft' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. INTEGER d
* 2. INTEGER_ARRAY arr
*/
public static List<Integer> rotateLeft(int d, List<Integer> arr) {
List<Integer> rotated = new ArrayList<Integer>();
for(int i = d; i < arr.size(); i++) {
rotated.add(arr.get(i));
}
for(int i = 0; i < d; i++) {
rotated.add(arr.get(i));
}
return rotated;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int n = Integer.parseInt(firstMultipleInput[0]);
int d = Integer.parseInt(firstMultipleInput[1]);
List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> result = Result.rotateLeft(d, arr);
bufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(joining(" "))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
출처