|
| 1 | +# [2706. Buy Two Chocolates](https://leetcode.com/problems/buy-two-chocolates) |
| 2 | + |
| 3 | +[中文文档](/solution/2700-2799/2706.Buy%20Two%20Chocolates/README.md) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +<p>You are given an integer array <code>prices</code> representing the prices of various chocolates in a store. You are also given a single integer <code>money</code>, which represents your initial amount of money.</p> |
| 8 | + |
| 9 | +<p>You must buy <strong>exactly</strong> two chocolates in such a way that you still have some <strong>non-negative</strong> leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.</p> |
| 10 | + |
| 11 | +<p>Return <em>the amount of money you will have leftover after buying the two chocolates</em>. If there is no way for you to buy two chocolates without ending up in debt, return <code>money</code>. Note that the leftover must be non-negative.</p> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | +<p><strong class="example">Example 1:</strong></p> |
| 15 | + |
| 16 | +<pre> |
| 17 | +<strong>Input:</strong> prices = [1,2,2], money = 3 |
| 18 | +<strong>Output:</strong> 0 |
| 19 | +<strong>Explanation:</strong> Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p><strong class="example">Example 2:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>Input:</strong> prices = [3,2,3], money = 3 |
| 26 | +<strong>Output:</strong> 3 |
| 27 | +<strong>Explanation:</strong> You cannot buy 2 chocolates without going in debt, so we return 3. |
| 28 | +</pre> |
| 29 | + |
| 30 | +<p> </p> |
| 31 | +<p><strong>Constraints:</strong></p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li><code>2 <= prices.length <= 50</code></li> |
| 35 | + <li><code>1 <= prices[i] <= 100</code></li> |
| 36 | + <li><code>1 <= money <= 100</code></li> |
| 37 | +</ul> |
| 38 | + |
| 39 | +## Solutions |
| 40 | + |
| 41 | +<!-- tabs:start --> |
| 42 | + |
| 43 | +### **Python3** |
| 44 | + |
| 45 | +```python |
| 46 | +class Solution: |
| 47 | + def buyChoco(self, prices: List[int], money: int) -> int: |
| 48 | + prices.sort() |
| 49 | + cost = prices[0] + prices[1] |
| 50 | + return money if money < cost else money - cost |
| 51 | +``` |
| 52 | + |
| 53 | +### **Java** |
| 54 | + |
| 55 | +```java |
| 56 | +class Solution { |
| 57 | + public int buyChoco(int[] prices, int money) { |
| 58 | + Arrays.sort(prices); |
| 59 | + int cost = prices[0] + prices[1]; |
| 60 | + return money < cost ? money : money - cost; |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### **C++** |
| 66 | + |
| 67 | +```cpp |
| 68 | +class Solution { |
| 69 | +public: |
| 70 | + int buyChoco(vector<int>& prices, int money) { |
| 71 | + sort(prices.begin(), prices.end()); |
| 72 | + int cost = prices[0] + prices[1]; |
| 73 | + return money < cost ? money : money - cost; |
| 74 | + } |
| 75 | +}; |
| 76 | +``` |
| 77 | +
|
| 78 | +### **Go** |
| 79 | +
|
| 80 | +```go |
| 81 | +func buyChoco(prices []int, money int) int { |
| 82 | + sort.Ints(prices) |
| 83 | + cost := prices[0] + prices[1] |
| 84 | + if money < cost { |
| 85 | + return money |
| 86 | + } |
| 87 | + return money - cost |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### **TypeScript** |
| 92 | + |
| 93 | +```ts |
| 94 | +function buyChoco(prices: number[], money: number): number { |
| 95 | + prices.sort((a, b) => a - b); |
| 96 | + const cost = prices[0] + prices[1]; |
| 97 | + return money < cost ? money : money - cost; |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### **...** |
| 102 | + |
| 103 | +``` |
| 104 | +
|
| 105 | +``` |
| 106 | + |
| 107 | +<!-- tabs:end --> |
0 commit comments