题目原文:

Given a set of n integers S = {0,1,,N-1}and a sequence of requests of the following form:

  • Remove from S
  • Find the successor of x: the smallest in 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的更多相关文章

  1. Coursera Algorithms week1 查并集 练习测验:1 Social network connectivity

    题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which ...

  2. 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 ...

  3. Coursera Algorithms week1 算法分析 练习测验: Egg drop 扔鸡蛋问题

    题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg ...

  4. 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 ...

  5. Coursera Algorithms week2 基础排序 练习测验: Dutch national flag 荷兰国旗问题算法

    第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given ...

  6. Coursera Algorithms week2 基础排序 练习测验: Permutation

    题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one ...

  7. 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 ...

  8. 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 ...

  9. Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST

    题目原文: Given a binary tree where each 

随机推荐

  1. 六时车主 App 隐私政策

    六时车主 App 隐私政策   本应用尊重并保护所有使用服务用户的个人隐私权.为了给您提供更准确.更有个性化的服务,本应用会按照本隐私权政策的规定使用和披露您的个人信息.但本应用将以高度的勤勉.审慎义 ...

  2. R包

    查看默认安装包的位置 .libPaths() 移除包 remove.packages("package_name") 查看所有安装的包 library() 按 q 退出包列表   ...

  3. Xpath语法与lxml库

    1. Xpath 1 )什么是XPath? xpath(XML Path Language)是一门在XML和HTML文档中查找信息的语言,可用来在XML和HTML文档中对元素和属性进行遍历. 2) X ...

  4. swift-新手必看的基础部分

    Swift 是一门开发 iOS, OS X 和 watchOS 应用的新语言.然而,如果你有 C 或者 Objective-C 开发经验的话,你会发现 Swift 的很多内容都是你熟悉的. 常量和变量 ...

  5. 优化yum下载安装慢,不断换mirror

    不停地换mirror,为了解决这个问题,在网上搜了好多资料,总结出一个基于aliyun的mirror源 先检查:是否能正常上网,DNS是否正常,网关gw是否正常,若通过ping 不正常,则解决好网络, ...

  6. dubbo服务telnet命令的使用

    转自:https://www.cnblogs.com/feiqihang/p/4387330.html dubbo服务发布之后,我们可以利用telnet命令进行调试.管理.Dubbo2.0.5以上版本 ...

  7. 查看Linux中自带的jdk ,设置JAVA_HOME

    在配置hadoop是,进行格式化hadoop的时候,出现找不到jdk 我用centos7是64位的, 发现本机有java ,就找了一下其位置 找到了jdk-1.7.0_75 which java [r ...

  8. HDU 3401 Trade

    Trade Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 3401 ...

  9. shell函数返回值

    1.用return作为函数的退出状态,然后通过echo $?来得到函数返回值,注意,return返回的值限定为0-255. 例子: #! /bin/bash sum() { let "z=$ ...

  10. nyoj_289_苹果_20140307

    苹果 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 ctest有n个苹果,要将它放入容量为v的背包.给出第i个苹果的大小和价钱,求出能放入背包的苹果的总价钱最大值. ...