Skip to content

Commit 138f5f5

Browse files
committed
2116
1 parent aa1e9f5 commit 138f5f5

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

leetcode/2116/0.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"))()))"
2+
"010100"
3+
0 1 0
4+
-1 1 0
5+
-1
6+
-2
7+
-2
8+
-2

leetcode/2116/249.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"()()))()())(((()((()((()((()()()))(()()((()((()()(((()())))))()((()(()(())((()()((())))))))(())(())))()()()((()())())(()()))((((((())()())())))))())((((()())(())(())()()()(()(()((()))((((()((()((()())(())((((())(())))(()((((())))((()(((((()()))))((((()))))())()))))())"
2+
"0111000100000011110100010110101001110111010111110111111011101000100000011101010000110110001100100100100011000001110101101110011000000011111000111111111001011101100000100111010111010000001100011101000110101011001000100100111110110110100101010111111001000010000010010010"

leetcode/2116/89.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"())(()(()(())()())(())((())(()())((())))))(((((((())(()))))("
2+
"100011110110011011010111100111011101111110000101001101001111"

leetcode/2116/main.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
3+
*/
4+
5+
#include <bits/stdc++.h>
6+
7+
#include "../../lib/LeetCodeInputTemplate.hpp"
8+
#include "solution.cpp"
9+
using namespace std;
10+
11+
typedef vector<int> vi;
12+
typedef vector<vector<int>> vvi;
13+
14+
int main() {
15+
LeetCodeInput li("249.in");
16+
17+
auto l0 = li.get<string>(0);
18+
auto l1 = li.get<string>(1);
19+
20+
chrono::steady_clock::time_point begin = chrono::steady_clock::now();
21+
Solution solution;
22+
auto output = solution.canBeValid(l0, l1);
23+
chrono::steady_clock::time_point end = chrono::steady_clock::now();
24+
cout << "Time difference = "
25+
<< chrono::duration_cast<chrono::microseconds>(end - begin).count()
26+
<< "µs" << endl;
27+
28+
cout << output << endl;
29+
30+
// for (int i: output){
31+
// cout << i << ' ';
32+
// }
33+
34+
// for (auto i: output){
35+
// for (int j: i){
36+
// cout << j << ' ';
37+
// }
38+
// cout << '\n';
39+
// }
40+
41+
return 0;
42+
}

leetcode/2116/solution.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
3+
time: O(n)
4+
space: O(1)
5+
6+
Runtime
7+
42 ms
8+
Beats
9+
99.80%
10+
Memory
11+
22.1 MB
12+
Beats
13+
95.56%
14+
*/
15+
16+
#include <bits/stdc++.h>
17+
18+
using namespace std;
19+
20+
#define all(x) begin(x), end(x)
21+
typedef vector<int> vi;
22+
typedef vector<vector<int>> vvi;
23+
24+
class Solution {
25+
public:
26+
bool canBeValid(string& s, string& locked) {
27+
int n = size(s);
28+
if (n % 2 == 1) return false;
29+
30+
int state = 0;
31+
int flex = 0;
32+
int diff = 0;
33+
34+
for (int i = 0; i < n; ++i) {
35+
if (locked[i] == '1') {
36+
if (s[i] == '(') {
37+
++state;
38+
} else {
39+
--state;
40+
if (state == -1) {
41+
if (flex == 0) {
42+
return false;
43+
} else {
44+
++state;
45+
--flex;
46+
}
47+
}
48+
}
49+
} else {
50+
++flex;
51+
}
52+
}
53+
54+
if (state > flex) return false;
55+
56+
state = 0;
57+
flex = 0;
58+
59+
for (int i = n - 1; i >= 0; --i) {
60+
if (locked[i] == '1') {
61+
if (s[i] == ')') {
62+
++state;
63+
} else {
64+
--state;
65+
if (state == -1) {
66+
if (flex == 0) {
67+
return false;
68+
} else {
69+
++state;
70+
--flex;
71+
}
72+
}
73+
}
74+
} else {
75+
++flex;
76+
}
77+
}
78+
79+
return true;
80+
}
81+
};
82+
83+
const static auto initialize = [] {
84+
std::ios::sync_with_stdio(false);
85+
std::cin.tie(nullptr);
86+
std::cout.tie(nullptr);
87+
return nullptr;
88+
}();

0 commit comments

Comments
 (0)