알고리즘

정렬 - 프로그래머스

쿠와와 2021. 1. 9. 18:07

문제 : programmers.co.kr/learn/courses/30/parts/12198

K번째 수 

# sort_1.py
array = [1, 5, 2, 6, 3, 7, 4]
commands = [[2, 5, 3], [4, 4, 1], [1, 7, 3]]


def solution(array, commands):
    return [(sorted(array[a-1:b])[c-1]) for a, b, c in commands]


print(solution(array, commands))
# return [5, 6, 3]

 

가장 큰 수

# sort_2.py
from functools import reduce
numbers = [40, 403]  # [3, 30, 34, 5, 9]  # [6, 10, 2]        # [3, 30, 34, 5, 9]


def solution(numbers):
    numbers = list(map(str, numbers))
    numbers.sort(key=lambda x: (x[0], x[1 % len(x)], x[2 % len(x)], x[3 % len(x)]),  reverse=True)
    return str(int(''.join(numbers)))


print(solution(numbers))
# return "6210" 	9534330

 

H-index

# sort_3.py
# H-index =  과학자의 생산성과 영향력을 나타내는 지표
# 논문 n 편중 h번 이상 인용된 논문이 h 이상 나머지가 h 이하 인용되 었다면 h의 최댓값이 H-index
citations = [10, 11, 12, 13]  # [3, 0, 6, 1, 5]         # 인용횟수


def solution(citations):
    citations.sort(reverse=True)
    print(citations)
    for i in range(len(citations)):
        if i >= citations[i]:
            return i
    return len(citations)
    # return answer


print(solution(citations))

# def solution(citations):
#     return max([min(i+1,sorted(citations, reverse=True)[i]) for i in range(len(citations))])

'알고리즘' 카테고리의 다른 글

완전 탐색과 문제풀이 (프로그래머스)  (0) 2021.01.19
Heap (개념 및 프로그래머스 문제)  (0) 2021.01.14
스택, 큐 개념과 코드  (0) 2021.01.03
Hash  (0) 2021.01.01
NP-Completeness ( P, NP, EXP, NP-completeness )  (0) 2020.12.07