ahlight
개발 저장소
ahlight
전체 방문자
오늘
어제
  • 분류 전체보기 (197)
    • Java (7)
    • Spring (5)
    • JPA (2)
    • JavaScript (0)
    • Computer Science (12)
      • 디자인패턴, 프로그래밍 패러다임 (1)
      • 네트워크 (4)
      • 운영체제 (4)
      • 데이터베이스 (3)
      • 자료구조 (0)
    • 알고리즘 (1)
    • 프로그래머스 (13)
    • 백준 (94)
    • 서평 (3)
    • 회고 (1)
    • TIL (58)
    • 기타 (1)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

  • 클린코드
  • TDD
  • Java
  • 넥스트스텝
  • 라즈베리파이4 #홈서버 #포트포워딩 #dhcp

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ahlight

개발 저장소

백준

백준 1931번 자바☆

2023. 2. 15. 22:10

문제

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

 

1931번: 회의실 배정

(1,4), (5,7), (8,11), (12,14) 를 이용할 수 있다.

www.acmicpc.net

 

1. 접근 방식

역시 그리디 관련 문제는 처음 접근이 가장 중요한것 같다..

 

처음 접근을 회의 시작시간을 정렬하는 방법으로 했는데 결국 점점 산으로가 포기했다.

정답은 회의 종료시간 순으로 정렬을 시키고 종료시간이 같을 경우 시작시간 순으로 정렬을 해야한다.

 

해당 접근만 주의하면 크게 어렵지 않은 문제다.

 

2. 구현

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

	static int N;
	static Convention[] conventions;
	static int cnt;
	
	static class Convention implements Comparable<Convention> {
		long start;
		long end;

		public Convention(long start, long end) {
			this.start = start;
			this.end = end;
		}

		@Override
		public int compareTo(Convention o) {
        	//종료시간이 같다면 시작시간 순으로 정렬
			if (this.end == o.end) {
				return (int)(this.start - o.start);
			}
			return (int)(this.end - o.end);
		}
	}
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine());
		conventions = new Convention[N];
		for (int i=0; i<N; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			int start = Integer.parseInt(st.nextToken());
			int end = Integer.parseInt(st.nextToken());
			int gap = end - start;
			conventions[i] = new Convention(start, end);
		}
		Arrays.sort(conventions);
		
		long end = -1;
		for (int i=0; i<N; i++) {
			if (conventions[i].start >= end) {
				end = conventions[i].end;
				cnt++;
			}
		}
		System.out.println(cnt);
	}
}

 

'백준' 카테고리의 다른 글

백준 1747번 자바  (0) 2023.02.20
백준 1541번 자바  (0) 2023.02.15
백준 1456번 자바 ☆  (0) 2023.02.15
백준 1929번 자바  (0) 2023.02.15
백준 1744번 자바 ☆  (0) 2023.02.11
    '백준' 카테고리의 다른 글
    • 백준 1747번 자바
    • 백준 1541번 자바
    • 백준 1456번 자바 ☆
    • 백준 1929번 자바
    ahlight
    ahlight

    티스토리툴바