IPO

ㅋㅋ·2023년 2월 23일
0

알고리즘-leetcode

목록 보기
117/135

투자할 수 있는 회사 수를 의미하는 정수 k,

초기 투자할 수 있는 자본금을 의미하는 정수 w,

인덱스를 기준으로 회사의 필요 투자금과 이익이 담긴 정수형 벡터 capital과 profits를 받는다.

문제는 이익을 최대로 했을 때의 최종 자본금을 반환하는 것이다.

class Solution {
public:
    int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {
        

        int length = profits.size();
        vector<pair<int ,int>> candidates(length);
        for (int i = 0; i < length; ++i)
        {
            candidates[i] = {capital[i], profits[i]};
        }

        priority_queue<int, vector<int>> pq;

        std::sort(candidates.begin(), candidates.end());

        int index{0};
        while (0 < k)
        {
            while (index < length)
            {
                if (w < candidates[index].first)
                {
                    break;
                }
                
                pq.push(candidates[index].second);

                ++index;
            }

            if (pq.empty())
            {
                break;
            }

            w += pq.top();
            pq.pop();
            
            --k;
        }

        return w;
    }
};

0개의 댓글