`
jgsj
  • 浏览: 962874 次
文章分类
社区版块
存档分类
最新评论

LeetCode Unique Paths

 
阅读更多

Unique Paths

A robot is located at the top-left corner of amxngrid (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?


Above is a 3 x 7 grid. How many possible unique paths are there?

Note:mandnwill be at most 100.

这里Note:的含义是如果数值过大会溢出的。

这道题对我来说是很新颖的了,因为我之前没见过。但是其实解法却是非常老套的,我一开始居然没想到,所以总结了一下:


方法论总结:
遇上新颖没见过的题目:
1) 查看其特征;

2) 搜索大脑有什么熟悉的题目,可以套的,或者相似的可以退到出来的;

3) 罗列出来这些题目:全排列, 水槽,TwoSum,等等;

4) 搜索大脑里面有什么熟悉的算法;

5) 罗列出来:动态规划法,递归,回溯,二叉树遍历,贪心法,递归树,分治法,观察特征计算法,等等,肯定有可以套的算法的!

6)最后选定算法解题;

7)没有优化的算法,可以使用暴力法,先解决再说!


去掉无用信息,错误解法,一定要把头脑中的一团乱麻理清,整理出逻辑来!
最后实在没招了,想办法让人提示,主要是提示用什么算法,能给个思路更好了,O(∩_∩)O~


在leetcode论坛上的找了提示,一下子就很多思路了。而且写出的程序也很简单。
这道题是属于思想难,但是程序容易的题目。

下面是经验证结果是正确的回溯法,但是超时。
Leetcode上的有回溯法程序需要5行代码,其实不用,我这里只需要2行,O(∩_∩)O~
int uniquePathsBackTrack(int m, int n) {
		if(m==1 || n==1) return 1;
		return uniquePaths(m-1, n) + uniquePaths(m, n-1);
	}

动态规划法:

int uniquePaths(int m, int n) 
	{
		vector<vector<int> > table(m, vector<int>(n, 1));
		for (int i = 1; i < m; i++)
		{
			for (int j = 1; j < n; j++)
			{
				table[i][j] = table[i-1][j] + table[i][j-1];
			}
		}
		return table[m-1][n-1];
	}

省内存的动态规划法:
//Save space
	int uniquePaths2(int m, int n)
	{
		vector<int> table(n, 1);
		for (int i = 1; i < m; i++)
		{
			for (int j = 1; j < n; j++)
			{
				table[j] += table[j-1];
			}
		}
		return table[n-1];
	}
可以进一步提高一点时间效率:
//Improve efficency further
	int uniquePaths3(int m, int n)
	{
		int a = min(m,n);
		int b = max(m,n);
		vector<int> table(b, 1);
		for (int i = 1; i < a; i++)
		{
			table[i] *= 2; //n<m的时候会出错,所以要用ab处理一下
			for (int j = i + 1; j < b; j++)
			{
				table[j] += table[j-1];
			}
		}
		return table[b-1];
	}

//2014-2-2 update
	int uniquePaths(int m, int n)
	{
		if (n == 1 || m == 1) return 1;

		int *A = new int[n];
		for (int i = 1; i <= n; i++)
		{
			A[i-1] = i;//i-1写出i会造成下标超范围
		}

		for (int i = 2; i < m; i++)
		{
			for (int j = 1; j < n; j++)
			{
				A[j] += A[j-1];
			}
		}
		return A[n-1];
	}





分享到:
评论

相关推荐

    pcw0118#ZXBlog#LeetCode-62.Unique Paths(不同的路径数量)(简单dp)1

    1、记忆化 2、二维dp 3、滚动优化

    leetcode分类-LeetCode:力码

    leetcode 分类 LeetCode Progress 128/154 Other Solutions C++,有详细思路解释 python,部分有解释 Java,部分有解释 Java Associated Documents and Resources Peter norvig神牛Python代码写的很飘逸,果然是有LISP...

    gasstationleetcode-leetcode:LeetcodeOJ解决方案

    leetcode 【演示记录】 报告 展示 2017/03/06 1.二和,167.二和二 2107/03/06 15.3 总和,16.3 总和最近,18.4 总和,11.最多水的容器 2017/03/09 62.Unique Paths, 63.Unique Paths II, 64.Minimum Path Sum 2017/...

    leetcode答案-leetcode:leetcode

    leetcode 答案 leetcode 08/18 Unique Paths 应该是简单的数学排列组合问题,提炼一下其实就一句话:有m个黑球,n个白球,有多少种不同的排列方式。 我数学太差,没找到答案,直接上了动态规划。 Unique Paths II ...

    leetcode1477-coding-for-fun:编码乐趣

    62.unique-paths.cpp [TODO] 5. 最长回文子串.cpp 第 3 天: [TODO] 96.unique-binary-search-trees.cpp [TODO] 70. 爬楼梯.cpp [TODO] 746. min-cost-climbing-stairs.cpp 第 4 天: [待办事项] leetcode/1143。 ...

    leetcode棋盘-Algorithms:通用算法实现

    unique_paths.py(类似于 chess_board.c!)+ unique_paths2.py chess_board.c 效率测试 bin_search.py 素数.py 零知识.py Leetcode:现在很多小方案都是leetcode,我主要是为了好玩。 著名算法:Knuth-Morris-Pratt...

    扩展矩阵leetcode-Leetcode:LeetcodeAnswer-Java

    扩展矩阵leetcode ...uniquePaths 63.不同路径II uniquePathsWithObstacles 139.单词拆分 wordBreak 279.完全平方数 numSquares 排序 56.合并区间 merge 75.颜色分类 sortColors 179.最大数 largestNumber 324.摆

    leetcode叫数-Leetcode_JS:leetcode编程题,javascript版本

    Unique Paths 这道题是一道动态规划的题,还算简单。状态转移方程为: path[i][j] = path[i-1][j] + path[i][j-1]; i和j表示的是i*j的网格从左上角到右下角的路径数量。 并且初始的时候path[i][j]都为1。 为方便起见...

    lrucacheleetcode-LeetcodeSolution:Leetcode刷题的分类和答案记录

    63.Unique Paths II 64.最小路径和 70.爬楼梯 72.编辑距离 122. 买卖股票的最佳时机 II 152.最大积子阵列 198.强盗 322.硬币变化 贪心算法 45.跳跃游戏II 55.跳跃游戏 哈希 1.二和 3.最长不重复字符的子串 49.组字谜...

    leetcode530-Leetcode:新的开始

    leetcode 530 力码 全部的: 易(173/237+x) 中(144/437+x) 硬(4/x) 问题 1.Two Sum(dict) 7.(跳过)(数学) 9.(跳过)(串串技巧) 11.盛水最多的容器 12.(跳过)(问题不好) 13.(跳过)(蛮力) 14.(跳过)...

    leetcode算法题主函数如何写-visual-your-algo:网站暂停升级中

    uniquePaths = function(m, n) { let dp = new Array(m).fill(0).map(() =&gt; new Array(n)); // dp[r][c] represents the number of possible paths from row = 0, col = 0 to row = r, col = c for (let row = 0; ...

    leetcode正方体堆叠-TakeUforward-SDE_180:TakeUforward-SDE_180

    leetcode正方体收藏TakeUforward-...Unique Paths Reverse Pairs (Leetcode) 从 GFG 中通过拼图(自行搜索) Day4: (Hashing) 2 Sum 问题 4 Sum 问题 Longest Consecutive Sequence Longest Subarray with 0 sum 给定

    cpp-算法精粹

    Unique Paths II N-Queens N-Queens II Restore IP Addresses Combination Sum Combination Sum II Combination Sum III Generate Parentheses Sudoku Solver Word Search 总结 分治法 Pow(x,n) Sqrt(x) 贪心法 Jump...

Global site tag (gtag.js) - Google Analytics