LeetCode 62/63/120/64 Unique PathsI/II Triangle/Min sum Path/Rectangle Area--DP
来源:互联网 发布:c4d r18 for mac下载 编辑:程序博客网 时间:2024/06/13 10:43
一:unique Path
题目:
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?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
链接:https://leetcode.com/problems/unique-paths/
分析:此题很明显是动态规划问题,用F[m][n],其中F[i][j]表示在(i,j)位置时的最大方案数,他就等于F[i+1][j]+F[i][j+1].
代码:
class Solution {public: int uniquePaths(int m, int n) { int **path = new int*[m]; for(int i = 0; i < m; i++){ path[i] = new int[n]; memset(path[i], 0, sizeof(int)*n); } for(int i = 0; i < m; i++) // 初始化 path[i][n-1] = 1; for(int i = 0; i < n; i++) path[m-1][i] = 1; for(int i = m-2; i >=0; i--){ for(int j = n-2; j >= 0; j--){ path[i][j] = path[i+1][j] + path[i][j+1]; // DP算法 } } int pathes = path[0][0]; for(int i = 0; i < m; i++) delete []path[i]; delete []path; return pathes; }};
二:Unique PathII
题目:
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.
链接:https://leetcode.com/problems/unique-paths-ii/
分析:此题就是在上题中增加了一些障碍物,这对初始化会带来一定影响。
class Solution {public: int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); if(obstacleGrid[m-1][n-1] || obstacleGrid[0][0]) return 0; int **path = new int*[m]; for(int i = 0; i < m; i++){ path[i] = new int[n]; memset(path[i], 0, sizeof(int)*n); } path[m-1][n-1] = 1; for(int i = m-2; i >= 0; i--){ // 有障碍物的初始化 对最后一行最后一列初始化 if(obstacleGrid[i][n-1]) path[i][n-1] = 0; else path[i][n-1] = path[i+1][n-1]; } for(int i = n-2; i >= 0; i--) if(obstacleGrid[m-1][i]) path[m-1][i] = 0; else path[m-1][i] = path[m-1][i+1]; for(int i = m-2; i >=0; i--){ for(int j = n-2; j >= 0; j--){ if(obstacleGrid[i][j]) path[i][j] = 0; else path[i][j] = path[i+1][j] + path[i][j+1]; // DP算法 } } int pathes = path[0][0]; for(int i = 0; i < m; i++) delete []path[i]; delete []path; return pathes; }};三:Triangle
题目:
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[ [2], [3,4], [6,5,7], [4,1,8,3]]
The minimum path sum from top to bottom is 11
(i.e., 2 + 3 + 5 + 1 = 11).
分析:用F[i]表示位置为i时的最小和,他就=triangle[i]+min(F[i], F[i+1]) 典型的DP
代码:
class Solution {public: int minimumTotal(vector<vector<int> > &triangle) { int lastCols = triangle[triangle.size()-1].size(); int *f = new int[lastCols]; for(int i = 0; i < lastCols; i++){ f[i] = triangle[triangle.size()-1][i]; // 初始化 } for(int i = triangle.size()-2; i >=0; i--){ for(int j = 0; j < triangle[i].size(); j++){ f[j] = triangle[i][j] + min(f[j], f[j+1]); // 递归式 } } int result = f[0]; delete []f; return result; }};
四:Minimum Path Sum
题目:
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
链接:https://leetcode.com/problems/minimum-path-sum/
代码:
class Solution {public: int minPathSum(vector<vector<int> > &grid) { int m = grid.size(); if(m == 0) return 0; int n = grid[0].size(); if(n == 0) return 0; int **path = new int*[m]; for(int i = 0; i < m; i++){ path[i] = new int[n]; memset(path[i], 0, sizeof(int)*n); } path[m-1][n-1] = grid[m-1][n-1]; for(int i = m-2; i >= 0; i--){ // 动态规划 初始化 path[i][n-1] = path[i+1][n-1] + grid[i][n-1]; } for(int i = n-2; i >= 0; i--){ path[m-1][i] = path[m-1][i+1]+ grid[m-1][i]; } for(int i = m-2; i >= 0; i--){ // 动态规划迭代 for(int j = n-2; j >= 0; j--) path[i][j] = grid[i][j] + min(path[i+1][j], path[i][j+1]); } int result = path[0][0]; for(int i = 0; i < m; i++) delete []path[i]; delete []path; return result; }};
五:Rectangle Area
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.
For example, given the following matrix:
1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0Return 4.
分析:此题为动态规划问题,时间复杂度肯定是O(M*N),空间复杂度可以降低为O(min{M,N})。迭代式为square[i][j] = min(min(square[i][j+1],square[i+1][j]), square[i+1][j+1]);
代码:
class Solution {public: int maximalSquare(vector<vector<char>>& matrix) { int m = matrix.size(); if(m == 0) return 0; int n = matrix[0].size(); int **square = new int*[m]; for(int i = 0; i < m; i++){ square[i] = new int[n]; memset(square[i], 0, sizeof(int)*n); } int maxSquare = 0; for(int i = 0; i < m; i++){ square[i][n-1] = matrix[i][n-1]-'0'; maxSquare = max(maxSquare, square[i][n-1]); } for(int i = 0; i < n; i++){ square[m-1][i] = matrix[m-1][i] - '0'; maxSquare = max(maxSquare, square[m-1][i]); } for(int i = m-2; i >= 0; i--){ for(int j = n-2; j >= 0; j--){ if(matrix[i][j] == '1') square[i][j] =min(min(square[i+1][j], square[i][j+1]), square[i+1][j+1])+1; else square[i][j] = 0; maxSquare = max(maxSquare, square[i][j]); } } return maxSquare*maxSquare; }};
下面给出一份O(N)空间复杂度的代码:
class Solution {public: int maximalSquare(vector<vector<char>>& matrix) { int m = matrix.size(); if(m == 0) return 0; int n = matrix[0].size(); int **square = new int*[2]; for(int i = 0; i < 2; i++){ square[i] = new int[n]; memset(square[i], 0, sizeof(int)*n); } int maxSquare = 0; /* for(int i = 0; i < 2; i++){ square[i][n-1] = matrix[i][n-1]-'0'; maxSquare = max(maxSquare, square[i][n-1]); }*/ for(int i = 0; i < n; i++){ square[0][i] = matrix[m-1][i] - '0'; maxSquare = max(maxSquare, square[0][i]); } for(int i = m-2; i >= 0; i--){ square[1][n-1] = matrix[i][n-1]-'0'; maxSquare = max(maxSquare, square[1][n-1]); int j = n-2; for(j = n-2; j >= 0; j--){ if(matrix[i][j] == '1') square[1][j] =min(min(square[0][j], square[1][j+1]), square[0][j+1])+1; // 下、右、右下三个的最小者 else square[1][j] = 0; maxSquare = max(maxSquare, square[1][j]); if(j+2 < n) square[0][j+2] = square[1][j+2]; } if(j+2 < n) square[0][j+2] = square[1][j+2]; square[0][0] = square[1][0]; } return maxSquare*maxSquare; }};
- LeetCode 62/63/120/64 Unique PathsI/II Triangle/Min sum Path/Rectangle Area--DP
- leetcode做题总结,动态规划I(Triangle,Unique PathsI/II,Minimum Path Sum,Climbing Stairs,Jump Game,Word Break)
- Leetcode: Unique Path I & II, Minimum Path Sum, Triangle
- LeetCode 64/62/63. Minimum Path Sum/ Unique Paths i, ii
- [LeetCode] Unique Paths、Unique Paths II、Minimum Path Sum
- leetcode Unique Paths & Unique Paths II & Minimum Path Sum
- [Leetcode]Unique Path II
- [leetcode] Unique Path II
- Unique Path II--LeetCode
- [Leetcode]Unique Path II
- Leetcode Unique Path II
- 【LeetCode】Unique Paths I && II && Minimum Path Sum
- LeetCode: Path Sum II
- LeetCode Path Sum II
- [Leetcode] Path Sum II
- LeetCode: Path Sum II
- [LeetCode] Path Sum II
- 【leetcode】Path Sum II
- 黑马程序员:Java基础学习——异常
- android xml文件的序列化--XmlSerializer简单用法
- 浏览器内核
- 哈希表的C++实现
- 第一章第五题
- LeetCode 62/63/120/64 Unique PathsI/II Triangle/Min sum Path/Rectangle Area--DP
- Android开发规范
- xpath学习
- 22
- C++中各种类型的成员变量的初始化方法(mark-好)
- Android如何通过shared_user_id获取系统权限
- leetcode-111 Minimum Depth of Binary Tree
- teste
- storm安装配置