๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’๊ณต๋ถ€/์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.2] ๊ตฌ๋ช…๋ณดํŠธ

by rindev 2020. 12. 26.

 

 

programmers.co.kr/learn/courses/30/lessons/42885

 

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

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

programmers.co.kr

์ฒ˜์Œ์—” ํ•ฉ์„ ๊ตฌํ•˜๊ณ  pop ํ•˜๋Š” ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ์ง€๋งŒ... ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋– ์„œ ๊ฒฐ๊ตญ ํ’€์ด๋ฅผ ๊ฒ€์ƒ‰ํ•ด๋ณด์•˜๋‹คใ… ใ… 

์ง์ ‘ list๋ฅผ ์–ด๋–ป๊ฒŒ ์กฐ์ž‘ํ•˜์ง€๋ง๊ณ  index๋งŒ ํ™œ์šฉํ•˜๋Š” ํ’€์ด๊ฐ€ ์žˆ์–ด์„œ ์ด์šฉํ•ด์„œ ํ’€์–ด๋ดค์Œ.... ๊ฐˆ๊ธธ์ด ๋„ˆ๋ฌด๋„ˆ๋ฌด ๋ฉ€๋‹ค..

 

1. people์„ sortํ•ด์„œ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค

2. ๊ฐ€์žฅ ๊ฐ€๋ฒผ์šด ์‚ฌ๋žŒ / ๊ฐ€์žฅ ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ์„ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค → light = 0, heavy = len(people)-1

3-1. ๋‘˜์ด ๊ฐ™์ด ํƒˆ ์ˆ˜ ์žˆ์œผ๋ฉด ํƒœ์›๋‹ˆ๋‹ค → light +=1, heavy -= 1, two += 1

3-2. ๋‘˜์ด ๊ฐ™์ด ํƒ€์ง€ ๋ชป ํ•  ๊ฒฝ์šฐ ๋ฌด๊ฑฐ์šด ์‚ฌ๋žŒ์€ ํ˜ผ์ž ๋ณด๋ƒ…๋‹ˆ๋‹ค → heavy -= 1

      (๊ฐ€๋ฒผ์šด ์‚ฌ๋žŒ์€ ๋‹ค๋ฅธ์‚ฌ๋žŒ๊ณผ ๊ฐ™์ด ํƒˆ ์ง€๋„ ๋ชจ๋ฅด๋ฏ€๋กœ ์ผ๋‹จ ๋ณด๋ฅ˜)

4. 3์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜๋‹ค๊ฐ€, light < heavy๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ๋ฒ—์–ด๋‚ฉ๋‹ˆ๋‹ค

5. ์ „์ฒด ์‚ฌ๋žŒ ์ˆ˜์—์„œ 2์ธ1์กฐ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋นผ์ฃผ๋ฉด ๋‹ต์ž…๋‹ˆ๋‹ค → len(people) - two

 

๋ฐ‘์—๋Š” ์ด๋ฅผ ์„ค๋ช…ํ•œ ํ’€์ด

 

 

 

 

๐Ÿ™‹‍โ™€๏ธ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

๋”๋ณด๊ธฐ

 

def solution(people, limit):
    people.sort()
    
    light = 0
    heavy = len(people)-1 #people์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค
    two = 0
    
    while light < heavy:
        if people[light] + people[heavy] <= limit:
            light += 1
            two += 1 #๋‘๋ช…์ด์„œ ๋‚˜๊ฐ„ ์ˆ˜
        heavy -= 1
                
    return len(people)-two

๋Œ“๊ธ€