알고리즘 스터디 6일차

창고지기·2025년 6월 28일
0

알고리즘스터디

목록 보기
6/22
post-thumbnail

1. 크레인 인형뽑기 게임

2) 문제 분석 및 풀이

1) 설계, 분석

2) 풀이

#include <string>
#include <vector>
#include <stack>

using namespace std;

bool isSameElement(stack<int> st, const int& e)
{
    if (st.empty()) return false;

    if (st.top() == e) return true;

    return false;
}

int solution(vector<vector<int>> board, vector<int> moves) {
    stack<int> crane;
    int answer = 0;

    for (auto e : moves)
    {
        for (int i = 0; i < board.size(); i++)
        {
            int target = board[i][e-1];
            if (target != 0)
            {
                if (isSameElement(crane, target))
                {
                    crane.pop();
                    answer++;
                    answer++;
                }
                else 
                {
                    crane.push(target);
                }
                board[i][e-1] = 0;
                break;
            }
        }

        stack<int> temp;
        while (!crane.empty())
        {
            temp.push(crane.top());
            crane.pop();
        }

        while (!temp.empty())
        {

            crane.push(temp.top());
            temp.pop();
        }
    }
    return answer;
}

2. 카드 뭉치

2) 문제 분석 및 풀이

1) 설계, 분석

2) 풀이

#include <string>
#include <vector>

using namespace std;

string solution(vector<string> cards1, vector<string> cards2, vector<string> goal) {
    string answer = "";
    int index1 =0 , index2 = 0;
    bool found = true;
    for (int i = 0; i < goal.size(); i++)
    {
        auto target = goal[i];
        if (index1 <  cards1.size() && cards1[index1] == target)
        {
            index1++;
        }
        else if (index2 <  cards2.size() && cards2[index2] == target) 
        {
            index2++;
        }
        else 
        {
            found = false;
            break;
        }
    }

    answer = found ? "Yes" : "No";

    return answer;
}

3. 기능 개발

2) 문제 분석 및 풀이

1) 설계, 분석

2) 풀이

#include <string>
#include <vector>

using namespace std;

vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> answer;
    vector<int> days;
    for(int i=0;i<progresses.size();i++)
    {
        int day=0;
        while(progresses[i]<100)
        {
            progresses[i]+=speeds[i];
            day++;
        }
        days.push_back(day);
    }

    int cnt=0;
    int maxDay=0;
    for(int i=0;i<days.size();i++)
    {
        if(maxDay<days[i])
        {
            if(cnt!=0)
                answer.push_back(cnt);
            maxDay=days[i];
            cnt=1;
        }
        else
            cnt++;
    }
    if(cnt!=0)
        answer.push_back(cnt);

    return answer;
}
profile
일단 창고에 넣어놓으면 언젠가는 쓰겠지

0개의 댓글