[프로그래머스] K번째수&H-Index&주식가격

Chaejung·2022년 7월 18일
0

알고리즘_Python

목록 보기
7/22

1.정렬-K번째수

def solution(array, commands):
    answer=[]
    for condition in commands:
        firstArray = array[(condition[0]-1):condition[1]]
        secondArray = sorted(firstArray)
        answer.append(secondArray[condition[2]-1])
    
    return answer

2.정렬-H-Index

https://sumin-itstory.tistory.com/6
깊은 복사
https://inkkim.github.io/python/%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EB%A6%AC%EC%8A%A4%ED%8A%B8-%EB%B3%B5%EC%82%AC/

def solution(citations):
    citations.sort()
    hCount = 0
    answer = 0
    while len(citations) > 0:
        maxCit = max(citations)
        if maxCit <= citations.count(maxCit)+hCount:
            answer = maxCit
            break
        else:
            citations.pop()
            hCount += 1
    return answer

print(solution([10, 100])) # 0
print(solution([33, 30, 20, 15, 7, 6, 5, 4]))
print(solution([1, 8, 5, 3, 4, 9]))
def solution(citations):
    #입력값 깊은 복사
    entCitations = [] + citations
    citations.sort()
    #비교한 뒤 뽑아낼 수 카운터
    hCount = 0
    answer = 0
    #가장 큰 수부터 비교하면서 비교한 수는 뽑아내고 입력 배열의 크기가 0이 될 때까지 반복
    while len(citations) > 0:
        maxCit = max(citations)
        if maxCit <= citations.count(maxCit)+hCount:
            #[6, 6, 6, 6, 6, 1]과 같은 경우, 6 다음에 1을 비교하게 되는데,
            #그 사이가 h-index가 되는 경우, hCount가 곧 h-index가 된다.
            if maxCit < hCount:
                answer = hCount
                return answer
            answer = maxCit
            return answer
        else:
            citations.pop()
            hCount += 1
    #[10, 100]과 같은 경우 아래 조건문이 없는 경우 h-index가 0이 된다.
    #총 논문의 수가 곧 h-index가 된다.
    if min(entCitations)>=len(entCitations):
        answer = len(entCitations)
    return answer

print(solution([10, 100])) # 0 답은 2
print(solution([33, 30, 20, 15, 7, 6, 5, 4]))
print(solution([6, 6, 6, 6, 6]))
print(solution([2, 2, 2, 3, 3, 4]))
print(solution([6, 6, 6, 6, 6, 1])) #1 답은 5

def solution(citations):
    answer = 0
    if min(citations)>=len(citations):
        answer = len(citations)
    
    citations.sort()
    hCount = 0
    
    while len(citations) > 0:
        maxCit = max(citations)
        if maxCit <= citations.count(maxCit)+hCount:
            if maxCit < hCount:
                answer = hCount
                return answer
            answer = maxCit
            return answer
        else:
            citations.pop()
            hCount += 1
    return answer

print(solution([10, 100])) # 0 답은 2
print(solution([33, 30, 20, 15, 7, 6, 5, 4]))
print(solution([6, 6, 6, 6, 6]))
print(solution([2, 2, 2, 3, 3, 4]))
print(solution([6, 6, 6, 6, 6, 1])) #1 답은 5

3.스택/큐-주식가격

from collections import deque

def solution(prices):
    basicQueue = deque([])
    resultQueue = deque([])
    #[N-1, N-2, ..., 0]인 기본 큐 만들기
    #오름차순인 경우 기본 큐가 답이므로 기본으로 정함
    for i in range(len(prices)):
        basicQueue.appendleft(i)

    queueIndex = 0
    #가격의 인덱스를 정하고, 하나씩 판별한 뒤 앞에서부터 제거
    while queueIndex<len(prices):
        #이중 반복문을 빠져나오기 위한 변수
        breakValue = False
        #가격이 하락하는 지점끼리의 차이
        downIndex = 0
        for i in range(queueIndex+1, len(prices)):
            if prices[queueIndex]>prices[i]:
                downIndex = i-queueIndex
                resultQueue.append(downIndex)
                breakValue = True
                break
        #끝까지 하락하는 지점이 없다면 기본 큐 그대로 넣기
        if breakValue==False:
            resultQueue.append(basicQueue[queueIndex]-downIndex)
        queueIndex+=1
    answer=list(resultQueue)
    return answer
#print(solution([1, 2, 3, 2, 2])) #[4, 3, 1, 1, 0]
print(solution([4, 2, 5, 7, 1])) #[1, 3, 2, 1, 0]
print(solution([7, 5, 4, 2, 8])) #[1, 1, 1, 1, 0]
print(solution([1, 8, 3, 2, 1])) #[4, 1, 1, 1, 0]
print(solution([2, 5, 3, 7, 1])) #[4, 1, 2, 1, 0]

from collections import deque

def solution(prices):
    result = []
    prices = deque(prices)
    while prices:
        downCount = 0
        currentPrice = prices.popleft()
        for nextPrice in prices:
            if currentPrice > nextPrice:
                downCount += 1
                break
            downCount += 1
        result.append(downCount)
    return result
print(solution([1, 2, 3, 2, 2])) #[4, 3, 1, 1, 0]
print(solution([4, 2, 5, 7, 1])) #[1, 3, 2, 1, 0]
print(solution([7, 5, 4, 2, 8])) #[1, 1, 1, 1, 0]
print(solution([1, 8, 3, 2, 1])) #[4, 1, 1, 1, 0]
print(solution([2, 5, 3, 7, 1])) #[4, 1, 2, 1, 0]

리스트 문제 풀 때 꼭 한 번씩은 들어가는 곳
https://wikidocs.net/14#x-count

profile
프론트엔드 기술 학습 및 공유를 활발하게 하기 위해 노력합니다.

0개의 댓글

관련 채용 정보