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
2fd101c
commit 95d6873
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/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(D) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
|
||
add_executable(D main2.cpp) |
47 changes: 47 additions & 0 deletions
47
1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/main.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,47 @@ | ||
/// Source : https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/ | ||
/// Author : liuyubobobo | ||
/// Time : 2019-07-06 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <cassert> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Halve max depth | ||
/// Time Complexity: O(2n) | ||
/// Space Complexity: O(1) | ||
class Solution { | ||
public: | ||
vector<int> maxDepthAfterSplit(string s) { | ||
|
||
int bound = get_depth(s) / 2; | ||
|
||
vector<int> res(s.size(), 0); | ||
int stack = 0; | ||
for(int i = 0; i < s.size(); i ++){ | ||
if(s[i] == '(') stack ++, res[i] = (stack > bound); | ||
else res[i] = (stack > bound), stack --; | ||
|
||
} | ||
return res; | ||
} | ||
|
||
private: | ||
int get_depth(const string& s){ | ||
|
||
int res = 0, stack = 0; | ||
for(char c: s) | ||
if(c == '(') stack ++, res = max(res, stack); | ||
else stack --; | ||
assert(!stack); | ||
return res; | ||
} | ||
}; | ||
|
||
|
||
int main() { | ||
|
||
return 0; | ||
} |
33 changes: 33 additions & 0 deletions
33
1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/main2.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,33 @@ | ||
/// Source : https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/ | ||
/// Author : liuyubobobo | ||
/// Time : 2019-07-07 | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <cassert> | ||
|
||
using namespace std; | ||
|
||
|
||
/// Alternatively Assign 0 and 1 | ||
/// Time Complexity: O(n) | ||
/// Space Complexity: O(1) | ||
class Solution { | ||
public: | ||
vector<int> maxDepthAfterSplit(string s) { | ||
|
||
vector<int> res(s.size(), 0); | ||
|
||
int stack = 0; | ||
for(int i = 0; i < s.size(); i ++) | ||
if(s[i] == '(') stack ++, res[i] = stack % 2; | ||
else res[i] = stack % 2, stack --; | ||
return res; | ||
} | ||
}; | ||
|
||
|
||
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 |
---|---|---|
|
@@ -753,4 +753,5 @@ email: [[email protected]](mailto:[email protected]) | |
| 1108 | [Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/) | [无] | [C++](1108-Defanging-an-IP-Address/cpp-1108/) | | | | ||
| 1109 | [Corporate Flight Bookings](https://leetcode.com/problems/corporate-flight-bookings/) | [无] | [C++](1109-Corporate-Flight-Bookings/cpp-1009/) | | | | ||
| 1110 | [Delete Nodes And Return Forest](https://leetcode.com/problems/delete-nodes-and-return-forest/) | [无] | [C++](1110-Delete-Nodes-And-Return-Forest/cpp-1110/) | | | | ||
| 1111 | [Maximum Nesting Depth of Two Valid Parentheses Strings](https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/) | [无] | [C++](1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/) | | | | ||
| | | | | | | |