File tree Expand file tree Collapse file tree 2 files changed +19
-22
lines changed Expand file tree Collapse file tree 2 files changed +19
-22
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * PROBLEM: 2706: Buy Two Chocolates
3
+ */
4
+ public class BuyTwoChocolates {
5
+ public int buyChoco (int [] prices , int money ) {
6
+ int cheapestChocolate = Math .min (prices [0 ], prices [1 ]);
7
+ int secondCheapestChocolate = Math .max (prices [0 ], prices [1 ]);
8
+ for (int i = 2 ; i < prices .length ; i ++) {
9
+ if (prices [i ] < cheapestChocolate ) {
10
+ secondCheapestChocolate = cheapestChocolate ;
11
+ cheapestChocolate = prices [i ];
12
+ } else if (prices [i ] < secondCheapestChocolate ) {
13
+ secondCheapestChocolate = prices [i ];
14
+ }
15
+ }
16
+ int totalCost = cheapestChocolate + secondCheapestChocolate ;
17
+ return totalCost <= money ? (money - totalCost ) : money ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change 1
- public String removeOuterParentheses (String S ) {
2
- Stack <Character > stack = new Stack <>();
3
- StringBuilder sb = new StringBuilder ();
4
- for (int i = 0 ; i < S .length (); i ++) {
5
- char c = S .charAt (i );
6
- if (stack .isEmpty () && c == '(' ) {
7
- stack .push (c );
8
- continue ;
9
- }
10
- if (stack .size () == 1 && c == ')' ) {
11
- stack .pop ();
12
- continue ;
13
- }
14
- if (c == '(' ) {
15
- stack .push (c );
16
- } else {
17
- stack .pop ();
18
- }
19
- sb .append (c );
20
- }
21
- return sb .toString ();
22
- }
You can’t perform that action at this time.
0 commit comments