LeetCode 179 Zhou race

Just finished cross-examination paper, to organize a fresh.
This week's tournament title overall, fairly calm, but want to get the points still need some skills, compared to the last time, this time on the subject of the complexity of the data card start time of the algorithm, and some of the wording violence may not some problems.
A one said.

Each generates a string of characters is an odd number

Title
this problem, emm, can be regarded as a topic people confuse sex. I stared at the title, repeatedly confirmed, finally, understand the rules of only a limited number of characters in the string appear odd, but requires character is a lowercase letter, not the number required for each character.
Therefore, the practice is simple
when the length is odd when one of the character can be completely filled
when the length is an even number when a filler other character, all the rest can be filled with a character
I chose two characters a and b

Code:

class Solution {
    public String generateTheString(int n) {
		String ans = "";
		if((n & 1) == 1)
		{
			for(int i = 1;i <= n;i++)
			{
				ans += "a";
			}
		}
		else
		{
			for(int i = 1;i < n;i++)
			{
				ans += "a";
			}
			ans += "b";
		}
		return ans;
    }
}

Still want an emotion: Java string handling really easy! ! !


Lamp switches III

Title
this problem, violence want to simulate n side should be well written, but the data fifty thousand of view, this is not appropriate.

Here are two approaches:

O (nlogn)

Need to find ways to optimize the complexity to O (nlogn). Game, to see the front of the bright lights have to all the conditions, my first reaction was a need for a structure to access logn range and. So I think the Fenwick tree.

Is substantially step, after each lighting a lamp, in which position +1. Determines whether it will turn blue, it will find the front section and is equal to its index, which is determined in front of a lamp lit whether the number is the number of lamps.

If you turn blue, then traverse back then lit the lamp, they should also be blue, met off the stops.
After processing the light of this moment, the number of blue lights and a few moments to compare statistics can answer.

Of course, the subject may also be considered when seeking a moment, the number of the block of FIG communication, each lighting a lamp, and at this point it is to insert and connect the front and rear lit lamp. Meet the requirements of the moment, a number of connected blocks and the first lamp must be included in the communication block.
It can be maintained with a disjoint-set.

Fenwick tree Code:

class Solution {
    int[] a = new int[100050];
	boolean[] isOpen = new boolean [100050];
	
	private int lowbit(int k)
	{
		return k & (-k);
	}
	
	private int get(int k)
	{
		int sum = 0;
		while(k > 0)
		{
			sum += a[k];
			k -= lowbit(k);
		}
		return sum;
	}
	
	private void add(int k,int num,int n)
	{
		while(k <= n)
		{
			a[k] += num;
			k += lowbit(k);
		}
	}
	
	public int numTimesAllBlue(int[] light) {
		int ans = 0;
		int l = light.length;
		int sum = 0;
		for(int i = 0,k;i < l;i++)
		{
			add(light[i],1,l);
			isOpen[light[i]] = true;
			
			if(get(light[i]) == light[i])
			{
				k = light[i];
				while(k <= l && isOpen[k])
				{
					sum++;
					k++;
				}
			}
			if(sum == i + 1)
			{
				ans++;
			}
		}
		
		return ans;
    }
}

O (n)

In fact, this question was observed specificity of blue, i.e., must all the lights on the front, then if a lamp lighting the blue, then it must be in front of the lamp (excluding the first lamp) blue . It is determined whether there is no need to use blue complexity lO (logn) of a, O (1) compare to.
The final complexity is O (n)

O (n) Code:

class Solution {
    boolean[] isBlue = new boolean[100000];
	boolean[] isOpen = new boolean[100000];
	public int numTimesAllBlue(int[] light) {
		int ans = 0;
		int l = light.length;
		int sum = 0;
		isBlue[0] = true;
		for(int i = 0,k;i < l;i++)
		{
			isOpen[light[i]] = true;
			if(isBlue[light[i] - 1])
			{
				k = light[i];
				while(k <= l && isOpen[k])
				{
					isBlue[k] = true;
					sum++;
					k++;
				}	
			}
			if(sum == i + 1)
			{
				ans++;
			}
		}
		
		return ans;
    }
}

Ok. . . Many simple


The time required to inform all employees

Topic
of this question and the last question is traversing the tree. For this topic, built after the tree, it is the time to notice the side length of the child node. A traversal longest path. Complexity of O (n)
Code:

class Solution {
    class V
	{
		V(int k,int next)
		{
			this.k = k;
			this.next = next;
		}
		int k;
		int next;
	}
	V[] nxt;
	int[] head;
	int tot = 0;
	
	private int dfs(int k,int[] time,int sum)
	{
		int ans = sum;
		for(int i = head[k];i != 0;i = nxt[i].next)
		{
			ans = Math.max(ans,dfs(nxt[i].k,time,sum + time[k]));
		}
		return ans;
	}
	
	public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) 
	{
		nxt = new V[n + 5];
		head = new int [n];
		
		for(int i = 0;i < n;i++)
		{
			if(manager[i] == -1)
			{
				continue;
			}
			nxt[++tot] = new V(i,head[manager[i]]);
			head[manager[i]] = tot;
		}
		return dfs(headID,informTime,0);
    }
}

Game code to write ugly, do not mind


T-second position after the frog

Topic
of this question is actually also a tree traversal. It should be noted that:

  • This tree is a tree without roots, to be marked access
  • The probability distribution of child nodes are equal, so the probability of entering the child nodes should be to their probability divided by the number of child nodes
  • 1 node with probability 1.0
  • There are two cases to let the shop there is a probability: no child nodes or to the point when the time is zero.
  • The frog does not meet the point on the bar situation is unlikely to stop there, that probability is zero

Code:

class Solution {
    class V
	{
		V(int k,int next)
		{
			this.k = k;
			this.next = next;
		}
		int k;
		int next;
	}
	V[] nxt;
	int[] head,sNum;
	int tot = 0;
	double[] p;
	boolean[] vis;
	
	private void dfs(int k,double po,int t)
	{
		vis[k] = true;
		if(t == 0 || sNum[k] == 1)
		{
			p[k] = po;
			return;
		}
		po /= (sNum[k] - 1);
		for(int i = head[k];i != 0;i = nxt[i].next)
		{
			if(vis[nxt[i].k])
			{
				continue;
			}
			dfs(nxt[i].k,po,t - 1);
		}
	}
	
	public double frogPosition(int n, int[][] edges, int t, int target) 
	{
		p = new double[n + 1];
		nxt = new V[n * 4];
		head = new int[n + 1];
		sNum = new int[n + 1];
		vis = new boolean[n + 1];
		
		for(int i = 0;i < edges.length;i++)
		{
			nxt[++tot] = new V(edges[i][0],head[edges[i][1]]);
			head[edges[i][1]] = tot;
			
			nxt[++tot] = new V(edges[i][1],head[edges[i][0]]);
			head[edges[i][0]] = tot;
			
			sNum[edges[i][0]]++;
			sNum[edges[i][1]]++;
		}
		
		sNum[1]++;
		dfs(1,1.0,t);
		
		return p[target];
    }
}
Published 17 original articles · won praise 2 · Views 439

Guess you like

Origin blog.csdn.net/wayne_lee_lwc/article/details/104729815