algorithm/문제 풀이

백준 11650번 (좌표 정렬하기) 자바 Java11

ssoheeh 2021. 1. 8. 01:58

문제 : 좌표 정렬하기

시간 제한 : 1 sec

메모리 제한 : 256 MB

 

 

 

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
import java.util.*;
 
class Point implements Comparator<Point>{
    int xpos;
    int ypos;
    Point(){
        
    }
    Point(int x, int y){
        this.xpos = x;
        this.ypos = y;
    }
    
    public int compare(Point o1, Point o2) {
        // TODO Auto-generated method stub
        if(o1.xpos==o2.xpos) {
            return o1.ypos-o2.ypos;
        }else
            return o1.xpos-o2.xpos;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Point> p = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            p.add(new Point(sc.nextInt(),sc.nextInt()));
        }
        Collections.sort(p, new Point());
        for (int i = 0; i < n; i++) {
            System.out.println(p.get(i).xpos+" "+p.get(i).ypos);
        }
    }
}
cs

 

배열로 주구장창 돌렸는데 시간초과로 화나서 미뤄뒀던 문제,,,

오늘 갑자기 풀어볼까 하고 아예 새로 다시 풀었더니 풀렸다

Comparator 사용하고 새로운 Class도 만들어서 푼 문제는 백준에서 처음인 듯!

시간 초과로 안 풀렸던 문제 풀리면 화나는데 기분 좋아 :( 이게 뭔 기분일까