본문 바로가기

Algorithm37

Python 알고리즘 끄적끄적 시간초과 발생 요인반복적인 SUM문제 참고 : 스택/큐 - 다리를 지나는 트럭 (프로그래머스)sum은 시간복잡도가 O(N)인데 반복문 for이나 while을 사용하면 시간복잡도가 O(n^2)이 될 수 있음아래 예시는 sum으로 인해 시간초과가 발생한 경우로 sum을 반복적으로 하는 것이 아닌 계산된 값을 이용while len(truck_list) !=0: b = bridge.popleft() S -= b if S + truck_list[0] > weight: # sum 함수에서 시간 초과 발생 time +=1 bridge.append(0) else: t = truck_list.popleft() bridge.append(t) .. 2025. 1. 20.
★★★ 스택/큐 - 다리를 지나는 트럭 (프로그래머스) https://school.programmers.co.kr/learn/courses/30/lessons/42583 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr  from collections import dequedef solution(bridge_length, weight, truck_weights): l = len(truck_weights) bridge = deque([0] * bridge_length) truck_list = deque(truck_weights) S, time = 0, 0 while len(truck_list) !=0: b = bri.. 2025. 1. 20.
스택/큐 - 프로세스 (프로그래머스) https://school.programmers.co.kr/learn/courses/30/lessons/42587 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr from collections import dequedef solution(priorities, location): prior_list = deque() answer = [] l = len(priorities) prior = deque(sorted(priorities, reverse = True)) for i in range(l): prior_list.append((priorities[i], i)) .. 2025. 1. 18.
스택/큐 - 올바른 괄호 (프로그래머스) https://school.programmers.co.kr/learn/courses/30/lessons/12909 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr  from collections import dequedef solution(s): if s[0] == ')': return False else: par_list = deque() for i in s: if i == '(': par_list.append(i) else: if len(par_list) == .. 2025. 1. 18.