当前位置:网站首页>[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;
}
边栏推荐
- 01. Solr7.3.1 deployment and configuration of jetty under win10 platform
- Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
- Scenario based technology architecture process based on tidb - Theory
- Structure - C language
- 美国费城发生“安全事故” 2名警察遭枪杀
- Thymeleaf th:with局部变量的使用
- 分享 20 个稀奇古怪的 JS 表达式,看看你能答对多少
- R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置
- LeetCode_ 3 (longest substring without repeated characters)
- The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
猜你喜欢
CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion
Redis如何实现多可用区?
LeetCode_ 2 (add two numbers)
How to choose the appropriate certificate brand when applying for code signing certificate?
How to deeply understand the design idea of "finite state machine"?
为什么我认识的机械工程师都抱怨工资低?
Principle and performance analysis of lepton lossless compression
区间 - 左闭右开
Share 20 strange JS expressions and see how many correct answers you can get
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
随机推荐
Mysql database installation tutorial under Linux
R语言使用ggplot2包的geom_histogram函数可视化直方图(histogram plot)
CYCA少儿形体礼仪 宁波市培训成果考核圆满落幕
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
Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
Redis如何实现多可用区?
Structure - C language
直播预告|如何借助自动化工具落地DevOps(文末福利)
Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million
C - Divisors of the Divisors of An Integer Gym - 102040C
Discussion on memset assignment
The simplest way to open more functions without certificates
Google eventbus usage details
How does redis implement multiple zones?
C language -- structure and function
Guofu hydrogen energy rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, and 360million yuan of accounts receivable exceed the revenue
魅族新任董事長沈子瑜:創始人黃章先生將作為魅族科技產品戰略顧問
How to call the function mode of one hand and one machine
TiFlash 面向编译器的自动向量化加速
ASP.NET大型外卖订餐系统源码 (PC版+手机版+商户版)