2023-12-29 13:40:49 +01:00
|
|
|
class Solution {
|
2024-03-02 21:01:53 +01:00
|
|
|
public int buyChoco(int[] prices, int money) {
|
|
|
|
int[] mins = new int[2];
|
|
|
|
mins[0] = mins[1] = Integer.MAX_VALUE;
|
2024-01-19 07:12:37 +01:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
for (int p : prices) {
|
|
|
|
if (p < mins[0]) {
|
|
|
|
mins[1] = mins[0];
|
|
|
|
mins[0] = p;
|
|
|
|
} else if (p < mins[1]) {
|
|
|
|
mins[1] = p;
|
|
|
|
}
|
|
|
|
}
|
2023-12-29 13:40:49 +01:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
int leftover = money - mins[0] - mins[1];
|
|
|
|
if (leftover < 0) {
|
|
|
|
return money;
|
2023-12-29 13:40:49 +01:00
|
|
|
}
|
2024-03-02 21:01:53 +01:00
|
|
|
return leftover;
|
|
|
|
}
|
2023-12-29 13:40:49 +01:00
|
|
|
}
|