IT 개발자_S

해커랭크 Climbing the leaderboard 본문

카테고리 없음

해커랭크 Climbing the leaderboard

Soso12 2020. 3. 30. 23:22
반응형

1. 입력값 배열이 주어지고, 해당하는 점수를 삽입 했을때 해당 점수의 rank를 output 함

여기서 배열에 추가하고 중복값을 제거하여 .rank 를 부여했지만 

이중 for문 O(n^2) 으로 인해 runtime error 발생

0#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the climbingLeaderboard function below.
def climbingLeaderboard(scores, alice):

    res =[] # 정답
    rankArr= [] # rank
    rank = 0 #순위 
    cnt = 0 
    for i in range( len(alice)) :
        scores.append(alice[i])

        rankArr = list(set(scores))
        rankArr.sort(reverse=True)
        # 100 50 40 20 5 
        print(rankArr)
        rank =0
        for j in range (len(rankArr)) :
            rank +=1
            if(rankArr[j] == alice[i]) :
                res.append(rank)
        

    return res

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    scores_count = int(input())

    scores = list(map(int, input().rstrip().split()))

    alice_count = int(input())

    alice = list(map(int, input().rstrip().split()))

    result = climbingLeaderboard(scores, alice)

    fptr.write('\n'.join(map(str, result)))
    fptr.write('\n')

    fptr.close()

 

해결방안.

idx 를 통하여 runtime error 방지 및  for문을 한번 사용하여 해결

0#!/bin/python3

import math
import os
import random
import re
import sys



# Complete the climbingLeaderboard function below.
def climbingLeaderboard(scores, alice):

    res =[] # 정답
    # alice 오름차순 #scores 내림차순 
    # idx 를 이용
    idx = 0
    # 6 4 2 1
    # 100 50 40 20 10
    leaderboard = sorted(set(scores), reverse = True)
    idx = len(leaderboard)

    for a in alice :
        print("a",a)
        while (leaderboard[idx -1] <= a  and idx >0) :

            idx -=1
        if(idx <0) :
            res.append(1)
            continue

        res.append(idx+1)

    return res

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    scores_count = int(input())

    scores = list(map(int, input().rstrip().split()))

    alice_count = int(input())

    alice = list(map(int, input().rstrip().split()))

    result = climbingLeaderboard(scores, alice)

    fptr.write('\n'.join(map(str, result)))
    fptr.write('\n')

    fptr.close()
반응형
Comments