闲下来后,需要讲最近涉及到的算法全部整理一下,有个indice,方便记忆宫殿的查找

MIT的算法课,地球上最好: Design and Analysis of Algorithms

本篇需要重新整理,按照以下归类去理解”思想“,而非”算法细节“。

找到解决问题的”思维突破口“。

“贪心算法” 算是 "动态规划" 的前置课程。

在数据结构graph中的优化问题也大量涉及到了”Greedy Method"。

也有五大常用算法之说:算法设计之五大常用算法设计方法总结

一、【分治法】

二、【动态规划法】

三、【贪心算法】

四、【回溯法】

五、【分支限界法】

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.

  1. 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. (递归,数学推导)
  2. 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.
  3. Analytical thinking puzzles   A mix  of various puzzles for developing creative thinking and your analytical ability.

分治思想


经典算法思想——分治(Divide-and-Conquer)

基本步骤

  • 分解:将原问题分解为若干个规模较小,相互独立,与原问题形式相同的子问题
  • 解决:若子问题规模较小而容易被解决则直接解,否则递归地解各个子问题
  • 合并:将各个子问题的解合并为原问题的解

复杂性分析

一个分治法将规模为n的问题分成k个规模为n/m的子问题去解。设分解阈值  ,且最小子解规模为1的问题消耗一个单位时间。

设将原问题分解为k个子问题以及用merge将K个子问题的解合并为原问题的解需用f(n)个单位时间,用T(n)表示该分治法解规模为|P|=n的问题所需的计算时间: 

求解的一些经典问题

  1. 二分搜索
  2. 大整数乘法
  3. Strassen矩阵乘法
  4. 棋盘覆盖
  5. 合并排序
  6. 快速排序
  7. 线性时间选择
  8. 最接近点对问题
  9. 循环赛日程表
  10. 汉诺塔

分治练习


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.

1.1. The Party Problem

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…)

Answer:

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次手,与每对夫妇中的某一位各握了一次。

1.2. The Ten Thieves Problem

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?

You might want to try to solve this puzzle before reading further …

本质:一个人分割,一个人选择

一个公平的过程每一个阶段都是公平的。讲不同的人分配到不同的阶段,并具备完全自主权。

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 solveAssume 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)).

1.3. Finding the False Coins

(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)

1.4. Breaking a chocolate

(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)

1.5. The Five Pirates

(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 !!!)

1.6. One Way Streets

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".

1.7. 俄罗斯方块

避免那个小方块的漏洞的情况下,如何放积木摆满棋盘。

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] 面试题之犄角旮旯 第贰章的更多相关文章

  1. [c++] 面试题之犄角旮旯 第壹章

    记录C/C++语言相关的问题. 算法可视化:https://visualgo.net/en <data structure and algorithm in c++> By Adam 有免 ...

  2. [LeetCode] 面试题之犄角旮旯 第叁章

    题库:LeetCode题库 - 中等难度 习题:网友收集 - zhizhiyu 此处应为一个简单的核心总结,以及练习笔记. 查找一个数“在不在”?桶排序理论上貌似不错. 回文问题 ----> [ ...

  3. [Code] 烧脑之算法模型

    把博客的算法过一遍,我的天呐多得很,爱咋咋地! 未来可考虑下博弈算法. 基本的编程陷阱:[c++] 面试题之犄角旮旯 第壹章[有必要添加Python] 基本的算法思想:[Algorithm] 面试题之 ...

  4. 程序员编程艺术:第三章续、Top K算法问题的实现

    程序员编程艺术:第三章续.Top K算法问题的实现 作者:July,zhouzhenren,yansha.     致谢:微软100题实现组,狂想曲创作组.     时间:2011年05月08日    ...

  5. 从零开始山寨Caffe·拾贰:IO系统(四)

    消费者 回忆:生产者提供产品的接口 在第捌章,IO系统(二)中,生产者DataReader提供了外部消费接口: class DataReader { public: ......... Blockin ...

  6. 转载 ACM训练计划

    leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...

  7. ACM学习

    转:ACM大量习题题库   ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库.   US ...

  8. (转载)ACM训练计划,先过一遍基础再按此拼搏吧!!!!

    ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO ht ...

  9. 牛人的ACM经验 (转)

    一:知识点     数据结构:       1,单,双链表及循环链表       2,树的表示与存储,二叉树(概念,遍历)二叉树的                    应用(二叉排序树,判定树,博弈 ...

随机推荐

  1. 小白学Python(5)——python-pptx简单应用

    python-pptx允许您创建新的演示文稿以及对现有演示文稿进行更改. 实际上,它只允许您对现有演示文稿进行更改:只是,如果您从一个没有幻灯片的演示文稿开始,一开始感觉就像是从头开始创建一个幻灯片. ...

  2. Kafka监控工具汇总

    对于大数据集群来说,监控功能是非常必要的,通过日志判断故障低效,我们需要完整的指标来帮我们管理Kafka集群.本文讨论Kafka的监控以及一些常用的第三方监控工具. 一.Kafka Monitorin ...

  3. net core WebApi——文件分片上传与跨域请求处理

    目录 前言 开始 测试 跨域 小结 @ 前言 在之前整理完一套简单的后台基础工程后,因为业务需要鼓捣了文件上传跟下载,整理完后就迫不及待的想分享出来,希望有用到文件相关操作的朋友可以得到些帮助. 开始 ...

  4. 微服务架构 - 网关 Spring Cloud Gateway

    Spring Cloud Gateway 工作原理 客户端向 Spring Cloud Gateway 发出请求,如果请求与网关程序定义的路由匹配,则将其发送到网关 Web 处理程序,此处理程序运行特 ...

  5. linux系统破解密码。

    Linux系统Centos7及RedHat7破解密码 步骤如下: 1.开机之后按"e"键 2.找到以linux16的开头的行在行尾添加 rd.break console=tty0 ...

  6. 【在 Nervos CKB 上做开发】Nervos CKB脚本编程简介[2]:脚本基础

    CKB脚本编程简介[2]:脚本基础 原文作者:Xuejie 原文链接:Introduction to CKB Script Programming 2: Script 本文译者:Shooter,Jas ...

  7. 90001PS相关操作

    第一章   PS基础操作 1.1 PS界面介绍 (1)界面包含:菜单栏.状态样式栏.工具栏.绘图区域.工作区. (2)布局可以在左上角进行切换,区分主要为工作区的侧重点不同. (3)布局想恢复可在窗口 ...

  8. Java虚拟机详解(七)------虚拟机监控和分析工具(1)——命令行

    通过前面的几篇博客,我们介绍了Java虚拟机的内存分配以及内存回收等理论知识,了解这些知识对于我们在实际生产环境中提高系统的运行效率是有很大的帮助的.但是话又说回来,在实际生产环境中,线上项目正在运行 ...

  9. 完结撒花!129 集 21 个小时,松哥自制的 Spring Boot2 系列视频教程杀青啦!

    松哥的 Spring Boot 教程分为几个阶段. 2016 松哥最早在 2016 年底的时候开始写 Spring Boot 系列的教程,记得当时在广州上班,年底那段时间在深圳出差,在深圳人生地不熟, ...

  10. Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value)

    Leetcode之深度优先搜索(DFS)专题-513. 找树左下角的值(Find Bottom Left Tree Value) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,在树的最后一行找到最 ...