🤗 나의 풀이
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'climbingLeaderboard' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
# 1. INTEGER_ARRAY ranked
# 2. INTEGER_ARRAY player
#
def climbingLeaderboard(ranked, player):
# Write your code here
result = []
sort_ranked = sorted(set(ranked), reverse=True)
player.sort(reverse = True)
len_ranked = len(sort_ranked)
x = 0
for score in player :
while x < len_ranked and sort_ranked[x] > score :
x += 1
result.append(x + 1)
return result[::-1]
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
ranked_count = int(input().strip())
ranked = list(map(int, input().rstrip().split()))
player_count = int(input().strip())
player = list(map(int, input().rstrip().split()))
result = climbingLeaderboard(ranked, player)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()
아래 코드를 수정해서 통과했다.
⭐ 수정한 부분
- player 내림차순 정렬
- 변수 x를 for문 밖으로 꺼내서 시작점으로 사용(매번 0부터 시작하면 시간 초과가 나는 게 당연하다.)
- 결과 result를 거꾸로 뒤집어서 반환
# 실패
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'climbingLeaderboard' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
# 1. INTEGER_ARRAY ranked
# 2. INTEGER_ARRAY player
#
def climbingLeaderboard(ranked, player):
# Write your code here
result = []
sort_ranked = sorted(set(ranked), reverse=True)
len_ranked = len(sort_ranked)
for score in player :
x = 0
while x < len_ranked and sort_ranked[x] > score :
x += 1
result.append(x + 1)
return result
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
ranked_count = int(input().strip())
ranked = list(map(int, input().rstrip().split()))
player_count = int(input().strip())
player = list(map(int, input().rstrip().split()))
result = climbingLeaderboard(ranked, player)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()
제출하면 몇 개가 시간초과가 뜬다.
동점은 같은 등수니까 set으로 중복된 값들은 제거했다.
set은 정렬이 안 되니까 sorted로 내림차순 정렬한다.
참고 👇
https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem?isFullScreen=true
반응형
'coding test > HackerRank' 카테고리의 다른 글
[HackerRank] Matrix Layer Rotation (1) | 2023.05.18 |
---|---|
[HackerRank] Queen's Attack 2 (0) | 2023.05.17 |
[HackerRank] Non-Divisible Subset (0) | 2023.05.16 |
[HackerRank] Extra Long Factorials (0) | 2023.05.15 |
[HackerRank] Forming a Magic Square (0) | 2023.05.14 |