diff --git a/1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/CMakeLists.txt b/1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/CMakeLists.txt new file mode 100644 index 00000000..bee29d53 --- /dev/null +++ b/1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.14) +project(D) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(D main2.cpp) \ No newline at end of file diff --git a/1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/main.cpp b/1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/main.cpp new file mode 100644 index 00000000..6282d848 --- /dev/null +++ b/1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/main.cpp @@ -0,0 +1,47 @@ +/// Source : https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/ +/// Author : liuyubobobo +/// Time : 2019-07-06 + +#include +#include +#include + +using namespace std; + + +/// Halve max depth +/// Time Complexity: O(2n) +/// Space Complexity: O(1) +class Solution { +public: + vector maxDepthAfterSplit(string s) { + + int bound = get_depth(s) / 2; + + vector 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; +} \ No newline at end of file diff --git a/1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/main2.cpp b/1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/main2.cpp new file mode 100644 index 00000000..51d3afe1 --- /dev/null +++ b/1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/cpp-1111/main2.cpp @@ -0,0 +1,33 @@ +/// Source : https://leetcode.com/problems/maximum-nesting-depth-of-two-valid-parentheses-strings/ +/// Author : liuyubobobo +/// Time : 2019-07-07 + +#include +#include +#include + +using namespace std; + + +/// Alternatively Assign 0 and 1 +/// Time Complexity: O(n) +/// Space Complexity: O(1) +class Solution { +public: + vector maxDepthAfterSplit(string s) { + + vector 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; +} \ No newline at end of file diff --git a/readme.md b/readme.md index 12bec6cc..9e8e0219 100644 --- a/readme.md +++ b/readme.md @@ -753,4 +753,5 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 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/) | | | | | | | | | | \ No newline at end of file