โ“  ๋ฌธ์ œ

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

 

๐Ÿ›Ž๏ธ  ํ’€์ด ๊ณผ์ •

๊ทผ๋ฌด ํƒœ๋„ ์ ์ˆ˜๋Š” ๋‚ด๋ฆผ์ฐจ์ˆœ, ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜์˜ max๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  ๊ฐ€์žฅ ์ฒซ๋ฒˆ ์งธ ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜๋ฅผ ๋„ฃ์–ด์ค€๋‹ค. (์—ฌ๊ธฐ์„œ๋Š” max_y)
์ด max_y๊ฐ’์€ ํ˜„์žฌ ๋ณด๋‹ค ๊ทผ๋ฌด ํƒœ๋„ ์ ์ˆ˜๊ฐ€ ๋†’์€ ์ง์ „ ๋™๋ฃŒ๋“ค ์ค‘ ๊ฐ€์žฅ ๋†’์€ ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

์—ฌ๊ธฐ์„œ ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ์ด์œ ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š”๋ฐ,
์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ–ˆ์„ ๋•Œ max_y๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๋น„๊ตํ•˜๊ฒŒ ๋˜๋ฉด
[2, 1], [2, 2] -> ๋‘˜ ๋‹ค ์ธ์„ผํ‹ฐ๋ธŒ ํ›„๋ณด
[2, 2], [2, 1] -> [2, 1]์€ ํ›„๋ณด์—์„œ ํƒˆ๋ฝ
์œผ๋กœ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธด๋‹ค.

๋”ฐ๋ผ์„œ ๋‚ด๋ฆผ์ฐจ์ˆœ / ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋ฉด
[[3, 2], [3, 2], [2, 1], [2, 2], [1, 4]]
๋‹ค์Œ๊ณผ ๊ฐ™๊ณ , max_y์˜ ์ดˆ๊ธฐ๊ฐ’์—๋Š” 2๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.

์—ฌ๊ธฐ์„œ ๋งŒ์•ฝ ํ˜„์žฌ ๋ฐ˜๋ณต๋ฌธ์˜ ์œ„์น˜๊ฐ€ [1, 4]๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ํ˜„์žฌ๊นŒ์ง€์˜ max_y๋Š” 2๊ฐ€ ๋œ๋‹ค.
๊ทผ๋ฌด ํƒœ๋„ ์ ์ˆ˜์ธ 1์€ ์ž‘์ง€๋งŒ max_y์ธ 2๋ณด๋‹ค ๋™๋ฃŒ ํ‰๊ฐ€ ์ ์ˆ˜์ธ 4๊ฐ€ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ์ธ์„ผํ‹ฐ๋ธŒ ํ›„๋ณด๊ฐ€ ๋œ๋‹ค.

์™„ํ˜ธ์˜ ๋“ฑ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ์ธ์„ผํ‹ฐ๋ธŒ ํ›„๋ณด์ผ ์‹œ, ์™„ํ˜ธ์˜ ์ ์ˆ˜ ํ•ฉ๊ณผ ๋น„๊ตํ•œ๋‹ค. (answer)

 

๐Ÿ’ป  ํ’€์ด

def solution(scores):
  wanho = scores[0]
  wanho_sum = sum(scores[0])
  scores.sort(key=lambda x: (-x[0], x[1]))
  answer, max_y = 1, scores[0][1]

  for index, score in enumerate(scores):
    if score[1] < max_y:
      if score == wanho:
        return -1
      scores[index] = [-1, -1]
    else:
      max_y = score[1]
      if wanho_sum < sum(score):
        answer += 1

  return answer

print(solution([[2,2], [1,4], [3,2], [3,2], [2,1]]))

 

๐Ÿ’ป  ํ’€์ด (์‹œ๊ฐ„์ดˆ๊ณผ)

def solution(scores):
  # ์™„ํ˜ธ์˜ ์ ์ˆ˜ ํ•ฉ
  wanho = scores[0][0] + scores[0][1]
  rank = 0
  for index, score in enumerate(scores):
    if index != 0 and score[0] + score[1] <= wanho:
      continue
    for s in scores:
      if score[0] < s[0] and score[1] < s[1]:
        if index == 0:
          return -1
        break
    else:
      rank += 1
  return rank

print(solution([[2,2], [1,4], [3,2], [3,2], [2,1]]))

+ Recent posts