[ 알고리즘 ]/Stack, Queue

    [프로그래머스/C++] 올바른 괄호

    [프로그래머스/C++] 올바른 괄호

    📌문제 https://school.programmers.co.kr/learn/courses/30/lessons/12909 🎖️난이도 Level 2 ✔️풀이 #include #include //#include => 스택은 벡터로 구현하는게 더 접근 쉬움! #include using namespace std; bool solution(string s) { if (s[0] == ')') return false; vector stack; // string 아니고 char (문자 기호) for (int i = 0; i < s.length(); i++) { if (s[i] == '(') { stack.push_back(s[i]); continue; } if (!stack.empty()) { // "())" 의 경우..

    [프로그래머스] 다리를 지나는 트럭

    📌문제 https://programmers.co.kr/learn/courses/30/lessons/42583 🎖️난이도 Level 2 ✔️풀이 # sol (시간 초과 에러 해결! ==> len()함수 사용 x, 변수로 대체) def solution(bridge_length, weight, truck_weights): sec = 0 on_bridge = [0] * bridge_length weight_sum = 0 # sum() 시간초과 해결하기 위해 변수 추가 while on_bridge: # 0조차도 다 없어지기 전까지 (빈배열이 아니면) 무한 반복 sec += 1 # 한번 다리(큐)를 조작할 때마다 시간은 1초씩 추가 tmp_pop = on_bridge.pop(0) weight_sum -= tmp_p..

    [프로그래머스] 프린터

    📌문제 https://programmers.co.kr/learn/courses/30/lessons/42587 🎖️난이도 Level 2 ✔️풀이 # sol1 (내 location(인덱스)를 이동시키면서 반복하다가 맨앞 & max이면 return order) def solution(priorities,location): order = 0 while priorities: if priorities[0] == max(priorities): # max() 써도 시간 초과 안남 order += 1 priorities.pop(0) if location == 0: return order else: location -= 1 else: priorities.append(priorities.pop(0)) if location ..

    [프로그래머스] 기능개발

    📌문제 https://programmers.co.kr/learn/courses/30/lessons/42586 🎖️난이도 Level 2 ✔️풀이 # sol1 def solution(progresses, speeds): import math answer = [] l = len(progresses) left_days = [math.ceil((100-progresses[i])/speeds[i]) for i in range(l)] baepo_num = 1 tmp_max = left_days[0] for item in left_days[1:]: if item > tmp_max: # 같아도 continue answer.append(baepo_num) tmp_max = item baepo_num = 1 continue..