Skip to content

Commit 2981c58

Browse files
Add solutions for new topics
1 parent b486572 commit 2981c58

File tree

3 files changed

+111
-10
lines changed

3 files changed

+111
-10
lines changed

solutions/bellman-ford.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Bellman Ford Algorithm
2+
3+
## Cheapest Flights Within K Stops
4+
5+
```cpp
6+
class Solution {
7+
public:
8+
int findCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k) {
9+
vector<int> dist(n, 1e9);
10+
11+
dist[src] = 0;
12+
13+
// K relaxation
14+
15+
for(int i = 0; i <= k; i++) {
16+
auto temp = dist;
17+
18+
for(auto &e : flights) {
19+
int u = e[0], v = e[1], w = e[2];
20+
21+
dist[v] = min(dist[v], temp[u] + w);
22+
}
23+
}
24+
25+
if(dist[dst] == 1e9) {
26+
return -1;
27+
} else {
28+
return dist[dst];
29+
}
30+
}
31+
};
32+
```
33+
34+
## Network Delay Time
35+
36+
```cpp
37+
class Solution {
38+
public:
39+
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
40+
vector<int> dist(n, 1e9);
41+
42+
dist[k - 1] = 0;
43+
44+
for(int i = 0; i < n; i++) {
45+
46+
for(auto &e : times) {
47+
int u = e[0] - 1, v = e[1] - 1, w = e[2];
48+
if(dist[u] != 1e9) {
49+
dist[v] = min(dist[v], dist[u] + w);
50+
}
51+
}
52+
}
53+
54+
int answer = *max_element(dist.begin(), dist.end());
55+
56+
if(answer == 1e9) {
57+
return -1;
58+
} else {
59+
return answer;
60+
}
61+
}
62+
};
63+
```

solutions/custom-comparator.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Custom Comparator
2+
3+
## Sort Array By Parity
4+
5+
```cpp
6+
vector<int> sortArrayByParity(vector<int>& nums) {
7+
sort(nums.begin(), nums.end(), [&](int a, int b) {
8+
return (a % 2) < (b % 2);
9+
});
10+
11+
return nums;
12+
}
13+
```
14+
15+
## Sort The People
16+
17+
```cpp
18+
vector<string> sortPeople(vector<string>& names, vector<int>& heights) {
19+
int N = names.size();
20+
vector<int> idx(N, 0);
21+
iota(idx.begin(), idx.end(), 0);
22+
23+
sort(idx.begin(), idx.end(), [&](auto &i, auto &j) {
24+
return heights[i] > heights[j];
25+
});
26+
27+
vector<string> ans;
28+
29+
for (auto &a : idx) {
30+
ans.push_back(names[a]);
31+
}
32+
33+
return ans;
34+
}
35+
```

solutions/segment-tree.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
- [Sum of Even Numbers After Queries](#sum-of-even-numbers-after-queries)
2-
- [Range Sum Query Mutable](#range-sum-query-mutable)
3-
- [Longest Increasing Subsequence - II](#longest-increasing-subsequence---ii)
4-
- [Shifting Letters - II](#shifting-letters---ii)
5-
- [Falling Squares](#falling-squares)
1+
# Segment Tree
62

7-
# Sum of Even Numbers After Queries
3+
- [Segment Tree](#segment-tree)
4+
- [Sum of Even Numbers After Queries](#sum-of-even-numbers-after-queries)
5+
- [Range Sum Query Mutable](#range-sum-query-mutable)
6+
- [Longest Increasing Subsequence - II](#longest-increasing-subsequence---ii)
7+
- [Shifting Letters - II](#shifting-letters---ii)
8+
- [Falling Squares](#falling-squares)
9+
10+
## Sum of Even Numbers After Queries
811

912
```cpp
1013
struct segtree {
@@ -64,7 +67,7 @@ public:
6467
};
6568
```
6669
67-
# Range Sum Query Mutable
70+
## Range Sum Query Mutable
6871
6972
```cpp
7073
struct segtree {
@@ -134,7 +137,7 @@ private:
134137
};
135138
```
136139

137-
# Longest Increasing Subsequence - II
140+
## Longest Increasing Subsequence - II
138141

139142
```cpp
140143
struct segtree {
@@ -191,7 +194,7 @@ public:
191194
};
192195
```
193196
194-
# Shifting Letters - II
197+
## Shifting Letters - II
195198
196199
```cpp
197200
class Solution {
@@ -247,7 +250,7 @@ public:
247250
};
248251
```
249252

250-
# Falling Squares
253+
## Falling Squares
251254

252255
```cpp
253256
class Solution {

0 commit comments

Comments
 (0)