Skip to content

Commit 097cc7a

Browse files
authored
Create 2706-buy-two-chocolates.js
1 parent e650410 commit 097cc7a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Diff for: javascript/2706-buy-two-chocolates.js

+15
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)