当前位置:网站首页>[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;
}边栏推荐
- LeetCode_ 2 (add two numbers)
- 乌卡时代下,企业供应链管理体系的应对策略
- 让秒杀狂欢更从容:大促背后的数据库(下篇)
- 美国费城发生“安全事故” 2名警察遭枪杀
- [learning notes] stage test 1
- R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
- 为什么我认识的机械工程师都抱怨工资低?
- R language ggplot2 visualization: use ggplot2 to visualize the scatter diagram, and use the labs parameter to customize the X axis label text (customize X axis labels)
- 网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理
- Postgresql 13 安装
猜你喜欢

What are the advantages and characteristics of SAS interface

LeetCode_ 2 (add two numbers)

Thymeleaf th:with局部变量的使用

分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少

Tdengine biweekly selection of community issues | phase III

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

Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module

Zhizhen new energy rushes to the scientific innovation board: the annual revenue is 220million, and SAIC venture capital is the shareholder

Lepton 无损压缩原理及性能分析

分享 12 个最常用的正则表达式,能解决你大部分问题
随机推荐
关于memset赋值的探讨
Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
Loop invariant
How to introduce devsecops into enterprises?
Thymeleaf th:with局部变量的使用
TDengine 社区问题双周精选 | 第三期
区间 - 左闭右开
R语言ggplot2可视化:可视化折线图、使用theme函数中的legend.position参数自定义图例的位置
享你所想。智创未来
软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】
How to make a second clip of our media video without infringement
Disjoint Set
如何深入理解“有限状态机”的设计思想?
美国费城发生“安全事故” 2名警察遭枪杀
PHP - fatal error: allowed memory size of 314572800 bytes exhausted
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
最简单不用证书也可以多开功能的方式
TiFlash 源码解读(四) | TiFlash DDL 模块设计及实现分析
Google eventbus usage details
网上电子元器件采购商城:打破采购环节信息不对称难题,赋能企业高效协同管理