코딩테스트/자료구조(스택,큐,해시,힙) 5

[큐] (프로그래머스_42583) 다리를 지나는 트럭 - 파이썬

링크: https://school.programmers.co.kr/learn/courses/30/lessons/42583✅ bridge의 길이를 유지하기 위해 0으로 채우고 시간이 지날때마다 우선 popleft✅ sum 대신 total을 써야 시간 초과안남from collections import dequedef solution(bridge_length, weight, truck_weights): bridge = deque([ 0 for _ in range(bridge_length)]) trucks = deque([ truck for truck in truck_weights]) answer = 0 total = 0 while trucks: cur_truck ..

[큐/힙] (프로그래머스_42587) 프로세스 - 파이썬

링크: https://school.programmers.co.kr/learn/courses/30/lessons/42587✅ 순서가 첫번째부터임 리스트 0번으로 return하면 당연히 틀림 ✅ 최대 힙임 (최소힙으로 습관적으로 쓰지 말것)✅ 가장 우선 순위가 높은 프로세스를 찾을 때 max() 나 any()를 쓰는 거 보단 힙을 사용하는게 더 빠름  최신 풀이: 큐+힙 사용(더 빠름)이전 풀이: 큐+any사용시간 ✅ 최신 풀이: 큐+힙 사용from collections import dequefrom heapq import heappush,heappopdef solution(priorities, location): q = deque() hq = [] for i,p in enumerate(pr..

[해시] (프로그래머스_42577) 전화번호 목록 - 파이썬

✅ 해시 풀이- dict.fromkeys([arr],value)- 하나씩 더해가면서 자기자신아니면서 dict에 있는지 비교def solution(phone_book): d = dict.fromkeys(phone_book,1) for num in phone_book: temp = "" for n in num: temp+=n if temp in d and temp!=num: return False return True ✅ 정렬 풀이 - 일단 정렬- 파이썬 문자열의 y.startswith(x)사용하여 판단 (+endswith()도 있음)def solution(phone_book): phone_boo..

[해시] 해시 이론

✅ 파이썬에서 해시(Hash)는 Dictionary 를 통해 구현 가능함 ✅ 언제 사용? 1) 키-값으로 저장할때 (숫자 아닌값으로 저장-찾을때)2) 빠른 접근-탐색 시 O(1)3) 집계 필요 시  -> from collections import Counter ✅ Dictionary 사용법#dict.fromkeys([arr],value)arr원소를 key로하고 value를 가진 딕셔너리가 생성됨keys = ['a', 'b', 'c']value = 0d = dict.fromkeys(keys, value)print(d) # 출력: {'a': 0, 'b': 0, 'c': 0}# 빈딕셔너리 생성dict1 = {} # {}dict2 = dict() # {}# 특정 key-value쌍을 가진 dictionary ..