|
| 1 | +## 1. Brute Force |
| 2 | + |
| 3 | +::tabs-start |
| 4 | + |
| 5 | +```python |
| 6 | +class Solution: |
| 7 | + def buyChoco(self, prices: List[int], money: int) -> int: |
| 8 | + res = -1 |
| 9 | + for i in range(len(prices)): |
| 10 | + for j in range(i + 1, len(prices)): |
| 11 | + if prices[i] + prices[j] <= money: |
| 12 | + res = max(res, money - prices[i] - prices[j]) |
| 13 | + return res if res != -1 else money |
| 14 | +``` |
| 15 | + |
| 16 | +```java |
| 17 | +public class Solution { |
| 18 | + public int buyChoco(int[] prices, int money) { |
| 19 | + int res = -1; |
| 20 | + for (int i = 0; i < prices.length; i++) { |
| 21 | + for (int j = i + 1; j < prices.length; j++) { |
| 22 | + if (prices[i] + prices[j] <= money) { |
| 23 | + res = Math.max(res, money - prices[i] - prices[j]); |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + return res == -1 ? money : res; |
| 28 | + } |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +```cpp |
| 33 | +class Solution { |
| 34 | +public: |
| 35 | + int buyChoco(vector<int>& prices, int money) { |
| 36 | + int res = -1; |
| 37 | + for (int i = 0; i < prices.size(); i++) { |
| 38 | + for (int j = i + 1; j < prices.size(); j++) { |
| 39 | + if (prices[i] + prices[j] <= money) { |
| 40 | + res = max(res, money - prices[i] - prices[j]); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + return res == -1 ? money : res; |
| 45 | + } |
| 46 | +}; |
| 47 | +``` |
| 48 | +
|
| 49 | +```javascript |
| 50 | +class Solution { |
| 51 | + buyChoco(prices, money) { |
| 52 | + let res = -1; |
| 53 | + for (let i = 0; i < prices.length; i++) { |
| 54 | + for (let j = i + 1; j < prices.length; j++) { |
| 55 | + if (prices[i] + prices[j] <= money) { |
| 56 | + res = Math.max(res, money - prices[i] - prices[j]); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + return res === -1 ? money : res; |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +::tabs-end |
| 66 | + |
| 67 | +### Time & Space Complexity |
| 68 | + |
| 69 | +* Time complexity: $O(n ^ 2)$ |
| 70 | +* Space complexity: $O(1)$ extra space. |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## 2. Sorting |
| 75 | + |
| 76 | +::tabs-start |
| 77 | + |
| 78 | +```python |
| 79 | +class Solution: |
| 80 | + def buyChoco(self, prices: List[int], money: int) -> int: |
| 81 | + prices.sort() |
| 82 | + buy = prices[0] + prices[1] |
| 83 | + return money if buy > money else money - buy |
| 84 | +``` |
| 85 | + |
| 86 | +```java |
| 87 | +public class Solution { |
| 88 | + public int buyChoco(int[] prices, int money) { |
| 89 | + Arrays.sort(prices); |
| 90 | + int buy = prices[0] + prices[1]; |
| 91 | + return buy > money ? money : money - buy; |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +```cpp |
| 97 | +class Solution { |
| 98 | +public: |
| 99 | + int buyChoco(vector<int>& prices, int money) { |
| 100 | + sort(prices.begin(), prices.end()); |
| 101 | + int buy = prices[0] + prices[1]; |
| 102 | + return buy > money ? money : money - buy; |
| 103 | + } |
| 104 | +}; |
| 105 | +``` |
| 106 | +
|
| 107 | +```javascript |
| 108 | +class Solution { |
| 109 | + buyChoco(prices, money) { |
| 110 | + prices.sort((a, b) => a - b); |
| 111 | + let buy = prices[0] + prices[1]; |
| 112 | + return buy > money ? money : money - buy; |
| 113 | + } |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +::tabs-end |
| 118 | + |
| 119 | +### Time & Space Complexity |
| 120 | + |
| 121 | +* Time complexity: $O(n \log n)$ |
| 122 | +* Space complexity: $O(1)$ or $O(n)$ depending on the sorting algorithm. |
| 123 | + |
| 124 | +--- |
| 125 | + |
| 126 | +## 3. Greedy |
| 127 | + |
| 128 | +::tabs-start |
| 129 | + |
| 130 | +```python |
| 131 | +class Solution: |
| 132 | + def buyChoco(self, prices: list[int], money: int) -> int: |
| 133 | + min1 = min2 = float('inf') |
| 134 | + |
| 135 | + for p in prices: |
| 136 | + if p < min1: |
| 137 | + min1, min2 = p, min1 |
| 138 | + elif p < min2: |
| 139 | + min2 = p |
| 140 | + |
| 141 | + leftover = money - min1 - min2 |
| 142 | + return leftover if leftover >= 0 else money |
| 143 | +``` |
| 144 | + |
| 145 | +```java |
| 146 | +public class Solution { |
| 147 | + public int buyChoco(int[] prices, int money) { |
| 148 | + int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE; |
| 149 | + |
| 150 | + for (int p : prices) { |
| 151 | + if (p < min1) { |
| 152 | + min2 = min1; |
| 153 | + min1 = p; |
| 154 | + } else if (p < min2) { |
| 155 | + min2 = p; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + int leftover = money - min1 - min2; |
| 160 | + return leftover >= 0 ? leftover : money; |
| 161 | + } |
| 162 | +} |
| 163 | +``` |
| 164 | + |
| 165 | +```cpp |
| 166 | +class Solution { |
| 167 | +public: |
| 168 | + int buyChoco(vector<int>& prices, int money) { |
| 169 | + int min1 = INT_MAX, min2 = INT_MAX; |
| 170 | + |
| 171 | + for (int p : prices) { |
| 172 | + if (p < min1) { |
| 173 | + min2 = min1; |
| 174 | + min1 = p; |
| 175 | + } else if (p < min2) { |
| 176 | + min2 = p; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + int leftover = money - min1 - min2; |
| 181 | + return leftover >= 0 ? leftover : money; |
| 182 | + } |
| 183 | +}; |
| 184 | +``` |
| 185 | + |
| 186 | +```javascript |
| 187 | +class Solution { |
| 188 | + buyChoco(prices, money) { |
| 189 | + let min1 = Infinity, min2 = Infinity; |
| 190 | + |
| 191 | + for (const p of prices) { |
| 192 | + if (p < min1) { |
| 193 | + min2 = min1; |
| 194 | + min1 = p; |
| 195 | + } else if (p < min2) { |
| 196 | + min2 = p; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + const leftover = money - min1 - min2; |
| 201 | + return leftover >= 0 ? leftover : money; |
| 202 | + } |
| 203 | +} |
| 204 | +``` |
| 205 | + |
| 206 | +::tabs-end |
| 207 | + |
| 208 | +### Time & Space Complexity |
| 209 | + |
| 210 | +* Time complexity: $O(n)$ |
| 211 | +* Space complexity: $O(1)$ extra space. |
0 commit comments