Skip to content

Commit

Permalink
bug fix and comments updated
Browse files Browse the repository at this point in the history
  • Loading branch information
srinivasvadige committed Jan 19, 2025
1 parent 6630740 commit 9f2dc4b
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions Algorithms/Strings/ParenthesesPerfectionKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ You are given an initial parentheses sequence represented by the string s, along
If the user used the 0th indexed and 2nd indexed parentheses from the bag and add them to the start and end of the
string respectively, then the final balanced sequence will be "(0)" with a total EfficiencyScore of 4 + (-3) = 1. There are
no other combinations of adding parentheses that can yield a balanced sequence with total EfficiencyScore greater than 1, Hence return 1 as answer.
consider ")(", ")((", ")(()))" scenarios
</pre>
Expand All @@ -79,8 +81,6 @@ public static void main(String[] args) {
efficiency_rating = [3,4,2,-4,-1,-3]
print(find_max_to_balance(s, kit, efficiency_rating)) # Output: 6
")(", ")((", ")(()))" scenarios
the map of kit and efficiency_rating looks like:
) ( ( ) ) )
3 4 2 -4 -1 -3
Expand All @@ -94,7 +94,13 @@ public static void main(String[] args) {
3 -1 -3 -4
* *
i.e we need 4 + 3 + (-1) => 6
"(" openOrphanCount == ")" close efficiency counter-part and vice-versa
s = ")(("
Here, we have 2 openOrphans and 1 closeOrphans then we need to add 2 maxCloseEfficiencies and 2 maxOpenEfficiency
i.e
2 openOrphans + 1 closeOrphans=> 2 ")" closeEff + 1 "(" openEff => 3+(-1)+4 => 6
*/
// TODO: validate open & close count with different test cases
public static int findMaxToBalanceUsingPriorityQueueMyApproach(String s, String kit, int[] efficiencyRating) {
Expand All @@ -120,13 +126,16 @@ else if (s1.equals(")")) {
else closePQ.add(efficiencyRating[i]);
}

// sum all the orphans efficiency score
// "(" openOrphanCount = ")" close counter part and vice-versa
int max = 0;
while(open>0) {
for (;open>0;open--)
max += closePQ.isEmpty()?0:closePQ.poll();

while(close>0) {
max += openPQ.isEmpty()?0:openPQ.poll();
open--;
close--;
}
for (;close>0;close--)
max += closePQ.isEmpty()?0:closePQ.poll();

return max;
}
Expand Down

0 comments on commit 9f2dc4b

Please sign in to comment.