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
随机推荐
- 【译】x86程序员手册20-6.3.4门描述符守卫程序入口
6.3.4 Gate Descriptors Guard Procedure Entry Points 门描述符守卫程序入口 To provide protection for control tra ...
- 【技术累积】【点】【java】【26】@Value默认值
@Value 该注解可以把配置文件中的值赋给属性 @Value("${shit.config}") private String shit; 要在xml文件中设置扫描包+place ...
- 在iOS项目中嵌入RN代码
1:在项目跟目录下创建一个ReactComponent文件夹.目录结构如下: 2: 在ReactComponent文件夹下新建一个 package.json 文件 { "name" ...
- 【Apache Kafka】一、Kafka简介及其基本原理
对于大数据,我们要考虑的问题有很多,首先海量数据如何收集(如Flume),然后对于收集到的数据如何存储(典型的分布式文件系统HDFS.分布式数据库HBase.NoSQL数据库Redis),其次存储 ...
- 类 Fabric 主机管理程序开发
类 Fabric 主机管理程序开发:1. 运行程序列出主机组或者主机列表2. 选择指定主机或主机组3. 选择让主机或者主机组执行命令或者向其传输文件(上传/下载4. 充分使用多线程或多进程5. 不同主 ...
- 1040 有几个PAT (25 分)
题目链接:1040 有几个PAT (25 分) 做这道题目,遇到了新的困难.解决之后有了新的收获,甚是欣喜! 刚开始我用三个vector数组存储P A T三个字符出现的位置,然后三层for循环,根据字 ...
- SpringBoot杂记
一.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: •application.properties •application.yml 配置文件的作用:修改SpringBoo ...
- windows下db2的一些使用心得(不含安装)
1.安装完成后开始菜单栏里会有一个 DB2 Command Window - Administrator 打开这个命令窗口 2.db2 db2,启动 3.list databse directory ...
- windows 2013(codevs 1695)
题目描述 Description 话说adamyi编的Windows 2013超时了(- -!),所以他不得不在自己家门口亲眼见证这个电影般的场景.虽然他不想错过这个美妙的时刻,但是他的肚子一再抗议, ...
- hdu_1048_The Hardest Problem Ever_201311052052
The Hardest Problem Ever Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...