Skip to content

Commit 201b8e9

Browse files
authored
feat: add solutions to lc problem: No.2706 (#2158)
No.2706.Buy Two Chocolates
1 parent 9e8c727 commit 201b8e9

File tree

8 files changed

+304
-58
lines changed

8 files changed

+304
-58
lines changed

solution/2700-2799/2706.Buy Two Chocolates/README.md

+115-14
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45+
**方法一:排序**
46+
47+
我们可以将巧克力的价格从小到大排序,然后取前两个价格相加,就是我们购买两块巧克力的最小花费 $cost$。如果这个花费大于我们拥有的钱数,那么我们就返回 `money`,否则返回 `money - cost`
48+
49+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 `prices` 的长度。
50+
51+
**方法一:一次遍历**
52+
53+
我们可以在一次遍历中找到最小的两个价格,然后计算花费。
54+
55+
时间复杂度 $O(n)$,其中 $n$ 是数组 `prices` 的长度。空间复杂度 $O(1)$。
56+
4557
<!-- tabs:start -->
4658

4759
### **Python3**
@@ -56,6 +68,19 @@ class Solution:
5668
return money if money < cost else money - cost
5769
```
5870

71+
```python
72+
class Solution:
73+
def buyChoco(self, prices: List[int], money: int) -> int:
74+
a = b = inf
75+
for x in prices:
76+
if x < a:
77+
a, b = x, a
78+
elif x < b:
79+
b = x
80+
cost = a + b
81+
return money if money < cost else money - cost
82+
```
83+
5984
### **Java**
6085

6186
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -70,6 +95,24 @@ class Solution {
7095
}
7196
```
7297

98+
```java
99+
class Solution {
100+
public int buyChoco(int[] prices, int money) {
101+
int a = 1000, b = 1000;
102+
for (int x : prices) {
103+
if (x < a) {
104+
b = a;
105+
a = x;
106+
} else if (x < b) {
107+
b = x;
108+
}
109+
}
110+
int cost = a + b;
111+
return money < cost ? money : money - cost;
112+
}
113+
}
114+
```
115+
73116
### **C++**
74117

75118
```cpp
@@ -83,6 +126,25 @@ public:
83126
};
84127
```
85128
129+
```cpp
130+
class Solution {
131+
public:
132+
int buyChoco(vector<int>& prices, int money) {
133+
int a = 1000, b = 1000;
134+
for (int x : prices) {
135+
if (x < a) {
136+
b = a;
137+
a = x;
138+
} else if (x < b) {
139+
b = x;
140+
}
141+
}
142+
int cost = a + b;
143+
return money < cost ? money : money - cost;
144+
}
145+
};
146+
```
147+
86148
### **Go**
87149

88150
```go
@@ -96,6 +158,24 @@ func buyChoco(prices []int, money int) int {
96158
}
97159
```
98160

161+
```go
162+
func buyChoco(prices []int, money int) int {
163+
a, b := 1001, 1001
164+
for _, x := range prices {
165+
if x < a {
166+
a, b = x, a
167+
} else if x < b {
168+
b = x
169+
}
170+
}
171+
cost := a + b
172+
if money < cost {
173+
return money
174+
}
175+
return money - cost
176+
}
177+
```
178+
99179
### **TypeScript**
100180

101181
```ts
@@ -106,35 +186,56 @@ function buyChoco(prices: number[], money: number): number {
106186
}
107187
```
108188

189+
```ts
190+
function buyChoco(prices: number[], money: number): number {
191+
let [a, b] = [1000, 1000];
192+
for (const x of prices) {
193+
if (x < a) {
194+
b = a;
195+
a = x;
196+
} else if (x < b) {
197+
b = x;
198+
}
199+
}
200+
const cost = a + b;
201+
return money < cost ? money : money - cost;
202+
}
203+
```
204+
109205
### **Rust**
110206

111207
```rust
112208
impl Solution {
113209
pub fn buy_choco(mut prices: Vec<i32>, money: i32) -> i32 {
114210
prices.sort();
115-
116-
let sum = prices[0] + prices[1];
117-
if sum > money {
211+
let cost = prices[0] + prices[1];
212+
if cost > money {
118213
return money;
119214
}
120-
121-
money - sum
215+
money - cost
122216
}
123217
}
124218
```
125219

126220
```rust
127221
impl Solution {
128-
pub fn buy_choco(mut prices: Vec<i32>, money: i32) -> i32 {
129-
prices.sort();
130-
131-
let sum = prices.iter().take(2).sum::<i32>();
132-
133-
if sum > money {
134-
return money;
222+
pub fn buy_choco(prices: Vec<i32>, money: i32) -> i32 {
223+
let mut a = 1000;
224+
let mut b = 1000;
225+
for &x in prices.iter() {
226+
if x < a {
227+
b = a;
228+
a = x;
229+
} else if x < b {
230+
b = x;
231+
}
232+
}
233+
let cost = a + b;
234+
if money < cost {
235+
money
236+
} else {
237+
money - cost
135238
}
136-
137-
money - sum
138239
}
139240
}
140241
```

solution/2700-2799/2706.Buy Two Chocolates/README_EN.md

+115-14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838

3939
## Solutions
4040

41+
**Solution 1: Sorting**
42+
43+
We can sort the prices of the chocolates in ascending order, and then add the first two prices to get the minimum cost $cost$ of buying two chocolates. If this cost is greater than the money we have, then we return `money`. Otherwise, we return `money - cost`.
44+
45+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Where $n$ is the length of the array `prices`.
46+
47+
**Solution 2: One-pass Traversal**
48+
49+
We can find the two smallest prices in one pass, and then calculate the cost.
50+
51+
The time complexity is $O(n)$, where $n$ is the length of the array `prices`. The space complexity is $O(1)$.
52+
4153
<!-- tabs:start -->
4254

4355
### **Python3**
@@ -50,6 +62,19 @@ class Solution:
5062
return money if money < cost else money - cost
5163
```
5264

65+
```python
66+
class Solution:
67+
def buyChoco(self, prices: List[int], money: int) -> int:
68+
a = b = inf
69+
for x in prices:
70+
if x < a:
71+
a, b = x, a
72+
elif x < b:
73+
b = x
74+
cost = a + b
75+
return money if money < cost else money - cost
76+
```
77+
5378
### **Java**
5479

5580
```java
@@ -62,6 +87,24 @@ class Solution {
6287
}
6388
```
6489

90+
```java
91+
class Solution {
92+
public int buyChoco(int[] prices, int money) {
93+
int a = 1000, b = 1000;
94+
for (int x : prices) {
95+
if (x < a) {
96+
b = a;
97+
a = x;
98+
} else if (x < b) {
99+
b = x;
100+
}
101+
}
102+
int cost = a + b;
103+
return money < cost ? money : money - cost;
104+
}
105+
}
106+
```
107+
65108
### **C++**
66109

67110
```cpp
@@ -75,6 +118,25 @@ public:
75118
};
76119
```
77120
121+
```cpp
122+
class Solution {
123+
public:
124+
int buyChoco(vector<int>& prices, int money) {
125+
int a = 1000, b = 1000;
126+
for (int x : prices) {
127+
if (x < a) {
128+
b = a;
129+
a = x;
130+
} else if (x < b) {
131+
b = x;
132+
}
133+
}
134+
int cost = a + b;
135+
return money < cost ? money : money - cost;
136+
}
137+
};
138+
```
139+
78140
### **Go**
79141

80142
```go
@@ -88,6 +150,24 @@ func buyChoco(prices []int, money int) int {
88150
}
89151
```
90152

153+
```go
154+
func buyChoco(prices []int, money int) int {
155+
a, b := 1001, 1001
156+
for _, x := range prices {
157+
if x < a {
158+
a, b = x, a
159+
} else if x < b {
160+
b = x
161+
}
162+
}
163+
cost := a + b
164+
if money < cost {
165+
return money
166+
}
167+
return money - cost
168+
}
169+
```
170+
91171
### **TypeScript**
92172

93173
```ts
@@ -98,35 +178,56 @@ function buyChoco(prices: number[], money: number): number {
98178
}
99179
```
100180

181+
```ts
182+
function buyChoco(prices: number[], money: number): number {
183+
let [a, b] = [1000, 1000];
184+
for (const x of prices) {
185+
if (x < a) {
186+
b = a;
187+
a = x;
188+
} else if (x < b) {
189+
b = x;
190+
}
191+
}
192+
const cost = a + b;
193+
return money < cost ? money : money - cost;
194+
}
195+
```
196+
101197
### **Rust**
102198

103199
```rust
104200
impl Solution {
105201
pub fn buy_choco(mut prices: Vec<i32>, money: i32) -> i32 {
106202
prices.sort();
107-
108-
let sum = prices[0] + prices[1];
109-
if sum > money {
203+
let cost = prices[0] + prices[1];
204+
if cost > money {
110205
return money;
111206
}
112-
113-
money - sum
207+
money - cost
114208
}
115209
}
116210
```
117211

118212
```rust
119213
impl Solution {
120-
pub fn buy_choco(mut prices: Vec<i32>, money: i32) -> i32 {
121-
prices.sort();
122-
123-
let sum = prices.iter().take(2).sum::<i32>();
124-
125-
if sum > money {
126-
return money;
214+
pub fn buy_choco(prices: Vec<i32>, money: i32) -> i32 {
215+
let mut a = 1000;
216+
let mut b = 1000;
217+
for &x in prices.iter() {
218+
if x < a {
219+
b = a;
220+
a = x;
221+
} else if x < b {
222+
b = x;
223+
}
224+
}
225+
let cost = a + b;
226+
if money < cost {
227+
money
228+
} else {
229+
money - cost
127230
}
128-
129-
money - sum
130231
}
131232
}
132233
```
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
class Solution {
2-
public:
3-
int buyChoco(vector<int>& prices, int money) {
4-
sort(prices.begin(), prices.end());
5-
int cost = prices[0] + prices[1];
6-
return money < cost ? money : money - cost;
7-
}
1+
class Solution {
2+
public:
3+
int buyChoco(vector<int>& prices, int money) {
4+
int a = 1000, b = 1000;
5+
for (int x : prices) {
6+
if (x < a) {
7+
b = a;
8+
a = x;
9+
} else if (x < b) {
10+
b = x;
11+
}
12+
}
13+
int cost = a + b;
14+
return money < cost ? money : money - cost;
15+
}
816
};

0 commit comments

Comments
 (0)