728x90
문제
: 주어진 수를 정렬하되, 메모리랑 시간을 절약하라는 문제
문제 링크 : https://www.acmicpc.net/problem/10989
원리
1. ArrayList로 값을 받아 Collections를 이용한 sort [메모리 초과] ...호오...역시 호락호락하지 않군요오
2. int[ ]로 받아서 정렬 [ 통과 ] ...근데 메모리 384776KB에 시간 2716ms....^-^....
나의 코드
1. ArrayList 방식
더보기
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
List<Integer> list = new ArrayList<>();
for(int i=0; i<T ; i++) list.add(Integer.parseInt(br.readLine()));
list.stream().sorted().forEach(a -> {
try {
bw.write(""+a+"\n");
} catch (IOException e) {
throw new RuntimeException(e);
}
});
bw.close();
}
}
2. Array 방식
public class no10989 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(br.readLine());
int[] arr = new int[T];
for(int i=0; i<T ; i++) arr[i] = Integer.parseInt(br.readLine());
Arrays.stream(arr).sorted().forEach(a -> {
try {
bw.write(""+a+"\n");
} catch (IOException e) {
throw new RuntimeException(e);
}
});
bw.close();
}
}
레퍼런스 코드
참고 링크 : https://st-lab.tistory.com/107
728x90
'알고리즘 저장소 (일반방식과 나만의 풀이) > JAVA' 카테고리의 다른 글
[코딩테스트] 줌 인터넷 (ZUM) (0) | 2023.01.22 |
---|---|
[백준] no2108: 통계학 (0) | 2023.01.21 |
[백준] no2839: 설탕 배달 (0) | 2023.01.20 |
[백준] no7568: 덩치 (0) | 2023.01.19 |
[백준] no11650: 좌표 정렬하기 (0) | 2023.01.18 |