๐Ÿ“น[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋‹จ์†์นด๋ฉ”๋ผ

Chobbyยท2022๋…„ 3์›” 24์ผ
0

Programmers

๋ชฉ๋ก ๋ณด๊ธฐ
24/349

๋ฌธ์ œ ์„ค๋ช…

๊ณ ์†๋„๋กœ๋ฅผ ์ด๋™ํ•˜๋Š” ๋ชจ๋“  ์ฐจ๋Ÿ‰์ด ๊ณ ์†๋„๋กœ๋ฅผ ์ด์šฉํ•˜๋ฉด์„œ ๋‹จ์†์šฉ ์นด๋ฉ”๋ผ๋ฅผ ํ•œ ๋ฒˆ์€ ๋งŒ๋‚˜๋„๋ก ์นด๋ฉ”๋ผ๋ฅผ ์„ค์น˜ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๊ณ ์†๋„๋กœ๋ฅผ ์ด๋™ํ•˜๋Š” ์ฐจ๋Ÿ‰์˜ ๊ฒฝ๋กœ routes๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋ชจ๋“  ์ฐจ๋Ÿ‰์ด ํ•œ ๋ฒˆ์€ ๋‹จ์†์šฉ ์นด๋ฉ”๋ผ๋ฅผ ๋งŒ๋‚˜๋„๋ก ํ•˜๋ ค๋ฉด ์ตœ์†Œ ๋ช‡ ๋Œ€์˜ ์นด๋ฉ”๋ผ๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•˜์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • ์ฐจ๋Ÿ‰์˜ ๋Œ€์ˆ˜๋Š” 1๋Œ€ ์ด์ƒ 10,000๋Œ€ ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • routes์—๋Š” ์ฐจ๋Ÿ‰์˜ ์ด๋™ ๊ฒฝ๋กœ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉฐ routes[i][0]์—๋Š” i๋ฒˆ์งธ ์ฐจ๋Ÿ‰์ด ๊ณ ์†๋„๋กœ์— ์ง„์ž…ํ•œ ์ง€์ , routes[i][1]์—๋Š” i๋ฒˆ์งธ ์ฐจ๋Ÿ‰์ด ๊ณ ์†๋„๋กœ์—์„œ ๋‚˜๊ฐ„ ์ง€์ ์ด ์ ํ˜€ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ฐจ๋Ÿ‰์˜ ์ง„์ž…/์ง„์ถœ ์ง€์ ์— ์นด๋ฉ”๋ผ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์–ด๋„ ์นด๋ฉ”๋ผ๋ฅผ ๋งŒ๋‚œ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค.
  • ์ฐจ๋Ÿ‰์˜ ์ง„์ž… ์ง€์ , ์ง„์ถœ ์ง€์ ์€ -30,000 ์ด์ƒ 30,000 ์ดํ•˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

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

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

-5 ์ง€์ ์— ์นด๋ฉ”๋ผ๋ฅผ ์„ค์น˜ํ•˜๋ฉด ๋‘ ๋ฒˆ์งธ, ๋„ค ๋ฒˆ์งธ ์ฐจ๋Ÿ‰์ด ์นด๋ฉ”๋ผ๋ฅผ ๋งŒ๋‚ฉ๋‹ˆ๋‹ค.

-15 ์ง€์ ์— ์นด๋ฉ”๋ผ๋ฅผ ์„ค์น˜ํ•˜๋ฉด ์ฒซ ๋ฒˆ์งธ, ์„ธ ๋ฒˆ์งธ ์ฐจ๋Ÿ‰์ด ์นด๋ฉ”๋ผ๋ฅผ ๋งŒ๋‚ฉ๋‹ˆ๋‹ค.

ํ’€์ด ์‹œ์ž‘

function solution(routes) {
    routes.sort((a,b) => a[0] - b[0])
    // ์ฒ˜์Œ ์ฐจ๋ฅผ ๊ฒ€์‚ฌํ•ด์•ผํ•จ
    let setCamera = 1
    let prevOut = routes[0][1]
    for(let i = 1 ; i < routes.length ; i ++) {
        const [carIn,carOut] = routes[i]
        // ๊ฒน์น˜์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ ๋‚˜๊ฐ€๋Š” ์‹œ์ ์„ ํ˜„์žฌ ์‹œ์ ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ์นด๋ฉ”๋ผ๋ฅผ 1๋Œ€ ์ถ”๊ฐ€ ์„ค์น˜ํ•œ๋‹ค
        if(prevOut < carIn) {
            setCamera++
            prevOut = carOut
        } 
        
        // ๊ฒน์นœ ๊ฒฝ์šฐ ์ด์ „ ์ฐจ๋Ÿ‰๋ณด๋‹ค ํ˜„์žฌ ์ฐจ๋Ÿ‰์ด ๋จผ์ € ๋‚˜๊ฐ„๋‹ค๋ฉด ๋‹ค์Œ ์นด๋ฉ”๋ผ ์„ค์น˜๋ฅผ ์œ„ํ•ด ํ˜„์žฌ ์ฐจ๋Ÿ‰์˜ ๋‚˜๊ฐ€๋Š” ์‹œ์ ์œผ๋กœ ์‹œ์ ์„ ๋ณ€๊ฒฝ
        if(prevOut > carOut) {
            prevOut = carOut
        }
    }
    return setCamera
}
profile
๋‚ด ์ง€์‹์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€๋‹ดํ•จ

0๊ฐœ์˜ ๋Œ“๊ธ€