We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent e650410 commit 097cc7aCopy full SHA for 097cc7a
javascript/2706-buy-two-chocolates.js
@@ -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