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 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,根据这个思路,代码如下

 1 import edu.princeton.cs.algs4.StdOut;
 2
 3 public class Successor {
 4     private int num;
 5     private int[] id;
 6     private boolean[] isRemove;
 7
 8     public Successor(int n){
 9         num = n;
10         id = new int[n];
11         isRemove = new boolean[n];
12         for (int i = 0; i < n; i++) {
13             id[i] = i;
14             isRemove[i] = false;
15         }
16     }
17
18     public int find(int p) {
19         while (p != id[p])
20             p = id[p];
21         return p;
22     }
23
24     public void union(int p, int q) {
25         //此处的union取较大根
26         int pRoot = find(p);
27         int qRoot = find(q);
28         if (pRoot == qRoot)
29             return;
30         else if (pRoot < qRoot)
31             id[pRoot] = qRoot;
32         else
33             id[qRoot] = pRoot;
34     }
35
36     public void remove(int x) {
37         isRemove[x] = true;
38         //判断相邻节点是否也被remove掉了,如果remove掉就union
39         if (x>0 && isRemove[x-1]){
40             union(x,x-1);
41         }
42         if (x<num-1 && isRemove[x+1]){
43             union(x,x+1);
44         }
45     }
46
47     public int getSuccessor(int x) {
48         if(x<0 || x>num-1){//越界异常
49             throw new IllegalArgumentException("访问越界!");
50         }else if(isRemove[x]){
51             if(find(x)+1 > num-1) //x以及大于x的数都被remove掉了,返回-1
52                 return -1;
53             else //所有remove数集中最大值+1,就是successor
54                 return find(x)+1;
55         }else {//x未被remove,就返回x自身
56             return x;
57         }
58     }
59
60     public static void main(String[] args) {
61         Successor successor = new Successor(10);
62         successor.remove(2);
63         successor.remove(4);
64         successor.remove(3);
65         StdOut.println("the successor is : " + successor.getSuccessor(3));
66         successor.remove(7);
67         successor.remove(9);
68         StdOut.println("the successor is : " + successor.getSuccessor(9));
69     }
70 }
时间: 2024-04-19 02:29:35

Coursera Algorithms week1 练习测验3:Successor with delete的相关文章

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 the connected component containing i. The operations, union(), connected(), and find() should all take logarithmic time or better. 1 import edu.princ

Coursera Algorithms week1 Interview Questions: 3Sum in quadratic time

题目要求: Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case. You may assume that you can sort the n integers in time proportional to n2 or better. 分析: <算法4>这本书提供的TwoSumFast解法为NlogN,ThreeSumFast解法为N2logN,根据课后练

Coursera Algorithms week1 Interview Questions: Search in a bitonic array

题目要求: An array is bitonic if it is comprised of an increasing sequence of integers followed immediately by a decreasing sequence of integers. Write a program that, given a bitonic array of n distinct integer values, determines whether a given integer

Coursera Algorithms week3 归并排序 练习测验1: Merging with smaller auxiliary array

题目原文: Suppose that the subarray a[0] to a[n-1] is sorted and the subarray a[n] to a[2*n-1] is sorted. How can you merge the two subarrays so that a[0] to a[2*n-1] is sorted using an auxiliary array of length n (instead of 2n) 分析: 对两个大小分别为n的有序子数组进行归并,

Coursera Algorithms week3 快速排序 练习测验: Nuts and bolts

题目原文: Nuts and bolts. A disorganized carpenter has a mixed pile of n nuts and n bolts. The goal is to find the corresponding pairs of nuts and bolts. Each nut fits exactly one bolt and each bolt fits exactly one nut. By fitting a nut and a bolt toget

Coursera Algorithms week2 栈和队列 练习测验: Stack with max

题目原文: Stack with max. Create a data structure that efficiently supports the stack operations (push and pop) and also a return-the-maximum operation. Assume the elements are reals numbers so that you can compare them. 分析: 该题目要求在实现正常stack的push和pop操作外,还

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 constant amount of extra space. 1 public void traverse(BST<Key,Value> bst) { 2 traverse(bst.root.left, bst.root); 3 } 4 5 private void traverse(Node curre

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

题目原文: Given a binary tree where each  ???????? contains a key, determine whether it is a binary search tree. Use extra space proportional to the height of the tree. 分析: 就是递归遍历BST,将每个节点x分别与x.left和x.right比大小. 1 public boolean isBST(BST<Key,Value> bst)

Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)

题目原文 Selection in two sorted arrays. Given two sorted arrays a[] and b[], of sizes n1 and n2, respectively, design an algorithm to find the kth largest key. The order  of growth of the worst case running time of your algorithm should be logn, where n