题目原文: 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. import edu.princet…
题目原文描述: Given a social network containing. n members and a log file containing m timestamps at which times pairs of members formed friendships, design an algorithm to determine the earliest time at which all members are connected (i.e., every member…
题目原文: 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 logarithmi…
题目原文: Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg breaks if it is dropped from floor T or higher and does not break otherwise. Your goal is to devise a strategy to determine the value of T given the…
题目要求: 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,根据课后练…
第二周课程的Elementray Sorts部分练习测验Interview Questions的第3题荷兰国旗问题很有意思.题目的原文描述如下: Dutch national flag. Given an array of n buckets, each containing a red, white, or blue pebble, sort them by color. The allowed operations are: swap(i,j): swap the pebble in buc…
题目原文: Given two integer arrays of size n , design a subquadratic algorithm to determine whether one is a permutation of the other. That is, do they contain exactly the same entries but, possibly, in a different order. 本质上就是求两个数组排序后是否相等,鉴于本节课学的是选择.插入.…
题目原文: Given two arrays a[] and b[], each containing n distinct 2D points in the plane, design a subquadratic algorithm to count the number of points that are contained both in array a[] and array b[]. 题目的目标就是计算重复point的个数,很简单,代码如下 import java.awt.Poin…
题目原文: Design an algorithm to perform an inorder traversal of a binary search tree using only a constant amount of extra space. public void traverse(BST<Key,Value> bst) { traverse(bst.root.left, bst.root); } private void traverse(Node current, Node p…
1. Java autoboxing and equals(). Consider two double values a and b and their corresponding Double value x and y. Find values such that (…
题目原文: Decimal dominants. Given an array with n keys, design an algorithm to find all values that occur more than  n/10 times. The expected running time of your algorithm should be linear. 分析: 直观上将n个元素遍历一遍,并记录每个元素出现的次数就可以实现,虽然时间复杂度是O(n),但是空间复杂度却高达n,这肯…
题目原文 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…
题目原文: 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…
题目原文: Shuffling a linked list. Given a singly-linked list containing n items, rearrange the items uniformly at random. Your algorithm should consume a logarithmic (or constant) amount of extra memory and run in time proportional to nlogn in the worst…
题目原文: An inversion in an array a[] is a pair of entries a[i] and a[j] such that i<j but a[i]>a[j]. Given an array, design a linearithmic algorithm to count the number of inversions. 分析: 如果没有性能限制,用插入排序算法可以实现.题目性能被限制在nlogn,又是归并排序的练习题,很显然要实现个归并排序,并在里面计…
题目原文: 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操作外,还…
题目原文: 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的有序子数组进行归并,…
题目原文: Implement a queue with two stacks so that each queue operations takes a constant amortized number of stack operations. 题目要求用栈实现队列的所有操作. package week2; import java.util.Stack; /** * Queue with two stacks. Implement a queue with two stacks so tha…
题目原文详见http://coursera.cs.princeton.edu/algs4/assignments/collinear.html 程序的主要目的是寻找n个points中的line segment,line segment的要求就是包含不少于4个点. 作业包含三部分程序实现: 一.Point compareTo()用来比较本节点this与其他节点that的大小:假如this节点坐标(x0, y0),that节点坐标(x1, y1),只有y0 < y1或(y0==y1 &&…
题目地址:http://coursera.cs.princeton.edu/algs4/assignments/kdtree.html 分析: Brute-force implementation. 蛮力实现的方法比较简单,就是逐个遍历每个point进行比较,实现下述API就可以了,没有什么难度. import java.util.ArrayList; import java.util.TreeSet; import edu.princeton.cs.algs4.Point2D; import…
题目原文:http://coursera.cs.princeton.edu/algs4/assignments/8puzzle.html 题目要求:设计一个程序解决8 puzzle问题以及该问题的推广,例如8-puzzle是3*3,程序要能解决n*n的同类问题(2 ≤ n < 128) 典型的8 puzzle如下: 算法设计参照A*搜索算法,即使不了解A*搜索算法,题目也已经将解法解释的很具体了. Best-first search:设计参照A* 搜索算法.定义一个 search node类,包…
作业原文:http://coursera.cs.princeton.edu/algs4/assignments/queues.html 这次作业与第一周作业相比,稍微简单一些.有三个编程练习:双端队列(Deque)设计.随机队列(Randomized Queue)设计,还有一个排列组合类Permutation. 一.双端队列Deque 设计要求:A double-ended queue or deque (pronounced "deck") is a generalization o…
题目来源http://coursera.cs.princeton.edu/algs4/assignments/percolation.html 作业分为两部分:建立模型和仿真实验. 最关键的部分就是建立模型对象.模型对象要求如下: The model.  We model a percolation system using an n-by-n grid of sites. Each site is either open or blocked. A full site is an open s…
这个是自己写的算法,如果有大牛,麻烦帮我并行化.初学者则可以学到不少东西. 产生测试用例 import java.io.*; import java.util.Random; public class ProduceCase { public static void main(String[] argvs){ File file2 = new File("D:\\YounG\\TestCases\\MySet\\test.txt"); FileWriter fw = null; Buf…
week1查漏补缺 经过第一周的学习后,在蓝墨云班课上做了一套31道题的小测试,下面是对测试题中遇到的错误的分析和总结: 一.背记题 不属于Java后继技术的是? Ptyhon Java后继技术有? JDBC.JSP.XML.Java EE等 不属于Java集成开发而环境的是? PyCharm Java集成开发环境有? NetBean.MyEclipse.IntelliJ IDEA等 Java源文件扩展名,字节码扩展名? .java . .class Java的三大特性? 面向对象.平台无关.动…
交集:http://www.boost.org/doc/libs/1_56_0/libs/geometry/doc/html/geometry/reference/algorithms/intersection.html 并集:http://www.boost.org/doc/libs/1_56_0/libs/geometry/doc/html/geometry/reference/algorithms/union_.html boost geometry下载地址:https://svn.boo…
LinQ是指集成化查询语言,通过映射将数据库内的表名变为C#的类名,将列名作为属性名,将表的关系作为类的成员对象.O--M--R O-Object对象 R-Relation关系 M-Mapping映射 LinQ包括:LinQ to SQL,针对SQL的查询,它是一个带有可视化的操作界面的ORM工具. LinQ to Object,用来实现对内存中集合对象的查询. LinQ to DataSet,对强类型化或弱类型化的DataSet或独立的DataTable进行查询. LinQ to Entity…
@(132 - ACM | 算法) Algorithm | Coursera - by Robert Sedgewick > Tip: Focus on WHAT is really important! > Don't just copy it! > Don't look at the subtitle > Practice is the key. Just Do it! Backup Coursera - Algorithms booksite P.S. iff == if a…
题目链接: http://poj.org/problem?id=2524 Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 39369   Accepted: 18782 Description There are so many different religions in the world today that it is difficult to keep track of…