Skip to content

Commit 623d672

Browse files
authored
Merge pull request #3542 from aadil42/patch-83
Create 2706-buy-two-chocolates.js
2 parents b605973 + 345920d commit 623d672

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

javascript/2706-buy-two-chocolates.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Greedy | Array
3+
* Time O(n) | Space O(1)
4+
* https://leetcode.com/problems/buy-two-chocolates
5+
* @param {number[]} prices
6+
* @param {number} money
7+
* @return {number}
8+
*/
9+
var buyChoco = function(prices, money) {
10+
11+
const [cheapestChocolate] = prices.splice(prices.indexOf(Math.min(...prices)), 1);
12+
const [secondCheapestChocolate] = prices.splice(prices.indexOf(Math.min(...prices)), 1);
13+
const leftOverMoney = money - (cheapestChocolate + secondCheapestChocolate);
14+
return leftOverMoney > -1 ? leftOverMoney : money;
15+
};

0 commit comments

Comments
 (0)