From 8464c7c2bac253ea153fe3caa6d351fecf2a4c13 Mon Sep 17 00:00:00 2001 From: ZAID646 Date: Thu, 8 May 2025 20:50:03 +0530 Subject: [PATCH 1/7] Added C++ solution for 0770.Basic Calculator IV --- .../0770.Basic Calculator IV/solutions.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 solution/0700-0799/0770.Basic Calculator IV/solutions.cpp diff --git a/solution/0700-0799/0770.Basic Calculator IV/solutions.cpp b/solution/0700-0799/0770.Basic Calculator IV/solutions.cpp new file mode 100644 index 0000000000000..6b8645453d6f0 --- /dev/null +++ b/solution/0700-0799/0770.Basic Calculator IV/solutions.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; +struct Poly{ + map, long> d; + Poly(long v=0){ if(v) d[{}]=v; } + Poly(const string &s){ d[{s}]=1; } +}; +Poly add(const Poly &a,const Poly &b){ Poly r=a; for(auto &p:b.d) r.d[p.first]+=p.second; for(auto it=r.d.begin();it!=r.d.end();){ if(it->second==0) r.d.erase(it++); else ++it;} return r; } +Poly sub(const Poly &a,const Poly &b){ Poly r=a; for(auto &p:b.d) r.d[p.first]-=p.second; for(auto it=r.d.begin();it!=r.d.end();){ if(it->second==0) r.d.erase(it++); else ++it;} return r; } +Poly mul(const Poly &a,const Poly &b){ Poly r; for(auto &p:a.d) for(auto &q:b.d){ auto v=p.first; v.insert(v.end(),q.first.begin(),q.first.end()); sort(v.begin(),v.end()); r.d[v]+=p.second*q.second; } for(auto it=r.d.begin();it!=r.d.end();){ if(it->second==0) r.d.erase(it++); else ++it;} return r; } +class Solution { +public: + vector basicCalculatorIV(string expr, vector& evv, vector& evi){ + unordered_map mp; + for(int i=0;i toks; + string t; + for(char c:expr){ + if(c==' '){ if(!t.empty()){ toks.push_back(t); t.clear(); }} + else if(strchr("()+-*",c)){ + if(!t.empty()){ toks.push_back(t); t.clear(); } + toks.push_back(string(1,c)); + } else t.push_back(c); + } + if(!t.empty()) toks.push_back(t); + int i=0; + function parseE, parseT, parseP; + parseP = [&]{ string s=toks[i++]; if(s=="("){ Poly r = parseE(); i++; return r;} if(isdigit(s[0])) return Poly(stol(s)); return mp.count(s)? Poly(mp[s]) : Poly(s); }; + parseT = [&]{ Poly r=parseP(); while(i,long>> v(res.d.begin(), res.d.end()); + sort(v.begin(), v.end(), [](auto &a, auto &b){ if(a.first.size()!=b.first.size()) return a.first.size()>b.first.size(); return a.first ans; + for(auto &p:v) if(p.second){ + string s = to_string(p.second); + for(auto &var:p.first) s += "*" + var; + ans.push_back(s); + } + return ans; + } +}; \ No newline at end of file From 44860479371fe724b09a3e5bdb164b70b12b590d Mon Sep 17 00:00:00 2001 From: ZAID646 Date: Thu, 8 May 2025 22:09:41 +0530 Subject: [PATCH 2/7] Added C++ solution for 2000-2099/2045.Second Minimum Time to Reach Destination --- .../Solutions.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 solution/2000-2099/2045.Second Minimum Time to Reach Destination/Solutions.cpp diff --git a/solution/2000-2099/2045.Second Minimum Time to Reach Destination/Solutions.cpp b/solution/2000-2099/2045.Second Minimum Time to Reach Destination/Solutions.cpp new file mode 100644 index 0000000000000..5451622e9f44c --- /dev/null +++ b/solution/2000-2099/2045.Second Minimum Time to Reach Destination/Solutions.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include +using namespace std; + +class Solution { +public: + int secondMinimum(int n, vector>& edges, int time, int change) { + vector> adj(n + 1); + for (auto& e : edges) { + adj[e[0]].push_back(e[1]); + adj[e[1]].push_back(e[0]); + } + vector> dist(n + 1, {INT_MAX, INT_MAX}); + queue> q; + dist[1][0] = 0; + q.emplace(1, 0); + + while (!q.empty()) { + auto [u, t] = q.front(); + q.pop(); + for (int v : adj[u]) { + int cycles = t / change; + int wait = (cycles % 2 == 1 ? change - (t % change) : 0); + int t2 = t + wait + time; + if (t2 < dist[v][0]) { + dist[v][1] = dist[v][0]; + dist[v][0] = t2; + q.emplace(v, t2); + } else if (t2 > dist[v][0] && t2 < dist[v][1]) { + dist[v][1] = t2; + q.emplace(v, t2); + } + } + } + + return dist[n][1]; + } +}; \ No newline at end of file From 966eae6efe1de0495bcff057199f4f85020dd776 Mon Sep 17 00:00:00 2001 From: ZAID646 Date: Thu, 8 May 2025 22:57:13 +0530 Subject: [PATCH 3/7] Added C++ solution for 3400-3499/3435. Frequencies of Shortest Supersequences --- .../Solutions.cpp | 248 ++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 solution/3400-3499/3435.Frequencies of Shortest Supersequences/Solutions.cpp diff --git a/solution/3400-3499/3435.Frequencies of Shortest Supersequences/Solutions.cpp b/solution/3400-3499/3435.Frequencies of Shortest Supersequences/Solutions.cpp new file mode 100644 index 0000000000000..367da39f60a8c --- /dev/null +++ b/solution/3400-3499/3435.Frequencies of Shortest Supersequences/Solutions.cpp @@ -0,0 +1,248 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +class Solution +{ +public: + vector> supersequences(vector &words) + { + const int ALPHA = 26; + bool used[ALPHA] = {}; + for (auto &w : words) + for (char c : w) + used[c - 'a'] = true; + + vector charMap(ALPHA, -1); + vector chars; + int charCount = 0; + for (int c = 0; c < ALPHA; c++) + { + if (used[c]) + { + charMap[c] = charCount++; + chars.push_back('a' + c); + } + } + + vector> graph(charCount, vector(charCount)); + vector selfLoop(charCount); + for (auto &w : words) + { + int u = charMap[w[0] - 'a'], v = charMap[w[1] - 'a']; + if (u == v) + selfLoop[u] = true; + else + graph[u][v] = true; + } + + vector disc(charCount, -1), low(charCount), comp(charCount, -1); + vector inStack(charCount); + stack stk; + int time = 0, sccTotal = 0; + + function tarjan = [&](int u) + { + disc[u] = low[u] = time++; + stk.push(u); + inStack[u] = true; + + for (int v = 0; v < charCount; v++) + { + if (!graph[u][v]) + continue; + if (disc[v] == -1) + { + tarjan(v); + low[u] = min(low[u], low[v]); + } + else if (inStack[v]) + { + low[u] = min(low[u], disc[v]); + } + } + + if (low[u] == disc[u]) + { + while (true) + { + int v = stk.top(); + stk.pop(); + inStack[v] = false; + comp[v] = sccTotal; + if (v == u) + break; + } + sccTotal++; + } + }; + + for (int i = 0; i < charCount; i++) + if (disc[i] == -1) + tarjan(i); + + vector> sccGroups(sccTotal); + for (int i = 0; i < charCount; i++) + sccGroups[comp[i]].push_back(i); + + vector> sccGraph(sccTotal); + vector inDegree(sccTotal); + for (int u = 0; u < charCount; u++) + { + for (int v = 0; v < charCount; v++) + { + if (graph[u][v] && comp[u] != comp[v]) + { + sccGraph[comp[u]].push_back(comp[v]); + inDegree[comp[v]]++; + } + } + } + + queue q; + vector topoOrder; + for (int i = 0; i < sccTotal; i++) + if (inDegree[i] == 0) + q.push(i); + + while (!q.empty()) + { + int u = q.front(); + q.pop(); + topoOrder.push_back(u); + for (int v : sccGraph[u]) + { + if (--inDegree[v] == 0) + q.push(v); + } + } + + auto isAcyclic = [](const vector> &g, int mask, int n) + { + vector removed(n); + for (int i = 0; i < n; i++) + if (mask & (1 << i)) + removed[i] = true; + + vector deg(n); + for (int u = 0; u < n; u++) + { + if (removed[u]) + continue; + for (int v = 0; v < n; v++) + { + if (!removed[v] && g[u][v]) + deg[v]++; + } + } + + queue q; + int cnt = 0; + int total = n - __builtin_popcount(mask); + for (int i = 0; i < n; i++) + if (!removed[i] && deg[i] == 0) + q.push(i); + + while (!q.empty()) + { + int u = q.front(); + q.pop(); + cnt++; + for (int v = 0; v < n; v++) + { + if (!removed[v] && g[u][v] && --deg[v] == 0) + q.push(v); + } + } + return cnt == total; + }; + + auto findMinFVS = [&](const vector> &g, int n) + { + set> patterns; + for (int sz = 0; sz <= n; sz++) + { + bool found = false; + for (int mask = 0; mask < (1 << n); mask++) + { + if (__builtin_popcount(mask) != sz) + continue; + if (isAcyclic(g, mask, n)) + { + vector freq(n, 1); + for (int i = 0; i < n; i++) + if (mask & (1 << i)) + freq[i] = 2; + patterns.insert(freq); + found = true; + } + } + if (found) + break; + } + return vector>(patterns.begin(), patterns.end()); + }; + + vector>> sccPatterns(sccTotal); + for (int i = 0; i < sccTotal; i++) + { + auto &group = sccGroups[i]; + if (group.size() == 1) + { + sccPatterns[i] = selfLoop[group[0]] ? vector>{{2}} : vector>{{1}}; + continue; + } + + vector> subgraph(group.size(), vector(group.size())); + vector localToGlobal(group.size()); + for (int j = 0; j < group.size(); j++) + { + localToGlobal[j] = group[j]; + if (selfLoop[group[j]]) + subgraph[j][j] = true; + for (int k = 0; k < group.size(); k++) + { + if (graph[group[j]][group[k]]) + subgraph[j][k] = true; + } + } + sccPatterns[i] = findMinFVS(subgraph, group.size()); + } + + vector> result = {{}}; + for (int scc : topoOrder) + { + vector> newResult; + for (auto &freq : result) + { + for (auto &pattern : sccPatterns[scc]) + { + vector newFreq = freq; + newFreq.resize(charCount); + for (int i = 0; i < sccGroups[scc].size(); i++) + { + int globalIdx = sccGroups[scc][i]; + newFreq[globalIdx] = pattern[i]; + } + newResult.push_back(newFreq); + } + } + result = move(newResult); + } + + set> uniqueFreqs; + for (auto &freq : result) + { + vector finalFreq(ALPHA); + for (int i = 0; i < charCount; i++) + finalFreq[chars[i] - 'a'] = freq[i]; + uniqueFreqs.insert(finalFreq); + } + + return vector>(uniqueFreqs.begin(), uniqueFreqs.end()); + } +}; \ No newline at end of file From 1da1fb7fa5d4b5047320fb12df0955cd5349e223 Mon Sep 17 00:00:00 2001 From: ZAID646 Date: Fri, 9 May 2025 18:10:12 +0530 Subject: [PATCH 4/7] Added c++ solution for 2000-2099 / 2097. Valid Arrangement of Pair --- .../Solutions.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 solution/2000-2099/2097.Valid Arrangement of Pairs/Solutions.cpp diff --git a/solution/2000-2099/2097.Valid Arrangement of Pairs/Solutions.cpp b/solution/2000-2099/2097.Valid Arrangement of Pairs/Solutions.cpp new file mode 100644 index 0000000000000..861e626725903 --- /dev/null +++ b/solution/2000-2099/2097.Valid Arrangement of Pairs/Solutions.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +using namespace std; + +class Solution { +public: + vector> validArrangement(vector>& pairs) { + unordered_map> graph; + unordered_map out_degree; + + for (auto& p : pairs) { + graph[p[0]].push(p[1]); + out_degree[p[0]]++; + out_degree[p[1]]--; + } + + int start = pairs[0][0]; + for (auto& [node, degree] : out_degree) { + if (degree > 0) { + start = node; + break; + } + } + + vector> result; + stack path; + path.push(start); + + while (!path.empty()) { + int current = path.top(); + if (!graph[current].empty()) { + path.push(graph[current].top()); + graph[current].pop(); + } else { + if (path.size() > 1) { + int end = path.top(); path.pop(); + result.push_back({path.top(), end}); + } else { + path.pop(); + } + } + } + + reverse(result.begin(), result.end()); + return result; + } +}; \ No newline at end of file From f9e372059964df1a342dc52233442f17a75eac0e Mon Sep 17 00:00:00 2001 From: ZAID646 Date: Fri, 9 May 2025 18:30:24 +0530 Subject: [PATCH 5/7] Added c++ solution for 1500-1599 / 1591.Strange Printer II --- .../1591.Strange Printer II/Solutions.cpp | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 solution/1500-1599/1591.Strange Printer II/Solutions.cpp diff --git a/solution/1500-1599/1591.Strange Printer II/Solutions.cpp b/solution/1500-1599/1591.Strange Printer II/Solutions.cpp new file mode 100644 index 0000000000000..1b1668c945b8f --- /dev/null +++ b/solution/1500-1599/1591.Strange Printer II/Solutions.cpp @@ -0,0 +1,59 @@ +class Solution { + public: + bool isPrintable(vector>& targetGrid) { + int m = targetGrid.size(), n = targetGrid[0].size(); + + const int MAXC = 60; + vector seen(MAXC+1,false); + vector minR(MAXC+1, m), maxR(MAXC+1, -1); + vector minC(MAXC+1, n), maxC(MAXC+1, -1); + + for(int i=0;i> adj(MAXC+1); + vector indeg(MAXC+1,0); + for(int c=1;c<=MAXC;c++){ + if(!seen[c]) continue; + for(int i=minR[c]; i<=maxR[c]; i++){ + for(int j=minC[c]; j<=maxC[c]; j++){ + int d = targetGrid[i][j]; + if(d!=c && !adj[c].test(d)){ + adj[c].set(d); + indeg[d]++; + } + } + } + } + + // Kahn's algorithm on at most 60 nodes + queue q; + int totalColors = 0; + for(int c=1;c<=MAXC;c++){ + if(!seen[c]) continue; + totalColors++; + if(indeg[c]==0) q.push(c); + } + + int seenCount = 0; + while(!q.empty()){ + int u = q.front(); q.pop(); + seenCount++; + for(int v=1;v<=MAXC;v++){ + if(adj[u].test(v) && --indeg[v]==0){ + q.push(v); + } + } + } + + return seenCount == totalColors; + } + }; \ No newline at end of file From dbe8616cf889cecf4939aff3d449f75621e5cd8e Mon Sep 17 00:00:00 2001 From: ZAID646 Date: Fri, 9 May 2025 18:48:16 +0530 Subject: [PATCH 6/7] Added c++ code for 3500-3599-/-3530.-Maximum-Profit-from-Valid-Topological-Order-in-DAG --- .../Solutions.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 solution/3500-3599/3530.Maximum Profit from Valid Topological Order in DAG/Solutions.cpp diff --git a/solution/3500-3599/3530.Maximum Profit from Valid Topological Order in DAG/Solutions.cpp b/solution/3500-3599/3530.Maximum Profit from Valid Topological Order in DAG/Solutions.cpp new file mode 100644 index 0000000000000..b935c3bf514dd --- /dev/null +++ b/solution/3500-3599/3530.Maximum Profit from Valid Topological Order in DAG/Solutions.cpp @@ -0,0 +1,29 @@ +class Solution { + public: + int maxProfit(int n, vector>& edges, vector& score) { + vector prereq(n, 0); + for (auto &e : edges) { + int u = e[0], v = e[1]; + prereq[v] |= (1 << u); + } + + int N = 1 << n; + vector dp(N, INT_MIN); + dp[0] = 0; + + for (int mask = 0; mask < N; ++mask) { + if (dp[mask] < 0) continue; + int pos = __builtin_popcount(mask) + 1; + int free = (~mask) & (N - 1); + for (int i = 0; i < n; ++i) { + if ((free & (1 << i)) + && (mask & prereq[i]) == prereq[i]) { + int m2 = mask | (1 << i); + dp[m2] = max(dp[m2], dp[mask] + score[i] * pos); + } + } + } + + return dp[N - 1]; + } + }; \ No newline at end of file From d3507ce58f8ba8302b71cd6328b1330e202a5712 Mon Sep 17 00:00:00 2001 From: ZAID646 Date: Fri, 9 May 2025 19:02:26 +0530 Subject: [PATCH 7/7] Added c++ solution for 3500-3599 / 3534. Path Existence Queries in a Graph II --- .../Solutions.cpp | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 solution/3500-3599/3534.Path Existence Queries in a Graph II/Solutions.cpp diff --git a/solution/3500-3599/3534.Path Existence Queries in a Graph II/Solutions.cpp b/solution/3500-3599/3534.Path Existence Queries in a Graph II/Solutions.cpp new file mode 100644 index 0000000000000..d2ea944ae48f2 --- /dev/null +++ b/solution/3500-3599/3534.Path Existence Queries in a Graph II/Solutions.cpp @@ -0,0 +1,58 @@ +class Solution { + public: + vector pathExistenceQueries(int n, + vector& nums, + int maxDiff, + vector>& queries) { + vector> A(n); + for(int i=0;i pos(n); + for(int i=0;i R(n); + int r = 0; + for(int l=0; l> jump(LOG, vector(n)); + for(int i=0;iint { + if(p>q) return INT_MAX/2; + if(p==q) return 0; + int hops = 0; + int cur = p; + for(int k=LOG-1;k>=0;k--){ + int nxt = jump[k][cur]; + if(nxt < q) { + hops += (1< ans; + ans.reserve(queries.size()); + for(auto &qr: queries){ + int u=qr[0], v=qr[1]; + int pu=pos[u], pv=pos[v]; + if(pu>pv) swap(pu,pv); + int h = minHops(pu,pv); + ans.push_back(h >= INT_MAX/2 ? -1 : h); + } + return ans; + } + }; \ No newline at end of file