-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sri Hari: Batch-4/Neetcode-All/Added-articles (#3767)
* Batch-4/Neetcode-All/Added-articles * Batch-4/Neetcode-All/Added-articles
- Loading branch information
1 parent
ff45c13
commit e3af24d
Showing
25 changed files
with
10,644 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
## 1. Brute Force | ||
|
||
::tabs-start | ||
|
||
```python | ||
class Solution: | ||
def buyChoco(self, prices: List[int], money: int) -> int: | ||
res = -1 | ||
for i in range(len(prices)): | ||
for j in range(i + 1, len(prices)): | ||
if prices[i] + prices[j] <= money: | ||
res = max(res, money - prices[i] - prices[j]) | ||
return res if res != -1 else money | ||
``` | ||
|
||
```java | ||
public class Solution { | ||
public int buyChoco(int[] prices, int money) { | ||
int res = -1; | ||
for (int i = 0; i < prices.length; i++) { | ||
for (int j = i + 1; j < prices.length; j++) { | ||
if (prices[i] + prices[j] <= money) { | ||
res = Math.max(res, money - prices[i] - prices[j]); | ||
} | ||
} | ||
} | ||
return res == -1 ? money : res; | ||
} | ||
} | ||
``` | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int buyChoco(vector<int>& prices, int money) { | ||
int res = -1; | ||
for (int i = 0; i < prices.size(); i++) { | ||
for (int j = i + 1; j < prices.size(); j++) { | ||
if (prices[i] + prices[j] <= money) { | ||
res = max(res, money - prices[i] - prices[j]); | ||
} | ||
} | ||
} | ||
return res == -1 ? money : res; | ||
} | ||
}; | ||
``` | ||
```javascript | ||
class Solution { | ||
buyChoco(prices, money) { | ||
let res = -1; | ||
for (let i = 0; i < prices.length; i++) { | ||
for (let j = i + 1; j < prices.length; j++) { | ||
if (prices[i] + prices[j] <= money) { | ||
res = Math.max(res, money - prices[i] - prices[j]); | ||
} | ||
} | ||
} | ||
return res === -1 ? money : res; | ||
} | ||
} | ||
``` | ||
|
||
::tabs-end | ||
|
||
### Time & Space Complexity | ||
|
||
* Time complexity: $O(n ^ 2)$ | ||
* Space complexity: $O(1)$ extra space. | ||
|
||
--- | ||
|
||
## 2. Sorting | ||
|
||
::tabs-start | ||
|
||
```python | ||
class Solution: | ||
def buyChoco(self, prices: List[int], money: int) -> int: | ||
prices.sort() | ||
buy = prices[0] + prices[1] | ||
return money if buy > money else money - buy | ||
``` | ||
|
||
```java | ||
public class Solution { | ||
public int buyChoco(int[] prices, int money) { | ||
Arrays.sort(prices); | ||
int buy = prices[0] + prices[1]; | ||
return buy > money ? money : money - buy; | ||
} | ||
} | ||
``` | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int buyChoco(vector<int>& prices, int money) { | ||
sort(prices.begin(), prices.end()); | ||
int buy = prices[0] + prices[1]; | ||
return buy > money ? money : money - buy; | ||
} | ||
}; | ||
``` | ||
```javascript | ||
class Solution { | ||
buyChoco(prices, money) { | ||
prices.sort((a, b) => a - b); | ||
let buy = prices[0] + prices[1]; | ||
return buy > money ? money : money - buy; | ||
} | ||
} | ||
``` | ||
|
||
::tabs-end | ||
|
||
### Time & Space Complexity | ||
|
||
* Time complexity: $O(n \log n)$ | ||
* Space complexity: $O(1)$ or $O(n)$ depending on the sorting algorithm. | ||
|
||
--- | ||
|
||
## 3. Greedy | ||
|
||
::tabs-start | ||
|
||
```python | ||
class Solution: | ||
def buyChoco(self, prices: list[int], money: int) -> int: | ||
min1 = min2 = float('inf') | ||
|
||
for p in prices: | ||
if p < min1: | ||
min1, min2 = p, min1 | ||
elif p < min2: | ||
min2 = p | ||
|
||
leftover = money - min1 - min2 | ||
return leftover if leftover >= 0 else money | ||
``` | ||
|
||
```java | ||
public class Solution { | ||
public int buyChoco(int[] prices, int money) { | ||
int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE; | ||
|
||
for (int p : prices) { | ||
if (p < min1) { | ||
min2 = min1; | ||
min1 = p; | ||
} else if (p < min2) { | ||
min2 = p; | ||
} | ||
} | ||
|
||
int leftover = money - min1 - min2; | ||
return leftover >= 0 ? leftover : money; | ||
} | ||
} | ||
``` | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int buyChoco(vector<int>& prices, int money) { | ||
int min1 = INT_MAX, min2 = INT_MAX; | ||
|
||
for (int p : prices) { | ||
if (p < min1) { | ||
min2 = min1; | ||
min1 = p; | ||
} else if (p < min2) { | ||
min2 = p; | ||
} | ||
} | ||
|
||
int leftover = money - min1 - min2; | ||
return leftover >= 0 ? leftover : money; | ||
} | ||
}; | ||
``` | ||
|
||
```javascript | ||
class Solution { | ||
buyChoco(prices, money) { | ||
let min1 = Infinity, min2 = Infinity; | ||
|
||
for (const p of prices) { | ||
if (p < min1) { | ||
min2 = min1; | ||
min1 = p; | ||
} else if (p < min2) { | ||
min2 = p; | ||
} | ||
} | ||
|
||
const leftover = money - min1 - min2; | ||
return leftover >= 0 ? leftover : money; | ||
} | ||
} | ||
``` | ||
|
||
::tabs-end | ||
|
||
### Time & Space Complexity | ||
|
||
* Time complexity: $O(n)$ | ||
* Space complexity: $O(1)$ extra space. |
Oops, something went wrong.