[백준] 11650번: 좌표 정렬하기

ByWindow·2021년 8월 31일
0

Algorithm

목록 보기
55/104
post-thumbnail

📝문제

오랜만에 정렬 메서드를 사용하는 문제를 풀게 되었다.
Comparator 인터페이스에 문제에서 제시하는 조건을 넣어서 해결했다.
출력부분에 괜히 이중 for문 사용했다가 시간초과가 났다ㅎㅎ 그냥 좋게 for문 하나만 돌도록 했다.

📌코드

package Baekjoon;

/**
 * comparator 사용하는 방법 : 시간초과 -> 출력 부분 이중for문 사용 안 하니까 해결됨
 */

import java.util.*;
import java.io.*;

public class BOJ11650 {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        StringTokenizer st;
        int[][] pos = new int[n][2];
        for(int i = 0; i < n; i++){
            st = new StringTokenizer(br.readLine());
            for(int j = 0; j < 2; j++){
                pos[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        Arrays.sort(pos, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if(o1[0] == o2[0]) return o1[1] - o2[1];
                else return o1[0] - o2[0];
            }
        });
        for(int[] i : pos){
            System.out.println(i[0] + " " + i[1]);
        }
    }
}
profile
step by step...my devlog

0개의 댓글