当前位置:网站首页>[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;
}边栏推荐
- Security analysis of Web Architecture
- The function of qualifier in C language
- Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
- 详解Vue适时清理keepalive缓存方案
- tidb-dm报警DM_sync_process_exists_with_error排查
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用labs参数自定义X轴的轴标签文本(customize X axis labels)
- Thymeleaf 使用后台自定义工具类处理文本
- 美国费城发生“安全事故” 2名警察遭枪杀
- [learning notes] connectivity and circuit of graph
- leetcode:881. 救生艇
猜你喜欢

无密码身份验证如何保障用户隐私安全?

PHP - fatal error: allowed memory size of 314572800 bytes exhausted

Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute

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

日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”

如何深入理解“有限状态机”的设计思想?

软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】

Opengauss database source code analysis series articles -- detailed explanation of dense equivalent query technology (Part 2)

Solution of commercial supply chain collaboration platform in household appliance industry: lean supply chain system management, boosting enterprise intelligent manufacturing upgrading

Scenario based technology architecture process based on tidb - Theory
随机推荐
The forked VM terminated without saying properly goodbye
2022年国内正规的期货公司平台有哪些啊?方正中期怎么样?安全可靠吗?
【学习笔记】图的连通性与回路
ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)
非技术部门,如何参与 DevOps?
Time to calculate cron expression based on cronsequencegenerator
R语言ggplot2可视化:可视化折线图、使用theme函数中的legend.position参数自定义图例的位置
详解Vue适时清理keepalive缓存方案
LeetCode_ 69 (square root of x)
CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
Discussion on memset assignment
How to introduce devsecops into enterprises?
Sorter evolution of ticdc 6.0 principle
CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
R language ggplot2 visualization: visual line graph, using legend in theme function The position parameter defines the position of the legend
Enjoy what you want. Zhichuang future
Security analysis of Web Architecture
C language -- structure and function
注意!软件供应链安全挑战持续升级