---
# System prepended metadata

title: LC 2706. Buy Two Chocolates
tags: [easy, leedcode, python]

---

# LC 2706. Buy Two Chocolates

### [Problem link](https://leetcode.com/problems/buy-two-chocolates/)

###### tags: `leedcode` `python` `easy`

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.

You must buy  **exactly**  two chocolates in such a way that you still have some  **non-negative**  leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.

Return the amount of money you will have leftover after buying the two chocolates. 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.

**Example 1:** 

```
Input: prices = [1,2,2], money = 3
Output: 0
Explanation: Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0.
```

**Example 2:** 

```
Input: prices = [3,2,3], money = 3
Output: 3
Explanation: You cannot buy 2 chocolates without going in debt, so we return 3.
```

 **Constraints:** 

- <code>2 <= prices.length <= 50</code>
- <code>1 <= prices[i] <= 100</code>
- <code>1 <= money <= 100</code>


## Solution 1
```python=
class Solution:
    def buyChoco(self, prices: List[int], money: int) -> int:
        prices.sort()
        
        if sum(prices[:2]) > money:
            return money
        return money - sum(prices[:2])
        
```

>### Complexity
>n = prices.length
>|             | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1  | O(nlogn)        | O(n)             |


## Note
x
