[프로그래머스] 주식가격
·
Algorithm
문제 풀이 #1(이중 반복문 - O(n^2))def solution(prices): res = [] last_index = len(prices) - 1 #첫 인덱스부터 반복하며 비교 for i in range(len(prices)) : #마지막 인덱스일 경우, 0 추가 if i == last_index : res.append(0) break #현재 비교하는 인덱스부터 끝까지 비교 for j in range(i + 1, len(prices)) : if prices[i] > prices[j] : res.append(j - i) ..
[프로그래머스] 기능개발
·
Algorithm
문제 풀이 #1import mathdef solution(progresses, speeds): stack = [] #기술 배포 대기 스택 res = [] for i in range(len(progresses)) : # days : 기능 개발까지 걸리는 일수 # 나눗셈의 결과가 ex. 7.3일 이렇게 나오면 8일이 걸리므로 math.ceil 사용 days = math.ceil((100 - progresses[i]) / speeds[i]) #배포 대기중인 max일수가 현재 비교하고 있는 기능의 개발 days보다 작으면 if stack and max(stack) 현재 비교중인 days stack에 추가 re..
[프로그래머스] 귤 고르기
·
Algorithm
문제 풀이from collections import defaultdictdef solution(k, tangerine): sizes = defaultdict(int) box = 0 res = 0 #귤의 크기를 key, 귤의 수를 value로 가지는 dict 생성 for t in tangerine : sizes[t] += 1 #개수별로 sort sizes = dict(sorted(sizes.items(), key = lambda x: x[1], reverse = True)) #가장 많은 귤부터 box에 추가, k개보다 많아지면 res 반환 for size, value in sizes.items() : box += value ..
[프로그래머스] 짝지어 제거하기
·
Algorithm
문제 틀렸던 풀이(시간 초과)#짝지어진 문자열을 제거하는 함수 정의def rm_pair(s): #중복 방지를 위해 set() 사용 c_set = set() #짝지어 제거하기이므로 문자 * 2 해서 set에 넣음 for c in s : c_set.add(c * 2) for pair in c_set : s = s.replace(pair, '') return sdef solution(s): while True: s_after = rm_pair(s) #짝지어진 문자열 제거 전후가 같으면 : 더이상 제거할 문자 X if s == s_after : #문자열이 모두 제거되었을때 ..
[프로그래머스] 최댓값과 최솟값
·
Algorithm
문제 풀이- 레벨 2라고 해서 당황했는데, 레벨보다 정답률을 보고 문제를 선택하는게 좋을 것 같다.def solution(s): #int type으로 리스트에 매핑 num_list = list(map(int, s.split())) res = str(min(num_list)) + " " + str(max(num_list)) return res 1. int type으로 문자열에 있는 숫자들 매핑하여 리스트에 추가2. max, min값을 공백과 함께 res 문자열에 할당
[프로그래머스] 붕대 감기
·
Algorithm
문제 풀이def solution(bandage, health, attacks): #최대 체력, 현재 시간 설정 max_health = health time_now = 0 for attack in attacks : #현재 시간에서 다음 공격까지는 붕대를 감는 시간 healing_time = attack[0] - time_now #healing time #붕대 감기 시전시간 이상의 healing time있을 시에, 추가 회복 if healing_time // bandage[0] >= 1 : health += (healing_time // bandage[0]) * bandage[2] ..