https://school.programmers.co.kr/learn/courses/30/lessons/120876

 

๋ฌธ์ œ

 

๋ฌธ์ œ ์„ค๋ช…

์„ ๋ถ„ 3๊ฐœ๊ฐ€ ํ‰ํ–‰ํ•˜๊ฒŒ ๋†“์—ฌ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธ ์„ ๋ถ„์˜ ์‹œ์ž‘๊ณผ ๋ ์ขŒํ‘œ๊ฐ€ [[start, end], [start, end], [start, end]] ํ˜•ํƒœ๋กœ ๋“ค์–ด์žˆ๋Š” 2์ฐจ์› ๋ฐฐ์—ด lines๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋‘ ๊ฐœ ์ด์ƒ์˜ ์„ ๋ถ„์ด ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์˜ ๊ธธ์ด๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด๋ณด์„ธ์š”.

lines๊ฐ€ [[0, 2], [-3, -1], [-2, 1]]์ผ ๋•Œ ๊ทธ๋ฆผ์œผ๋กœ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์„ ๋ถ„์ด ๋‘ ๊ฐœ ์ด์ƒ ๊ฒน์นœ ๊ณณ์€ [-2, -1], [0, 1]๋กœ ๊ธธ์ด 2๋งŒํผ ๊ฒน์ณ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

์ œํ•œ ์‚ฌํ•ญ

  • lines์˜ ๊ธธ์ด = 3
  • lines์˜ ์›์†Œ์˜ ๊ธธ์ด = 2
  • ๋ชจ๋“  ์„ ๋ถ„์€ ๊ธธ์ด๊ฐ€ 1 ์ด์ƒ์ž…๋‹ˆ๋‹ค.
  • lines์˜ ์›์†Œ๋Š” [a, b] ํ˜•ํƒœ์ด๋ฉฐ, a, b๋Š” ๊ฐ๊ฐ ์„ ๋ถ„์˜ ์–‘ ๋์  ์ž…๋‹ˆ๋‹ค.
  • -100 ≤ a < b ≤ 100

 

์ž…์ถœ๋ ฅ ์˜ˆ

lines result
[[0, 1], [2, 5], [3, 9]] 2
[[-1, 1], [1, 3], [3, 9]] 0
[[0, 5], [3, 9], [1, 10]] 8

 

 

์†Œ์Šค ์ฝ”๋“œ
# 1
def solution(lines):
    answer = 0
    count = [0]*200
    
    starts = []
    ends = []

    for line in lines:
        start, end = line

        starts.append(start)
        ends.append(end)

        for num in range(start, end):
            count[num+100] += 1

    for cnt in count[100+min(starts):100+max(ends)+1]:
        if cnt > 1:
            answer += 1

    return answer

 

ํ’€์ด # 1

  • ์›์†Œ๊ฐ€ 0์ธ ๊ธธ์ด 200์˜ ๋ฆฌ์ŠคํŠธ count ์ƒ์„ฑ
    • ์„ ๋ถ„์ด ์  (-100,100) ๋ฒ”์œ„์— ์žˆ์œผ๋ฏ€๋กœ ๊ธธ์ด๊ฐ€ 200
  • ๊ฐ ์„ ๋ถ„์˜ ์‹œ์ž‘ ๋ฐ ๋ ์ขŒํ‘œ๋ฅผ ๋‹ด์„ starts / ends ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
  • linelist ๋Œ๊ธฐ
    • ๊ฐ ์„ ๋ถ„์˜ ์‹œ์ž‘ ๋ฐ ๋ ์ขŒํ‘œ ์ถ”๊ฐ€
    • ์‹œ์ž‘๊ณผ ๋ ์ขŒํ‘œ ์‚ฌ์ด ์ˆซ์ž์— +1 (count ๋ฆฌ์ŠคํŠธ ๋ฒ”์œ„๊ฐ€ -100~100์ด๋ฏ€๋กœ ์ธ๋ฑ์Šค์— +100)
  • ์‹œ์ž‘ ์ขŒํ‘œ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜์™€ ๋ ์ขŒํ‘œ ์ค‘ ๊ฐ€์žฅ ํฐ ์ˆ˜ ๋ฒ”์œ„์—์„œ count๊ฐ€ 2๋ณด๋‹ค ํฌ๋ฉด ์„ ๋ถ„์ด ๊ฒน์น˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ +1

 

# 2
def solution(lines):
    s1 = set(i for i in range(lines[0][0], lines[0][1]))
    s2 = set(i for i in range(lines[1][0], lines[1][1]))
    s3 = set(i for i in range(lines[2][0], lines[2][1]))
    
    return len((s1 & s2) | (s2 & s3) | (s1 & s3))

 

ํ’€์ด #2

  • ๊ฐ ๋ผ์ธ์˜ ์‹œ์ž‘ ์ขŒํ‘œ๋ถ€ํ„ฐ ๋ ์ขŒํ‘œ๊นŒ์ง€์˜ ์ˆซ์ž๋ฅผ ์„ธํŠธ๋กœ ์ƒ์„ฑ (s1 / s2 / s3)
  • ์„ ๋ถ„๋ผ๋ฆฌ์˜ ๊ต์ง‘ํ•ฉ(&)๋“ค์˜ ํ•ฉ์ง‘ํ•ฉ(|) ๊ธธ์ด๋ฅผ ์ถœ๋ ฅ

 

 

(-100,100) ๋ฒ”์œ„๋ฅผ ๋งŒ๋“ค์–ด ๋†“๋Š” ๊ฑธ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ์—ˆ๋Š”๋ฐ ๋ฒ”์œ„๋ฅผ ๋งŒ๋“ค๊ณ  ๋‚˜๋‹ˆ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๊ตณ์ด ์‹œ์ž‘๊ณผ ๋ ์ขŒํ‘œ์˜ min/max๋Š” ๊ตฌํ•˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์„ ๊ฑฐ ๊ฐ™๋‹ค. ๋‘ ๋ฒˆ์งธ ํ’€์ด์ฒ˜๋Ÿผ ์„ธํŠธ์—์„œ ํ•ฉ์ง‘ํ•ฉ๊ณผ ๊ต์ง‘ํ•ฉ์„ ์ด์šฉํ•˜๋ฉด ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ธ๋ฐ...๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด ๋‚ด๋Š” ๊ฒŒ ํ•ญ์ƒ ์–ด๋ ต๋‹ค...

+ Recent posts