Skip to content

Commit

Permalink
1124 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Jul 15, 2019
1 parent 63430a4 commit f777665
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 1124-Longest-Well-Performing-Interval/cpp-1124/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.14)
project(C)

set(CMAKE_CXX_STANDARD 14)

add_executable(C main3.cpp)
39 changes: 39 additions & 0 deletions 1124-Longest-Well-Performing-Interval/cpp-1124/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
57 changes: 57 additions & 0 deletions 1124-Longest-Well-Performing-Interval/cpp-1124/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/// Source : https://leetcode.com/problems/longest-well-performing-interval/
/// Author : liuyubobobo
/// Time : 2019-07-15

#include <iostream>
#include <vector>

using namespace std;


/// Presum + Binary Search
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int longestWPI(vector<int>& hours) {

for(int& e: hours)
e = (e > 8 ? 1 : -1);

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 l = 0, r = n;
while(l < r){
int mid = (l + r + 1) / 2;
if(ok(presum, mid))
l = mid;
else
r = mid - 1;
}
return l;
}

private:
bool ok(const vector<int>& presum, int len){

int minv = INT_MAX;
for(int i = 0; i + len < presum.size(); i ++){
minv = min(minv, presum[i]);
if(presum[i + len] - minv > 0)
return true;
}
return false;
}
};


int main() {

vector<int> hours = {9,9,6,0,6,6,9};
cout << Solution().longestWPI(hours) << endl;

return 0;
}
46 changes: 46 additions & 0 deletions 1124-Longest-Well-Performing-Interval/cpp-1124/main3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/// Source : https://leetcode.com/problems/longest-well-performing-interval/
/// Author : liuyubobobo
/// Time : 2019-07-15

#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;


/// Presum + Hash Map
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int longestWPI(vector<int>& hours) {

for(int& e: hours)
e = (e > 8 ? 1 : -1);

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];

unordered_map<int, int> pos;
int res = 0;
for(int i = 1; i <= n; i ++)
if(presum[i] > 0) res = max(res, i);
else{
if(!pos.count(presum[i])) pos[presum[i]] = i;
if(pos.count(presum[i] - 1)) res = max(res, i - pos[presum[i] - 1]);
}
return res;
}
};


int main() {

vector<int> hours = {9,9,6,0,6,6,9};
cout << Solution().longestWPI(hours) << endl;

return 0;
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -766,4 +766,6 @@ email: [[email protected]](mailto:[email protected])
| 1119 | [Remove Vowels from a String](https://leetcode.com/problems/remove-vowels-from-a-string/) | [] | [C++](1119-Remove-Vowels-from-a-String/cpp-1119/) | | |
| 1120 | [Maximum Average Subtree](https://leetcode.com/problems/maximum-average-subtree/) | [] | [C++](1120-Maximum-Average-Subtree/cpp-1120/) | | |
| 1121 | [Divide Array Into Increasing Sequences](https://leetcode.com/problems/divide-array-into-increasing-sequences/) | [] | [C++](1121-Divide-Array-Into-Increasing-Sequences/cpp-1121/) | | |
| | | | | | |
| 1124 | [Longest Well-Performing Interval](https://leetcode.com/problems/longest-well-performing-interval/) | [] | [C++](1124-Longest-Well-Performing-Interval/cpp-1124/) | | |
| | | | | | |

0 comments on commit f777665

Please sign in to comment.