개발자-H 입니다.

BOJ - 좌표 정렬하기 본문

Algorithm/문제 풀이

BOJ - 좌표 정렬하기

개발자-H 2021. 8. 28. 14:57

 

https://www.acmicpc.net/problem/11650

 

11650번: 좌표 정렬하기

첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000) 좌표는 항상 정수이고, 위치가 같은 두 점은 없다.

www.acmicpc.net

 

  • 주어진 정렬 조건을 이용하여 우선 순위 큐에 넣고 쭈욱 출력했다.
  • 1788ms 풀이법은 Node에 ToString을 구현하여 넣은 방법이다
  • 916ms 풀이법은 큐를 출력하는 While문 안에서 String을 만들어 Builder에 넣고 출력했다.
  • 880ms 풀이법에서는 ToStirng 구현에서 사용했었던 String.format을 변경하였다.
  • String.format 함수가 문자열 바인딩 함수가 편하긴한데 생각보다 많이 느렸다.
  • 다른 사람 답안을 보니 정렬 문제라 그런지 배열에 넣고 Sort 돌린 답안들이 많았다.

 

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.PriorityQueue;


public class Main {
    public static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[] args) throws Exception {
        int N = Integer.parseInt(br.readLine());

        PriorityQueue<Node> nodes = new PriorityQueue<>();

        for (int i = 0; i < N; i++) {
            String[] s = br.readLine().split(" ");
            int x = Integer.parseInt(s[0]);
            int y = Integer.parseInt(s[1]);

            nodes.offer(new Node(x, y));
        }

        StringBuilder builder = new StringBuilder();
        while (!nodes.isEmpty()) {
            builder.append(nodes.poll());
        }

        System.out.println(builder);
    }

    static class Node implements Comparable<Node> {
        public int x;
        public int y;

        public Node(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int compareTo(Node other) {
            if (this.x == other.x) {
                return this.y - other.y;
            }
            return this.x - other.x;
        }

        @Override
        public String toString() {
            return this.x + " " + this.y + "\n";
        }
    }
}

 

 

 

'Algorithm > 문제 풀이' 카테고리의 다른 글

BOJ - 트리의 부모 찾기  (0) 2021.08.30
BOJ - 부녀회장이 될테야  (0) 2021.08.29
프로그래머스 - 카카오프렌즈 컬러링북  (0) 2021.08.17
BOJ - 동전 0  (0) 2021.08.16
BOJ - ATM  (0) 2021.08.16
Comments