Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite3af24d

Browse files
authored
Sri Hari: Batch-4/Neetcode-All/Added-articles (neetcode-gh#3767)
* Batch-4/Neetcode-All/Added-articles* Batch-4/Neetcode-All/Added-articles
1 parentff45c13 commite3af24d

25 files changed

+10644
-7
lines changed

‎articles/buy-two-chocolates.md‎

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
##1. Brute Force
2+
3+
::tabs-start
4+
5+
```python
6+
classSolution:
7+
defbuyChoco(self,prices: List[int],money:int) ->int:
8+
res=-1
9+
for iinrange(len(prices)):
10+
for jinrange(i+1,len(prices)):
11+
if prices[i]+ prices[j]<= money:
12+
res=max(res, money- prices[i]- prices[j])
13+
return resif res!=-1else money
14+
```
15+
16+
```java
17+
publicclassSolution {
18+
publicintbuyChoco(int[]prices,intmoney) {
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+
classSolution {
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+
classSolution:
80+
defbuyChoco(self,prices: List[int],money:int) ->int:
81+
prices.sort()
82+
buy= prices[0]+ prices[1]
83+
return moneyif buy> moneyelse money- buy
84+
```
85+
86+
```java
87+
publicclassSolution {
88+
publicintbuyChoco(int[]prices,intmoney) {
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+
classSolution {
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+
classSolution:
132+
defbuyChoco(self,prices: list[int],money:int) ->int:
133+
min1= min2=float('inf')
134+
135+
for pin 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 leftoverif leftover>=0else money
143+
```
144+
145+
```java
146+
publicclassSolution {
147+
publicintbuyChoco(int[]prices,intmoney) {
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+
}elseif (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+
classSolution {
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+
classSolution {
188+
buyChoco(prices,money) {
189+
let min1=Infinity, min2=Infinity;
190+
191+
for (constpof prices) {
192+
if (p< min1) {
193+
min2= min1;
194+
min1= p;
195+
}elseif (p< min2) {
196+
min2= p;
197+
}
198+
}
199+
200+
constleftover= 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

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp