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

Commit de80de6

Browse files
committed
add algoexpert solutions
1 parent 446ee31 commit de80de6

File tree

11 files changed

+1323
-0
lines changed

11 files changed

+1323
-0
lines changed

‎algoexpert/03-jul.cpp‎

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long int ll;
6+
typedef pair<int, int> ii;
7+
typedef vector<int> vi;
8+
typedef vector<string> vs;
9+
10+
#define len(container) int((container).size())
11+
#define all(c) (c).begin(), (c).end()
12+
13+
template <typename ITER>
14+
void show(ITER begin, ITER end) {
15+
for (int i = 1; begin != end; i++) {
16+
printf("%d ", *(begin++));
17+
if (i % 20 == 0 or begin == end) printf("\n");
18+
}
19+
};
20+
21+
inline bool isValid(int x, int y, int R, int C) {
22+
return x >= 0 && x < R && y >= 0 && y < C;
23+
}
24+
25+
struct TreeNode {
26+
int val;
27+
TreeNode* left;
28+
TreeNode* right;
29+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
30+
};
31+
32+
int maxSubsetSumNoAdjacent(vector<int> array) {
33+
const int N = array.size();
34+
if (N == 0) {
35+
return 0;
36+
}
37+
if (N == 1) {
38+
return array[0];
39+
}
40+
41+
int tmp;
42+
int prev = 0, curr = array[0];
43+
44+
for (int i = 1; i < N; ++i) {
45+
tmp = prev;
46+
prev = curr;
47+
curr = max(tmp + array[i], curr);
48+
}
49+
50+
return curr;
51+
}
52+
53+
int numberOfWaysToMakeChange(int n, vector<int> denoms) {
54+
if (n == 0) {
55+
return 1;
56+
}
57+
const int D = denoms.size();
58+
vector<int> ways(n + 1, 0);
59+
ways[0] = 1;
60+
for (int i = 0; i < D; ++i) {
61+
int d = denoms[i];
62+
for (int j = n; j >= 0; --j) {
63+
int k = j / d;
64+
for (int l = 1; l <= k; ++l) {
65+
ways[j] += ways[j - l * d];
66+
}
67+
}
68+
}
69+
return ways[n];
70+
}
71+
// Instead of iterating in reverse way, we can just iterate normal way with
72+
// adding the denom value. Time: O(nd) | Space: O(n)
73+
74+
int minNumberOfCoinsForChange(int n, vector<int> denoms) {
75+
vector<int> ways(n + 1, INT_MAX);
76+
ways[0] = 0;
77+
for (auto& denom : denoms) {
78+
for (int i = denom; i < n + 1; ++i) {
79+
if (ways[i - denom] != INT_MAX)
80+
ways[i] = min(ways[i], ways[i - denom] + 1);
81+
}
82+
}
83+
return ways[n] == INT_MAX ? -1 : ways[n];
84+
}
85+
86+
vector<vector<int>> maxSumIncreasingSubsequence(vector<int> array) {
87+
const int N = array.size();
88+
vector<int> parents(N, -1);
89+
vector<int> lis(N, 0); // lis = array
90+
lis[0] = array[0];
91+
for (int i = 1; i < N; ++i) {
92+
lis[i] = array[i];
93+
for (int j = 0; j < i; ++j) {
94+
if (array[j] < array[i] && lis[i] < lis[j] + array[i]) {
95+
lis[i] = lis[j] + array[i];
96+
parents[i] = j;
97+
}
98+
}
99+
}
100+
101+
auto it = max_element(all(lis));
102+
int st = it - lis.begin();
103+
vi ans;
104+
105+
while (st >= 0) {
106+
ans.push_back(array[st]);
107+
st = parents[st];
108+
}
109+
110+
reverse(all(ans));
111+
112+
return {
113+
{*it}, // Sum of sequence.
114+
ans, // Elements of the sequence.
115+
};
116+
}
117+
118+
vector<char> longestCommonSubsequence(string str1, string str2) {
119+
int l1 = str1.length(), l2 = str2.length();
120+
vector<vector<int>> dp(l1 + 1, vector<int>(l2 + 1, 0));
121+
int im = 0, jm = 0;
122+
for (int i = 1; i <= l1; ++i) {
123+
for (int j = 1; j <= l2; ++j) {
124+
if (str1[i - 1] == str2[j - 1]) {
125+
dp[i][j] = 1 + dp[i - 1][j - 1];
126+
} else
127+
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
128+
if (dp[i][j] > dp[im][jm]) {
129+
im = i;
130+
jm = j;
131+
}
132+
}
133+
}
134+
135+
if (im == 0 && jm == 0) {
136+
return {};
137+
}
138+
139+
vector<char> ans;
140+
while (im >= 1 && jm >= 1) { // OR im != 0 && jm != 0
141+
if (str1[im - 1] == str2[jm - 1]) {
142+
ans.push_back(str1[im - 1]);
143+
im--;
144+
jm--;
145+
} else if (dp[im][jm] == dp[im][jm - 1]) {
146+
jm--;
147+
} else
148+
im--;
149+
}
150+
reverse(ans.begin(), ans.end());
151+
return ans;
152+
}
153+
// Time: O(nm) | Space: O(nm)
154+
155+
int minNumberOfJumps(vector<int> array) {
156+
const int N = array.size();
157+
vector<int> jumps(N, INT_MAX);
158+
jumps[0] = 0;
159+
for (int i = 0; i < N; ++i) {
160+
for (int j = i + 1; j <= min(N - 1, i + array[i]); ++j) {
161+
jumps[j] = min(jumps[j], 1 + jumps[i]);
162+
}
163+
}
164+
return jumps[N - 1];
165+
}
166+
// Time: O(n^2) | Space: O(n)
167+
// Advance solution: O(n) using maxReach.
168+
// maxReach = array[0]
169+
// i = 1-> end: maxReach = max(maxReach, i + array[i])
170+
// step--
171+
// if (step==0) jump++; steps = maxReach - i
172+
173+
// https://www.algoexpert.io/questions/Square%20of%20Zeroes
174+
// Time: O(n^3) | Space: O(n^2)
175+
bool squareOfZeroes(vector<vector<int>> matrix) {
176+
const int N = matrix.size();
177+
vector<vector<int>> upper(N, vector<int>(N, 0));
178+
vector<vector<int>> down(N, vector<int>(N, 0));
179+
vector<vector<int>> left(N, vector<int>(N, 0));
180+
vector<vector<int>> right(N, vector<int>(N, 0));
181+
for (int i = 0; i < N; ++i) {
182+
for (int j = 1; j < N; ++j) {
183+
if (matrix[i][j] == 0) {
184+
left[i][j] = 1 + left[i][j - 1];
185+
}
186+
}
187+
188+
for (int j = N - 2; j >= 0; --j) {
189+
if (matrix[i][j] == 0) {
190+
right[i][j] = 1 + right[i][j + 1];
191+
}
192+
}
193+
}
194+
195+
for (int j = 0; j < N; ++j) {
196+
for (int i = 1; i < N; ++i) {
197+
if (matrix[i][j] == 0) {
198+
up[i][j] = 1 + up[i][j - 1];
199+
}
200+
}
201+
202+
for (int i = N - 2; i >= 0; ++i) {
203+
if (matrix[i][j] == 0) {
204+
down[i][j] = 1 + down[i][j + 1];
205+
}
206+
}
207+
}
208+
// calc by check for each coordinate
209+
// the number of up and left, then check the down and right on the diagonal.
210+
return false;
211+
}
212+
213+
int kadanesAlgorithm(vector<int> array) {
214+
int curr = 0;
215+
int maxSoFar = INT_MIN;
216+
for (int i = 0; i < array.size(); ++i) {
217+
curr = max(array[i], array[i] + curr);
218+
maxSoFar = max(maxSoFar, curr);
219+
}
220+
return maxSoFar;
221+
}
222+
223+
// https://www.algoexpert.io/questions/Topological%20Sort
224+
unordered_map<int, vi> adjList;
225+
unordered_map<int, int> vis;
226+
227+
bool dfs(int j, vi& ans) {
228+
if (vis[j] == 1) {
229+
return false;
230+
} else if (vis[j] == 2) {
231+
cout << j << endl;
232+
return true;
233+
}
234+
vis[j] = 2;
235+
for (auto neighbor : adjList[j]) {
236+
if (dfs(neighbor, ans)) {
237+
return true;
238+
}
239+
}
240+
vis[j] = 1;
241+
ans.push_back(j);
242+
243+
return false;
244+
}
245+
246+
vector<int> topologicalSort(vector<int> jobs, vector<vector<int>> deps) {
247+
vi ans;
248+
249+
for (auto& dep : deps) {
250+
adjList[dep[0]].push_back(dep[1]);
251+
}
252+
253+
for (auto& job : jobs) {
254+
if (dfs(job, ans)) {
255+
return {};
256+
}
257+
}
258+
reverse(ans.begin(), ans.end());
259+
return ans;
260+
}
261+
262+
int main(int argc, char* argv[]) {
263+
// [1]
264+
// 75, 105, 120, 75, 90, 135
265+
// [1, 2, 3, 4, 5, 6, 7, 8]
266+
// [[3, 1], [8, 1], [8, 7], [5, 7], [5, 2], [1, 4], [1, 6], [1, 2], [7, 6]]
267+
vector<int> denoms = {1, 5};
268+
cout << numberOfWaysToMakeChange(6, denoms);
269+
return 0;
270+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /