diff --git a/1124-Longest-Well-Performing-Interval/cpp-1124/CMakeLists.txt b/1124-Longest-Well-Performing-Interval/cpp-1124/CMakeLists.txt new file mode 100644 index 00000000..fd2e1e0c --- /dev/null +++ b/1124-Longest-Well-Performing-Interval/cpp-1124/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.14) +project(C) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(C main3.cpp) \ No newline at end of file diff --git a/1124-Longest-Well-Performing-Interval/cpp-1124/main.cpp b/1124-Longest-Well-Performing-Interval/cpp-1124/main.cpp new file mode 100644 index 00000000..52c10eea --- /dev/null +++ b/1124-Longest-Well-Performing-Interval/cpp-1124/main.cpp @@ -0,0 +1,39 @@ +/// Source : https://leetcode.com/problems/longest-well-performing-interval/ +/// Author : liuyubobobo +/// Time : 2019-07-13 + +#include +#include + +using namespace std; + + +/// Presum + Brute Force +/// Time Complexity: O(n^2) +/// Space Complexity: O(n) +class Solution { +public: + int longestWPI(vector& hours) { + + for(int& e: hours) + e = (e > 8 ? 1 : 0); + + int n = hours.size(); + vector 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; +} \ No newline at end of file diff --git a/1124-Longest-Well-Performing-Interval/cpp-1124/main2.cpp b/1124-Longest-Well-Performing-Interval/cpp-1124/main2.cpp new file mode 100644 index 00000000..275fba3d --- /dev/null +++ b/1124-Longest-Well-Performing-Interval/cpp-1124/main2.cpp @@ -0,0 +1,57 @@ +/// Source : https://leetcode.com/problems/longest-well-performing-interval/ +/// Author : liuyubobobo +/// Time : 2019-07-15 + +#include +#include + +using namespace std; + + +/// Presum + Binary Search +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int longestWPI(vector& hours) { + + for(int& e: hours) + e = (e > 8 ? 1 : -1); + + int n = hours.size(); + vector 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& 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 hours = {9,9,6,0,6,6,9}; + cout << Solution().longestWPI(hours) << endl; + + return 0; +} \ No newline at end of file diff --git a/1124-Longest-Well-Performing-Interval/cpp-1124/main3.cpp b/1124-Longest-Well-Performing-Interval/cpp-1124/main3.cpp new file mode 100644 index 00000000..78331388 --- /dev/null +++ b/1124-Longest-Well-Performing-Interval/cpp-1124/main3.cpp @@ -0,0 +1,46 @@ +/// Source : https://leetcode.com/problems/longest-well-performing-interval/ +/// Author : liuyubobobo +/// Time : 2019-07-15 + +#include +#include +#include + +using namespace std; + + +/// Presum + Hash Map +/// Time Complexity: O(n) +/// Space Complexity: O(n) +class Solution { +public: + int longestWPI(vector& hours) { + + for(int& e: hours) + e = (e > 8 ? 1 : -1); + + int n = hours.size(); + vector presum(n + 1, 0); + for(int i = 1; i <= n; i ++) + presum[i] = presum[i - 1] + hours[i - 1]; + + unordered_map 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 hours = {9,9,6,0,6,6,9}; + cout << Solution().longestWPI(hours) << endl; + + return 0; +} \ No newline at end of file diff --git a/readme.md b/readme.md index b9fecfb4..b069504c 100644 --- a/readme.md +++ b/readme.md @@ -766,4 +766,6 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 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/) | | | | | | | | | | \ No newline at end of file