![]() |
VOOZH | about |
Given a number N representing the length of a bracket sequence consisting of brackets '(', ')'. The actual sequence is not known beforehand. Given the values of both brackets '(' and ')' if placed at index in the expression.
The task is to find the minimum sum possible of any bracket sequence of length N using the above information.
Here adj[i][0] represents the value assigned to ')' bracket at ith index and adj[i][1] represents the value assigned to '(' bracket at ith index.
Constraints:
Examples:
Input : N = 4
adj[N][2] ={{5000, 3000},
{6000, 2000},
{8000, 1000},
{9000, 6000}}
Output : 19000
Assigning first index as '(' for proper
bracket expression is (_ _ _ .
Now all the possible bracket expressions are ()() and (()).
where '(' denotes as adj[i][1] and ')' denotes as adj[i][0].
Hence, for ()() sum is 3000+6000+1000+9000=19000.
and (()), sum is 3000+2000+8000+9000=220000.
Thus answer is 19000
Input : N = 4
adj[N][2] = {{435, 111},
{43, 33},
{1241, 1111},
{234, 22}}
Output : 1499
Algorithm:
Below is the implementation of the above approach:
19000
Time Complexity: O(N2)