当前位置:网站首页>[weekly replay] the 81st biweekly match of leetcode
[weekly replay] the 81st biweekly match of leetcode
2022-06-27 13:09:00 【Executory peduncle】
Catalog
1、 Statistical asterisk
1) Title Description
Give you a string
s, Every time Two Continuous vertical line'|'by a pair . In other words , The first and the second'|'It's a couple , The third and the fourth'|'It's a couple , And so on .
Please returnbe not inBetween vertical line pairs ,s in'*'Number of .
Be careful , Each vertical line'|'MetropolisjustBelong to a pair .
2) Original link
3) Thinking analysis
- ( 1 ) (1) (1) First count out all
*The number of , Then subtract two liang|Previous*The number of is the answer , UseListSave all|The subscript , Go through it in pairs .
4) Template code
class Solution {
public int countAsterisks(String s) {
int n=s.length();
char[] c=s.toCharArray();
int ans=0;
List<Integer> list=new ArrayList<>();
for (int i=0;i<n;++i){
char g=c[i];
if (g=='|') list.add(i);
if (g=='*') ans++;
}
int len=list.size();
int gg=0;
for (int i = 0; i <len; i+=2) {
int l=list.get(i);
int r=list.get(i+1);
for (int j = l+1; j <r; j++) {
if (c[j]=='*') gg++;
}
}
return ans-gg;
}
}
5) Algorithm and time complexity
Algorithm : simulation
Time complexity : Traverse the string once , The complexity is O ( n ) O(n) O(n).
2、 Count the logarithm of points that cannot reach each other in an undirected graph
1) Title Description
Give you an integer
n, Means a sheet of Undirected graph There isnNodes , The number is0Ton - 1. And give you a two-dimensional array of integersedges, amongedges[i] = [ai, bi]Representation nodeaiandbiThere is a Undirected edge .
Please return Unable to reach each other Different Number of pairs .
2) Original link
LeetCode.6106: Count the logarithm of points that cannot reach each other in an undirected graph
3) Thinking analysis
- ( 1 ) (1) (1) And look up the set of template questions , Using arrays
wSave additional information , The number of points in each connected block , For each connected block , The number of all expected unconnected points is S S S, There are :
S = ( l o n g ) ( ( n − w [ i ] ) ∗ w [ i ] S=(long)((n-w[i])*w[i] S=(long)((n−w[i])∗w[i]
Because the answer doesn't consider the order , Two points are only one answer , So the final answer will double , We need to take each connected block to get S S S Add and divide 2 2 2. - ( 2 ) (2) (2) We found that , Indeed, the essence is to find the size of each connected block , So whatever we use DFS still BFS Also very easy to write .
4) Template code
class Solution {
int N=100010;
int[] q=new int[N];
int[] w=new int[N];
public long countPairs(int n, int[][] edges) {
for (int i = 0; i < n; i++) {
q[i]=i;
w[i]=1;
}
for (int[] g:edges){
int a=g[0];
int b=g[1];
a=find(a);
b=find(b);
if (a!=b){
q[a]=b;
w[b]+=w[a];
}
}
long ans=0;
Set<Integer> set=new HashSet<>();
for (int i = 0; i < n; i++) {
int a=find(i);
if (set.contains(a)) continue;
ans+= (long)(n -w[a]) *w[a];
set.add(a);
}
return ans/2;
}
int find(int x){
if (q[x]!=x) q[x]=find(q[x]);
return q[x];
}
}
5) Algorithm and time complexity
Algorithm : Union checking set 、BFS、DFS
Time complexity : No specific analysis
3、 Maximum XOR and after operation
1) Title Description
I'll give you a subscript from 0 The starting array of integers
nums. In one operation , choice arbitrarily Non-negative integerxAnd a subscripti, to updatenums[i]bynums[i] AND (nums[i] XOR x).
Be careful ,AND It's a bit by bit sum operation ,XOR Is a bitwise XOR operation .
Please execute Any time update operation , And back tonumsAll elements inMaximumBitwise XOR and .
2) Original link
3) Thinking analysis
- ( 1 ) (1) (1) For bit operations , We know that it has something to do with binary , And binary will not exceed... In the data range
32position . Binary median and bit are independent of each other , In order to find the rule, we consider the case of each binary bit . - ( 2 ) (2) (2) Suppose we consider the second y y y position , Because it's binary , therefore y y y The value of can only be 0 0 0 perhaps 1 1 1, Now let's assume y y y by 0, Then there are 0 A N D ( 0 X O R x ) 0AND(0XORx) 0AND(0XORx), because 0 0 0 And any value above is 0 0 0, So no matter x The number of this bit can only be 0 0 0, If we assume y y y by 1 1 1, Then there are 1 A N D ( 0 X O R x ) 1AND(0XORx) 1AND(0XORx), This situation needs to be discussed , If x x x by 1 1 1, The final result is 1 1 1, Otherwise 0 0 0.
- ( 3 ) (3) (3) So we found that , When the binary number of a number is y y y Is it 0 0 0 when , It cannot change , When the first y y y Is it 1 1 1 when , It can become 0 0 0. For all numbers in the array , If there is a number of th y y y Position as 1 1 1, Then we can guarantee the number of others y y y All of them are 0 0 0 Or become 0 0 0. Make all numbers guarantee the th after XOR y y y Position as 1 1 1. In order to make the value bigger , It is necessary to guarantee more 1 1 1, Let's decide whether every bit has 1 1 1 that will do , That is to say, all numbers or above are the answer .
4) Template code
class Solution {
public int maximumXOR(int[] nums) {
int n=nums.length;
int g=nums[0];
for(int i=1;i<n;++i) g|=nums[i];
return g;
}
}
5) Algorithm and time complexity
Algorithm : An operation
Time complexity : Traversing the array is O ( n ) O(n) O(n)
4、 The number of different die sequences
1) Title Description
Give you an integer
n. You need to roll one 6 Face dicenTime . Please meet the following requirements , Find out Different The number of dice in a sequence :
1、 Any in the sequence adjacent Digital greatest common divisor by1.
2、 In sequence equal Between the values of , There are at least2A number of other values . Formally , If the firstiThe value of a roll of dice be equal to The firstjThe second is worth , thatabs(i - j) > 2.
Please return different sequences of The total number . Because the answer can be big , Please correct the answer 1 0 9 + 7 10^9+7 109+7 Remainder After the return .
If at least one element of two sequences is different , Then they are treated as different sequences .
2) Original link
3) Thinking analysis
- ( 1 ) (1) (1) It must be linear at a glance d p dp dp problem , Considering the i i i Throw dice for the first time i − 1 i-1 i−1 and i − 2 i-2 i−2 Secondary related , We need to use 3D d p dp dp Storage state is easy to transfer . Definition f [ i ] [ k ] [ u ] f[i][k][u] f[i][k][u] For the first time i i i The number of points thrown is u u u, The first i − 1 i-1 i−1 Next is k k k Number of alternatives .
- ( 2 ) (2) (2) We can preprocess which points can be used as adjacent points , For the first i i i Throw the sieve again, and then go to the triple loop enumeration j , k , u j,k,u j,k,u, If it is judged to be true , There's a transfer equation :
f [ i ] [ j ] [ k ] = ( f [ i ] [ j ] [ k ] + f [ i − 1 ] [ u ] [ j ] ) f[i][j][k] = (f[i][j][k] + f[i-1][u][j]) f[i][j][k]=(f[i][j][k]+f[i−1][u][j])
4) Template code
class Solution {
int N=10010;
int[][][] f=new int[N][7][7];
boolean[][] st=new boolean[7][7];
int mod=1000000007;
public int distinctSequences(int n) {
if (n==1) return 6;
for (int i = 1; i <=6; i++) {
for (int j = 1; j <=6; j++) {
if (i!=j&&gcd(i,j)==1){
f[2][i][j]=1;
st[i][j]=true;
}
}
}
for (int i = 3; i <=n; i++) {
for (int j = 1; j <=6; j++) {
for (int k = 1; k <=6; k++) {
if (st[j][k]&&j!=k){
for (int u = 1; u <=6; u++) {
if (st[u][j]&&k!=u&&j!=u){
f[i][j][k] = (f[i][j][k] + f[i-1][u][j]) % mod;
}
}
}
}
}
}
int res=0;
for (int i = 1; i <=6; i++) {
for (int j = 1; j <=6; j++) {
res=(res+f[n][i][j])%mod;
}
}
return res;
}
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
}
5) Algorithm and time complexity
Algorithm :dp
Time complexity : O ( n m 3 ) O(nm^3) O(nm3), Office m m m by 6, Because the sieve only has 6 Face to face .
5、 Weekly summary
The third question can not be analyzed by bit operation , The fourth question does not write three-dimensional d p dp dp, I am a s b sb sb.
边栏推荐
猜你喜欢

Deeply convinced plan X - system foundation summary

Esp32s3 iperf routine test esp32s3 throughput test

Uni app develops wechat applet to dynamically render pages and dynamically change the order of page component modules
![[medical segmentation] unet3+](/img/93/1e9728a3dbebbf3bd9ce015552ce7a.jpg)
[medical segmentation] unet3+

What kind of air conditioner is this?

VS调试技巧

Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing

Tiktok practice ~ public / private short video interchange

Good luck today
![[tcaplusdb knowledge base] Introduction to tcaplusdb tcapulogmgr tool (I)](/img/ce/b58e436e739a96b3ba6d2d33cf8675.png)
[tcaplusdb knowledge base] Introduction to tcaplusdb tcapulogmgr tool (I)
随机推荐
Daily question brushing record (6)
Review summary of database
清楚的自我定位
Make learning pointer easier (2)
微服务之配置管理中心
再懂已是曲中人
l六月集训(第27天) —— 图
Explore tidb lightning source code to solve the found bugs
It is so simple to remove the payment restrictions on VIP, YuQue and Zhihu in Baidu Library
深信服X计划-系统基础总结
Nmcli team bridge basic configuration
快讯:华为启动鸿蒙开发者大赛;腾讯会议发布“万室如意”计划
Win10彻底永久关闭自动更新的步骤
Stack calculation (whether the order of entering and leaving the stack is legal) - Code
ACL 2022 | TAMT proposed by Chinese Academy of Sciences: TAMT: search for a portable Bert subnet through downstream task independent mask training
[dynamic programming] - Knapsack Problem
Industry insight - how should brand e-commerce reshape growth under the new retail format?
L June training (day 27) - figure
大小端字节序
中证500股指期货怎么开户,国内正规的股指期货平台有哪些,在哪里开户最安全?


