Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 232acfa

Browse files
authored
SriHari: Batch-3/Neetcode-All/Added articles (neetcode-gh#3760)
* small changes * Batch-3/Neetcode-All/Added-articles * Batch-3/Neetcode-All/Added-articles * Batch-3/Neetcode-All/Added-articles * Batch-3/Neetcode-All/Added-articles * Batch-3/Neetcode-All/Added-articles * Batch-3/Neetcode-All/Added-articles * Batch-3/Neetcode-All/Added-articles
1 parent a0098a6 commit 232acfa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+16586
-13
lines changed

articles/best-time-to-buy-and-sell-stock-ii.md

Lines changed: 458 additions & 0 deletions
Large diffs are not rendered by default.

articles/brick-wall.md

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
## 1. Brute Force
2+
3+
::tabs-start
4+
5+
```python
6+
class Solution:
7+
def leastBricks(self, wall: List[List[int]]) -> int:
8+
n = len(wall)
9+
m = 0
10+
for brick in wall[0]:
11+
m += brick
12+
13+
gaps = [[] for _ in range(n)]
14+
for i in range(n):
15+
gap = 0
16+
for brick in wall[i]:
17+
gap += brick
18+
gaps[i].append(gap)
19+
20+
res = n
21+
for line in range(1, m):
22+
cuts = 0
23+
for i in range(n):
24+
if line not in gaps[i]:
25+
cuts += 1
26+
27+
res = min(res, cuts)
28+
return res
29+
```
30+
31+
```java
32+
public class Solution {
33+
public int leastBricks(List<List<Integer>> wall) {
34+
int n = wall.size();
35+
int m = 0;
36+
for (int brick : wall.get(0)) {
37+
m += brick;
38+
}
39+
40+
List<List<Integer>> gaps = new ArrayList<>();
41+
for (int i = 0; i < n; i++) {
42+
gaps.add(new ArrayList<>());
43+
int gap = 0;
44+
for (int brick : wall.get(i)) {
45+
gap += brick;
46+
gaps.get(i).add(gap);
47+
}
48+
}
49+
50+
int res = n;
51+
for (int line = 1; line < m; line++) {
52+
int cuts = 0;
53+
for (int i = 0; i < n; i++) {
54+
if (!gaps.get(i).contains(line)) {
55+
cuts++;
56+
}
57+
}
58+
res = Math.min(res, cuts);
59+
}
60+
61+
return res;
62+
}
63+
}
64+
```
65+
66+
```cpp
67+
class Solution {
68+
public:
69+
int leastBricks(vector<vector<int>>& wall) {
70+
int n = wall.size();
71+
int m = 0;
72+
for (int brick : wall[0]) {
73+
m += brick;
74+
}
75+
76+
vector<vector<int>> gaps(n);
77+
for (int i = 0; i < n; i++) {
78+
int gap = 0;
79+
for (int brick : wall[i]) {
80+
gap += brick;
81+
gaps[i].push_back(gap);
82+
}
83+
}
84+
85+
int res = n;
86+
for (int line = 1; line < m; line++) {
87+
int cuts = 0;
88+
for (int i = 0; i < n; i++) {
89+
if (find(gaps[i].begin(), gaps[i].end(), line) == gaps[i].end()) {
90+
cuts++;
91+
}
92+
}
93+
res = min(res, cuts);
94+
}
95+
96+
return res;
97+
}
98+
};
99+
```
100+
101+
```javascript
102+
class Solution {
103+
/**
104+
* @param {number[][]} wall
105+
* @return {number}
106+
*/
107+
leastBricks(wall) {
108+
const n = wall.length;
109+
let m = 0;
110+
for (const brick of wall[0]) {
111+
m += brick;
112+
}
113+
114+
const gaps = Array.from({ length: n }, () => []);
115+
for (let i = 0; i < n; i++) {
116+
let gap = 0;
117+
for (const brick of wall[i]) {
118+
gap += brick;
119+
gaps[i].push(gap);
120+
}
121+
}
122+
123+
let res = n;
124+
for (let line = 1; line < m; line++) {
125+
let cuts = 0;
126+
for (let i = 0; i < n; i++) {
127+
if (!gaps[i].includes(line)) {
128+
cuts++;
129+
}
130+
}
131+
res = Math.min(res, cuts);
132+
}
133+
134+
return res;
135+
}
136+
}
137+
```
138+
139+
::tabs-end
140+
141+
### Time & Space Complexity
142+
143+
* Time complexity: $O(m * n * g)$
144+
* Space complexity: $O(n * g)$
145+
146+
> Where $m$ is the sum of widths of the bricks in the first row, $n$ is the number of rows and $g$ is the average number of gaps in each row.
147+
148+
---
149+
150+
## 2. Hash Map
151+
152+
::tabs-start
153+
154+
```python
155+
class Solution:
156+
def leastBricks(self, wall: List[List[int]]) -> int:
157+
countGap = {0: 0}
158+
159+
for r in wall:
160+
total = 0
161+
for i in range(len(r) - 1):
162+
total += r[i]
163+
countGap[total] = 1 + countGap.get(total, 0)
164+
165+
return len(wall) - max(countGap.values())
166+
```
167+
168+
```java
169+
public class Solution {
170+
public int leastBricks(List<List<Integer>> wall) {
171+
HashMap<Integer, Integer> countGap = new HashMap<>();
172+
countGap.put(0, 0);
173+
174+
for (List<Integer> row : wall) {
175+
int total = 0;
176+
for (int i = 0; i < row.size() - 1; i++) {
177+
total += row.get(i);
178+
countGap.put(total, countGap.getOrDefault(total, 0) + 1);
179+
}
180+
}
181+
182+
int maxGaps = 0;
183+
for (int count : countGap.values()) {
184+
maxGaps = Math.max(maxGaps, count);
185+
}
186+
187+
return wall.size() - maxGaps;
188+
}
189+
}
190+
```
191+
192+
```cpp
193+
class Solution {
194+
public:
195+
int leastBricks(vector<vector<int>>& wall) {
196+
unordered_map<int, int> countGap;
197+
countGap[0] = 0;
198+
199+
for (const auto& row : wall) {
200+
int total = 0;
201+
for (size_t i = 0; i < row.size() - 1; ++i) {
202+
total += row[i];
203+
countGap[total]++;
204+
}
205+
}
206+
207+
int maxGaps = 0;
208+
for (const auto& [key, value] : countGap) {
209+
maxGaps = max(maxGaps, value);
210+
}
211+
212+
return wall.size() - maxGaps;
213+
}
214+
};
215+
```
216+
217+
```javascript
218+
class Solution {
219+
/**
220+
* @param {number[][]} wall
221+
* @return {number}
222+
*/
223+
leastBricks(wall) {
224+
const countGap = new Map();
225+
countGap.set(0, 0);
226+
for (const row of wall) {
227+
let total = 0;
228+
for (let i = 0; i < row.length - 1; i++) {
229+
total += row[i];
230+
countGap.set(total, (countGap.get(total) || 0) + 1);
231+
}
232+
}
233+
return wall.length - Math.max(...countGap.values());
234+
}
235+
}
236+
```
237+
238+
::tabs-end
239+
240+
### Time & Space Complexity
241+
242+
* Time complexity: $O(N)$
243+
* Space complexity: $O(g)$
244+
245+
> Where $N$ is the total number of bricks in the wall and $g$ is the total number of gaps in all the rows.

0 commit comments

Comments
 (0)