Skip to content
Closed
42 changes: 42 additions & 0 deletions solution/0700-0799/0770.Basic Calculator IV/solutions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <bits/stdc++.h>
using namespace std;
struct Poly{
map<vector<string>, 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<string> basicCalculatorIV(string expr, vector<string>& evv, vector<int>& evi){
unordered_map<string,long> mp;
for(int i=0;i<evv.size();i++) mp[evv[i]] = evi[i];
vector<string> 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<Poly()> 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<toks.size() && toks[i]=="*"){ i++; r = mul(r, parseP()); } return r; };
parseE = [&]{ Poly r=parseT(); while(i<toks.size()&&(toks[i]=="+"||toks[i]=="-")){ string op=toks[i++]; Poly p=parseT(); r = (op=="+"? add(r,p) : sub(r,p)); } return r; };
Poly res = parseE();
vector<pair<vector<string>,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<b.first; });
vector<string> 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;
}
};
59 changes: 59 additions & 0 deletions solution/1500-1599/1591.Strange Printer II/Solutions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class Solution {
public:
bool isPrintable(vector<vector<int>>& targetGrid) {
int m = targetGrid.size(), n = targetGrid[0].size();

const int MAXC = 60;
vector<bool> seen(MAXC+1,false);
vector<int> minR(MAXC+1, m), maxR(MAXC+1, -1);
vector<int> minC(MAXC+1, n), maxC(MAXC+1, -1);

for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
int c = targetGrid[i][j];
seen[c] = true;
minR[c] = min(minR[c], i);
maxR[c] = max(maxR[c], i);
minC[c] = min(minC[c], j);
maxC[c] = max(maxC[c], j);
}
}

vector<bitset<MAXC+1>> adj(MAXC+1);
vector<int> 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<int> 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;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <array>
#include <climits>
#include <queue>
#include <vector>
using namespace std;

class Solution {
public:
int secondMinimum(int n, vector<vector<int>>& edges, int time, int change) {
vector<vector<int>> adj(n + 1);
for (auto& e : edges) {
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(e[0]);
}
vector<array<int, 2>> dist(n + 1, {INT_MAX, INT_MAX});
queue<pair<int, int>> 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];
}
};
48 changes: 48 additions & 0 deletions solution/2000-2099/2097.Valid Arrangement of Pairs/Solutions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <vector>
#include <unordered_map>
#include <stack>
using namespace std;

class Solution {
public:
vector<vector<int>> validArrangement(vector<vector<int>>& pairs) {
unordered_map<int, stack<int>> graph;
unordered_map<int, int> 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<vector<int>> result;
stack<int> 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;
}
};
Loading