DevYoon

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ธ์‚ฌ ๊ณ ๊ณผ (Python) ๋ณธ๋ฌธ

PS/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ธ์‚ฌ ๊ณ ๊ณผ (Python)

gimewn 2023. 2. 3. 18:49

link ๐Ÿ”— https://school.programmers.co.kr/learn/courses/30/lessons/152995

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๋ฌธ์ œ ์„ค๋ช…

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

๊ฐ ์‚ฌ์›์˜ ๊ทผ๋ฌด ํƒœ๋„ ์ ์ˆ˜์™€ ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜ ๋ชฉ๋ก scores์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์™„ํ˜ธ์˜ ์„์ฐจ๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ scores์˜ ๊ธธ์ด ≤ 100,000
  • scores์˜ ๊ฐ ํ–‰์€ ํ•œ ์‚ฌ์›์˜ ๊ทผ๋ฌด ํƒœ๋„ ์ ์ˆ˜์™€ ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ [a, b] ํ˜•ํƒœ์ž…๋‹ˆ๋‹ค.
    • scores[0]์€ ์™„ํ˜ธ์˜ ์ ์ˆ˜์ž…๋‹ˆ๋‹ค.
    • 0 ≤ a, b ≤ 100,000
  • ์™„ํ˜ธ๊ฐ€ ์ธ์„ผํ‹ฐ๋ธŒ๋ฅผ ๋ฐ›์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ -1์„ return ํ•ฉ๋‹ˆ๋‹ค.

 


 

๐Ÿ”ฅ ๊ทผ๋ฌดํƒœ๋„์™€ ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์—ญ์ˆœ ์ •๋ ฌํ•˜์—ฌ ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜๋งŒ ๋น„๊ตํ•ด์„œ for๋ฌธ ์‚ฌ์šฉ ํšŸ์ˆ˜๋ฅผ ์ค„์ด๋Š” ๊ฒƒ๊นŒ์ง„ ์•„์ด๋””์–ด๋ฅผ ์ƒ๊ฐํ•ด ์ž‘์„ฑํ–ˆ๋Š”๋ฐ... ๋ญ”๊ฐ€ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ ์‹ถ์—ˆ๋‹ค๐Ÿค” scores์˜ ์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ 10๋งŒ์ด๊ธฐ์— ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์ง€ ์•Š์„๊นŒ ํ–ˆ๋Š”๋ฐ, ์•„๋‹ˆ๋‚˜ ๋‹ค๋ฅผ๊นŒ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€...!!

๐Ÿ”ฅ ๋‹ค๋ฅธ ๋ถ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉฐ ์ •๋ ฌ์„ ํ•˜๋˜, ๊ทผ๋ฌดํƒœ๋„๋Š” ์—ญ์ˆœ, ๋™๋ฃŒ ํ‰๊ฐ€๋Š” ๊ธฐ๋ณธ ์ˆœ์„œ๋กœ ์ •๋ ฌํ•˜๋ฉด ์ด์ค‘for๋ฌธ์„ ํ™œ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ๊ฑธ ๊นจ๋‹ฌ์•˜๋‹ค!

def solution(scores):
    answer = 1
    wanho = scores[0]
    length = len(scores)
    
    # ๊ทผ๋ฌดํƒœ๋„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์—ญ์ˆœ ์ •๋ ฌ, ๋™๋ฃŒ ํ‰๊ฐ€๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ธฐ๋ณธ ์ •๋ ฌ
    scores.sort(key=lambda x: (-x[0], x[1]))
    
    # ์ตœ๋Œ€ ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜
    max_coworker = 0
    # ์ธ์„ผํ‹ฐ๋ธŒ ๊ฐ€๋Šฅ ์—ฌ๋ถ€
    check = [True]*length
    
    for idx in range(length):
        # ์ตœ๋Œ€ ๋™๋ฃŒ ํ‰๊ฐ€์™€ ์ ์ˆ˜๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ ํฌ๋‹ค๋ฉด?
        # ๊ทผ๋ฌดํƒœ๋„๊ฐ€ ๋ณธ์ธ ์ ์ˆ˜๋ณด๋‹ค ๋†’์€ ์‚ฌ๋žŒ ์ค‘ ๋™๋ฃŒํ‰๊ฐ€ ์ ์ˆ˜๊นŒ์ง€ ๋” ๋†’์€ ์‚ฌ๋žŒ์€ ์—†์Œ -> ์ธ์„ผํ‹ฐ๋ธŒ ๊ฐ€๋Šฅ
        if max_coworker <= scores[idx][1]:
            # ์ตœ๋Œ€ ๋™๋ฃŒ ํ‰๊ฐ€ ๊ต์ฒด
            max_coworker = scores[idx][1]
            # ํ•ฉ์‚ฐ ์ ์ˆ˜๊ฐ€ ์™„ํ˜ธ์˜ ์ ์ˆ˜๋ณด๋‹ค ๋†’๋‹ค -> ๋” ๋†’์€ ๋“ฑ์ˆ˜ -> answer ++
            if sum(scores[idx]) > sum(wanho):
                answer += 1
                
        # ์ตœ๋Œ€ ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜๋ณด๋‹ค ์ ๋‹ค๋ฉด?
        # ๊ทผ๋ฌดํƒœ๋„์™€ ๋™๋ฃŒํ‰๊ฐ€ ์ ์ˆ˜ ๋ชจ๋‘ ๋ณธ์ธ ์ ์ˆ˜๋ณด๋‹ค ๋†’์€ ์‚ฌ๋žŒ ์กด์žฌ
        elif max_coworker > scores[idx][1]:
            # ์ธ์„ผํ‹ฐ๋ธŒ ๋ถˆ๊ฐ€๋Šฅ ํ‘œ๊ธฐ
            check[idx] = False
            # ์ ์ˆ˜ ๊ตฌ์„ฑ์ด ์™„ํ˜ธ์™€ ๊ฐ™๋‹ค๋ฉด ์™„ํ˜ธ๋„ ์ธ์„ผํ‹ฐ๋ธŒ ๋ถˆ๊ฐ€๋Šฅ -> -1 ๋ฆฌํ„ด
            if scores[idx] == wanho:
                return -1
    
    return answer