new problems

This commit is contained in:
2026-09-20 23:31:20 +01:00
parent afdab7d4bd
commit 8e44324146
27 changed files with 581 additions and 0 deletions
@@ -0,0 +1 @@
https://leetcode.com/problems/largest-rectangle-in-histogram/description/?envType=problem-list-v2&envId=22fmu8yh
@@ -0,0 +1,106 @@
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
// ----- TYPES -----
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define FASTIO cin.tie(0)->sync_with_stdio(0)
#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str())
#define NL '\n'
#define CM ", "
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int n; cin >> n
#define takeII(n,m) int n, m; cin >> n >> m
#define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k
#define takeL(n) ll n; cin >> n
#define takeS(s) string s; cin >> s
#define takeV(v,n) vi v(n); for (auto &x : v) cin >> x
#define takeVL(v,n) vector<ll> v(n); for (auto &x : v) cin >> x
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- PAIRS -----
#define fi first
#define se second
// ----- OUTPUT -----
#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define DBG2(a,b) cerr << #a "=" << (a) << CM << #b "=" << (b) << endl
#define printA(a,L,R) cerr << '['; FE(i,L,R) cerr << a[i] << (i==R?"]\n":" ")
#define printV(a) printA(a,0,SZ(a)-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
#define SZ(c) ((int)(c).size())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(ALL(c), greater<>())
#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end())
#define pb push_back
int main() {
STDIO("histogram");
// [1,2,3,4]
string s; getline(cin, s);
for (char &c : s) if (!isdigit(c) && c != '-') c = ' ';
stringstream ss(s); vector<int> h; int x;
while (ss >> x) h.push_back(x);
int n = h.size();
stack<pair<int,int>> stk;
vector<int> left(n), right(n);
F(i,0,n) {
int l = i;
while (!stk.empty() && stk.top().first > h[i]) {
l = stk.top().second;
stk.pop();
}
if (l != i) l = left[l];
left[i] = l;
stk.push({h[i], i});
}
FDE(i,n-1,0) {
int r = i;
while (!stk.empty() && stk.top().first > h[i]) {
r = stk.top().second;
stk.pop();
}
if (r != i) r = right[r];
right[i] = r;
stk.push({h[i], i});
}
int res = 0;
F(i,0,n) {
int width = (right[i] - left[i] + 1);
int new_res = width * h[i];
REMAX(res, new_res);
}
cout << res;
}
@@ -0,0 +1 @@
[2,4]
@@ -0,0 +1 @@
[2,1,5,6,2,3]
+1
View File
@@ -0,0 +1 @@
https://leetcode.com/problems/longest-common-subsequence/?envType=problem-list-v2&envId=oizxjoit
@@ -0,0 +1,89 @@
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
// ----- TYPES -----
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define FASTIO cin.tie(0)->sync_with_stdio(0)
#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str())
#define NL '\n'
#define CM ", "
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int n; cin >> n
#define takeII(n,m) int n, m; cin >> n >> m
#define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k
#define takeL(n) ll n; cin >> n
#define takeS(s) string s; cin >> s
#define takeV(v,n) vi v(n); for (auto &x : v) cin >> x
#define takeVL(v,n) vector<ll> v(n); for (auto &x : v) cin >> x
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- PAIRS -----
#define fi first
#define se second
// ----- OUTPUT -----
#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define DBG2(a,b) cerr << #a "=" << (a) << CM << #b "=" << (b) << endl
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,SZ(a)-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
#define SZ(c) ((int)(c).size())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(ALL(c), greater<>())
#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end())
#define pb push_back
int main() {
STDIO("longest-common-subsequence");
takeS(s1);
takeS(s2);
int n = s1.size(), m = s2.size();
vector<vector<int>> dp(m, vector<int>(n));
dp[0][0] = s1[0] == s2[0] ? 1 : 0;
F(i,1,n) {
dp[0][i] = max(dp[0][i-1], s1[i] == s2[0] ? 1 : 0);
}
F(j,1,m) {
dp[j][0] = max(dp[j-1][0], s1[0] == s2[j] ? 1 : 0);
}
F(i,1,n) {
F(j,1,m) {
if (s1[i] == s2[j]) {
dp[j][i] = dp[j-1][i-1] + 1;
} else {
dp[j][i] = max(dp[j-1][i], dp[j][i-1]);
}
}
}
cout << dp[m-1][n-1];
}
@@ -0,0 +1,2 @@
abcde
ace
@@ -0,0 +1,2 @@
abc
abc
@@ -0,0 +1,2 @@
abc
def
+1
View File
@@ -0,0 +1 @@
https://leetcode.com/problems/lru-cache/?envType=problem-list-v2&envId=22fmu8yh
View File
+85
View File
@@ -0,0 +1,85 @@
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <unordered_map>
#include <vector>
using namespace std;
// ----- TYPES -----
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define FASTIO cin.tie(0)->sync_with_stdio(0)
#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str())
#define NL '\n'
#define CM ", "
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int n; cin >> n
#define takeII(n,m) int n, m; cin >> n >> m
#define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k
#define takeL(n) ll n; cin >> n
#define takeS(s) string s; cin >> s
#define takeV(v,n) vi v(n); for (auto &x : v) cin >> x
#define takeVL(v,n) vector<ll> v(n); for (auto &x : v) cin >> x
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- PAIRS -----
#define fi first
#define se second
// ----- OUTPUT -----
#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define DBG2(a,b) cerr << #a "=" << (a) << CM << #b "=" << (b) << endl
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,SZ(a)-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
#define SZ(c) ((int)(c).size())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(ALL(c), greater<>())
#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end())
#define pb push_back
// struct LRU_Node {
// int key;
// int val;
// LRU_Node* prev;
// LRU_Node* next;
// };
//
// class LRU_Cache {
// public:
// LRU_Node* tail;
// LRU_Node* head;
// }
int main() {
FASTIO;
list<int> lst;
unordered_map<int, list<int>::iterator> mp;
lst.push_back(3);
mp[2] = lst.end()--;
auto it = mp.find(2);
cout << *it->second << NL;
}
+1
View File
@@ -0,0 +1 @@
https://leetcode.com/problems/minimum-window-substring/?envType=problem-list-v2&envId=oizxjoit
+163
View File
@@ -0,0 +1,163 @@
#include <algorithm>
#include <queue>
#include <sstream>
#include <cstdio>
#include <iostream>
#include <stack>
#include <unordered_map>
#include <vector>
using namespace std;
// ----- TYPES -----
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define FASTIO cin.tie(0)->sync_with_stdio(0)
#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str())
#define NL '\n'
#define CM ", "
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int n; cin >> n
#define takeII(n,m) int n, m; cin >> n >> m
#define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k
#define takeL(n) ll n; cin >> n
#define takeS(s) string s; cin >> s
#define takeV(v,n) vi v(n); for (auto &x : v) cin >> x
#define takeVL(v,n) vector<ll> v(n); for (auto &x : v) cin >> x
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- PAIRS -----
#define fi first
#define se second
// ----- OUTPUT -----
#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define DBG2(a,b) cerr << #a "=" << (a) << CM << #b "=" << (b) << endl
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,SZ(a)-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
#define SZ(c) ((int)(c).size())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(ALL(c), greater<>())
#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end())
#define pb push_back
struct condoms {
int x;
};
int main() {
STDIO("minimum-window-substring");
takeS(s);
takeS(t);
// If t has more chars than s, s cannot possibly have all chars of t
if (t.size() > s.size() || t.size() == 0) {
cout << "" << NL;
return 0;
}
queue<int> tst;
tst.push(2);
condoms* condom = new condoms;
stack<condoms*> stk({condom});
tst.front();
stk.pop();
stringstream ss;
// winTCount tells us how many chars of t we have in our window
int winTCount = 0;
// Use tFreq to count frequency of chars in t
// winFreq is frequency of (important) chars in window
unordered_map<char, int> tFreq, winFreq;
for (const char& x : t) tFreq[x]++;
// Find l, the first s[l] that occurs in t
int l = 0;
for (; l < s.size(); l++) {
if (tFreq.count(s[l])) {
winFreq[s[l]]++;
winTCount++;
break;
}
}
// Check if OOB
if (l == s.size()) {
cout << "" << NL;
return 0;
}
// Find r, the substr s[l...r] that satisfies min window criteria
int r = l;
if (t.size() > 1) {
for (r=l+1; r < s.size(); r++) {
if (tFreq.count(s[r]) && winFreq[s[r]] < tFreq[s[r]]) {
winFreq[s[r]]++;
winTCount++;
}
if (winTCount == t.size()) break;
}
}
// Check if OOB
if (r == s.size()) {
if (winTCount == t.size()) {
cout << s;
} else {
cout << "" << NL;
}
return 0;
}
int best_len = r-l+1, best_l = l, best_r = r;
DBG(s.substr(best_l, best_r-best_l+1));
// Keep moving window until invalid
while (r < s.size()) {
// Remove s[l], we will need to increase r until we find s[l] again
char desire = s[l];
// Move l to next char occuring in t
l++;
while (l < s.size() && !tFreq.count(s[l])) l++;
// Move r until we find 'desire' again
r++;
while (r < s.size() && s[r] != desire) r++;
if (r >= s.size() || l >= s.size()) break;
DBG(desire);
cerr << s.substr(l, r-l+1) << NL;
int len = r - l + 1;
if (len <= best_len) {
best_len = len;
best_l = l;
best_r = r;
}
}
DBG(s);
DBG(t);
cout << s.substr(best_l, best_r-best_l+1) << NL;
return 0;
}
@@ -0,0 +1,2 @@
ADOBECODEBANC
AABC
@@ -0,0 +1,2 @@
a
a
@@ -0,0 +1,2 @@
a
aa
@@ -0,0 +1,2 @@
ADOBECODEBANC
ABC
+1
View File
@@ -0,0 +1 @@
https://leetcode.com/problems/number-of-atoms/description/\?envType\=problem-list-v2\&envId\=dfh05knr
+114
View File
@@ -0,0 +1,114 @@
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <sstream>
#include <stack>
#include <map>
#include <vector>
using namespace std;
// ----- TYPES -----
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
// ----- FILE IO -----
#define STDIN(filename) freopen(filename, "r", stdin)
#define STDOUT(filename) freopen(filename, "w", stdout)
#define FASTIO cin.tie(0)->sync_with_stdio(0)
#define STDIO(filename) FASTIO; string filenamee(filename); STDIN((filenamee + ".in").c_str()); STDOUT((filenamee + ".out").c_str())
#define NL '\n'
#define CM ", "
// ----- FOR LOOPS -----
#define F(i,L,R) for (int i = L; i < R; i++)
#define FE(i,L,R) for (int i = L; i <= R; i++)
#define FD(i,L,R) for (int i = L; i > R; i--)
#define FDE(i,L,R) for (int i = L; i >= R; i--)
// ----- WEZ -----
#define takeI(n) int n; cin >> n
#define takeII(n,m) int n, m; cin >> n >> m
#define takeIII(n,m,k) int n, m, k; cin >> n >> m >> k
#define takeL(n) ll n; cin >> n
#define takeS(s) string s; cin >> s
#define takeV(v,n) vi v(n); for (auto &x : v) cin >> x
#define takeVL(v,n) vector<ll> v(n); for (auto &x : v) cin >> x
#define whileZ takeI(T); while(T--)
//
// ----- MAX MIN -----
#define REMAX(a,b) (a)=max((a),(b))
#define REMIN(a,b) (a)=min((a),(b))
// ----- PAIRS -----
#define fi first
#define se second
// ----- OUTPUT -----
#define YESNO(b) cout << ((b) ? "YES" : "NO") << NL
// ----- DEBUG -----
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl
#define DBG2(a,b) cerr << #a "=" << (a) << CM << #b "=" << (b) << endl
#define printA(a,L,R) FE(i,L,R) cerr << a[i] << (i==R?'\n':' ')
#define printV(a) printA(a,0,SZ(a)-1)
// ----- STL DS
#define ALL(c) (c).begin(),(c).end()
#define SZ(c) ((int)(c).size())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(ALL(c), greater<>())
#define UNIQ(c) SORT(c), (c).erase(unique(ALL(c)), (c).end())
#define pb push_back
string print_uomap(const map<string, int>& mp) {
stringstream ss;
for (const auto& [key, val] : mp) {
ss << key;
if (val > 1) ss << val;
}
return ss.str();
}
int main() {
STDIO("number-of-atoms");
string formula;
cin >> formula;
stringstream ss(formula);
stack<map<string, int>> stk;
stk.push({});
char ch;
while (ss >> ch) {
if (ch == '(') {
stk.push({});
} else if (ch == ')') {
int mult = 1;
if (isdigit(ss.peek())) ss >> mult;
auto old = stk.top(); stk.pop();
auto& nw = stk.top();
for (auto& [key, val] : old) {
nw[key] += val * mult;
}
}
else /** isElement */ {
string elem;
int mult = 1;
elem += ch;
while (islower(ss.peek())) {
ss >> ch;
elem += ch;
}
if (isdigit(ss.peek())) {
ss >> mult;
}
stk.top()[elem] += mult;
}
}
print_uomap(stk.top());
return 0;
}
+1
View File
@@ -0,0 +1 @@
H2O
+1
View File
@@ -0,0 +1 @@
Mg(OH)2
+1
View File
@@ -0,0 +1 @@
K4(ON(SO3)2)2