coursera Algorithms week1 Interview Questions

题目2:

Union-find with specific canonical element. Add a method find() to the union-find data type so that find(i) returns the largest element in the connected component containing i. The operations, union()connected(), and find() should all take logarithmic time or better.

For example, if one of the connected components is {1,2,6,9}, then the find()method should return 9 for each of the four elements in the connected components.

分析:

与加权 quick-union算法思路大概一致,在union()方法中进行根节点的控制,使得根节点始终是最大的数。

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class UF {
	private int[] id;	//分量id
	private int count;	//分量数量
	
	public UF(int N) {
		//初始化分量id数组
		count = N;
		id = new int[N];
		for (int i = 0; i < N; i++)
			id[i] = i;
	}
	
	public int count() {
		return count;
	}
	
	public boolean connected(int p, int q) {
		return find(p) == find(q);
	}
	
	public int find(int p) {
		while (p != id[p])
			p = id[p];
		return p;
	}
	
	public void union(int p, int q) {
		int i = find(p);
		int j = find(q);
		if (i == j)
			return;
		if (i > j) 
			id[j] = i;
		else
			id[i] = j;
		count--;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//解决由StdIn得到的动态连通性问题
		int N = StdIn.readInt();
		UF uf = new UF(N);
		while (!StdIn.isEmpty()) {
			int p = StdIn.readInt();
			int q = StdIn.readInt();
			if (uf.connected(p, q))	continue;
			uf.union(p, q);
			StdOut.println(p + "" + q);
		}
		StdOut.println(uf.count() + "components");
	}	
}                                                                                                                       
 

题目三:

Successor with delete. Given a set of n integers S={0,1,...,n1} and a sequence of requests of the following form:

  • Remove x from S
  • Find the successor of x: the smallest y in S such that yx.

design a data type so that all operations (except construction) take logarithmic time or better in the worst case.

分析:

是对题目2的应用;每次remove(x)都是将 x 和 x + 1 进行连接,每次remove(x)会使数组的x消失可以等价成寻找根节点的模型。getsuccessor(x)即是返回 x 的根节点。

import edu.princeton.cs.algs4.StdOut;


public class Successor {
	private int[] id;		//父链接索引
	public Successor(int N) {
		id = new int[N];
		for(int i = 0; i < N; i++)
			id[i] = i;
	}
	public int find(int p) {
		//找到根节点
		while(p != id[p])
			p = id[p];
		return p;
	}
	public void remove(int p) {
		union(p, p + 1);
	}
	public void union(int p, int q) {
		int i = id[p];
		int j = id[q];
		if (i == j)
			return;
		//将较大的数作为根节点
		else if (i > j)
			id[q] = i;
		else
			id[p] = j;
	}
	public int getsuccessor(int p) {
		return find(p);
	}
	public static void main(String[] args) {
		Successor s = new Successor(10);
		s.remove(6);
		s.remove(5);
		s.remove(3);
		StdOut.println("the successor of 3 is " + s.getsuccessor(3));
		s.remove(4);
		s.remove(7);
		StdOut.println("the successor of 3 is " + s.getsuccessor(3));
		StdOut.println("the successor of 1 is " + s.getsuccessor(1));
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值