Posted on

Description

Submission

class Solution {
public:
    int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {
        int remain = 0;

        int maxProfit = -1;
        int maxRounds = 0;
        int profit = -1;
        int accumulatedProple = 0;
        for(int i = 0; i < customers.size() || remain; ++i) {
            if(i < customers.size())
                remain += customers[i];

            int nPeople = 0;
            if(remain > 4) {
                remain -= 4;
                nPeople = 4;
            } else {
                nPeople = remain;
                remain = 0;
            }
            accumulatedProple += nPeople;

            profit = accumulatedProple * boardingCost - (i + 1) * runningCost;

            if(profit > maxProfit) {
                maxProfit = profit;
                maxRounds = i + 1;
            }
        }
        if(profit < 0) maxRounds = -1;
        return maxRounds;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *