当前位置:网站首页>[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
[summary of leetcode weekly competition] the 81st fortnight competition of leetcode (6.25)
2022-07-05 14:27:00 【Let it beSun】
Weekly Links : competition - Power button (LeetCode)81 A fortnight game
subject 1 2315. Statistical asterisk
Ideas :
Direct traversal of simple topics , Use a variable to identify whether it is within the vertical line pair
Code :
class Solution {
public int countAsterisks(String s) {
int count = 0;
int d = 0;
for(char c:s.toCharArray()){
if(d==1){
if(c=='|'){
d = 0;
}
}else{
if(c=='|'){
d = 1;
}
if(c=='*'){
count++;
}
}
}
return count;
}
}subject 2 Count the logarithm of points that cannot reach each other in an undirected graph
Ideas :
Union checking set , See 【 Algorithm notes 】 Focus on summary and collection _Let it beSun The blog of -CSDN Blog
Mainly find the size of each connected component
In limine TLE( Overtime ) The idea of : Calculate the size of each connected component directly by multiplication
for example :

At this time, the sizes of several connected components are {4,2,1} Now calculate ans = 4*2+4*1+2*1 = 14
But time complexity O(n^2) Overtime ;
Calculation method 1
So consider for each node , Computing node i The number of disconnected points , Because there is repetition in the calculation of each node , So the answer is to sum and divide by 2.
for(int i=0;i<n;i++){
ans += (long)(n-sz[find(i)]);
}
return ans/2;Calculation method 2
You can consider the logarithm of all points n*(n-1)/2 , Subtract the number of nodes inside each connected component a*(a-1)/2
Code :
Corresponding calculation method 1 :
class Solution {
int[] f = new int[100005];
int[] sz = new int[100005];
public int find(int x){
if(f[x]==x) return x;
return f[x] = find(f[x]);
}
public void unite(int x,int y){
int fx = find(x);
int fy = find(y);
if(fx!=fy){
f[fx] = fy;
sz[fy] += sz[fx];
sz[fx] = 0;
}
}
public long countPairs(int n, int[][] edges) {
for(int i=0;i<n;i++){
f[i] = i;
sz[i] = 1;
}
long ans = 0;
for(int i=0;i<edges.length;i++){
int a = edges[i][0];
int b = edges[i][1];
unite(a,b);
}
for(int i=0;i<n;i++){
ans += (long)(n-sz[find(i)]);
}
return ans/2;
}
}Corresponding calculation method 2 :
class Solution {
int[] f = new int[100005];
int[] sz = new int[100005];
public int find(int x){
if(f[x]==-1) return x;
return f[x] = find(f[x]);
}
public void unite(int x,int y){
int fx = find(x);
int fy = find(y);
if(fx!=fy){
f[fx] = fy;
sz[fy] += sz[fx];
sz[fx] = 0;
}
}
public long countPairs(int n, int[][] edges) {
for(int i=0;i<n;i++){
f[i] = -1;
sz[i] = 1;
}
long ans = 0;
for(int i=0;i<edges.length;i++){
int a = edges[i][0];
int b = edges[i][1];
unite(a,b);
}
for(int i=0;i<n;i++){
if(f[i]==-1){
ans += (long)sz[i]*(sz[i]-1)/2;
}
}
return (long)n*(n-1)/2-ans;
}
}subject 3 2317. Maximum XOR and after operation
Ideas
It is to count whether each bit in this array appears 1, For all binary bits , As long as this bit appears in the array 1, So this one in the answer is also 1.
So the answer is the result of bitwise or of all numbers . Complexity O(n).
Code
Method 1 : Direct statistics
class Solution {
public int maximumXOR(int[] nums) {
int[] bit = new int[32];
for(int i=0;i<32;i++){
for(int num:nums){
if( ((num>>i)&1) == 1){
bit[i]++;
break;
}
}
}
int res = 0;
int t = 1;
for(int i=0;i<32;i++){
if(i!=0){
t = t*2;
}
res += bit[i]==0?0:t;
}
return res;
}
}Method 2 : Press bit or
public int maximumXOR(int[] nums) {
int ans = 0;
for (int num : nums) ans |= num;
return ans;
}边栏推荐
- Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
- Chow Tai Fook fulfills the "centenary commitment" and sincerely serves to promote green environmental protection
- R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the coef function to obtain the log odds ratio corresponding to eac
- Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 2)
- Sorter evolution of ticdc 6.0 principle
- tidb-dm报警DM_sync_process_exists_with_error排查
- leetcode:881. 救生艇
- How does redis implement multiple zones?
- 强联通分量
- Oneconnect listed in Hong Kong: with a market value of HK $6.3 billion, ye Wangchun said that he was honest and trustworthy, and long-term success
猜你喜欢

openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)

What are the advantages and characteristics of SAS interface

How can non-technical departments participate in Devops?

TiCDC 6.0原理之Sorter演进

CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕

【leetcode周赛总结】LeetCode第 81 场双周赛(6.25)

区间 - 左闭右开

How to deeply understand the design idea of "finite state machine"?

How does redis implement multiple zones?

The IPO of Ruineng industry was terminated: the annual revenue was 447million and it was planned to raise 376million
随机推荐
The function of qualifier in C language
注意!软件供应链安全挑战持续升级
Tiflash compiler oriented automatic vectorization acceleration
软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
The forked VM terminated without saying properly goodbye
【学习笔记】阶段测试1
How to make a second clip of our media video without infringement
总量分析 核算方法和势方法 - 分摊分析
01. Solr7.3.1 deployment and configuration of jetty under win10 platform
C language -- structure and function
Tdengine biweekly selection of community issues | phase III
直播预告|如何借助自动化工具落地DevOps(文末福利)
Mysql database installation tutorial under Linux
不相交集
Shenziyu, the new chairman of Meizu: Mr. Huang Zhang, the founder, will serve as the strategic adviser of Meizu's scientific and technological products
R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置
TiFlash 面向编译器的自动向量化加速
Longest common subsequence dynamic programming
Scenario based technology architecture process based on tidb - Theory