解题心得:
题目长且绕,直接看测试样例的解析有助于更快把握题目核心需求(即关注样例的输入、运算逻辑、输出)
题面
原题链接1599. 经营摩天轮的最大利润 - 力扣(LeetCode)
AC代码
class Solution {
public:
int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {
int lft = customers[0], win = 0, mx_win = 0, res = -1;
for(int i=1; ; i++){
if(lft == 0 && i >= customers.size()) break;
//计算和加上转第i次所得利润
if(lft >= 4){
win += 4 * boardingCost - runningCost;
lft -= 4;
}
else{
win += lft * boardingCost - runningCost;
lft = 0;
}
//更新最大利润对应次数轮转res
if(mx_win < win)
{
mx_win = win;
res = i;
}
if(i < customers.size())
{
lft += customers[i];
}
}
return res;
}
};