[leetcode]1375. 灯泡切换器III

[leetcode]1375. 灯泡切换器III
[leetcode]1375. 灯泡切换器III

[leetcode]1375. 灯泡切换器III
[leetcode]1375. 灯泡切换器III

解题思路

其实很简单,一开始没理解题意

题目要求所有灯都亮的次数

代码

class Solution {
public:
    int numTimesAllBlue(vector<int>& light) {
        int res = 0;
        vector<bool>blue(light.size()+1, false); 
        priority_queue<int,vector<int>,less<int>>pq;
        pq.push(0);
        blue[0] = true;
        for(int i = 0; i < light.size(); i++)
        {
            if(light[i] < pq.top())
            {
                if(pq.size() == pq.top())
                {
                    blue[pq.top()] = true;
                    res++;
                    
                }
            }
            else if(light[i] > pq.top())
            {
                if((blue[pq.top()] == true) && light[i] == pq.top() + 1)
                {
                    blue[light[i]] = true;
                    res++;
                }
            }
            pq.push(light[i]);
        }
        return res;
    }
};

=发完看了别人题解后更新=

要什么大根堆啊。。。

[leetcode]1375. 灯泡切换器III

上一篇:细节决定成败--记初学Power Query之分组汇总问题解决


下一篇:N个最小和