forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63430a4
commit f777665
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
1124-Longest-Well-Performing-Interval/cpp-1124/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/) | | | | ||
| | | | | | | |