05-16 14:15
Recent Posts
Recent Comments
관리 메뉴

miinsun

[Algorithm]μ•Œκ³ λ¦¬μ¦˜ μžλ°”_68 νšŒμ˜μ‹€ λ°°μ • λ³Έλ¬Έ

Algorithm/Java

[Algorithm]μ•Œκ³ λ¦¬μ¦˜ μžλ°”_68 νšŒμ˜μ‹€ λ°°μ •

miinsun 2022. 3. 8. 22:31

πŸ’¬ λ¬Έμ œ μ„€λͺ…

ν•œ 개의 νšŒμ˜μ‹€μ΄ μžˆλŠ”λ° 이λ₯Ό μ‚¬μš©ν•˜κ³ μž ν•˜λŠ” n개의 νšŒμ˜λ“€μ— λŒ€ν•˜μ—¬ νšŒμ˜μ‹€ μ‚¬μš©ν‘œλ₯Ό λ§Œλ“€λ €κ³  ν•œλ‹€.

각 νšŒμ˜μ— λŒ€ν•΄ μ‹œμž‘μ‹œκ°„κ³Ό λλ‚˜λŠ” μ‹œκ°„μ΄ μ£Όμ–΄μ Έ 있고,
각 νšŒμ˜κ°€ κ²ΉμΉ˜μ§€ μ•Šκ²Œ ν•˜λ©΄μ„œ νšŒμ˜μ‹€μ„ μ‚¬μš©ν•  수 μžˆλŠ” μ΅œλŒ€μˆ˜μ˜ 회의λ₯Ό 찾아라.

단, νšŒμ˜λŠ” ν•œλ²ˆ μ‹œμž‘ν•˜λ©΄ 쀑간에 쀑단될 수 μ—†μœΌλ©° ν•œ νšŒμ˜κ°€ λλ‚˜λŠ” 것과 λ™μ‹œμ— λ‹€μŒ νšŒμ˜κ°€ μ‹œμž‘λ  수 μžˆλ‹€.

 

πŸ”¨ μž…μΆœλ ₯ 예

- μž…λ ₯ 

  • 첫째 쀄에 회의의 수 n(1<=n<=100,000)이 주어진닀.
  • λ‘˜μ§Έ 쀄뢀터 n+1 μ€„κΉŒμ§€ 각 회의의 정보가 μ£Όμ–΄μ§€λŠ”λ° 이것은 곡백을 사이에 두고 회의의 μ‹œμž‘μ‹œκ°„κ³Ό λλ‚˜λŠ” μ‹œκ°„μ΄ 주어진닀.
  • νšŒμ˜μ‹œκ°„μ€ 0μ‹œλΆ€ν„° μ‹œμž‘ν•œλ‹€.
  • 회의의 μ‹œμž‘μ‹œκ°„κ³Ό λλ‚˜λŠ” μ‹œκ°„μ˜ 쑰건은 (μ‹œμž‘μ‹œκ°„ <= λλ‚˜λŠ” μ‹œκ°„)μž…λ‹ˆλ‹€.
5
1 4
2 3
3 5
4 6
5 7

 

- 좜λ ₯

  •  μ²«μ§Έ 쀄에 μ΅œλŒ€ μ‚¬μš©ν•  수 μžˆλŠ” 회의 수λ₯Ό 좜λ ₯ν•˜μ—¬λΌ.
 
3

 

 

πŸ’» Solution.java

  1. λ¨Όμ € λλ‚˜λŠ” μˆœμ„ κΈ°μ€€μœΌλ‘œ μ •λ ¬ν•œλ‹€.
  2. κ°€μž₯ 빨리 λλ‚˜λŠ” 회의λ₯Ό κΈ°μ€€μœΌλ‘œ, λ‹€μŒ νšŒμ˜λŠ” λλ‚˜λŠ” μ‹œκ°„λ³΄λ‹€ ν¬κ±°λ‚˜ 같은 회의λ₯Ό μ„ νƒν•œλ‹€.
  3. λ§Œμ•½ λλ‚˜λŠ” μˆœμ„œκ°€ 같은 νšŒμ˜κ°€ 있으면, μ‹œμž‘ν•˜λŠ” μˆœμ„œλ₯Ό κΈ°μ€€μœΌλ‘œ 회의λ₯Ό μ •λ ¬ν•œλ‹€.
/* νšŒμ˜μ‹€ λ°°μ • :: 그리디 μ•Œκ³ λ¦¬ */

import java.util.*;
class Time implements Comparable<Time>{
	public int s, e;
	Time(int s, int e){
		this.s = s;
		this.e = e;
	}
	
	@Override
	public int compareTo(Time o) { //objectκ°€ 더 크면 음수 리턴, thisκ°€ 더 크면 μ–‘μˆ˜ 리턴, 같은 값은 0 리턴
		if(this.e == o.e) return this.s - o.s;
		else return this.e - o.e;
	}
}

public class Main {
	
	public int solution(ArrayList<Time> arr, int n) {
		int cnt = 0;
		
		Collections.sort(arr);
		int et = 0;
		for(Time ob : arr) {
			if(ob.s >= et) {
				cnt++;
				et=ob.e;
			}
		}
		return cnt;
 	}
	
	public static void main(String[] args){
		Main main = new Main();
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		ArrayList<Time> arr = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			int x = sc.nextInt();
			int y = sc.nextInt();
			arr.add(new Time(x, y));
		}
		
		System.out.println(main.solution(arr, n));
		
		sc.close();
		return ;
	}
}
Comments