- 浏览: 1094475 次
文章分类
- 全部博客 (379)
- S2SH (16)
- stuts2 (0)
- java语言 (81)
- JSP (17)
- <html>元素 (11)
- javaweb (4)
- web容器 (3)
- ext (23)
- javaScript (48)
- ant (1)
- liferay (1)
- sql (9)
- css (42)
- 浏览器设置 (3)
- office_world (1)
- eclipse (4)
- 其它 (28)
- 操作系统 (5)
- android (6)
- Struts2 (11)
- RegEx (3)
- mysql (5)
- BigDATA (1)
- Node.js (1)
- Algorithm (10)
- Apache Spark (1)
- 数据库 (5)
- linux (2)
- git (1)
- Adobe (3)
- java语言,WebSocket (1)
- Maven (3)
- SHELL (1)
- XML (2)
- 数学 (2)
- Python (2)
- Java_mysql (1)
- ReactJS (6)
- 养生 (4)
- Docker (1)
- Protocols (3)
- java8 (2)
- 书籍 (1)
- Gradle (2)
- AngularJS (5)
- SpringMVC (2)
- SOAP (1)
- BootstrapCSS (1)
- HTTP协议 (1)
- OAuth2 (1)
最新评论
-
Lixh1986:
Java并发编程:自己动手写一把可重入锁https://blo ...
Java之多线程之Lock与Condition -
Lixh1986:
http://win.51apps.com.cn/https: ...
temp -
ztwsl:
不错,支持很好
HttpServletRequest和ServletRequest的区别 -
guodongkai:
谢谢您能将知识精华汇编总结,让初学者们从原理中学会和提高。
javaScript之function定义 -
kangwen23:
谢谢了,顶顶
struts2中的ValueStack学习
经典排列组合与动态规划题
一、原题:
解法一
解法二
解法三
解法四
二、增加些难度
-
一、原题:
// https://leetcode.com/problems/unique-paths/ /* A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below). How many possible unique paths are there? +---+---+---+---+---+---+---+---+ |ROB| | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | | | +---+---+---+---+---+---+---+---+ | | | | | | | |end| +---+---+---+---+---+---+---+---+ */
解法一
/* // https://discuss.leetcode.com/topic/5623 // java-dp-solution-with-complexity-o-n-m The assumptions are: - When (n==0||m==0) the function always returns 1 since the robot can't go left or up. - For all other cells. The result = uniquePaths(m-1,n) + uniquePaths(m,n-1) Therefore I populated the edges with 1 first and use DP to get the full 2-D array. */ /* +----+----+----+----+----+----+----+----+ | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | +----+----+----+----+----+----+----+----+ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | +----+----+----+----+----+----+----+----+ | 1 | 3 | 6 | 10 | 15 | 21 | 28 | 36 | +----+----+----+----+----+----+----+----+ | 1 | 4 | 10 | 20 | 35 | 56 | 84 |120 | +----+----+----+----+----+----+----+----+ */ // Time complexity: O(n^2) public int uniquePaths(int m, int n) { int[][] map = new int[m][n]; for(int i = 0; i < m; i++){ map[i][0] = 1; } for(int j = 0; j < n; j++){ map[0][j] = 1; } for(int i = 1; i < m; i++){ for(int j = 1; j < n; j++){ map[i][j] = map[i-1][j] + map[i][j-1]; } } return map[m-1][n-1]; // the finish star. }
解法二
// Time complexity: O(n^2) public int uniquePaths(int m, int n) { int[]dp = new int[n]; dp[0] = 1; for(int i = 0; i < m; i++) for(int j = 1; j < n; j++) dp[j] = dp[j] + dp[j-1]; return dp[n-1]; // the finish star. }
解法三
// Time complexity: O(n) /* https://discuss.leetcode.com/topic/52660 // java-0ms-solution-with-explanations When we solve this problem, we should keep in mind that this is a permutation and combination problem of high school level. Therefore, we need not to use DP solution or recursive solution. Given m and n, there will be m+n-2 steps. Among these m+n-2 steps, n-1 steps are towards right and m-1 steps are towards down. the question is changed to: Select m-1 steps from m+n-2 steps. So, there will be (m-1)C(m+n-2) solutions, which is the same as (n-1)C(m+n-2). All we need is to write a program quickly calculating (m-1)C(m+n-2) or (n-1)C(m+n-2). */ public int uniquePaths(int m, int n) { long result = 1; int steps = m + n - 2; for(int i=0; i < Math.min(m-1,n-1); i++) result = result * (steps - i) / (i + 1); return (int)result; } /* 注意: result = result * (steps - i) / (i + 1); 不要写成 result *= (steps - i) / (i + 1); 的形式,否则后者在做除法时丢位导致结果错误。 */
解法四
// Time complexity: O(n) /* https://discuss.leetcode.com/topic/52660 // java-0ms-solution-with-explanations https://discuss.leetcode.com/topic/31724 // java-solution-0ms-4lines When we solve this problem, we should keep in mind that this is a permutation and combination problem of high school level. Therefore, we need not to use DP solution or recursive solution. Given m and n, there will be m+n-2 steps. Among these m+n-2 steps, n-1 steps are towards right and m-1 steps are towards down. the question is converted to: Select m-1 steps from m+n-2 steps. So, there will be (m-1)C(m+n-2) solutions, which is the same as (n-1)C(m+n-2). All we need is to write a program quickly calculating (m-1)C(m+n-2) or (n-1)C(m+n-2). */ /* 基础知识:(从 n 个数中 取出 m 个数) 排列公式: result = n!/(n-m)! 组合公式: result = n!/(n-m)!/m! ------------------------------------------------------ 该题是一个组合题: 7, 3 steps = 8 n = 3 result = 8!/(3!*(8-3)!) 8 7 6 5 4 3 2 - - - * - - - - 3 2 1 5 4 3 2 */ public int uniquePaths(int m, int n) { int steps = m + n - 2; int min = Math.min(m - 1, n - 1); long sum = 1, sub = 1; for(int i = 0; i < min; i++){ sum *= (steps - i); sub *= (min - i); } return (int)(sum/sub); } /* 注意:这种解法乘完再除,相乘的结果可能会造成溢位,而出错。 所以只适合数比较小的情况。 */
二、增加些难度
/* https://leetcode.com/problems/unique-paths-ii Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middle of a 3x3 grid as illustrated below. [ [0,0,0], [0,1,0], [0,0,0] ] The total number of unique paths is 2. Note: m and n will be at most 100. */ public int uniquePathsWithObstacles(int[][] obstacleGrid) { int width = obstacleGrid[0].length; int[] dp = new int[width]; dp[0] = 1; for (int[] row : obstacleGrid) { for (int j = 0; j < width; j++) { if (row[j] == 1) dp[j] = 0; else if (j > 0) dp[j] += dp[j - 1]; } } return dp[width - 1]; }
-
发表评论
-
TOPK
2019-03-03 13:11 594实现思路: TopN算法:从已经存在的数组中,找出最大(或最 ... -
二分查找有序数组并插入(不能解决TopK问题)
2019-03-03 11:00 995需求:将一个数插入(替换原来的数)到一个有续的数组中,插入成功 ... -
Algorithm之3sum closest及负数在Java中的表示
2017-01-23 14:41 849由一道算法题想到的 1 ... -
Algorithm之二分治应用之pow(x,n)
2017-01-23 08:36 975设计算法,求 x 的 n 次幂:pow(x, n) 算法一 ... -
Algorithm之排序之堆排序(Heap Sort)
2017-01-20 16:05 4249Algorithm之排序之堆排序 ... -
Algorithm之排序之归并排序
2017-01-18 20:54 686Algorithm之排序之归并排序 一、归并介绍 动画1: ... -
Algorithm之动态规划之最佳买股票时机
2017-01-16 14:22 2664动态规划(DP: Dynamic Programming) ... -
Algorithm之二分枚举之copy books
2017-01-13 16:29 1572copy books Before the inventio ... -
数据库之索引(Index)
2016-12-03 12:04 8707在数据之外,数据库系统还维护着满足特定查找算法的数据结构。 这 ...
相关推荐
Priority-Based Genetic Algorithm for Shortest Path Routing Problem in OSPF 主要介绍了遗传算法求解最短路径问题中的基于优先级的编码。这种编码方式可以很有效地解决图的最短路径等问题。
知识图谱推理方向的基础算法Path Ranking Algorithm详解
robot motion planning,A star algorithm
关于蚁群算法的机器人路径规划SCI期刊论文,融合了人工势场法,及几何法,对新手有帮助,结合经典的蚁群算法和人工势场法,
A Multi-stage Probabilistic Algorithm for Dynamic Path-Planning
An improved ant colony algorithm for robot path planning.rar
sqrt((x2-x0)^2+(y2-y0)^2)-sqrt((x1-x0)^2+(y1-y0)^2)=t
Algorithm-ngraph.path.zip,图中的路径查找,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
A* path finding algorithm in Javascript
本代码 利用 Dijkstra's Shortest Path Algorithm 求解有向图的最短路径。 包括 图的构建,求解过程的,排序使用的最小堆 等所有的源代码,并包括测试用例。 是学习最小堆 和 Dijkstra's Shortest Path Algorithm ...
A GRASP Path Relinking algorithm 2d and 3d bin packing
C语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件 algorithmC语言头文件...
Algorithm-algorithm.zip,这句话的意思是,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
Algorithm2e is an environment for writing algorithms in LATEX2e. An algorithm is defined as a floating object like figures. It provides macros that allow you to create different sorts of key words, ...
这些源代码被编码来解决约束设计问题。 只有四个问题。 此版本是以下研究的一部分: Yapici H,Cetinkaya NA 新的元启发式优化器:探路者算法。 应用软计算。 2019。
在线Demo: http://parkjs814.github.io/AlgorithmVisualizer算法目录层次结构 Path 描述/algorithm/category.json 此文件包含类别及其算法的列表。/algorithm/[category]/[algorithm]/desc.json该文件包含该算法...
path_ranking_algorithm调研报告.doc
烟花算法程序大全Fireworks Algorithm
1.河内之塔 2.Algorithm Gossip: 费式数列 3. 巴斯卡三角形 4.Algorithm Gossip: 三色棋 5.Algorithm Gossip: 老鼠走迷官 6.Algorithm Gossip: 老鼠走迷官(二) 7.Algorithm Gossip: 骑士走棋盘 8.Algorithm Gossip...