This is from book Cracking the coding interview, Gayle Laakmann Mcdowell. The flowchart can be used when you have a tech interview, coding contest or even make project. It reveals a creative way of thinking.

There are 7 steps.

1. Listen

2. Example

3. Brute Force

4. Optimize: BUD Optimization: Bottlenecks, Unnecessary works, Duplicated Work

5. Walk Through

6. Implement

7. Test

For detailed info, please refer the book

Give a high level summary on how to resolve algorithm puzzles. 

i. String, Array puzzles

Sorting

Some puzzles looks like hard, but it will be easier if you sort the array or string.

HashTable

Some puzzles can be resovled with brute-force with low efficiency. It will tremedously increase the efficiency with low space consume if HashTable be used, which is to map array or string to a alphabet[0]*26 array.

for Python programmer, the built-in data structure Dictionary is suible for making HashTable

Each key-value pair at Dict is seperated by colon(:), each pair eletment is seperated by comma(,), all Dict is included in {}, as follow shown:

d = {key1 : value1, key2 : value2 }

Key must be unique, value is not necessary

More detail:  http://www.runoob.com/python/python-dictionary.html

leetcode 387, 567

Double index

Some puzzles are easy but it will become hard if it require in-place operation. In this situation, double index will help resolve the puzzle. One index work ahead to traverse origin array while another index work behind to modify original array to generate target one.

leetcode 443, 48

Pay attention to index out bounds

Always pay heavy attention to index out bounds issue when using FOR LOOP, pay attention to edge condition of the FOR LOOP.

ii. Linked List

Multi-pointers

Kind like the the double index mentioned above, define multiple pointers to manipulate the linked list. Pay attention to the next pointer of nodes

Create an additional Head Node before Head

For some puzzle, in order to avoid different process on head node, usually create an additional Head Node before Head.

--------------------------------------我是分隔线--------------------下面是中文-----------------------

这个解题思路是Gayle Laakmann Mcdowell在她的书中 Cracking the coding interview说的.我们能在开发岗位技术面试,一些编程比赛,甚至在工作中都能用到这种思路.它给出了一种创造性思考的方式.

总共分七步.

1. 倾听

仔细听(如果是在面试中,面试管会口述问题),或者仔细看问题描述.包括一些细节比如数据的类型,输入数据的范围,一些约束条件等等

2. 举例子

当充分理解题目之后,可以画一些图或者举一些简单的例子来形象化问题.

3. 暴力穷举

先尽快用暴力穷举的方式相出解决方案,但是先不急着写代码.这是下一步优化的基础.

4. 优化

使用BUD原则来优化,B是指Bottlenecks,U是指Unnecessary Work,D是指Duplicated Work.

B具体来讲是找到算法中耗时最大的部分,然后对它优化,如果你优化的对象不是耗时最大的部分,那么即使能把这部分优化为O(1)的算法,整体上还是很慢.

U是指根据题目的描述,限制,有些计算是没有必要去做的,及时去掉这些无用功的计算.

D是指重复计算的部分,在动态规划中这个问题最明显.

5. 走一边

现在有了优化的算法了,在写代码之前脑海里或者纸上再过一遍算法

6. 写代码

目标是写漂亮的代码,可以模块化代码等让代码清晰,可读

7. 测试

首先进行算法上的测试,也就是用思路再走一遍代码,看有没有遗漏什么部分没?

接着对一些数学或者可能出现NULL的地方仔细检查下.

再是做small test cases,最后做special cases and edge cases.

下面就一些算法问题做个高度概括性的总结

i. 字符串,数组相关问题

排序

有些问题看起来很难解决,但是当对数组排序后,问题就好解决多了.

HashTable

有些问题用brute-force,效率很低,使用HashTable,把字符按照映射到字符数组alphabet[0]*26,能用较小的空间提高效率

对于使用python的程序员,在做HashTable的时候,要善于使用Python的特殊数据结构--“字典(Dictionary)”

字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下所示:

d = {key1 : value1, key2 : value2 }

键必须是唯一的,但值则不必。

值可以取任何数据类型,但键必须是不可变的,如字符串,数字或元组。

具体请参考 http://www.runoob.com/python/python-dictionary.html

leetcode 387, 567

双下标

有些问题需要做in-place的变化,可以使用双下标,一个下标走原始数组,一个下标跟在后边,修改原材数组成为新数组

leetcode 443, 48

注意下标越界

在做循环的时候,永远要留意边界是否越界

ii. 链表相关问题

多指针

有点像上面的双下标,按照问题要求可以多设几个指针,来进行对整个链表的操作,操作过程中注意next的指向。

Head前再加个Head Node

对有些问题,为了是head节点不需要特殊化处理,可以在head前面新建一个node,再进行操作。

 

A Problem-Solving FlowChart || 如何解决编程问题的更多相关文章

  1. 【BZOJ】1700: [Usaco2007 Jan]Problem Solving 解题

    [题意]给定n道题,每月末发放工资m,要求从1解到n,每道题需要在当月初付费ai,下月初付费bi,多道题可以安排在同月,求最少月数. [算法]DP [题解]参考自:[bzoj1700]Problem ...

  2. 【BZOJ1700】[Usaco2007 Jan]Problem Solving 解题 动态规划

    [BZOJ1700][Usaco2007 Jan]Problem Solving 解题 Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地 ...

  3. KXO151 Programming & Problem Solving

    Page 1 of 9KXO151 Programming & Problem SolvingAIEN-SOU - 2019Assignment 2Deadline for Submissio ...

  4. 学习笔记之Problem Solving with Algorithms and Data Structures using Python

    Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...

  5. bzoj 1700 Problem Solving 解题 dp

    [Usaco2007 Jan]Problem Solving 解题 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 492  Solved: 288[Sub ...

  6. AST11103 Problem Solving

    AST11103 Problem Solving with Programming SkillsAdditional Individual Assignment: Min-Game Programmi ...

  7. BZOJ 1700 [Usaco2007 Jan]Problem Solving 解题(单调DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1700 [题目大意] 共有p道题目要做,每个月收入只有n元,用于付钱做题之外的部分都会吃 ...

  8. 【一周读书】All life is problem solving

    书籍:<开放的智力> 采铜是我在知乎关注最早的大V之一,那时我脑里有一大堆疑惑和问题,是他的答案帮助我理清了思绪.我从他身上学习到对书籍的爱好,对思维方法的关注,对智慧的向往.读这本小集子 ...

  9. poj 3265 Problem Solving dp

    这个题目容易让人误以为是贪心就可以解决了,但是细想一下很容易举出反例. dp[i][j]表示解决了i个问题,最后一个月解决的问题数目. #include <iostream> #inclu ...

随机推荐

  1. 201421123042 《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 继承中的关键词:Soper,object,override,project, 1.2 尝试使用思维导图将这些关键词组织起来.注: ...

  2. python之路--day15---软件开发目录规范

    软件开发目录规范 bin--启动文件 conf--配置文件 core--核心代码 db--数据文件 lib--常用功能代码 log--日志文件 readme--软件介绍

  3. js的构造函数共用事例

    在使用构造函数去实现一种功能时,我们有时候往往需要实现这个功能,会因此产生多个堆内对象.这样就会造成堆内存滥用.占用不该占用的空间.为此我们可以利用函数把共用的内容封装起来.放便我们的使用.很多东西其 ...

  4. Spring入门(3-1)Spring的标签命名空间

    1.标签命名空间声明: 2.标签命名空间使用 标签默认的命名空间是 security:,可以不用带 security:,直接写标签,如: <http  <authentication-ma ...

  5. 上传视频使用ffmpeg自动截取缩略图

    上传视频之后,有的需要显示缩略图,而不是仅仅显示视频名称的列表,这时候就需要对上传的视频截取缩略图. 简单粗暴点,将以下代码作为工具类复制粘贴即可: package com.util; import ...

  6. SpringMVC(三):@RequestMapping中的URL中设定通配符,可以使用@PathVariable映射URL绑定的占位符

    1)带占位符的URL是Spring3.0新增的功能,该功能在SpringMVC向REST目标挺进发展过程中具有里程碑的意义. 2)通过@PathVariable可以将URL中占位符参数绑定到控制器处理 ...

  7. PHP实现统计在线人数功能示例

    本文实例讲述了PHP实现统计在线人数的方法.分享给大家供大家参考,具体如下: 我记得ASP里面统计在线人数用application 这个对象就可以了.PHP怎么设计? PHP对session对象的封装 ...

  8. Spring(3)——装配 Spring Bean 详解

    装配 Bean 的概述 前面已经介绍了 Spring IoC 的理念和设计,这一篇文章将介绍的是如何将自己开发的 Bean 装配到 Spring IoC 容器中. 大部分场景下,我们都会使用 Appl ...

  9. 1020关于MYCAT的安装和使用总结

    第一部分 读写分离配置 转自:http://www.51testing.com/html/34/369434-3686088.html 使用Mycat 做简单的读写分离(一) 原本使用的是amoeba ...

  10. 初级 Java 的 3 本进阶书

    1.Head First设计模式 这是我看过最幽默最搞笑最亲切同时又让我收获巨大的技术书籍!深入浅出,娓娓道来,有的地方能笑死你! 翻开一看,真如Erich Camma所说,简直欲罢不能.本书是Ore ...