From 8464c7c2bac253ea153fe3caa6d351fecf2a4c13 Mon Sep 17 00:00:00 2001 From: ZAID646 Date: Thu, 8 May 2025 20:50:03 +0530 Subject: [PATCH 1/2] 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/2] 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