b_lc_经营摩天轮的最大利润(超级模拟≈阅读理解)

思路
当当前利润profit大于历史最大利润mx时才更新turn次数或直接更新turn的次数都是不对的,因为你可能后面来了很多客人,但是前几次客人都为0,这时你直接更新turn就错了,比如:

[0,0,0,0,0,50]
100
1

题目说了,customers[i] 是在第 i 次轮转(下标从 0 开始)之前到达的新游客的数量,意味着每次客人来都要转(0也要)
所以,应该用一个t记录实时的转动次数

class Solution {
public:
    int minOperationsMaxProfit(vector<int>& cs, int bc, int rc) {    //boardingcost,runningcost
        int n=cs.size(), cur_people=0, profit=0, mx=INT_MIN, t=0, turn=0, on;

        for (int i=0; i<n || cur_people>0; ) {
            if (i<n) cur_people+=cs[i], i++;
            if (cur_people<4) on=cur_people;
            else on=4;
            cur_people-=on;
            profit+=on*bc-rc;
            t++;
            if (profit>mx) mx=profit, turn=t;
        }
        return profit<=0 ? -1 : turn;
    }
};
posted @ 2020-10-04 20:30  童年の波鞋  阅读(103)  评论(0编辑  收藏  举报