Skip to content

Commit 414f466

Browse files
committed
.
1 parent 38b9513 commit 414f466

File tree

56 files changed

+268
-17
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+268
-17
lines changed
Binary file not shown.

0301-0400/372. Super Pow.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
fast exponentiation. Let M=1337, b>>M, we can shrink b to O(M) by Euler's theorem.
1+
fast exponentiation. Let M=1337, if b is much larger than M, we can shrink b to O(M) by Euler's theorem.
22

0501-0600/548. Split Array with Equal Sum 8ms.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ class Solution {
127127
for (int i=1;i<n;++i)s[i]+=s[i-1];
128128
for (int i=n-2;i;--i)M[mp(s[n-1]-s[i],a[i])]=i;
129129
auto minmax=minmax_element(a.begin(),a.end());
130-
int minv=*minmax.first,maxv=*minmax.second;
131-
int mins=(s[n-1]-3*maxv)/4,maxs=(s[n-1]-3*minv)/4;
130+
int minv=*minmax.first,maxv=*minmax.second;
131+
int mins=(s[n-1]-3*maxv)/4,maxs=(s[n-1]-3*minv)/4;
132132
for (int i=1;i<n-5;++i){
133133
int s1=s[i-1];
134134
if (s1<mins||s1>maxs)continue;

0701-0800/704. Binary Search.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ int interpolation_search(int a[],int n,int x){
1313
}
1414
class Solution {
1515
public:
16-
int search(vector<int>& nums, int t) {
17-
int n=nums.size(),p=interpolation_search(&nums[0]-1,n,t);
18-
return p<=n&&nums[p-1]==t?p-1:-1;
19-
}
16+
int search(vector<int>& nums, int t) {
17+
int n=nums.size(),p=interpolation_search(&nums[0]-1,n,t);
18+
return p<=n&&nums[p-1]==t?p-1:-1;
19+
}
2020
};
2121

0701-0800/711. Number of Distinct Islands II 40ms.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ size_t get_hash(vector<int> &v){
1010
for (int j=0;j<u.size();++j)res=((long long)res*M+u[j])%P;
1111
}*/
1212
sort(v.begin(),v.end());
13-
for (int j=0;j<v.size();++j)res=((long long)res*M+v[j])%P;
13+
for (int j=0;j<v.size();++j)res=((long long)res*M+v[j])%P;
1414
return ((long long)res*M+n)%P;
1515
}
1616
struct point{

0701-0800/729. My Calendar I.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
predecessor search. O(n*pred(n)).
2+

0701-0800/731. My Calendar II.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
balanced tree. O(n log n).
2+

0701-0800/733. Flood Fill.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(nm).
2+

0701-0800/735. Asteroid Collision.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
stack. O(n).
2+
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
stack. O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(log n).
2+

0701-0800/740. Delete and Earn.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
partition the numbers into consecutive intervals, then run DP. O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
binary search. O(log n) after reading the input.
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DP. O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(L).
2+

0701-0800/754. Reach a Number.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
First repeatedly go towards the target until we go pass it. Then we need at most 2 more moves, and flip at most 2 previous moves, depending whether the difference is even or odd. O(1).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const int CH=255,N=5005,W=(1<<16)-1;
2+
int _a[26][N],*a[CH],a1[CH],b[N],l[N];
3+
class Solution {
4+
public:
5+
int numMatchingSubseq(const string &s, vector<string>& w) {
6+
int n=s.size(),m=w.size(),ans=0;
7+
for (int i=0;i<26;++i)a['a'+i]=_a[i],a1['a'+i]=0;
8+
for (int i=0;i<m;++i){
9+
l[i]=w[i].size();
10+
char c=w[i][0];
11+
a[c][a1[c]++]=i<<16;
12+
}
13+
for (int i=0;i<n;++i){
14+
int k=a1[s[i]];
15+
memcpy(b,a[s[i]],sizeof(int)*k);
16+
a1[s[i]]=0;
17+
for (int j=0;j<k;++j){
18+
int x=b[j]>>16,y=(b[j]&W)+1;
19+
if (y==l[x])++ans;
20+
else {
21+
char c=w[x][y];
22+
a[c][a1[c]++]=b[j]+1;
23+
}
24+
}
25+
}
26+
return ans;
27+
}
28+
};
29+
30+
//IO
31+
int _IO=[](){
32+
ios::sync_with_stdio(0);
33+
cin.tie(0); //cout.tie(0);
34+
return 0;
35+
}();
36+
37+
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(1).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dfs. O(n+m+output).
2+
57.2 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
count the number of x-y pairs and y-x pairs. O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
find connected component. O(nm).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
trie. O(L).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Let n denote the number of steps and let m denote the array length.
2+
DP. O(n*min(n,m)).
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const int N=50005,L=16,M=12;
2+
int ch[N],sta[N],d[N],h[N],sz[N],nxt[N],mark[N],stk[N],chain[N*2],c1[N],id[N],msk[N],
3+
g[N],kth[1<<M][M],_fa[N/M][L],*fa[N],*p,D,len,fa1,id1,_id1;
4+
void pre(){
5+
for (int i=1;i<(1<<M);++i){
6+
kth[i][0]=__builtin_ctz(i);
7+
for (int j=1;j<M;++j)kth[i][j]=kth[i-(i&-i)][j-1];
8+
}
9+
}
10+
class TreeAncestor {
11+
public:
12+
void dfs1(int x){
13+
id[id1++]=x; g[x]=_id1;
14+
for (int i=sta[x];i<sta[x+1];++i){
15+
int y=ch[i]; msk[y]=msk[x]|(1<<id1-_id1); dfs1(y);
16+
}
17+
}
18+
void dfs(int x){
19+
d[x]=D; stk[D++]=x; sz[x]=1; h[x]=0; nxt[x]=mark[x]=-1; bool flag=1;
20+
for (int i=sta[x];i<sta[x+1];++i){
21+
int y=ch[i]; dfs(y);
22+
sz[x]+=sz[y];
23+
if (sz[y]>M)flag=0,mark[x]=mark[y];
24+
if (h[y]>h[x])h[x]=h[y],nxt[x]=y;
25+
}
26+
--D; ++h[x];
27+
if (sz[x]<=M)flag=0;
28+
if (flag){
29+
mark[x]=x; fa[x]=_fa[fa1++];
30+
for (int i=0;i<L&&(1<<i)<=D;++i)fa[x][i]=stk[D-(1<<i)];
31+
}
32+
for (int i=sta[x];i<sta[x+1];++i){
33+
int y=ch[i];
34+
if (mark[x]!=-1&&sz[y]<=M)_id1=id1,msk[y]=1,dfs1(y);
35+
if (y!=nxt[x]){
36+
len+=h[y];
37+
for (int z=y;z!=-1;z=nxt[z])chain[c1[z]=--len]=z;
38+
len+=h[y];
39+
for (int end=len+h[y],z=x;z!=-1&&len<end;z=p[z])chain[len++]=z;
40+
}
41+
}
42+
}
43+
TreeAncestor(int n, vector<int>& _p) {
44+
p=&_p[0];
45+
if (!kth[2][1])pre();
46+
memset(sta,0,sizeof(int)*n);
47+
for (int i=1;i<n;++i)++sta[p[i]];
48+
for (int i=0,s=0,t;i<n;++i)t=sta[i],sta[i]=s,s+=t;
49+
for (int i=1;i<n;++i)ch[sta[p[i]]++]=i;
50+
for (int i=n;i;--i)sta[i]=sta[i-1]; sta[0]=0;
51+
D=len=fa1=id1=0; dfs(0); len+=h[0];
52+
for (int z=0;z!=-1;z=nxt[z])chain[c1[z]=--len]=z; len+=h[0];
53+
if (sz[0]<=M)_id1=id1,msk[0]=1,dfs1(0);
54+
}
55+
int getKthAncestor(int x, int k) {
56+
k=d[x]-k; if (k<0)return -1;
57+
if (mark[x]==-1){
58+
int y=id[g[x]];
59+
if (k<d[y])x=p[y];
60+
else return id[g[x]+kth[msk[x]][k-d[y]]];
61+
}
62+
x=mark[x];
63+
if (d[x]==k)return x;
64+
int l=31-__builtin_clz(d[x]-k); x=fa[x][l];
65+
return chain[c1[x]+d[x]-k];
66+
}
67+
};
68+
69+
//IO
70+
int _IO=[](){
71+
ios::sync_with_stdio(0);
72+
cin.tie(0); //cout.tie(0);
73+
return 0;
74+
}();
75+
76+
Binary file not shown.

1401-1500/1483. Kth Ancestor of a Tree Node.txt

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(nm).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
monotone queue. O(n).
2+

1601-1700/1675. Minimize Deviation in Array 196ms.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ class Solution {
66
int n=a.size(),ma=0,mi=inf;
77
int vmin[W+1],vmax[W+1],can[W+1];
88
for (int i=0;i<=W;++i)vmin[i]=inf,vmax[i]=0,can[i]=1;
9-
for (int i=0;i<n;++i)b[i]=a[i]&1?a[i]*2:a[i];
9+
for (int i=0;i<n;++i)b[i]=a[i]&1?a[i]*2:a[i];
1010
for (int i=0;i<n;++i)a[i]>>=__builtin_ctz(a[i]),ma=max(ma,a[i]);
1111
int c=__builtin_clz(ma);
1212
for (int i=0;i<n;++i){
1313
a[i]<<=__builtin_clz(a[i])-c;
1414
if (a[i]>ma)a[i]>>=1;
15-
a[i]=min(a[i],b[i]);
15+
a[i]=min(a[i],b[i]);
1616
mi=min(mi,a[i]);
1717
}
1818
int ans=ma-mi;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace Hash{
2+
typedef unsigned int uint;
3+
const uint S=17,S1=32-S,M=1996090921;
4+
struct node{
5+
int x,y,t;
6+
}h[(1<<S)+1005];
7+
int T=1;
8+
inline void insert(int x,int y){
9+
node *p=h+((uint)x*M>>S1);
10+
for (;p->t==T;++p)
11+
if (p->x==x){p->y+=y; return;}
12+
p->t=T; p->x=x; p->y=y;
13+
}
14+
inline bool find(int x){
15+
for (node *p=h+((uint)x*M>>S1);p->t==T;++p)
16+
if (p->x==x&&p->y)return 1;
17+
return 0;
18+
}
19+
} using namespace Hash;
20+
21+
class Solution {
22+
public:
23+
int maxOperations(vector<int>& a, int k) {
24+
int n=a.size(),ans=0; ++T;
25+
for (int i=0;i<n;++i)
26+
if (a[i]<k){
27+
if (find(k-a[i]))insert(k-a[i],-1),++ans;
28+
else insert(a[i],1);
29+
}
30+
return ans;
31+
}
32+
};
33+
34+
//IO
35+
int _IO=[](){
36+
ios::sync_with_stdio(0);
37+
cin.tie(0); //cout.tie(0);
38+
return 0;
39+
}();
40+
41+
185 KB
Binary file not shown.

1601-1700/1679. Max Number of K-Sum Pairs.txt

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Each subset can be viewed as an interval, we can prove that intervals cannot partially intersect.
2+
1. subset DP, let f[i][j] denote the minimum sum of incompatibilities when we distribute subset i, at most one subset is not full and starting at j. We add the subsets in the order of increasing right endpoint, so the last subset ends at the maximum element in i. O(2^n*n^2).
3+
4+
Remark. is this problem NP-hard? If we don't require the subsets be of equal size, then the problem can be solved by DP.
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1. DP, maintain the best and second best solution. O(n^2).
2+
2. similar to LCS, O(n^2/polylog n).
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
prefix sum. O(n).
2+

1601-1700/1686. Stone Game VI.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
They will greedily take the stones according to a[i]+b[i] in decreasing order. O(sort(n)).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
the answer is n-1. O(1).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
we can prove that the answer is the maximum digit. O(n).
2+

1601-1700/1690. Stone Game VII.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DP. O(n^2).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sorting according to max(w,l,h), then compute 2D LIS. O(n log^2 n).
2+
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The problem can be solved by a sliding window approach. We can prove that in the optimal solution, we always erase a "maximal" interval which contains unique elements. by "maximal" we mean, fix the left endpoint of the interval, we keep moving the right endpoint to the right, until the interval no longer contain unique elements. Then we can repeatedly move the left endpoint one step to the right, until the interval contains unique elements again. To check whether the current interval contain unique elements, we can use a hash table to maintain the elements within the interval. The running time is O(n).
2+

1601-1700/1696. Jump Game VI.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The problem can be solved by DP. Let's start with a simple solution.
2+
Let f[i] denote the largest score we can get when we end at position i. The transition is f[i]=max_{i-k<=j<=i-1} f[j]+nums[i]. The final output is f[n-1]. The running time is O(n^2).
3+
Now we further improve the running time. Use a monotone queue to maintain max_{i-k<=j<=i-1} f[j]. The running time is O(n).
4+
Binary file not shown.

README.md

+3

figs/1483.png

154 KB

figs/1679.png

209 KB

figs/792.png

200 KB

references.bib

+26-1
Original file line numberDiff line numberDiff line change
@@ -1197,9 +1197,34 @@ @inproceedings{coppersmith2002almost
11971197
organization={Springer}
11981198
}
11991199

1200+
@inproceedings{demaine2009cartesian,
1201+
title={On cartesian trees and range minimum queries},
1202+
author={Demaine, Erik D and Landau, Gad M and Weimann, Oren},
1203+
booktitle={International Colloquium on Automata, Languages, and Programming},
1204+
pages={341--353},
1205+
year={2009},
1206+
organization={Springer}
1207+
}
12001208

1209+
@inproceedings{chan2014succinct,
1210+
title={Succinct indices for path minimum, with applications to path reporting},
1211+
author={Chan, Timothy M and He, Meng and Munro, J Ian and Zhou, Gelin},
1212+
booktitle={European Symposium on Algorithms},
1213+
pages={247--259},
1214+
year={2014},
1215+
organization={Springer}
1216+
}
12011217

1202-
1218+
@article{bjorklund2014determinant,
1219+
title={Determinant sums for undirected hamiltonicity},
1220+
author={Bjorklund, Andreas},
1221+
journal={SIAM Journal on Computing},
1222+
volume={43},
1223+
number={1},
1224+
pages={280--299},
1225+
year={2014},
1226+
publisher={SIAM}
1227+
}
12031228

12041229

12051230

template.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ namespace Hash{
203203
int x,y,t;
204204
}h[(1<<S)+1005];
205205
int T=1;
206-
void insert(int x,int y){
206+
inline void insert(int x,int y){
207207
node *p=h+((uint)x*M>>S1);
208208
for (;p->t==T;++p)
209209
if (p->x==x){p->y=y; return;}
210210
p->t=T; p->x=x; p->y=y;
211211
}
212-
int* find(int x){
212+
inline int* find(int x){
213213
for (node *p=h+((uint)x*M>>S1);p->t==T;++p)
214214
if (p->x==x)return &p->y;
215215
return 0;

0 commit comments

Comments
 (0)