标签:lse long else return ack 时间 color bsp code
又是一道不会做的单调栈,菜哭
1 int longestWPI(vector<int>& hours) { 2 int size = hours.size(); 3 vector<int> vec(size+1 , 0); 4 for (int i = 1; i <= size; i++) 5 { 6 if (hours[i - 1] > 8) 7 vec[i] = vec[i - 1] + 1; 8 else 9 vec[i] = vec[i - 1] - 1; 10 } 11 stack<int> use; 12 for (int i = 0; i <= size ; i++) 13 { 14 if (use.empty() || vec[i] < vec[use.top()]) 15 use.push(i); 16 } 17 int res = 0; 18 for (int i = size ; i > -1; i--) 19 { 20 while (!use.empty()&&vec[i] > vec[use.top()]) 21 { 22 res = max(res, i - use.top()); 23 use.pop(); 24 } 25 } 26 return res; 27 }
标签:lse long else return ack 时间 color bsp code
原文地址:https://www.cnblogs.com/zouma/p/11687754.html