Solution to the problem of the 194th round of force deduction

I'm so angry. The last question is wrong. It's too late..... But it's OK to play in the infield. But it's a pity.

5440. Array exclusive or operation

Idea: according to the requirements of the topic, it is ok to directly XOR

class Solution {
    public int xorOperation(int n, int start) {
    	
    	int ans=0;
    	
    	for(int i=0;i<n;i++)
    		ans^=start+2*i;
    	
    	return ans;
    	
    }
}

5441. Guarantee that the file name is unique

Idea: for each file name, you can map the name and times.

Pit point: it is possible that a file name + (file times) constructed name has already appeared. It needs to be handled here!

class Solution {
    public String[] getFolderNames(String[] names) {
    	
    	int n=names.length;
    	String[] ans=new String[n];
    	Map<String,Integer> map=new HashMap<>();
    	
    	for(int i=0;i<n;i++) {
    		String s=names[i];
    		if(map.containsKey(s)) {
    			int num=map.get(s)+1;
    			String ss=s+"("+String.valueOf(num)+")";
    			while(map.containsKey(ss)) {
    				num++;
    				ss=s+"("+String.valueOf(num)+")";
    			}
    			ans[i]=ss;
    			map.put(s, num);
    			map.put(ss, 0);
    		}
    		else {
    			ans[i]=s;
    			map.put(s, 0);
    		}
    	}
    	
    	return ans;
    
    }
}

5442. Avoiding flooding

PS: at that time, many people were stuck in this question, and I ranked 13th after finishing this question..

Thinking: for every position where rains[i] is greater than 0, we don't need to worry about it. For the position where rains[i]=0, we need to consider which lake should be drained. We can be sure that if there is a risk of flood in a lake, we have to drain it in advance. But in order to ensure that there is no flood at the end, we have to do everything we can to ensure that there is no flood at all If a filled lake will rain one day later, we should choose the nearest day after the current day to drain the lake.

class Solution {
    public int[] avoidFlood(int[] rains) {
    	
    	int n=rains.length;
    	int[] ans=new int[n];
    	TreeSet<Integer> st=new TreeSet<>();
    	Map<Integer,Integer> map=new HashMap<>();
    	
    	for(int i=0;i<n;i++) {
    		if(rains[i]>0) {
    			ans[i]=-1;
    			if(map.containsKey(rains[i])) {
    				if(st.isEmpty())
    					return new int[0];
    				else {
    					Integer p=st.higher(map.get(rains[i]));
    					if(p==null) return new int[0];
    					else {
    						ans[(int)p]=rains[i];
    						st.remove(p);
    					}
    				}
    			}
    			map.put(rains[i], i);
    		}
    		else
    			st.add(i);
    	}
    	
    	Iterator<Integer> it=st.iterator();
    	while(it.hasNext())
    		ans[(int)it.next()]=1;
    	
    	return ans;
    	
    }
}

5443. Find the key edge and pseudo key edge in the minimum spanning tree

Idea: this problem is really annoying. It's almost a bare minimum spanning tree. For the determination of the critical edge, we consider that enumerating and deleting that edge will make the value of the minimum spanning tree larger. For the pseudo critical edge, we first add the edge without brain before generating the minimum spanning tree. If the cost of the generated minimum spanning tree does not change, then this edge is a pseudo critical edge.

Pit point: in the determination of critical edge, the operation of deleting edge may make it impossible to generate the minimum spanning tree at last, so special judgment is needed.

class Solution {
	
	private int[] f;
	private List<node> list;
	private boolean mark;
	class node implements Comparable<node>{
		int id,u,v,val;
		public node(int u,int v,int val) {
			this.u=u;
			this.v=v;
			this.val=val;
		}
		@Override
		public int compareTo(node o) {
			// Method stubs generated automatically by TODO
			return val-o.val;
		}
	}
	
	public List<List<Integer>> findCriticalAndPseudoCriticalEdges(int n, int[][] edges) {

		int val=0;
		f=new int[n];
    	list=new ArrayList<>();
    	List<List<Integer>> ans=new ArrayList<>();
    	
    	for(int i=0;i<2;i++)
    		ans.add(new ArrayList<>());
    	
    	for(int i=0;i<edges.length;i++) {
    		list.add(new node(edges[i][0],edges[i][1],edges[i][2]));
    		list.add(new node(edges[i][1],edges[i][0],edges[i][2]));
    	}
    	
    	val=MST(n,-1);
    	int index=edges.length-1;
    	
    	for(int i=list.size()-1;i>0;i-=2) {
    		
    		list.remove(i);
    		list.remove(i-1);
    		mark=false;
    		int now=MST(n,-1);
    		if(val<now || mark) ans.get(0).add(index);
    		list.add(new node(edges[index][0],edges[index][1],edges[index][2]));
    		list.add(new node(edges[index][1],edges[index][0],edges[index][2]));
    		index--;
    	}
    	
    	list.clear();
    	
    	for(int i=0;i<edges.length;i++) {
    		list.add(new node(edges[i][0],edges[i][1],edges[i][2]));
    		list.add(new node(edges[i][1],edges[i][0],edges[i][2]));
    	}
    	
    	index=0;
    	
    	for(int i=0;i<list.size();i+=2) {
    		
    		if(ans.get(0).contains(index)) {
    			index++;
    			continue;
    		}

    		int now=MST(n,index);
    		if(val==now) ans.get(1).add(index);
    		index++;
    	}
    	
    	return ans;
    }
	
	private int MST(int n,int index) {
		
		index=index*2;
		
		List<node> nowList=new ArrayList<>();
		
		int size=list.size(),sum=0,num=0;
		
		int t1,t2;
		
		for(int i=0;i<size;i++)
			nowList.add(list.get(i));
		
		for(int i=0;i<n;i++) f[i]=i;
		
		if(index>=0) {
			t1=find(list.get(index).u);
			t2=find(list.get(index).v);
			if(t1!=t2) {
				num++;
				sum+=list.get(index).val;
				//System.out.println(t1+" "+t2+" "+index+" "+sum);
				f[t1]=t2;
			}
		}
		
		Collections.sort(nowList);
		
		for(int i=0;i<size;i++) {
			t1=find(nowList.get(i).u);
			t2=find(nowList.get(i).v);
			if(t1!=t2) {
				num++;
				sum+=nowList.get(i).val;
				f[t1]=t2;
			}
		}
		
		if(num<n-1) mark=true;
		
		return sum;
	}
	
	private int find(int x) {
		if(f[x]==x)
			return x;
		return f[x]=find(f[x]);
	}
}

 

Posted on Sun, 21 Jun 2020 02:03:01 -0400 by vigiw