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
随机推荐
- CSS学习笔记----选择器
用过css的同志们都知道,选择器是非常重要的,如果选择器使用不当,即使你的css写的再好,但是不知道要用在哪个元素上,这不是英雄无用武之地吗? css,用过的都说好! 最基本的选择器,id选择器,类选 ...
- 【译】x86程序员手册06 - 2.4指令格式
2.4 Instruction Format 指令格式 The information encoded in an 80386 instruction includes a specification ...
- C#斐波那契数列方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 【JavaScript进阶】深入理解JavaScript中ES6的Promise的作用并实现一个自己的Promise
1.Promise的基本使用 // 需求分析: 封装一个方法用于读取文件路径,返回文件内容 const fs = require('fs'); const path = require('path') ...
- SOUI界面库 添加 windows系统文件图标皮肤
最近在学习soui界面库.其中有用到SListCtrl这个控件来现在文件信息.控件用法基本上和mfc 的CListCtrl差不多.也支持图标显示.但是图标是要自己加入图标图片的.这个就有点不好弄.于是 ...
- Linux常用解压缩命令
压 缩:tar -jcv -f filename.tar.bz2 要被压缩的文件或目录名称 查 询:tar -jtv -f filename.tar.bz2 解压缩:tar -jxv -f filen ...
- CentOS7.2下安装php加速软件Xcache
说明: php安装目录:/usr/local/php php.ini配置文件路径:/usr/local/php/etc/php.ini Nginx安装目录:/usr/local/nginx Nginx ...
- 34.分组聚合操作—bucket
主要知识点: 学习聚合知识 一.准备数据 1.家电卖场案例背景建立index 以一个家电卖场中的电视销售数据为背景,来对各种品牌,各种颜色的电视的销量和销售额,进行各种各样角度的分析 ...
- git 的简单使用(3)
Git鼓励大量使用分支: 查看分支:git branch 创建分支:git branch <name> 切换分支:git checkout <name> 创建+切换分支:git ...
- 学习Android
=========================================today start to learn Android================= 我们学习需要的软件: jd ...