ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋‹จ์†์นด๋ฉ”๋ผ

[[-20,15], [-14,-5], [-18,-13], [-5,-3]] 2

programmers.co.kr

 

๋ฌธ์ œ ํ’€์ด

 ๊ณ ์†๋„๋กœ๋ฅผ ์ด๋™ํ•˜๋Š” ๋ชจ๋“  ์ฐจ๋Ÿ‰์ด ๊ณ ์†๋„๋กœ๋ฅผ ์ด์šฉํ•˜๋ฉด์„œ ๋‹จ์†์šฉ ์นด๋ฉ”๋ผ๋ฅผ ํ•œ ๋ฒˆ์€ ๋งŒ๋‚˜๋„๋ก ์นด๋ฉ”๋ผ๋ฅผ ์„ค์น˜ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ์ด๋•Œ ์ตœ์†Œ ๋ช‡ ๋Œ€์˜ ์นด๋ฉ”๋ผ๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•˜๋Š”์ง€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ์ ‘ํ•˜์˜€์„ ๋•Œ๋Š” ์นด๋ฉ”๋ผ๋ฅผ ๋งŒ๋‚ฌ๋Š”์ง€ ๋ชจ๋“  ๊ตฌ๊ฐ„์„ ํ™•์ธํ•˜๋ฉฐ ๋ฌธ์ œ๋ฅผ ํ’€๊ณ ์ž ํ•˜์˜€๋‹ค. ์ •๋‹ต์€ ๋งž์•˜์ง€๋งŒ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ํ‘ผ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ๋” ์งง๊ณ , ๋น ๋ฅด๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋‹ค.๐Ÿ˜‚

 

 ์„ค์น˜๋œ ์นด๋ฉ”๋ผ๋ฅผ ์ง„์ž… ์ง€์ ์œผ๋กœ ๊ฐฑ์‹ ํ•ด ๊ฐ€๋ฉฐ, ์ง„์ž… ์ง€์ ์ด ์„ค์น˜๋œ ์นด๋ฉ”๋ผ์˜ ์œ„์น˜๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์นด๋ฉ”๋ผ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์œ„์น˜๋ฅผ ๊ฐฑ์‹ ํ•˜๋ฉด `O(N)`์˜ ์‹œ๊ฐ„์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

from math import inf


IN, OUT = 0, 1


def solution(routes):
    answer = 0
    routes.sort(key=lambda x: x[OUT])
    camera = -inf

    for route in routes:
        if route[IN] > camera:
            camera = route[OUT]
            answer += 1

    return answer

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€