๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋‹จ์†์นด๋ฉ”๋ผ ... ํƒ์š•๋ฒ•

๐ŸŽฎinspirer9 2023. 2. 27. 15:45
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

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

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

์ œํ•œ ์‚ฌํ•ญ

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

์ž…์ถœ๋ ฅ ์˜ˆ

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

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

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

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

ํ’€์ด

  • ํƒ์š•๋ฒ• ๊ทธ๋Ÿฐ๊ฑฐ ๋ชจ๋ฅด๊ฒ ๊ณ  ๊ทธ๋ƒฅ ๊ณ„์‚ฐ ๋ฌธ์ œ... ใ…ก.ใ…ก
  • ๋ฌด์ง€์„ฑ ์ฝ”๋”ฉ์œผ๋กœ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.
def solution(routes):
    routes.sort(key=lambda x:(x[1],x[0]))
    ์•ˆ์ฐํžŒ์ฐจ๋Ÿ‰ = len(routes)
    ์ฐฐ์นต = [1 for _ in range(len(routes))]
    
    ์นด๋ฉ”๋ผ_์„ค์น˜_์ˆ˜๋Ÿ‰ = 0
    ์นด๋ฉ”๋ผ_index = 0
    ์นด๋ฉ”๋ผ_์œ„์น˜ = routes[์นด๋ฉ”๋ผ_index][1]
    
    while ์•ˆ์ฐํžŒ์ฐจ๋Ÿ‰ > 0:
        for i in range(์นด๋ฉ”๋ผ_index,len(routes)):
            if ์ฐฐ์นต[i] == 1 and routes[i][0] <= ์นด๋ฉ”๋ผ_์œ„์น˜ and ์นด๋ฉ”๋ผ_์œ„์น˜ <= routes[i][1]:
                ์ฐฐ์นต[i] = 0
                ์•ˆ์ฐํžŒ์ฐจ๋Ÿ‰ -= 1
        for i in range(์นด๋ฉ”๋ผ_index,len(routes)):
            if ์ฐฐ์นต[i] == 1:
                ์นด๋ฉ”๋ผ_index = i
                ์นด๋ฉ”๋ผ_์œ„์น˜ = routes[์นด๋ฉ”๋ผ_index][1]
                ์นด๋ฉ”๋ผ_์„ค์น˜_์ˆ˜๋Ÿ‰ += 1
                break
    return ์นด๋ฉ”๋ผ_์„ค์น˜_์ˆ˜๋Ÿ‰ + 1
  • ํšจ์œจ์„ฑ ๋‚ฎ์€๊ฑฐ ๋ณด๋‹ˆ ์ •๋‹ต์€ ๋”ฐ๋กœ ์žˆ๋‚˜ ๋ด„
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.04ms, 10.5MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.06ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.09ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.6MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.3MB)
ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (6.44ms, 10.5MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (3.22ms, 10.5MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (14.72ms, 10.9MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.22ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (20.49ms, 11MB)
  • ๊ณ ์ˆ˜์˜ ํ’€์ด... ์ง€๋ ธ๋‹ค.
    • ๋‚œ ๋žŒ๋‹ค ์†ŒํŒ…ํ•  ๋•Œ ๊ณ ๋ฏผ ์—„์ฒญํ–ˆ๋Š”๋ฐ(์‹ฌ์ง€์–ด x[1]-x[0]ํ•ด์„œ ์งง์€ ๊ตฌ๊ฐ„์œผ๋กœ ์ฐจ๋Ÿ‰๋“ค ๋ชจ์œผ๊ณ  ๊ทธ๋žฌ๋‹ค)
      • ์ด ๋ถ„์€ ๊ทธ๋ƒฅ ์ฟจํ•˜๊ฒŒ x[1]ํ•ด๋ฒ„๋ฆผ
    • ๋งˆ์ง€๋ง‰ ์นด๋ฉ”๋ผ๊ฐ€ -3๋งŒ์— ์žˆ์–ด. ์ด๊ฒŒ ์™œ ๋ผ? ํ–ˆ๋Š”๋ฐ
      • ๋งˆ์ง€๋ง‰ ์นด๋ฉ”๋ผ๊ฐ€ ์ œ์ผ ๋งˆ์ง€๋ง‰์— ๋‚˜๊ฐ„ ๋…€์„๋ณด๋‹ค ์•ž์ด๋ฉด ์นด๋ฉ”๋ผ 1๋Œ€ ์„ค์น˜...
      • ๊ทธ๋ ‡๊ฒŒ ์•ž์ชฝ์œผ๋กœ ์ด๋™ํ•˜๋ฉด์„œ ๊ฒน์น˜๋Š” ๊ฒƒ๋“ค ์ถ”๋ฆฌ๋Š” ๊ฑด๊ฐ€๋ด„...
        • ๋‚˜์ฒ˜๋Ÿผ ์•ž๋’ค๋กœ ์นด๋ฉ”๋ผ ์œ„์น˜ ์ฒดํฌํ•  ํ•„์š”๊ฐ€ ์—†์—ˆ๋‹ค.
def solution(routes):
    routes = sorted(routes, key=lambda x: x[1])
    last_camera = -30000

    answer = 0

    for route in routes:
        if last_camera < route[0]:
            answer += 1
            last_camera = route[1]

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํšจ์œจ์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.61ms, 10.5MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.40ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.70ms, 10.5MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.05ms, 10.3MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (1.28ms, 10.6MB)

ํƒ์š•๋ฒ• ๋ฌธ์ œ ํ•˜๋‚˜ ๋‚จ์•˜๋‹ค.

728x90
๋ฐ˜์‘ํ˜•