Problem Statement
- The empty string "" is a correct sequence.
- If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
- If "X" is a correct sequence, then "(X)" is a correct sequence.
- Each correct parentheses sequence can be derived using the above rules.
We can define the depth and the cost of a correct parentheses sequence recursively as follows:
- The empty string "" has depth 0 and cost 0.
- Suppose that "S" = "(A)", where A is a correct parentheses sequence. Then we have depth(S) = depth(A)+1 and cost(S) = cost(A) + depth(S)^2.
- Suppose that "S" = "AB", where A and B are correct parentheses sequences. Then we have depth(S) = max(depth(A),depth(B)) and cost(S) = cost(A) + cost(B).
- The depth of "(((())))" is 4, the depth of "()()()" is 1, and the depth of "(())()" is 2.
- The cost of "(((())))" is 4^2 + 3^2 + 2^2 + 1^2 = 30, the cost of "()()()" is 1^2 + 1^2 + 1^2 = 3, and the cost of "(())()" is 6.
You are given a
Your primary goal is to make sure that both s1 and s2 are correct parentheses sequences. If this goal cannot be achieved, return -1.
Your secondary goal is to make cost(s1) + cost(s2) as small as possible. Compute and return the smallest possible value of cost(s1) + cost(s2).
Definition
- Class:
- ParenthesesDiv1Hard
- Method:
- minCost
- Parameters:
- String
- Returns:
- int
- Method signature:
- int minCost(String s)
- (be sure your method is public)
Notes
- Pay attention to the unusual time limit.
Constraints
- s will contain between 1 and 150 elements, inclusive.
- Each character in s will be '(' or ')'.
Examples
"(())"
Returns: 2
The optimal solution is to split s into s1 = s2 = "()". (For example, s1 will be the characters at indices 0 and 2 and s2 will be the characters at indices 1 and 3.) For this split we have cost(s1) = cost(s2) = 1.
"((())())(()()())"
Returns: 11
One optimal solution is: s = ((())())(()()()) s1 = () () ()()() s2 = (( ) )( ) Cost(s1) = 5, Cost(s2) = 6.
"())(()"
Returns: -1
This s cannot be split into two correct sequences.
"(((((((((())))))))))"
Returns: 110
"()"
Returns: 1
"(((())()()()((((()))))(((())))()()()))(()(()))"
Returns: 69
"()()()()()()()()()()()()()()()()()()(())()()()()()()()()()()()()()()()()()()()()()()()()()()()(())()()()()()()()()()(()())()(())()()()()()()()()()(())"
Returns: 75
"()()()()()()()()()()()()()((())())()()()(())(()(()))()()()(())()(())()()(()((()))())()(())()()()(())()()()((()))()(()()())()()()()()()(())()((())())()"
Returns: 93
"(((((((((()(()((((((((((((((()(((((((()())(((((((((((((((((((()(((()()(())()))()((())((((((())))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 17133
"((((()(((((((((((((((()((((((()((((((((((((((((((((((((((((((((((())(()((((((((()())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 27377
"(())()()()()(())()()()()()(()())()()()()()()()()()()()()()()()()(())()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()"
Returns: 75
"((()()(())((((((()(((()(((()(()(()(()((()))(()(()()((((())((())()(())()()((((((()((((()()))())(())((((()((()())))))(()))))))))))))))))))))))))))))))))"
Returns: 4626
"(()())()()(())()(())()()()()()()()()()()((()())()(())())()()(())()(((()()())))()()(()(()))()(())(())()(()())()()(())()()((()))()()(()()())()(())()()()"
Returns: 90
"((()(()(((()(()(((((()(((()(()(((((((()(((()(((((((((((()((((()((((())((((((()))((((((()(()(()))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 16290
"()()()()()(())()()()()()(())()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()(())()()()()()(())()(())()()()"
Returns: 75
"()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()(())()()()()()()()()()()()()()()"
Returns: 75
"(())()(())()((()))()()(())((()))(()())()(((((())()))))()(()((()))())()()()((()(())))()()(())(()(()))()()(())()()()()()()((())(())((()))()(()))(())()()"
Returns: 124
"((((((((()))((()(((((()((((((((()((()((()((()((((())()))()((())((((())(((((((()((()((()()((()(((((()))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 11759
"(((()(()(((((((((((()((((((((((((((((()((((((((((((((()((((((((((()(((((((((((((()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 28601
"()((())()())()()(()())((((()))(()(()()()))(()))(()))()()(())((()()(()()()))()())()()(((()()())()))()()(()(()((()(((()))()((()()((()((()()())))))))))))"
Returns: 280
"()()()()()(())()()()()()()()()()(())((()))()()()()()(()())(())(())()()(())()()()(())()()()()()()()()(()()())()((()))()()()()(())()()((()))()()((()))()"
Returns: 87
"(()((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 35151
"(())()()(()(()((()()((())())))))()((()))()()(())(())()()(()((())(()))())()((()))(())()(((((())))))()()((())())()()()()()()((()())()(())()(()())())()()"
Returns: 152
"(())(())()(()(()()(()()()((())()()())()))()(()))(())(())()()((()))((())()(()))()(())()()()()(())()(()())()()()()((())()(()))()(())()(()())()()(())()()"
Returns: 106
"()(((((()())))()()))()(()()(())(((())))())((())())()(()()())((((()(())(()))((()())))()))()()()()()()()()()()()()(()(()((((((())))())(()((((())))))))))"
Returns: 253
"(((((((((((((()(()((((()(()))((((((((((((())(()(((((((()(((((((((((((((((((()((())(((((())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 20848
"(())(((())((())())()()))((()(((())(())))))"
Returns: 57
"((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 18010
"()()()()()()(())(())()()()()()()()()()()()()()(())()()()()()()()()(())()()()()(())()()()()()()()()()()()()()()(())()()()((())())()(())(())()"
Returns: 73
"()()()((((((())))(()((((((()((((((()(((()()(((((()((((((((((((())())())(()((()()((()))))))))))))))))))))))))))))))))))))))"
Returns: 5374
"((((((((((()()(((((((((()(((()((((((((()((((((((((((((((()((((((((()))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 14650
"()()()(())()((())())(())()((()))"
Returns: 22
"()(((((()((()(())(((()())))((((((()()()()())))))))))))))"
Returns: 300
"(()()(((((()(((((((((((((()((((())))))))))))))))))))))))"
Returns: 1304
"()(())()()()()()()()()"
Returns: 11
"()()()()()()()()()()()()()()()(())()()()()()()()()()()()()()"
Returns: 30
"()()()()(())()()"
Returns: 8
"()()(())((())(())(())(()(())(((()(((((()())((())()((())((()(())))()()(()()((()(((((((()))))(())((())(((())(()(())())(()(()))))))))))))))))))))))"
Returns: 1228
"()()((()())()(())(())(()()))(()(()((()((())()((((()())(()((((((((())(((((((((((()(((((()))))))))))))))))))))))))))))))))"
Returns: 3311
"(()()(()))(())"
Returns: 10
"()()()()()()(())()()()()()()()()()()()()()()"
Returns: 22
"()()()()()()()(())()()()()(())()()()(())(())()()()()()()()(())()()(()())(())()()()()"
Returns: 42
"((()(())()))"
Returns: 12
"()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()"
Returns: 68
"()()()()()()()()()()()((()(())(((()))))()(()()))"
Returns: 46
"()()()()()()()()()()()()()()()()()"
Returns: 17
"((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 36594
"("
Returns: -1
")"
Returns: -1
"()))()()))())(()()))())()))))((())())))())(()(()()())))(()))))"
Returns: -1
")(())((("
Returns: -1
"(())(()((((((((((((((((((((("
Returns: -1
"))()))))())()))))(())(()))()))()))()()()()("
Returns: -1
"()(())())))())(((((()))(((())())(((())((()()))((()))((()"
Returns: -1
"())))()())))(()()))))()))())())()))))))))()())()))((((())(())(()()))))))((()))))()())()))))))(()((()"
Returns: -1
"(())))()()((()(((()))()(()())(())()))()))))()())())))((((((()()())()())))))()(())(()(((()()(())())()()()()((()))("
Returns: -1
")())())()"
Returns: -1
"((((((((((((((((()(((((((((((((((((((((("
Returns: -1
"(()((((()((((()((((((((((((()(((()(((()(())((())(((())()()"
Returns: -1
"))())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: -1
"))()))(((())))))(())))))())))))(()))((()))))(())())(()()))))()))())(((())"
Returns: -1
"()()))))(())))))()())))()))))))()()())))))))()))())())))()))))))))))()))))()))))()))()(())))))())())))))())))))())))))))))))()))))))"
Returns: -1
"(((((((()((((((((("
Returns: -1
")))()((()((()()())))(((())))((((((()())((())(()(((()))())))))))()()()())))())((()()((()(((()()())())))))())()))(()(())((((()())()"
Returns: -1
"((((((((()((((()()((((((()())))((())"
Returns: -1
"(((()(()((()()()((((((((()(((((((((((((((((("
Returns: -1
"(((()()))((((((((((((((((((((((((((((((()(((((((((((((((()(()(((((((((()((((((((((()(((("
Returns: -1
"((()(((((((((((((((((((((((((((((()(((()((((((()((((((((((()((((()((((((((((((((((((((()(()((((((((((((((((("
Returns: -1
"((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()(((((((((((((((((((((()(((((((("
Returns: -1
"()()()()(()())()()()()()(())()()(()())()()()()()()()()((()(()())))()(()(()()))()((()()))()()()(()()())(((())()))(())(())(((((()())()())()()))((())))()"
Returns: 118
"()()()()()()()()()()()()()()()()()()(())()()()()()()()()()()()()()()()(())()()()(())()()()()()()()()()()()(())()()()()()()()(())()()()()()()()()()()()"
Returns: 75
"()(()(()(()((((())))()(()())))((()()((()()))))()(())))()(()(())())(())()()(((()))(()(((()((())()))())))())()()()()(())(()(()))(())()(())()(())()(())()"
Returns: 202
"(())()()()(((()())))()(((()((())((()(((()(((()()()()(((())(()(((())(((((((()()(()((()()()(((())(((((())))()(((()(())))))))))))))))))))))))))))))))))))"
Returns: 4269
"()()()(()(()()()))((()((())))((()()((()(()())))))(()(()((()((((((((()((())()()))((())(()(()))))(())()())(()(((()())))))())()))(())))()))))()()()()()()"
Returns: 518
"((())())(()())()((()))()(()(()()))(()())()(()())()()()(()(()))((((()))()))((()(()())(((((()()))))(((((()))(())())))())()))(())()(())(())(()())((()()))"
Returns: 194
"()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()"
Returns: 75
"(())(()())(()())()()(()(()))(())(())()()()()()()()()()()(())()()(((())))()((()(()())))((()))()()()(())()()()()()((()))()()()(()())(())()()()()()()()()"
Returns: 96
"(((((()(())))()))())()()(())((())())()()()((()))()()(())(())()()(())()()(()(()()((())(())())())())(()((((()()))))(()()()(()()))(((())())(()))(((()))))"
Returns: 169
"(()(()(())()))()()((()(()(()((((((()())((())((((()))()))((((()()()()()()((((((((())())(()())(()))(((()))()((((())))((())(())(())))))))))))))))))))))))"
Returns: 1551
"(())((((((())(()((((()((((())())(((((()(())((()))(()))((((()(())(((()(((())))((()(((((((((((()()()(((())(((())))))))))))))))))))))))))))))))))))))))))"
Returns: 6670
"()()()()()()()()(())()()()()()()()()()()()()()(())(()())(())()()()()()(())()()()()()(())()()()()(())()(())()()()()()()()(())(()(()))((()(()))())()(())"
Returns: 84
"()(((((((((((((((((((((((((((((((((((((((((((((((((((((((()(((((((((((((((((()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 33783
"()(((((((((((((((((((((((((((((((()(((((((((((((((((((((()(((((((((((((((((((())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 32415
"()(()()())((())())()()()()()()()()()()()()()()((()))()(()(()())())()(())()()()()()()()()()(())()()()()((()))()()()()(())((()))((()))()()()(())()(()())"
Returns: 93
"(())()(())(((((((()))(()()))()(()(()(()((((((((()(()(()(()))()(((((((((((())())())()(((((()((((((()())(()()(((()))))))))))))))))))))))))))))))))))))))"
Returns: 5391
"(())()()()()()()(())()(())(())()()()(())()()()()()()()()(())()()(())((())()())()()()(())(())()()(())()()()()(())()()()()()()(())()()()()()()()()()()()"
Returns: 78
"()()()()()(())()()()()()()()((())((()())))(()())()()((()))()(())(()())(()())(())()()()()(())()()()()()()(()()(()()))()()(())()()(()(()(())))()()()()()"
Returns: 93
"((((((((((((((((((()(((((())((((((((()(()((((((((((((()((((((((((((((())((((((((((()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 26222
"(((((((((((((())((((((((((((((((((((()((((((((((((()((((((((((((((((((()(((((((())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 29825
"()(()(()))()(((())(((((())(()()()()((())()(()))(()()())))(()()(()())())()))))(()()(()()((()))((()(()(((()((()(()()))())))()))))((())()))()))()()()(())"
Returns: 381
"((()()(())))(())()(())()(())()(())(()())()(()()(((()(()))))((((()())))(())()))()((()))(()((()))(()))()()()(())()()()()(())()(()())()()(()((()(()))))()"
Returns: 140
"(()(()(((()())))))()((()(()(((()(((())((()((())))))))(((())(((((())()((()((()()(()(())))))(((((()(((()(()()())()))()(())(((())))())())))))))))))))))))"
Returns: 1167
"()()()()(())()()()()()()()(())()()()()()()()()()()()()()()()()()()()(())(())()()(())()()()(((())))()()(()())()(())((())())((())((()))())()()()()()(())"
Returns: 90
"()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()(())()()()()()()()()()()()()()()()()()()()()()()()"
Returns: 75
"(())()()()()((()))()()()()()((()))()(()(()))(())()()((())(()))()(())(())()()()()()()()(())()()()()()()((()()))()()()()()()(()(())())()()(()(())(()))()"
Returns: 96
"()(()((()(((()))((()())())))))(()((())(()())))(((()())()))((()))()()()(()())((((()(()()()()))())))()()()((())()()(((()(()())))))(()()((())()))()()()()"
Returns: 180
"(())(()(()((())())()()(((()))((((()()()(()(()((()))()((()()((()))(()()))(()))(((())(()(()))()(((()))()(())())(()()(((()(((((((()))))))))))))))))))))))"
Returns: 1240
"()(((()(((((((()(((((((((((((((((((((((((((((((((((((())(((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 29825
"()()()()()()()()(()())()()()((()))()()(()(()))()()()()()(())()(((()(()()))))()()()()()()()()()()()(())()()((()()))(())()()()()()(())(())(()())()()()()"
Returns: 98
"(())()()()(((()()())))()()()()((()))()(((())))()()(())()()()()()()((()(()()())()))((()))()()(()()()()()()(()))()()((()((()))))(())()()()()()(())()()()"
Returns: 116
"((((((()((((()((()()((((()(((()((((((()(((((()()((((()((()(())((())(((())()((((())(())(()()(((()((()()))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 10451
"(())()()()()()()()((()))()(())((()))((()()))(())(())()()(())(()())()(())(()())()((()))()()()(())()()()(())()()()()()()()()()()()(()(()))()()()(())()()"
Returns: 90
"()((((((((()(()(()(((((()()(()((()((())(()(((((()(()()((()())(((((((()((()())(((()(()())(((()()())(((((())))))))))))))))))))))))))))))))))))))))))))))"
Returns: 8677
"(())(()())()(()((((((()(((()(())((()))((()())(())()))()())()(()((()(()(()()(()(())))())()()))())())(((())))((((((())(()()(()))()))))()(((())))))))))))"
Returns: 548
"()(((((((((((((((((((((((((((()((((((((((((((((((((((((((((((((((((((((()()(((()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 31120
"()()()()((())()())((()())())(())()(())()()()(()(()))()()()()()()((()))(())()(()(((())))(()(()()())))()((()))()()()()(((())))(()()()()((())))(())()()()"
Returns: 119
"((()(((((((((((((((((((((((((((((((((((((((((((((()((((((()(((((((((((((())((((())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 29825
"((((((((((((((((((((((((((((((((((((((((((((()(((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))"
Returns: 35151
"()()(())()(())()()(())()()()()(())()()(()())()()()()()()()()()()()()(())()()()()()()()(()())()()()(()())()()()((()))()()()(())()()()(())()()()(()(()))"
Returns: 81
"(((((((((()()())())())()(()()()()())(((()())))(())(()()))((((())(())))(()((()(((((())()()(((())))())()())(()())()))(()))()((())(())((()()))())))))))))"
Returns: 579
"((()"
Returns: -1