Skip to content

Commit abf71d9

Browse files
committed
.
1 parent fc9b7f5 commit abf71d9

File tree

42 files changed

+194
-10
lines changed

Some content is hidden

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

42 files changed

+194
-10
lines changed

0301-0400/312. Burst Balloons.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DP, let f[i][j] denote the maximum coins we can collect by busting [i,j]. O(n^3).
2+

0701-0800/743. Network Delay Time.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
compute eccentricity, using shortest path in weighted directed graphs. O(m+n log log n) \cite{thorup2004integer}.
2+

0701-0800/752. Open the Lock.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bfs. O(10^4).
2+

0701-0800/753. Cracking the Safe.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Eulerian path, there always exist a solution. O(output).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
queue. O(n).
2+

0901-1000/934. Shortest Bridge.txt

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

0901-1000/935. Knight Dialer.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DP, use matrix and fast exponentiation. O(log n).
2+
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
note that we are not required to optimize the number of moves.
2+
use greedy, remove the stamps in reverse order. whenever we find a match, we can remove that substring and replace it by '?'s.
3+
for a fast implementation, we first find the leftmost full match, it will split the target into two parts. for the left part, the next match must be a prefix of stamp, and we can repeatedly do this until we remove the left part. the right part is of the form ?...?t, and we repeatedly remove a prefix of t that matches with a suffix of stamp. we then find the next full match, which again split t into two parts. the left part is of the form ?...?r?...?. we then repeatedly remove a suffix of r, until r matches a substring of stamp. use string hashing to compare two strings. O(n).
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
string sorting. O(sort(n,L)).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const int N=40005;
2+
int c[N];
3+
class Solution {
4+
public:
5+
int minIncrementForUnique(vector<int>& a) {
6+
int n=a.size(),ans=0,m=0,s=0;
7+
if (!n)return 0;
8+
for (int *i=&a[0],*end=i+n;i!=end;++c[*i],++i)
9+
if (*i>m)m=*i;
10+
for (int *i=c,*end=c+m;i<=end;++i){
11+
s+=*i;
12+
if (s>=1)--s,ans+=s;
13+
}
14+
ans+=s*(s-1)/2;
15+
for (int i=0;i<n;++i)--c[a[i]];
16+
return ans;
17+
}
18+
};
19+
20+
//IO
21+
int _IO=[](){
22+
ios::sync_with_stdio(0);
23+
cin.tie(0); //cout.tie(0);
24+
return 0;
25+
}();
26+
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
note that the values are unique. simulation. O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hashing. O(n).
2+
Binary file not shown.
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(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
two pointers. 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(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
O(n).
2+

0901-1000/994. Rotting Oranges.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bfs. O(nm).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
greedily flip the leftmost 1. O(n).
2+
+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
@@ -1,2 +1,3 @@
11
1. suffix tree. O(n).
22
2. minimum representation. O(n).
3+

1601-1700/1627. Graph Connectivity With Threshold 284ms.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ void dfs(int x){
88
if (k<=t)break;
99
if (!v[k])dfs(k);
1010
}
11-
if (x>t)
12-
for (int j=1;j<=p1;++j){
13-
int k=x*p[j];
14-
if (k>n)break;
15-
if (!v[k])dfs(k);
11+
if (x>t){
12+
for (int j=1;j<=p1;++j){
13+
int k=x*p[j];
14+
if (k>n)break;
15+
if (!v[k])dfs(k);
16+
}
1617
}
1718
}
1819
class Solution {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const int N=40005;
2+
int c[N],del[N];
3+
class Solution {
4+
public:
5+
int eatenApples(vector<int>& a, vector<int>& d) {
6+
int n=a.size(),m=0,s=0,s1=0,t,ans=0;
7+
for (int i=n-1;i>=0;--i)
8+
if (i+d[i]>m)m=i+d[i];
9+
memcpy(c,&a[0],sizeof(int)*n);
10+
memset(c+n,0,sizeof(int)*(m+1-n));
11+
memset(del,0,sizeof(int)*(m+1));
12+
for (int i=0;i<n;++i)del[i+d[i]]+=a[i];
13+
for (int i=0;i<=m;++i){
14+
s+=c[i]; t=del[i];
15+
if (t){
16+
if (s1>=t)s1-=t;
17+
else s-=t-s1,s1=0;
18+
}
19+
if (s)++s1,--s,++ans;
20+
}
21+
return ans;
22+
}
23+
};
24+
25+
//IO
26+
int _IO=[](){
27+
ios::sync_with_stdio(0);
28+
cin.tie(0); //cout.tie(0);
29+
return 0;
30+
}();
31+
Binary file not shown.

1701-1800/1705. Maximum Number of Eaten Apples.txt

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
typedef unsigned int uint;
2+
const int N=1000005,u=3,U=1<<u,L=30/u;
3+
struct node{
4+
node* c[U];
5+
int min; uint w;
6+
}p[N],*root=p;
7+
int g[U][U][1<<U],p1;
8+
void init(){
9+
for (int i=0;i<(1<<U);++i)
10+
for (int j=0;j<U;++j){
11+
int ma=-1,t=-1;
12+
for (int k=0;k<U;++k){
13+
if ((i&(1<<k))&&(j^k)>ma)ma=j^k,t=k;
14+
g[j][k][i]=t;
15+
}
16+
}
17+
}
18+
class Solution {
19+
public:
20+
vector<int> maximizeXor(vector<int>& a, vector<vector<int>>& q) {
21+
int n=a.size(),m=q.size(),mi=1<<30; vector<int> ans(m);
22+
root->w=0; p1=0;
23+
for (int i=0;i<n;++i){
24+
node *x=root; int y=a[i];
25+
if (y<mi)mi=y;
26+
for (int j=L-1;j>=0;--j){
27+
int t=(y>>j*u)&(U-1);
28+
if (x->w&(1<<t)){
29+
x=x->c[t];
30+
int &z=x->min; if (y<z)z=y;
31+
}
32+
else {
33+
x->w|=1<<t;
34+
x=x->c[t]=p+(++p1);
35+
x->w=0; x->min=y;
36+
}
37+
}
38+
}
39+
for (int i=0;i<m;++i){
40+
node *x=root; int y=q[i][0],b=q[i][1],j;
41+
if (b<mi){ans[i]=-1; continue;}
42+
for (j=L-1;j>=0;--j){
43+
int t=(y>>j*u)&(U-1);
44+
int r=(b>>j*u)&(U-1),h=g[t][r][x->w];
45+
if (h<r){x=x->c[h]; break;}
46+
else if (x->c[h]->min>b){x=x->c[g[t][r][x->w-(1<<r)]]; break;}
47+
x=x->c[h];
48+
}
49+
for (--j;j>=0;--j)x=x->c[g[(y>>j*u)&(U-1)][U-1][x->w]];
50+
ans[i]=x->min^y;
51+
}
52+
return ans;
53+
}
54+
};
55+
56+
//IO
57+
int _IO=[](){
58+
init();
59+
ios::sync_with_stdio(0);
60+
cin.tie(0); //cout.tie(0);
61+
return 0;
62+
}();
63+
Binary file not shown.

1701-1800/1707. Maximum XOR With an Element From Array.txt

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
the integers are distinct. find the maximum of the first n-k+1 numbers. O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
nth_element. O(n).
2+

1701-1800/1711. Count Good Meals.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reduce to two sum. O(n log U).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
enumerate the first split, use monotone pointers to maintain the range of the second split. O(n).
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
LCS. the numbers in target are distinct, so we can reduce to LIS. O(n log log n) \cite{fredman1975computing}.
2+

README.md

+1

figs/1705.png

194 KB

figs/1707.png

207 KB

figs/945.png

170 KB

references.bib

+12
Original file line numberDiff line numberDiff line change
@@ -1237,4 +1237,16 @@ @article{asathulla2019faster
12371237
publisher={ACM New York, NY, USA}
12381238
}
12391239

1240+
@article{thorup2004integer,
1241+
title={Integer priority queues with decrease key in constant time and the single source shortest paths problem},
1242+
author={Thorup, Mikkel},
1243+
journal={Journal of Computer and System Sciences},
1244+
volume={69},
1245+
number={3},
1246+
pages={330--353},
1247+
year={2004},
1248+
publisher={Elsevier}
1249+
}
1250+
1251+
12401252

0 commit comments

Comments
 (0)