본문 바로가기

알고리즘/프로그래머스

(29)
[프로그래머스 코딩테스트 고득점 Kit / 해시] 의상 (python) 풀이 def solution(clothes): answer = 1 dict_clothes = {} for i in range(len(clothes)): if clothes[i][1] in dict_clothes: dict_clothes[clothes[i][1]] += 1 else: dict_clothes[clothes[i][1]] = 1 for i in dict_clothes.values(): answer *= i+1 answer -= 1 return answer 내 풀이 clothes return [["yellow_hat", "headgear"], ["blue_sunglasses", "eyewear"], ["green_turban", "headgear"]] 5 [["crow_mask", "face"],..
[프로그래머스 코딩테스트 고득점 Kit / 해시] 전화번호 목록 (python) + 2024.06.10 재풀이풀이: setdef solution(phone_book): pb = set(phone_book) for number in phone_book: prefix = '' for n in number: prefix += n if prefix!= number and prefix in pb: return False return True 이전에 해시 알고리즘을 사용하기 위해 딕셔너리로 풀었는데, 다시 생각해보니 안에 전화번호가 있는지만 확인하는 용도로만 쓸거면 굳이 딕셔너리를 사용하기보다는 집합을 사용하는 게 나을 것 같았다. 전체 전화번호 리스트를 우선 pb 집합에 넣는다...
[프로그래머스 코딩테스트 고득점 Kit / 해시] 폰켓몬 (python) 풀이 def solution(nums): answer = 0 mon = set(nums) if len(mon)
[프로그래머스 코딩테스트 고득점 Kit / 해시] 완주하지 못한 선수 (python) 풀이 def solution(participant, completion): answer = '' cDic = {} for i in completion: if i in cDic: cDic[i] += 1 else: cDic[i] = 1 for i in participant: if i in cDic: cDic[i] -= 1 if cDic[i] == 0: del cDic[i] else: answer = i break return answer 배열로 풀었다가 효율성 테스트 때문에 애먹은 문제. 딕셔너리를 사용해서 풀었다. 딕셔너리는 해시를 사용하기 때문에 검색 시간복잡도가 O(1)이다. 딕셔너리는 d1 = {} 또는 d1 = dict() 으로 선언한다. 우선 completion 리스트의 각 값을 key로 해서 딕..
[프로그래머스 코딩테스트 고득점 Kit / 스택/큐] 주식가격 (python) 테스트 케이스 prices return [2, 1, 2, 3, 5, 2] [1, 4, 3, 2, 1, 0] [5, 2, 1, 2, 4, 1] [1, 1, 3, 2, 1, 0] 풀이 def solution(prices): answer = [0 for i in range(len(prices))] for i in range(len(prices) - 1): for j in range(i + 1, len(prices)): if prices[i]
[프로그래머스 코딩테스트 고득점 Kit / 스택/큐] 다리를 지나는 트럭 (python) 풀이 def solution(bridge_length, weight, truck_weights): answer = 0 truckInBridge = [] #현재 다리 위에 있는 트럭들의 남은 길이 left_weight = weight #현재 가능한 다리 무게 truck_index = 0 while(len(truck_weights) > truck_index): time = 0 # 다리에 올릴 수 없을 때 if left_weight < truck_weights[truck_index]: while(left_weight < truck_weights[truck_index]): left_weight += truck_weights.pop(0) truck_index -= 1 temp = truckInBridge.pop(..
[프로그래머스 코딩테스트 고득점 Kit / 스택/큐] 프로세스 (python) 풀이 def solution(priorities, location): answer = 0 countPrior = [0 for i in range(9)] for i in range(len(priorities)): countPrior[priorities[i] - 1] += 1 index = 0 curPrior = 9 while(True): if countPrior[curPrior - 1] == 0: curPrior -= 1 else: if priorities[index] == curPrior: answer += 1 countPrior[curPrior - 1] -= 1 if index == location: break if countPrior[curPrior - 1] == 0: curPrior -= 1 ind..
[프로그래머스 코딩테스트 고득점 Kit / 스택/큐] 기능개발 (python) 풀이from math import ceildef solution(progresses, speeds): answer = [] left = [] for i in range(len(progresses)): left.append(ceil((100 - progresses[i]) / speeds[i])) first = -1 cnt = 0 for i in left: if first 각 작업의 남은 일수를 left 배열에 담는다. (전체 완료율 100에서 현재 진행률 progresses를 뺀다. 그리고 speed로 나눠서 올림하면 남은 일수가 나온다.) 앞의 작업보다 뒤의 작업이 남은 일수가 적으면, 앞의 작업이 완료되는 순간 뒤에 있는 작..

반응형