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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ตฌ๋ช…๋ณดํŠธ

๋ฌด์ธ๋„์— ๊ฐ‡ํžŒ ์‚ฌ๋žŒ๋“ค์„ ๊ตฌ๋ช…๋ณดํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌ์ถœํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๋ช…๋ณดํŠธ๋Š” ์ž‘์•„์„œ ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 2๋ช…์”ฉ ๋ฐ–์— ํƒˆ ์ˆ˜ ์—†๊ณ , ๋ฌด๊ฒŒ ์ œํ•œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ๋žŒ๋“ค์˜ ๋ชธ๋ฌด๊ฒŒ๊ฐ€ [70kg, 50kg, 80kg, 5

programmers.co.kr

 

๋ฌธ์ œ ํ’€์ด

 ๋ฌด์ธ๋„์— ๊ฐ‡ํžŒ ์‚ฌ๋žŒ๋“ค์„ ๊ตฌ๋ช…๋ณดํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ํƒˆ์ถœ์‹œํ‚ค๊ณ ์ž ํ•  ๋•Œ, ์ตœ์†Œํ•œ์˜ ๊ตฌ๋ช…๋ณดํŠธ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ฌธ์ œ์˜ ์ œํ•œ ์กฐ๊ฑด์œผ๋กœ๋Š” ๋ณดํŠธ์—๋Š” ์ตœ๋Œ€ 2๋ช…๋งŒ ํƒ‘์Šนํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํƒ‘์Šนํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌด๊ฒŒ ์ œํ•œ์ด ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‚ฌ๋žŒ๋“ค์˜ ๋ฌด๊ฒŒ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ›„์—, ํ๋‚˜ ์ด๋ถ„ ํƒ์ƒ‰์„ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํ๋ฅผ ์ด์šฉํ•œ ํ’€์ด

  • people์„ ์ •๋ ฌ ํ›„, `deque`๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.
    • list๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด `list.pop(0)`์˜ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ O(N)์ด๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
  • ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ์‚ฌ๋žŒ + ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ์ด ๋ณดํŠธ์— ํƒ‘์Šนํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•œ๋‹ค.
    • ๋ฌด๊ฒŒ ์ œํ•œ์„ ์ดˆ๊ณผํ•œ๋‹ค๋ฉด, ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ์„ ๋จผ์ € ํƒˆ์ถœ์‹œํ‚จ๋‹ค.
    • ๋ฌด๊ฒŒ ์ œํ•œ์„ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด, ์ œ์ผ ๊ฐ€๋ฒผ์šด ์‚ฌ๋žŒ๊ณผ ์ œ์ผ ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ์„ ๋‘˜ ๋‹ค ํƒˆ์ถœ์‹œํ‚จ๋‹ค.
  • while๋ฌธ์„ ๋ฐ˜๋ณตํ•  ๋•Œ๋งˆ๋‹ค, ํ•˜๋‚˜์˜ ๊ตฌ๋ช…๋ณดํŠธ๊ฐ€ ์ถ”๊ฐ€๋˜๋Š” ๊ฒƒ์ด๋‹ค.
    • ๋งŒ์•ฝ while ๋ฌธ์„ ๋น ์ ธ๋‚˜์˜จ ํ›„์—, ์‚ฌ๋žŒ์ด ๋‚จ์•„์žˆ๋‹ค๋ฉด ๊ตฌ๋ช…๋ณดํŠธ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

์ด๋ถ„ ํƒ์ƒ‰์„ ์ด์šฉํ•œ ํ’€์ด

  • ํ๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ์‹๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๋ฌด๊ฒŒ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
  • strart, end๋ฅผ 0, len(people) -1๋กœ ์ง€์ •ํ•˜์—ฌ ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ์‚ฌ๋žŒ, ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ์œผ๋กœ ์ง€์ •ํ•œ๋‹ค.
  • ํ๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š” ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, 2๋ช…์ด์„œ ๋ณดํŠธ๋ฅผ ํƒˆ ์ˆ˜ ์žˆ๋‹ค๋ฉด start์™€ end๋ฅผ ๋ชจ๋‘ ๋ณ€๊ฒฝํ•œ๋‹ค.
    • ์ด์™€ ๋ฐ˜๋Œ€์ธ ๊ฒฝ์šฐ end๋งŒ ๋ณ€๊ฒฝํ•œ๋‹ค.
  • ์‚ฌ๋žŒ ์ˆ˜๋Š” ์ตœ๋Œ€๋กœ ํ•„์š”ํ•œ ๋ณดํŠธ ์ˆ˜๋ฅผ ์˜๋ฏธํ•˜๋ฏ€๋กœ ์‚ฌ๋žŒ์˜ ์ˆ˜ - ์ด๋ถ„ ํƒ์ƒ‰์„ ํ†ตํ•ด ๊ตฌํ•œ ๊ฐ’์„ ํ•˜๋ฉด ์›ํ•˜๋Š” ๋‹ต์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

deque๋ฅผ ํ™œ์šฉํ•œ ํ’€์ด

from collections import deque


def solution(people, limit):
    FIRST, LAST = 0, -1
    answer = 0
    people = deque(sorted(people))

    while len(people) > 1:
        # ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ ํ˜ผ์ž ๋ฐฐ ํƒ€๊ณ  ํƒˆ์ถœ.
        if people[FIRST] + people[LAST] > limit:
            people.pop()
        # 2๋ช…์ด์„œ ๋ฐฐ ํƒ€๊ณ  ํƒˆ์ถœ.
        else:
            people.popleft()
            people.pop()
        answer += 1

    return answer + 1 if people else answer

 

์ด๋ถ„ ํƒ์ƒ‰์„ ํ™œ์šฉํ•œ ํ’€์ด

def solution(people, limit):
    answer = 0
    people.sort()

    start, end = 0, len(people) - 1

    while start < end:
        if people[start] + people[end] <= limit:
            start += 1
            answer += 1
        end -= 1

    return len(people) - answer

 

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