forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
39 lines (29 loc) · 894 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/// Source : https://leetcode.com/problems/longest-well-performing-interval/
/// Author : liuyubobobo
/// Time : 2019-07-13
#include <iostream>
#include <vector>
using namespace std;
/// Presum + Brute Force
/// Time Complexity: O(n^2)
/// Space Complexity: O(n)
class Solution {
public:
int longestWPI(vector<int>& hours) {
for(int& e: hours)
e = (e > 8 ? 1 : 0);
int n = hours.size();
vector<int> presum(n + 1, 0);
for(int i = 1; i <= n; i ++)
presum[i] = presum[i - 1] + hours[i - 1];
int best = 0;
for(int end = 0; end < n; end ++)
for(int start = 0; start <= end && end - start + 1 > best; start ++)
if((presum[end + 1] - presum[start]) * 2 > end - start + 1)
best = max(best, end - start + 1);
return best;
}
};
int main() {
return 0;
}