Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[python3] ์ด์ฝ”ํ…Œ - ๋ชจํ—˜๊ฐ€ ๊ธธ๋“œ (ch.11 ๊ทธ๋ฆฌ๋”” - ์œ ํ˜•๋ณ„ ๊ธฐ์ถœ๋ฌธ์ œ)

๊ฐ์ž ๐Ÿฅ” 2021. 6. 22. 19:59
๋ฐ˜์‘ํ˜•

์ฐธ๊ณ ) ์ด๊ฒƒ์ด ์ทจ์—…์„ ์œ„ํ•œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ๋‹ค with ํŒŒ์ด์ฌ ์ฑ…์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘์„ฑ๋œ ๋ฌธ์ œ์™€ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. 
๋”ฐ๋ผ์„œ ๋ฌธ์ œ๋Š” ์ž์„ธํ•˜๊ฒŒ ์ ์ง€ ์•Š๊ณ , ๊ฐ„๋‹จํ•œ ์„ค๋ช…๊ณผ ์ œ ์ฝ”๋“œ๋งŒ ์˜ฌ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ชจํ—˜๊ฐ€ ๊ธธ๋“œ (ebook๊ธฐ์ค€ p.313)

- ๋ชจํ—˜๊ฐ€ N๋ช…
- ๊ฐ ๋ชจํ—˜๊ฐ€ ๋งˆ๋‹ค '๊ณตํฌ๋„'๋ฅผ ์ธก์ •, ๊ณตํฌ๋„๊ฐ€ ๋†’์€ ๋ชจํ—˜๊ฐ€๋Š” ์‰ฝ๊ฒŒ ๊ณตํฌ๋ฅผ ๋Š๊ปด ์œ„ํ—˜์ƒํ™ฉ ๋Œ€์ฒ˜๋Šฅ๋ ฅ ์ €ํ•˜
- ๊ธธ๋“œ์žฅ์€ ๊ณตํฌ๋„๊ฐ€ x ์ธ ๋ชจํ—˜๊ฐ€๋Š” ๋ฐ˜๋“œ์‹œ x๋ช… ์ด์ƒ์œผ๋กœ ๊ตฌ์„ฑํ•œ ๋ชจํ—˜๊ฐ€ ๊ทธ๋ฃน์— ์ฐธ์—ฌํ•ด์•ผ ์—ฌํ–‰์„ ๋– ๋‚  ์ˆ˜ ์žˆ์Œ
- ์ตœ๋Œ€ ๋ช‡ ๊ฐœ์˜ ๋ชจํ—˜๊ฐ€ ๊ทธ๋ฃน์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”๊ฐ€?
- N๋ช…์˜ ๋ชจํ—˜๊ฐ€์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์—ฌํ–‰์„ ๋– ๋‚  ์ˆ˜ ์žˆ๋Š” ๊ทธ๋ฃน์˜ ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜์‹œ์˜ค.

##### ์ž…๋ ฅ์กฐ๊ฑด
- ์ฒซ์จ‹์ค„ N 
- 1<= N <= 100,000
- ๋‘˜์จ‹์ค„, ๊ณตํฌ๋„์˜ ๊ฐ’์ด N ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๋กœ ์ฃผ์–ด์ง.

 

<๋ฌธ์ œํ’€์ด>

1. ์ฒซ๋ฒˆ์งธ ์‹œ๋„ (ํ‹€๋ฆฐ๋‹ต์ž„)

n = map(int, input())
people = list(map(int, input().split()))

def solution(n,people):
    group = 0
    
    people.sort()
    
    while people: # people์— ์•„๋ฌด๋„ ์—†์„๋–„๊นŒ์ง€ ์ง„ํ–‰
        num_max = people.pop()
        
        if num_max > len(people):
            break
        
        for i in range(0, num_max-1):
            people.pop(0)
        group += 1
    return group

solution(n,people)

๋ฐ”๋ณด๊ฐ™์ด ์ƒ๊ฐํ–ˆ๋‹ค. ์ถœ๋ ฅ์ด ์›ํ•˜๋Š”๋Œ€๋กœ ๋‚˜์˜ค๋Š”์ค„ ์•Œ์•˜๋Š”๋ฐ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ๊นŒ '์ตœ๋Œ€'๊ทธ๋ฃน ์ˆ˜๋ผ๋ฉด, '์ตœ์†Œ'์ธ์›์œผ๋กœ ์ตœ๋Œ€ํ•œ ๋งŽ์€ ๊ทธ๋ฃน์„ ๊ฒฐ์„ฑํ•ด์•ผํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ด ์ฝ”๋“œ๋Š” '์ตœ์†Œ'๊ทธ๋ฃน ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ˜•ํƒœ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค. 

2. ๋‘๋ฒˆ์งธ ์‹œ๋„ (๋งž๋Š” ๋‹ต์•ˆ)

n = map(int, input())
people = list(map(int, input().split()))

def solution(n, people):
    cnt = 0
    group = []
    people.sort()
    
    while people:
        min_num = people.pop(0)
        group.append(min_num)
        if max(group) == len(group):
            cnt += 1
            group = []
            
    return cnt

solution(n,people)

 

<์˜ค๋‹ต๋…ธํŠธ (์˜ˆ์‹œ๋‹ต์•ˆ๊ณผ ๋น„๊ต)>

์˜ˆ์”จ๋‹ต์•ˆ์€ ๋‹จ์ˆœํ•œ ๋ง์…ˆ๊ณผ if๋ฌธ์„ ํ™œ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค. if๋ฌธ ํŠน์„ฑ์ƒ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด count ๋˜๋Š” ํŠน์„ฑ์„ ์ž˜ ์ƒ๊ฐํ–ˆ์–ด์•ผ ํ–ˆ๋‹ค. ๋‚˜๋Š” ์ด ๊ณผ์ •์„ ์กฐ๊ธˆ ๋ณต์žกํ•˜๊ฒŒ ์ƒ๊ฐํ•œ ํƒ“์— while๋ฌธ์œผ๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ธ๊ณ , ์‹œ๊ฐ„์„ ์กฐ๊ธˆ ์žก์•„๋จน์—ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

๋ฐ˜์‘ํ˜•