[LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming
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.
Example:
Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum. 其实这个题目的思路是跟[LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming很像, 只不过一个是步数, 一个是minimal sum而已. 还是可以用滚动数组的方法, 只是需要注意
初始化即可. 1. Constraints
1) [0*0] 最小 2. Ideas Dynamic Programming T: O(m*n) S: O(n) optimal(using rolling array) 3. Code
class Solution(object):
def minPathSum(self, grid):
# S; O(m*n)
if not grid or len(grid[0]) == 0: return 0
m, n = len(grid), len(grid[0])
if m == 1 or n == 1: return sum(sum(each) for each in grid)
ans = grid
for i in range(m):
for j in range(n):
if i == 0 and j!= 0:
ans[i][j] = ans[i][j-1] + grid[i][j]
if j == 0 and i!= 0:
ans[i][j] = ans[i-1][j] + grid[i][j]
if j >0 and i >0 :
ans[i][j] = min(ans[i-1][j], ans[i][j-1]) + grid[i][j]
return ans[-1][-1]
2)
class Solution:
def minPathSum(self, grid):
m, n = len(grid), len(grid[0])
mem = [[0]* n for _ in range(m)]
mem[0][0] = grid[0][0]
for i in range(1, m):
mem[i][0] = grid[i][0] + mem[i - 1][0]
for i in range(1, n):
mem[0][j] = grid[0][j] + mem[0][j - 1]
for i in range(1, m):
for j in range(1, n):
mem[i][j] = grid[i][j] + min(mem[i - 1][j], mem[i][j - 1])
return mem[m - 1][n - 1]
4. Test cases
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
[LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming的更多相关文章
- [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] 64. Minimum Path Sum 最小路径和
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- LeetCode 64. Minimum Path Sum(最小和的路径)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...
- [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming
Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...
- [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
随机推荐
- 支持向量机SVM进阶
本文适合于对SVM基本概念有一点了解的童鞋. SVM基本概念: 最大边缘平面--基本原理:结构风险最小化 分类器的泛化误差 支持向量 问题描述: 请对一下数据,利用svm对其进行分类. 最 ...
- 决策树归纳算法之C4.5
前面学习了ID3,知道了有关“熵”以及“信息增益”的概念之后. 今天,来学习一下C4.5.都说C4.5是ID3的改进版,那么,ID3到底哪些地方做的不好?C4.5又是如何改进的呢? 在此,引用一下前人 ...
- Python在mysql中进行操作是十分容易和简洁的
首先声明一下,我用的是Windows系统! 1.在Python中对mysql数据库进行操作首先要导入pymysql模块,默认情况下,Python中是没有安装这个模块的, 可以在Windows的命令行中 ...
- 中国标准时间、‘yyyy-MM-dd’格式时间转为时间戳
中国标准时间转为时间戳 let _time="Tue Mar 20 2018 00:00:00 GMT+0800 (中国标准时间)"; console.log(Date.parse ...
- sencha touch NavigationView 嵌套 TabPanel 的问题
在st2.1之中,在NavigationView视图之中在嵌套一个TabPanel会有以下问题 下面我们监控TabPanel的activate事件和activeitemchange事件 会发现当首页加 ...
- jenkins 集成redmine
安装 可以使用jenkins的插件管理页面进行安装,也可以使用其id(redmine)在镜像中进行安装并重启镜像即可. 插件安装确认 重新启动后确认此插件已经安装完毕 设定内容 系统管理 -> ...
- jenkins定时任务未生效解决
近期在配置jenkins定时任务时,发现未生效,并没有按时触发任务 解决思路: 1.先查看下我们的定时任务有没有选择正确,如下说明: Poll SCM:定时检查源码变更,如果有更新就checkout最 ...
- Thinkphp框架下对某个字段查询数据的时候进行唯一过滤,返回唯一不同的值
方法一. DISTINCT 方法用于返回唯一不同的值 . *distinct方法的参数是一个布尔值. 用法: $data = $Model->Distinct(true)->field(' ...
- iOS - 开发一套代码多个app展示不同图标和名称
引言 公司项目重构之后,有了相对比较完善的开发体系,首先git分支分为日常.预发.生产三个主要分支,开发阶段都在日常(daily)分支下开相应功能的feature分支,开发完再合并. 我的iOS工程需 ...
- Spark2 Dataset聚合操作
data.groupBy("gender").agg(count($"age"),max($"age").as("maxAge&q ...