[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;
}
};
=发完看了别人题解后更新=
要什么大根堆啊。。。
标签:blue,pq,true,light,top,切换器,1375,res,III 来源: https://blog.csdn.net/qq_40691051/article/details/104730369