[Algorithm] Warm-up puzzles
闲下来后,需要讲最近涉及到的算法全部整理一下,有个indice,方便记忆宫殿的查找
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.
- Puzzles related to recursion and mathematical induction We start with a set of puzzles that are all related to recursion and mathematical induction. I have chosen interesting but also a bit tough puzzles to get you intrigued, so do not get discouraged if you find them hard. (递归,数学推导)
- Puzzles related to probability Understanding probability theory is necessary for evaluating the behaviour of an algorithm in average (i.e., in finding the expected run time of an algorithm for given probability distribution of inputs). Probability can be tricky and it often teases our “naïve” common sense, as you might experience below.
- Analytical thinking puzzles A mix of various puzzles for developing creative thinking and your analytical ability.
1. Puzzles related to recursion and mathematical induction
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 in 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 a 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 :
- above all wants to live ;
- given that he will be alive he wants to get as much gold as possible;
- given maximal possible amount of gold, he wants to see any other
- pirate killed, just for fun ;
- each pirate knows his exact position in line;
- all of the pirates are excellent puzzle solvers.
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/ | |||
[Algorithm] Warm-up puzzles的更多相关文章
- What are the 10 algorithms one must know in order to solve most algorithm challenges/puzzles?
QUESTION : What are the 10 algorithms one must know in order to solve most algorithm challenges/puzz ...
- 《algorithm puzzles》——谜题
这篇文章开始正式<algorithm puzzles>一书中的解谜之旅了! 狼羊菜过河: 谜题:一个人在河边,带着一匹狼.一只羊.一颗卷心菜.他需要用船将这三样东西运至对岸,然而,这艘船空 ...
- 《algorithm puzzles》——概述
这个专题我们开始对<algorithm puzzles>一书的学习,这本书是一本谜题集,包括一些数学与计算机起源性的古典命题和一些比较新颖的谜题,序章的几句话非常好,在这里做简单的摘录. ...
- codeforces A. Puzzles 解题报告
题目链接:http://codeforces.com/problemset/problem/337/A 题意:有n个学生,m块puzzles,选出n块puzzles,但是需要满足这n块puzzles里 ...
- HDU 4612 Warm up tarjan缩环+求最长链
Warm up Problem Description N planets are connected by M bidirectional channels that allow instant ...
- HDU 4612 Warm up(2013多校2 1002 双连通分量)
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
- HDU 4619 Warm up 2(2013多校2 1009 二分匹配)
Warm up 2 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total S ...
- codeforces 377A. Puzzles 水题
A. Puzzles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/33 ...
- hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
随机推荐
- Saltstack 安装 命令
主机规划 10.0.0.21 master 10.0.0.22 minion 10.0.0.23 minion 基础环境 [root@10.0.0.21 ~]# cat /etc/redhat-rel ...
- 设置Tab键缩进2字符
默认是1.74cm,是1.5字符. 论文是4个空格显示为4个点,或者全角下2个空格,显示为2个空格框.看上方的首航缩进标志是否在2. 看图 在段落 格式里面进行设置! 点上图的制表位 打开下图 选默认 ...
- api日常总结
异步加载JS和CSS <script type="text/javascript"> (function () { var s = document.createEle ...
- Shutting down CodePlex 03/31/2017
Almost 11 years after we created CodePlex, it’s time to say goodbye. We launched CodePlex in 2006 b ...
- iOS: lame框架将PCM录音转成MP3格式
lame框架将PCM录音转成MP3格式 1.lame下载地址:https://github.com/rbrito/lame,它是一个不可执行的文件,需要借助build-lame.sh脚本将其编译成.a ...
- ubuntu crontab 在时间段内随机执行一次
crontab 在linux下做定时任务的命令, 1. 基本格式 * * * * * cmd 第一个表示:分钟 1-59, 每分钟用 */1 第二个表示:小时 023 第三个表示:日期1-31 第四个 ...
- WIN8 Metro UI 风格下的微软报表开发与设计 Metro UI SSRS - BIWORK
开篇介绍 作为 BI 系统前端展现的报表,其重要性不言而喻,我们对于一个好的报表的要求也无非主要包含以下几点: 1. 数据完整和正确,数据质量没有问题 2. 友好的清晰的界面,整洁美观,有得体的格式 ...
- 关于redis性能问题分析和优化
一.如何查看Redis性能 info命令输出的数据可以分为10个分类,分别是: server,clients,memory,persistence,stats,replication,cpu,comm ...
- Sort_Buffer_Size 设置对服务器性能的影响
基础知识: 1. Sort_Buffer_Size 是一个connection级参数,在每个connection第一次需要使用这个buffer的时候,一次性分配设置的内存.2. Sort_Buffer ...
- 【SqlServer】SqlServer中的更新锁(UPDLOCK)
UPDLOCK.UPDLOCK 的优点是允许您读取数据(不阻塞其它事务)并在以后更新数据,同时确保自从上次读取数据后数据没有被更改.当我们用UPDLOCK来读取记录时可以对取到的记录加上更新锁,从而加 ...