https://leetcode.com/problems/longest-well-performing-interval/
将满足条件的视为+1,不满足的视为-1,计算前缀和。该题可以转换为计算和大于0的最小区间。
class Solution { public: int longestWPI(vector<int>& hours) { unordered_map<int,int> m; m[0] = -1; int days = 0; int result = 0; for(int i = 0;i < hours.size();i++) // end { if(hours[i] > 8) days += 1; else days -= 1; if(days > 0) { result = max(result ,i + 1); } else { if(m.find(days) == m.end()) { m[days] = i; } int expect = days - 1; if(m.find(expect) != m.end()) { result = max(result, i - m[expect]); } } } return result; } };
来源:https://www.cnblogs.com/fish1996/p/12275322.html