Skip to content

Commit

Permalink
1111 added.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Jul 7, 2019
1 parent 2fd101c commit 95d6873
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
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)
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;
}
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;
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/) | | |
| | | | | | |

0 comments on commit 95d6873

Please sign in to comment.