閒下來後,須要講最近涉及到的算法所有整理一下,有個indice,方便記憶宮殿的查找php
MIT的算法課,地球上最好: Design and Analysis of Algorithms
html
本篇須要從新整理,按照如下歸類去理解」思想「,而非」算法細節「。面試
找到解決問題的」思惟突破口「。算法
「貪心算法」 算是 "動態規劃" 的前置課程。數組
在數據結構graph中的優化問題也大量涉及到了」Greedy Method"。數據結構
也有五大經常使用算法之說:算法設計之五大經常使用算法設計方法總結ide
1、【分治法】大數據
2、【動態規劃法】優化
3、【貪心算法】ui
4、【回溯法】
5、【分支限界法】
Why Puzzles? Solving puzzles will help you sharpen your analytic skills and make you a better problem solver. More over, most of our puzzles can be solved using methods that are either the same or are related to various techniques that we will be using to design algorithms. Click on titles to get to the puzzles.
經典算法思想——分治(Divide-and-Conquer)
一個分治法將規模爲n的問題分紅k個規模爲n/m的子問題去解。設分解閾值 ,且最小子解規模爲1的問題消耗一個單位時間。
設將原問題分解爲k個子問題以及用merge將K個子問題的解合併爲原問題的解需用f(n)個單位時間,用T(n)表示該分治法解規模爲|P|=n的問題所需的計算時間:
We start with a set of puzzles that are all related to recursion and mathematical induction we study in Topic 1. I have chosen interesting but also a bit tough puzzles to get you intrigued, so do not get discouraged if you find them hard.
Tom and his wife Mary went to a party where four more couples were present. 五對夫婦,共十人
Not every one knew every everyone else, so people who did not know each other introduced themselves and shook hands. People that knew each other from before did not shake hands. Later that evening Tom got bored, so he walked around and asked all other guests (including his wife) how many hands they had shaken that evening, and got nine different answers. How many hands did Mary shake? (Hint: you will end up doing recursion on the number of couples…)
1. 總共10我的,每一個人不與本身握手,不與配偶握手,不與同一我的握超過一次手,因此每一個人最多握8次手,最少0次;
2. Mr.Smith問其它9我的握了幾回手,各人回答不同,因此每一個人的握手次數恰好爲0-8次,每種不一樣次數有1我的;
3. 有且只有一我的握了8次手,稱之爲A,即A與其配偶之外的全部人都握了手;【最屢次】
4. 記A的配偶爲a,除了A夫婦之外,全部人都至少握了1次手(和A),因此握手0次的確定是a;【最少次】
5. 從10我的中去掉A夫婦,由於A與其他每一個人握了1次手,而a沒有與別人握手,因此去掉A夫婦後,其它人的握手次數爲1-7(不算Mr.Smith),再去掉他 們各自與A握的那次手不算,則各人的握手次數爲0-6,仍是每種不一樣次數恰好有1我的;
6. 重複第3-5步4次,直到去掉4對夫婦,最終剩下Mr.&Mrs.Smith,這時Mrs.Smith的握手次數爲0,加上4次循環中去掉的4次握手,她總共握 了4次手,與每對夫婦中的某一位各握了一次。
Here is an 「ancient」 small puzzle:
Two thieves have robbed a warehouse and have to split a large pile of various items, without prices on them. How do they do this in a way that each thief thinks (believes) that he has got at least one half of the value of the whole pile?
本質:一我的分割,一我的選擇
一個公平的過程每個階段都是公平的。講不一樣的人分配到不一樣的階段,並具有徹底自主權。
The solution is that one of the two thieves splits the pile into two parts such that he thinks that both parts are of equal value.
The other one then chooses what he thinks is the better part.
It is easy to see that both thieves have reason to believe that they got at least a half (try to explain why).
Now here is the real puzzle for you to solve: Assume that ten thieves have robbed the warehouse. How do they split the pile of items so that each thief thinks that he has got at least one tenth of the total value of the pile? (Hint: This is quite a tough one. It is an example of a nested recursion (a recursion within a recursion)).
(a) We are given 27 coins of the same denomination; we know that one of them is counterfeit and that it is lighter than the others. Find the counterfeit coin by weighing coins on a pan balance only three times.
(b) We are given 12 coins and one of them is a fake but we do not know if it is heavier or lighter. Can you determine which one is a fake and if it is lighter or heavier by weighing coins on a pan balance three times only? ((a) and (b) are perfect examples of divide-and-conquer technique).
(c) We have 9 coins and three of them are heavier than the remaining six. Can you find the heavier coins by weighing coins on a pan balance only four times? (Hint: this is an example of the lower bound estimation of complexity of algorithms, i.e., of the minimal number of steps needed to execute an algorithm for a given input).
Answer:
(a) 9 + 9 + 9【分"三份"】
(b) 4 + 4 + 4
(c)
(a) Assume you are given a block of chocolate consisting of m by n squares. At each move you can break one piece into two (not necessarily equal) pieces (see the picture below). The goal is to get m × n separate squares. What is the least number of moves needed to achieve this goal and how should one do it?
(b) Assume now that you can put several pieces of chocolate on top of each other and break them in a single move. What is now the least number of moves needed to get m × n separate squares? (Hint: this is an example of estimating complexity of algorithms, i.e., the number of steps needed to execute an algorithm for a given input)
(a) There are five pirates who have to split 100 bars of gold. They all line up and proceed as follows:
i) The first pirate in line gets to propose a way to split up the gold (for example: everyone gets 20 bars)
ii) The pirates, including the one who proposed, vote on whether to accept the proposal. If the proposal is rejected, the prate who made the proposal is killed.
iii) The next pirate in line then makes his proposal, and the 4 pirates vote again. If the vote is tied (2 vs 2) then the proposing pirate is still killed. Only majority can accept a proposal. The process continues until a proposal is accepted or there is only one pirate left. Assume that every pirate :
Question : What proposal should the first pirate make ?
(b) Assume now there are 10 pirates splitting 1000 pieces of gold. What should the first pirate propose ?
(An interesting puzzle - recursion seems to be the ONLY way to solve it !!!)
In Elbonia all cities have a circular one-way highway around the city (in blue on the map below). All streets in the cities are one-way, and they all start and end on the circular highway (see the map). A block is a part of the city that is not intersected by any street. Design an algorithm that, given a map of a city, finds a block that can be circumnavigated while respecting all one-way signs. For example, the green block has such property, but the red one does not. What is the best possible expected (i.e., average) asymptotic run time of such an algorithm? (Again a recursion, but estimating the expected run time is hard…)
Answer:
Walk through the path and mark the areas around - clockwise or anticlockwise.
Or,
the problem became "How to find a smallest cycle in the Directed Graph".
避免那個小方塊的漏洞的狀況下,如何放積木擺滿棋盤。
Tricky: 2^n-1 mod 3 = 0
這個必然也必須的小漏洞分佈各個子方格的位置,如上圖技巧所示。Divide And Conquer的思想。
01. 找數組中的 duplicated/missing value。 | http://stackoverflow.com/questions/3492302/easy-interview-question-got-harder-given-numbers-1-100-find-the-missing-numbe |
02. Party找名人 | http://math.stackexchange.com/questions/847371/celebrity-problem-discrete-math |
03. 博客 - 面試題型總結 | http://blog.csdn.net/Hackbuteer1/article/category/899947 |
04. Big-O Cheat Sheet | Big-O Cheat Sheet |
05. linkcode 面試題 | https://www.lintcode.com/zh-cn/problem/# |
06. 博客 - 常見排序算法總結 | http://blog.csdn.net/speedme/article/details/23021467 |
07. 基數排序的逐步優化(與快排比較) | http://blog.csdn.net/yutianzuijin/article/details/22876017 |
08. 大數據算法面試題 | http://blog.csdn.net/v_july_v/article/details/6279498 |
09. linkcode 面試題 (盜版來源) | https://leetcode.com/problemset/algorithms/ |