그럴듯한 개발 블로그
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/42583

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 이상하게 이해하고 풀었다가 싹 다 밀고 다시 풀었다.. 이거 문제 진짜 알아듣기 힘든 거 같다 ㅂㄷㅂㄷ

프로그래머스 레벨 2면 백준 실버랬는데 푸는데 한 시간 걸렸다 분하다.... 큐문제는 많이 풀어봤는데... 내가 뻥골드라니

#include <string>
#include <vector>
#include <queue>

using namespace std;

int solution(int bridge_length, int weight, vector<int> truck_weights) 
{ // bridge_length : 다리 길이, weight : 다리에 올라갈 수 있는 최대 무게
    int answer = 0, cur_weight = 0, i = 0, air_cnt = 0;
    queue<int>  q;
    
    while (air_cnt < bridge_length || i < truck_weights.size()) // 큐에 공기만 들어있거나, 트럭이 다들어갔을때까지
    {
        answer++;
        if (q.size() >= bridge_length) // 넘칠 때
        { // 트럭나가
            if (q.front() == 0) // 나가는게 공기면
                air_cnt--;
            cur_weight -= q.front();
            q.pop();
        }
        if (i < truck_weights.size() && cur_weight + truck_weights[i] <= weight)
        { // 트럭들어가(안무너져)
            cur_weight += truck_weights[i];
            q.push(truck_weights[i++]); // 트럭들어가
        }
        else // 트럭들어가(무너져)
        {
            q.push(0); // 공기들어가
            air_cnt++;
        }
    }
    return answer;
}

먼가 야매로 푼 느낌? 다른사람의 풀이가 궁금하다

#include <string> // 프린트 가내수공업의 현장...
#include <vector>
#include <queue>
#include <iostream>

using namespace std;

int solution(int bridge_length, int weight, vector<int> truck_weights) 
{ // bridge_length : 다리 길이, weight : 다리에 올라갈 수 있는 최대 무게
    int answer = 0, cur_weight = 0, i = 0, air_cnt = 0;
    queue<int>  q;
    
    while (air_cnt < bridge_length || i < truck_weights.size()) // 큐에 공기만 들어있거나, 트럭이 다들어갔을때까지
    {
        answer++;
        cout << answer << ": ";
        if (q.size() >= bridge_length) // 넘칠 때
        { // 트럭나가
            cout << "나간트럭: " << q.front() << ' ';
            if (q.front() == 0) // 나갈게 공기면
                air_cnt--;
            cur_weight -= q.front();
            q.pop();
        }
        if (i < truck_weights.size() && cur_weight + truck_weights[i] <= weight)
        { // 트럭들어가(안무너져)
            cout << "들어간트럭: " << "truck_weights[i]";
            cur_weight += truck_weights[i];
            q.push(truck_weights[i++]);
        }
        else // 트럭들어가(무너져)
        {
            cout << "공기들어감";
            q.push(0); // 아무것도 안들어가기
            air_cnt++;
        }
        cout << '\n';
    }
    return answer;
}
반응형
profile

그럴듯한 개발 블로그

@donghyk2

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!