Skip to content

Commit ae22763

Browse files
authored
Create 2706-buy-two-chocolates.kt
1 parent 2c309ba commit ae22763

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

kotlin/2706-buy-two-chocolates.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
fun buyChoco(prices: IntArray, money: Int): Int {
3+
var min1 = Integer.MAX_VALUE
4+
var min2 = Integer.MAX_VALUE
5+
6+
for (p in prices) {
7+
if (p < min1) {
8+
min2 = min1
9+
min1 = p
10+
} else {
11+
min2 = minOf(min2, p)
12+
}
13+
}
14+
15+
val left = money - (min1 + min2)
16+
return if (left >= 0) left else money
17+
}
18+
}

0 commit comments

Comments
 (0)