java: add “2706. Buy Two Chocolates”
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
10369a5031
commit
ab78965115
1 changed files with 21 additions and 0 deletions
21
java/buy-two-chocolates.java
Normal file
21
java/buy-two-chocolates.java
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
class Solution {
|
||||||
|
public int buyChoco(int[] prices, int money) {
|
||||||
|
int[] mins = new int[2];
|
||||||
|
mins[0] = mins[1] = Integer.MAX_VALUE;
|
||||||
|
|
||||||
|
for (int p : prices) {
|
||||||
|
if (p < mins[0]) {
|
||||||
|
mins[1] = mins[0];
|
||||||
|
mins[0] = p;
|
||||||
|
} else if (p < mins[1]) {
|
||||||
|
mins[1] = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int leftover = money - mins[0] - mins[1];
|
||||||
|
if (leftover < 0) {
|
||||||
|
return money;
|
||||||
|
}
|
||||||
|
return leftover;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue