Skip to content

Commit ae875d2

Browse files
Merge pull request #25 from lxmn22nov/branch-lxmn2
Java: Sorting 2
2 parents 164a647 + 40052fe commit ae875d2

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

Java/Sorting/BuyTwoChocolates.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}
Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +0,0 @@
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-
}

0 commit comments

Comments
 (0)