Find Max To Balance
Learn this problemProblem statement
At Amazon, a user owns a unique tool called the "Parentheses Perfection Kit." This kit contains different types of parentheses, each with a specific efficiency rating. The goal is to create a balanced sequence of parentheses by adding zero or more parentheses from the kit to maximize the sequences total EfficiencyScore. The EfficiencyScore of a sequence is the sum of the efficiency ratings of the parentheses used from the kit.
A sequence is considered balanced if it has an equal number of opening '(' and closing ')' parentheses, with each opening parentheses properly matched with a closing one in the correct order (i.e., circular balance). For instance, sequences like (()),(), and (()()) are balanced, while sequences like ), ((), and (()())) are not.
You are given an initial parentheses sequence represented by the string s, along with a Parentheses Perfection Kit containing different types of parentheses in the form of the string kitParentheses and their respective efficiency ratings in the efficiencyRatings array (both of size m). The EfficiencyScore of the original strings is initially 0. You can use any number of unused parentheses from the kit to create the final sequence, as long as the final sequence remains balanced.
The task is to determine the maximum possible EfficiencyScore that can be achieved for the resulting balanced sequence.
Note: It is guaranteed that the sequence can be made balanced by adding zero or more parentheses from the kit.
Function
findMaxToBalance(s: String, kitParentheses: String, efficiencyRatings: int[]) → int
Complete the function findMaxToBalance in the editor.
findMaxToBalance has the following parameters:
- 1.
String s: the initial parentheses sequence - 2.
String kitParentheses: the parentheses available in the kit - 3.
int[] efficiencyRatings: the efficiency ratings of the parentheses in the kit
Returns
int: the maximum possible EfficiencyScore for the balanced sequence
Duplicate note, June 26, 2026: I found that this problem is a duplicate of Maximize Parentheses Efficiency Score . I recommend practicing that version because it is more complete. Going forward, I will only track updates on Maximize Parentheses Efficiency Score .
Examples
Example 1
s = "()"kitParentheses = "(())"efficiencyRatings = [4, 2, -3, -3]return = 1Example 2
s = "()"kitParentheses = ")(()))"efficiencyRatings = [3, 4, 2, -4, -1, -3]return = 6More Amazon problems
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Sort Bug Report FrequenciesOA · Seen Jul 2026
- Drone Delivery RouteOA · Seen Jul 2026