题目原文: 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,又是归并排序的练习题,很显然要实现个归并排序,并在里面计…
题目原文: 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…
题目原文: 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的有序子数组进行归并,…
题目原文 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…
题目原文: 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,这肯…
题目原文: 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…
题目原文: 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…
题目原文: 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 (…
题目原文: 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操作外,还…
题目原文: 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…
题目原文: 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…
题目原文: 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…
题目原文: 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…
几个比较常见的排序算法里头,这个我是比较生疏的一个,有一天突然被问了一个问题是,归并排序最大的特点是什么,我才想起这个算法来.下午又看不进书啦,就实现一下,记下来. 归并排序采取的是分治策略,就是先将数据不断地进行二分,然后分别排序子序列之后再不断地合并在一起. 归并排序与快排一样,时间复杂度是O(nlogn),是一个比较高效率的排序算法. vector<int> mergeSort(vector<int>& nums, int m, int n) { if (m == n…
题目原文详见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…
这周编程作业是实现检测点共线的算法.和排序算法有关系的地方在于,对斜率排序后可以很快的检测出来哪些点是共线的,另外这个算法的瓶颈也在于排序的性能. 一点收获: java传参数时传递的是值,这很多人都知道,只不过特别要注意的是对于引用类型,传递的是"引用的值",也就是说传递的是指针的副本,因此虽然似乎把一个数组传递进去了,但只要其他地方改动了这个数组,函数内部也会被影响.同理,返回值也是指针,如果不拷贝一份再返回,很有可能你在修改返回值的时候改动到了类的内部变量. 自己还不太明白的地方:…
第一次归并: a[0] a[1] a[2] a[3] a[4] a[5] a[6] 23 8 19 33 15 6 27 ↑             ↑ i     j 最开始i指向a[0],j指向a[1],比较a[0],a[1]大小,并进行swap a[0] a[1] a[2] a[3] a[4] a[5] a[6] 8 23 19 33 6 15 27 ↑                 ↑ i      j i指向j时候归并结束 第二次归并: a[0] a[1] a[2] a[3] a[4…
在做Coursera上的Algorithms第三周测验练习的时候有一道链表随机排序问题,刚开始没有什么思路,就想着先把单向链表归并排序实现了,再此基础上进行随机排序的改造.于是就结合归并排序算法,实现了单向链表的归并排序. import java.util.Iterator; /** * 对单向链表的由小到大归并排序 * @author evasean www.cnblogs.com/evasean/ * @param <T> */ public class MergeSortLinkedLi…
日志 20170410 Coursera机器学习 2017.11.28 update deeplearning 台大的机器学习课程:台湾大学林轩田和李宏毅机器学习课程 Coursera机器学习 Week 5: Neural Networks: Learning 本来上周开始该学习这个内容,也是先提交了作业,今天才来看看具体的代码:感觉这个课程本身对基础巩固很好.没有连续学习感觉有些有点忘了,最终的目的是自己能够推导这个内容. 本来想跟着学习搞个电子证书的,结果申请的到期时间是2017.3.31;…
Programming Assignment 3: Pattern Recognition 1.题目重述 原题目:Programming Assignment 3: Pattern Recognition 题目给定n个二维平面点,搜索能够连成线的大于等于四个点的集合.需要分别实现三个类,点的类,暴力搜索,快速搜索. 点的类需要实现根据点的坐标比较以及两个点根据某个点的斜率的比较. 暴力搜索和快速搜索均需要实现寻找点的功能. 2.分析 主要是分析如何实现暴力搜索和快速搜索. 2.1 暴力搜索 这个…