Make Unreal REAL.
article thumbnail
Level 2. 기능개발

 

 

progresses 벡터를 재활용해 개발 완료까지 필요한 일수를 먼저 계산했다.

 

매번 일수를 증가시키고, 최대 일수보다 큰 개발 기간이 나오면 배포하고 최대 일수를 갱신한다.

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

vector<int> solution(vector<int> progresses, vector<int> speeds)
{
    size_t size = progresses.size();
    vector<int> answer;

    auto func = [&](int a, int b) { return ceil((100.f - a) / b); };
    transform(progresses.begin(), progresses.end(), speeds.begin(), progresses.begin(), func);

    int maxIdx = 0, day = 0;

    for (int i = 0; i < size; ++i, ++day)
    {
        if (progresses[maxIdx] < progresses[i])
        {
            answer.emplace_back(day);
            maxIdx = i;
            day = 0;
        }
    }

    answer.emplace_back(day);

    return answer;
}

 

일수인 day 변수를 따로 관리하지 않고, answer 벡터의 마지막에 있는 값을 1씩 증가시켜도 된다.

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

vector<int> solution(vector<int> progresses, vector<int> speeds)
{
    size_t size = progresses.size();
    vector<int> answer {0};

    auto func = [&](int a, int b) { return ceil((100.f - a) / b); };
    transform(progresses.begin(), progresses.end(), speeds.begin(), progresses.begin(), func);

    int maxIdx = 0;

    for (int i = 0; i < size; ++i)
    {
        if (progresses[maxIdx] < progresses[i])
        {
            answer.emplace_back(1);
            maxIdx = i;
        }
        else
        {
            ++answer.back();
        }
    }

    return answer;
}

'자료구조 & 알고리즘 > 프로그래머스' 카테고리의 다른 글

Level 2. 위장  (0) 2023.04.10
Level 2. 더 맵게  (0) 2023.04.09
Level 2. 타겟 넘버  (0) 2023.04.07
Level 2. 스킬트리  (0) 2023.04.06
Level 2. 추억 점수  (0) 2023.04.05
profile

Make Unreal REAL.

@diesuki4

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

검색 태그