当前位置:网站首页>[brother hero's June training] day 26: check the collection
[brother hero's June training] day 26: check the collection
2022-07-27 10:21:00 【If I were Wen Shuai】
Series articles
【 Brother hero training in June 】 The first 01 God : Array
【 Brother hero training in June 】 The first 02 God : character string
【 Brother hero training in June 】 The first 03 God : Sort
【 Brother hero training in June 】 The first 04 God : greedy
【 Brother hero training in June 】 The first 05 God : Double pointer
【 Brother hero training in June 】 The first 06 God : The sliding window
【 Brother hero training in June 】 The first 07 God : Hash
【 Brother hero training in June 】 The first 08 God : The prefix and
【 Brother hero training in June 】 The first 09 God : Two points search
【 Brother hero training in June 】 The first 10 God : An operation
【 Brother hero training in June 】 The first 11 God : matrix
【 Brother hero training in June 】 The first 12 God : Linked list
【 Brother hero training in June 】 The first 13 God : Double linked list
【 Brother hero training in June 】 The first 14 God : Stack
【 Brother hero training in June 】 The first 15 God : Binary tree
【 Brother hero training in June 】 The first 16 God : queue
【 Brother hero training in June 】 The first 17 God : Breadth first search
【 Brother hero training in June 】 The first 18 God : Trees
【 Brother hero training in June 】 The first 19 God : Binary tree
【 Brother hero training in June 】 The first 20 God : Binary search tree
【 Brother hero training in June 】 The first 21 God : Pile up ( Priority queue )
【 Brother hero training in June 】 The first 22 God : Ordered set
【 Brother hero training in June 】 The first 23 God : Dictionary tree
【 Brother hero training in June 】 The first 24 God : Line segment tree
【 Brother hero training in June 】 The first 25 God : Tree array
【 Brother hero training in June 】 The first 26 God : Union checking set
List of articles
Union checking set
Union checking set , In some cases N In the application of set of elements , We usually start with each element forming a single set of elements , Then merge the set of elements belonging to the same group in a certain order , In the meantime, we need to repeatedly find out which set an element is in . In recent years, this kind of problems appear repeatedly in the international and domestic competitions of informatics . It is characterized by seemingly uncomplicated , But the amount of data is huge , If it is described by normal data structure , Often too large in space , The computer can't afford it ; Even in space , The time complexity of operation is also very high , It's impossible to run at the time specified in the competition (1~3 second ) Calculate the result required by the test question , It can only be described by concurrent query sets .
One 、 Detection loops in a two-dimensional grid
1559. Detection loops in a two-dimensional grid
class Solution {
class UnionFind{
int[] parent;
int[] size;
int n;
int setCount;
public UnionFind(int n){
parent = new int[n];
for(int i = 0;i<n;i++){
parent[i]=i;
}
size = new int[n];
Arrays.fill(size,1);
this.n=n;
setCount=n;
}
public int findset(int x){
return parent[x] == x?x:(parent[x]=findset(parent[x]));
}
public void unite(int x,int y){
// Make sure y Of size Small
if(size[x]<size[y]){
int temp = x;
x = y;
y=temp;
}
parent[y]=x;
size[x]+=size[y];
--setCount;
}
public boolean findAndUnite(int x,int y){
int parentX = findset(x);
int parentY = findset(y);
if(parentX !=parentY){
unite(parentX,parentY);
return true;
}
return false;
}
}
public boolean containsCycle(char[][] grid) {
int m = grid.length;
int n = grid[0].length;
UnionFind uf = new UnionFind(m*n);
for(int i=0;i<m;++i){
for(int j = 0;j<n;++j){
if(i>0 && grid[i][j]==grid[i-1][j]){
if(!uf.findAndUnite(i*n+j,(i-1)*n+j)){
return true;
}
}
if(j>0 && grid[i][j]==grid[i][j-1]){
if(!uf.findAndUnite(i*n+j,i*n+j-1)){
return true;
}
}
}
}
return false;
}
}
Two 、 Check if there is a valid path in the grid
1391. Check if there is a valid path in the grid
class Solution {
class UnionFind{
int[] f;
public void initset(){
f = new int[250010];
for(int i = 0;i<250010;i++){
f[i]=i;
}
}
public int findset(int x){
return f[x] == x?x:(f[x]=findset(f[x]));
}
public int unionset(int x,int y){
int fx=findset(x);
int fy=findset(y);
if(fx!=fy){
f[fx]=fy;
return 1;
}
return 0;
}
}
int[][] dir=new int[][] {
{
-1,0},{
0,1},{
1,0},{
0,-1}
};
int[][][] data=new int [][][]{
{
{
-1,-1,-1},{
-1,-1,-1},{
-1,-1,-1},{
-1,-1,-1}},
{
{
-1,-1,-1},{
1,3,5},{
-1,-1,-1},{
1,4,6}},
{
{
2,3,4},{
-1,-1,-1},{
2,5,6},{
-1,-1,-1}},
{
{
-1,-1,-1},{
-1,-1,-1},{
2,5,6},{
1,4,6}},
{
{
-1,-1,-1},{
1,3,5},{
2,5,6},{
-1,-1,-1}},
{
{
2,3,4},{
-1,-1,-1},{
-1,-1,-1},{
1,4,6}},
{
{
2,3,4},{
1,3,5},{
-1,-1,-1},{
-1,-1,-1}}
};
public boolean hasValidPath(int[][] grid) {
UnionFind uf=new UnionFind();
int i,j,k;
int m = grid.length;
int n= grid[0].length;
uf.initset();
for(i=0;i<m;i++){
for(j=0;j<n;++j){
for(k=0;k<4;++k){
int ti=i+dir[k][0];
int tj=j+dir[k][1];
if(ti<0||tj<0||ti==m||tj==n){
continue;
}
int a = i*n+j;
int b = ti*n+tj;
int ag = grid[i][j];
int bg = grid[ti][tj];
if(data[ag][k][0]==bg|| data[ag][k][1]==bg || data[ag][k][2]==bg){
uf.unionset(a,b);
}
}
}
}
return uf.findset(0)==uf.findset(m*n-1);
}
}
3、 ... and 、 Exchange elements in a string
1202. Exchange elements in a string
class Solution {
int[] f;
public void initset(int n){
f = new int[n];
for(int i = 0;i<n;i++){
f[i]=i;
}
}
public int findset(int x){
if(x!=f[x]){
f[x]=findset(f[x]);
}
return f[x];
}
public String smallestStringWithSwaps(String s, List<List<Integer>> pairs) {
int n = s.length();
initset(n);
for(int j=0; j<pairs.size();j++){
List<Integer> pa = pairs.get(j);
int pre = pa.get(0);
int aft = pa.get(1);
int preP = findset(pre);
int aftP = findset(aft);
if(preP == aftP){
continue;
}
f[preP] = aftP;
}
Map<Integer,PriorityQueue<Character>> map = new HashMap<>();
for(int k = 0;k<n;k++){
PriorityQueue<Character> queue = map.getOrDefault(findset(k),new PriorityQueue<>());
queue.offer(s.charAt(k));
map.put(f[k],queue);
}
StringBuilder sb=new StringBuilder();
for(int r=0;r<n;r++){
PriorityQueue<Character> tq = map.get(findset(r));
sb.append(tq.poll());
}
return sb.toString();
}
}
summary
边栏推荐
- 视觉SLAM十四讲笔记(一):第一讲+第二讲
- Snowflake vs. databricks who is better? The latest war report in 2022
- 语音数据采集-实时语音数据可视化
- Cannot start after installing MySQL 5.7.27 in CentOS 7? (Language bash)
- FTP 服务器
- Anaconda安装(非常详细)
- Matlab的不同进制转换
- Shell process control (emphasis), if judgment, case statement, let usage, for ((initial value; loop control condition; variable change)) and for variable in value 1 value 2 value 3..., while loop
- gyp ERR! configure error. gyp ERR! stack Error: gyp failed with exit code: 1
- Different binary conversion of MATLAB
猜你喜欢

About new_ Online_ Judge_ 1081_ Thoughts on Goldbach's conjecture

Sub query of database performance series

Wind10 configure ADB command

FTP 服务器

Shell的read 读取控制台输入、read的使用

Shell integrated application cases, archiving files, sending messages

Robotframework+eclispe environment installation

Review of in vivo detection

Anaconda installation (very detailed)

Shell operator, $((expression)) "or" $[expression], expr method, condition judgment, test condition, [condition], comparison between two integers, judgment according to file permission, judgment accor
随机推荐
pillow的原因ImportError: cannot import name ‘PILLOW_VERSION‘ from ‘PIL‘,如何安装pillow<7.0.0
备战金九银十Android面试准备(含面试全流程,面试准备工作面试题和资料等)
3D face reconstruction and dense alignment with position map progression network
open3d库的安装,conda常用指令,导入open3d时报这个错误Solving environment: failed with initial frozen solve. Retrying w
Practice and exploration of overseas site Seata of ant group
samba服务器
Reason for pilot importerror: cannot import name 'pilot_ Version 'from' PIL ', how to install pilot < 7.0.0
Fsm onehot 答题记录
Matlab draws the system response under different damping
Matlab/simulink sample sharing for solving differential equations
Oracle resizing data files
Ant高级-path和fileset
sql注入
Sub query of database performance series
Matlab-创建文字云
gyp ERR! configure error. gyp ERR! stack Error: gyp failed with exit code: 1
【英雄哥六月集训】第 23天: 字典树
【精选】如何完美的写 PHP 代码的呢?
【英雄哥六月集训】第 28天: 动态规划
WGAN、WGAN-GP、BigGAN