โ ๋ฌธ์
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]]))
'Algorithm | Language > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[programmers/Lv.3] ํํ ๊ฐ๋ฅํ ์ด์งํธ๋ฆฌ (0) | 2023.06.12 |
---|---|
[programmers/Lv.2] ๋ฆฌ์ฝ์ณ ๋ก๋ด (1) | 2023.06.05 |
[programmers/Lv.2] ๋ ํ ํฉ ๊ฐ๊ฒ ๋ง๋ค๊ธฐ (0) | 2023.03.10 |
[programmers/Lv.2] ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ (1) | 2023.03.10 |
[programmers/Lv.2] ํผ์์ ํ๋ ํฑํํ (1) | 2023.03.06 |