Coursera Algorithms week1 查并集 练习测验:3 Successor with delete
题目原文:
Given a set of n integers S = {0,1,…,N-1}and a sequence of requests of the following form:
- Remove x from S
- Find the successor of x: the smallest y in S such thaty>=x
design a data type so that all operations(except construction) take logarithmic time or better in the worst case.
分析
题目的要求有一个0~n-1的顺序排列序列S,从S中移除任意x,然后调用getSuccessor(x),方法将返回一个y,这个y是剩余还在S中满足y>=x的最小的数。举例说明S={0,1,2,3,4,5,6,7,8,9}时
remove 6,那么getSuccessor(6)=7
remove 5,那么getSuccessor(5)=7
remove 3,那么getSuccessor(3)=4
remove 4,那么getSuccessor(4)=7
remove 7,那么getSuccessor(7)=8, getSuccessor(3)=8
而对于没有remove的数x,getSuccessor(x)应该等于几呢?题目没有说,那么就认为等于自身好了,接着上面,getSuccessor(2)=2
根据上面的例子,可以看出,实际上是把所有remove的数做了union,root为子集中的最大值,那么getSuccessor(x)实际就是获取remove数中的最大值+1,根据这个思路,代码如下
import edu.princeton.cs.algs4.StdOut; public class Successor {
private int num;
private int[] id;
private boolean[] isRemove; public Successor(int n){
num = n;
id = new int[n];
isRemove = new boolean[n];
for (int i = 0; i < n; i++) {
id[i] = i;
isRemove[i] = false;
}
} public int find(int p) {
while (p != id[p])
p = id[p];
return p;
} public void union(int p, int q) {
//此处的union取较大根
int pRoot = find(p);
int qRoot = find(q);
if (pRoot == qRoot)
return;
else if (pRoot < qRoot)
id[pRoot] = qRoot;
else
id[qRoot] = pRoot;
} public void remove(int x) {
isRemove[x] = true;
//判断相邻节点是否也被remove掉了,如果remove掉就union
if (x>0 && isRemove[x-1]){
union(x,x-1);
}
if (x<num-1 && isRemove[x+1]){
union(x,x+1);
}
} public int getSuccessor(int x) {
if(x<0 || x>num-1){//越界异常
throw new IllegalArgumentException("访问越界!");
}else if(isRemove[x]){
if(find(x)+1 > num-1) //x以及大于x的数都被remove掉了,返回-1
return -1;
else //所有remove数集中最大值+1,就是successor
return find(x)+1;
}else {//x未被remove,就返回x自身
return x;
}
} public static void main(String[] args) {
Successor successor = new Successor(10);
successor.remove(2);
successor.remove(4);
successor.remove(3);
StdOut.println("the successor is : " + successor.getSuccessor(3));
successor.remove(7);
successor.remove(9);
StdOut.println("the successor is : " + successor.getSuccessor(9));
}
}
Coursera Algorithms week1 查并集 练习测验:3 Successor with delete的更多相关文章
- Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity
题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which ...
- Coursera Algorithms week1 查并集 练习测验: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 ...
- Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题
题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...
- Coursera Algorithms week1 算法分析 练习测验: 3Sum in quadratic time
题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case ...
- Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法
第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...
- Coursera Algorithms week2 基础排序 练习测验: Permutation
题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one ...
- Coursera Algorithms week2 基础排序 练习测验: Intersection of two sets
题目原文: Given two arrays a[] and b[], each containing n distinct 2D points in the plane, design a subq ...
- Coursera Algorithms week4 基础标签表 练习测验:Inorder traversal with constant extra space
题目原文: Design an algorithm to perform an inorder traversal of a binary search tree using only a const ...
- Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST
题目原文: Given a binary tree where each
随机推荐
- 文件下载之ServletOutputStream
使用response.getOutputStream可以获取ServletOutputStream,从而实现向页面发送流数据.但是需要注意的是,不能使用ajax进行请求,因为这样页面不会有任何反应,可 ...
- CAD处理键盘按钮被释放(com接口VB语言)
主要用到函数说明: MxDrawXCustomEvent::KeyUp 键盘按钮被释放,详细说明如下: 参数 说明 iVk 是按钮码,如F8,的值为#define VK_F8 0x77 返回0消息继续 ...
- includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。
注意:对象数组不能使用includes方法来检测. JavaScript Demo: Array.includes() var array1 = [1, 2, 3]; console.log(arra ...
- pop的运用
pop():弹出列表最后一个元素 练习题: num_list = [12, 45, 34,13, 100, 24, 56, 74, 109] one_list = [] two_list = [] t ...
- node版本管理工具nvm安装使用教程
一些安装包依赖一定的node版本,可以采用nvm管理node, 可以快速的进行版本切换. 操作系统: windows10, x64 常见版本工具: 1. nvmw, nvmm install node ...
- solaris roles cannot login directly
oracle@solaris:~$ su - root Password: Oracle Corporation SunOS root@solaris:~# cat /etc/user_attr # ...
- python之cookbook-day04
第一章:数据结构和算法 1.4 查找最大或最小的N个元素 问题: 怎样从一个集合中获得最大或者最小的 N 个元素列表? 解决方案: heapq 模块有两个函数:nlargest() 和 nsmalle ...
- Python - 面对对象(进阶)
目录 Python - 面对对象(进阶) 类的成员 一. 字段 二. 方法 三. 属性 类的修饰符 类的特殊成员 Python - 面对对象(进阶) 类的成员 一. 字段 字段包括:普通字段和静态字段 ...
- Delphi 10.3.2最新消息
官方已经发布消息,招内测人员了! https://www.barnsten.com/default/newsupdates/details?news_id=328 https://docs.googl ...
- 【codeforces 527C】Glass Carving
[题目链接]:http://codeforces.com/contest/527/problem/C [题意] 让你切割一个长方形; 只能横切或竖切; 让你实时输出切完之后最大的长方形的面积; [题解 ...