谜题一 保持一致 谜题 假设有一大群人排队等待观看棒球比赛.他们都是主场球迷,每个人都戴着队帽,但不是所有人都用同一种戴法,有些人正着戴,有些人反着戴. 假定你是保安,只有在全组球迷帽子戴法一致时才能让他们进入球场,要么全部正着戴,要么全部反着戴.因为每个人对正戴和反戴的定义并不相同.因此你不能对他们说把帽子正着戴或反着戴,只能告诉他们转一下帽子. 举个栗子(我们用 F 表示正戴,B 表示反戴) F F B B B F B B B F F B F 上面是一个13人的队伍,位置从0 ~ 12.你可
I Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
1 精确描述问题 第一章强调的重点在于”精确的描述问题“,这是程序开发的第一步 -- "Problem definition" 1.1 Precise problem statement 1) input: a file containing at most 107 positive intergers (each < 107); any interger occurs twice is an error; no other data is associated with t
问题描述:给定一个字符串,输出该字符串所有排列的可能.如输入“abc”,输出“abc,acb,bca,bac,cab,cba”. 虽然原理很简单,然而我还是折腾了好一会才实现这个算法……这里主要记录的是解决问题中的思路. 我实现的是最普通的递归算法,也没有除重,嗯非递归及除重的算法以后再补上吧. 实现过程 首先明确函数的输入和输出,输入是一个字符串,输出么对于JS而言用数组来表示最恰当了,所以函数的雏形应该是这样的: function permutate(str) { var result =
最近公共祖先LCA 双链BT 如果每个结点都有一个指针指向它的父结点,于是我们可以从任何一个结点出发,得到一个到达树根结点的单向链表.因此这个问题转换为两个单向链表的第一个公共结点(先分别遍历两个链表得到它们的长度,并求出两个长度之差.在长的链表上先遍历若干个点之后,再同步遍历两个链表,知道找到相同的结点,或者一直到链表结束). BST 不用递归recursive,迭代iterative就行 public int query(Node t, Node u, Node v) { int left
为了加深对缓存算法的理解,特转此篇,又由于本文内容过多,故不做翻译,原文地址Working Set页面置换算法 In the purest form of paging, processes are started up with none of their pages in memory. As soon as the CPU tries to fetch the first instruction, it gets a page fault, causing the operating sy