๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ
 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ฒด์œก๋ณต

์ ์‹ฌ์‹œ๊ฐ„์— ๋„๋‘‘์ด ๋“ค์–ด, ์ผ๋ถ€ ํ•™์ƒ์ด ์ฒด์œก๋ณต์„ ๋„๋‚œ๋‹นํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ ์—ฌ๋ฒŒ ์ฒด์œก๋ณต์ด ์žˆ๋Š” ํ•™์ƒ์ด ์ด๋“ค์—๊ฒŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ฃผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๋Š” ์ฒด๊ฒฉ ์ˆœ์œผ๋กœ ๋งค๊ฒจ์ ธ ์žˆ์–ด, ๋ฐ”๋กœ ์•ž๋ฒˆ๏ฟฝ

programmers.co.kr

 

๋ฌธ์ œ

์ ์‹ฌ์‹œ๊ฐ„์— ๋„๋‘‘์ด ๋“ค์–ด, ์ผ๋ถ€ ํ•™์ƒ์ด ์ฒด์œก๋ณต์„ ๋„๋‚œ๋‹นํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ ์—ฌ๋ฒŒ ์ฒด์œก๋ณต์ด ์žˆ๋Š” ํ•™์ƒ์ด ์ด๋“ค์—๊ฒŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ฃผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๋Š” ์ฒด๊ฒฉ ์ˆœ์œผ๋กœ ๋งค๊ฒจ์ ธ ์žˆ์–ด, ๋ฐ”๋กœ ์•ž๋ฒˆํ˜ธ์˜ ํ•™์ƒ์ด๋‚˜ ๋ฐ”๋กœ ๋’ท๋ฒˆํ˜ธ์˜ ํ•™์ƒ์—๊ฒŒ๋งŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 4๋ฒˆ ํ•™์ƒ์€ 3๋ฒˆ ํ•™์ƒ์ด๋‚˜ 5๋ฒˆ ํ•™์ƒ์—๊ฒŒ๋งŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒด์œก๋ณต์ด ์—†์œผ๋ฉด ์ˆ˜์—…์„ ๋“ค์„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ฒด์œก๋ณต์„ ์ ์ ˆํžˆ ๋นŒ๋ ค ์ตœ๋Œ€ํ•œ ๋งŽ์€ ํ•™์ƒ์ด ์ฒด์œก์ˆ˜์—…์„ ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ „์ฒด ํ•™์ƒ์˜ ์ˆ˜ n, ์ฒด์œก๋ณต์„ ๋„๋‚œ๋‹นํ•œ ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด lost, ์—ฌ๋ฒŒ์˜ ์ฒด์œก๋ณต์„ ๊ฐ€์ ธ์˜จ ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด reserve๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ฒด์œก์ˆ˜์—…์„ ๋“ค์„ ์ˆ˜ ์žˆ๋Š” ํ•™์ƒ์˜ ์ตœ๋Œ“๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์กฐ๊ฑด

  • ์ „์ฒด ํ•™์ƒ์˜ ์ˆ˜๋Š” 2๋ช… ์ด์ƒ 30๋ช… ์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • ์ฒด์œก๋ณต์„ ๋„๋‚œ๋‹นํ•œ ํ•™์ƒ์˜ ์ˆ˜๋Š” 1๋ช… ์ด์ƒ n๋ช… ์ดํ•˜์ด๊ณ  ์ค‘๋ณต๋˜๋Š” ๋ฒˆํ˜ธ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  • ์—ฌ๋ฒŒ์˜ ์ฒด์œก๋ณต์„ ๊ฐ€์ ธ์˜จ ํ•™์ƒ์˜ ์ˆ˜๋Š” 1๋ช… ์ด์ƒ n๋ช… ์ดํ•˜์ด๊ณ  ์ค‘๋ณต๋˜๋Š” ๋ฒˆํ˜ธ๋Š” ์—†์Šต๋‹ˆ๋‹ค.
  • ์—ฌ๋ฒŒ ์ฒด์œก๋ณต์ด ์žˆ๋Š” ํ•™์ƒ๋งŒ ๋‹ค๋ฅธ ํ•™์ƒ์—๊ฒŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์—ฌ๋ฒŒ ์ฒด์œก๋ณต์„ ๊ฐ€์ ธ์˜จ ํ•™์ƒ์ด ์ฒด์œก๋ณต์„ ๋„๋‚œ๋‹นํ–ˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ์ด ํ•™์ƒ์€ ์ฒด์œก๋ณต์„ ํ•˜๋‚˜๋งŒ ๋„๋‚œ๋‹นํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉฐ, ๋‚จ์€ ์ฒด์œก๋ณต์ด ํ•˜๋‚˜์ด๊ธฐ์— ๋‹ค๋ฅธ ํ•™์ƒ์—๊ฒŒ๋Š” ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ค„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ

n lost reserve return
5 [2, 4] [1, 3, 5] 5
5 [2, 4] [3] 4
3 [3] [1] 2

 

ํ’€์ด

def solution(n, lost, reserve):
    answer = 0

    ls = len(lost)

    for l in lost:
        if l in reserve:
            lost.pop(lost.index(l))
            reserve.pop(reserve.index(l))
            ls -= 1

    for l in lost:
        front = l -1
        back = l + 1
        if front in reserve:
            reserve.pop(reserve.index(front))
            ls -= 1
        elif back in reserve:
            reserve.pop(reserve.index(back))
            ls -= 1
        elif l in reserve:
            reserve.pop(reserve.index(l))
            ls -= 1

    answer = n - ls
    return answer

๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ž˜ ์ƒ๊ฐํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ. ๋‚ด๊ฐ€ ๊ฐ„๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋˜ ์ผ€์ด์Šค๋Š” ์ฒด์œก๋ณต์„ ์žƒ์–ด๋ฒ„๋ฆฐ ํ•™์ƒ๊ณผ ์—ฌ๋ฒŒ์˜ ์ฒด์œก๋ณต์ด ์žˆ๋Š” ํ•™์ƒ์ด ๊ฐ™์„ ๊ฒฝ์šฐ์˜€๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ๋ง๊ณ ๋„ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ์ฒดํฌํ•˜๋Š” ์—ฐ์Šต์ด ํ•„์š”ํ•˜๋‹ค๊ณ  ๋Š๊ผˆ์Œ.

๋ฐ˜์‘ํ˜•