【LeetCode】解题63:Unique Paths II

TensorFlow_PyTorch实战:构建AI人工智能回归模型 回归分析是机器学习的核心任务之一,用于预测连续型目标变量(如房价、股票价格、用户留存时间等)。基础线性回归模型的数学原理与代码实现多项式回归模型的过拟合处理与正则化技巧自定义数据集加载与数据预处理流程动态图(PyTorch)与静态图(TensorFlow)的编程范式对比模型评估指标与可视化分析方法核心概念:对比两大框架的架构设计,解析回归模型核心组件数学原理:推导线性回归的假设函数、损失函数及优化算法算法实现:提供TensorFlow和PyTorch的完整代码示例项目实战。 阅读详情

Problem 63: Unique Paths II [Medium]

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).

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.

Note: m and n will be at most 100.

Example:

Input:
[
[0,0,0],
[0,1,0],
[0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:

  1. Right -> Right -> Down -> Down
  2. Down -> Down -> Right -> Right

来源:LeetCode

解题思路

动态规划题,解题思路与Problem 62 Unique Paths类似。

状态方程:
s t e p [ i , j ] = ( s t e p [ i , j − 1 ] + s t e p [ i − 1 , j ] ) ∗ ( 1 − o b s t a c l e G r i d [ i , j ] ) step[i, j] = (step[i, j-1] + step[i-1, j])*(1 - obstacleGrid[i, j]) step[i,j]=(step[i,j1]+step[i1,j])(1obstacleGrid[i,j])
每一格可以通过左边格/上边格到达,因此到达的方法个数为左边一格的方法个数与上边一格的方法个数之和,如果当前格子有障碍(obstacleGrid[i, j] = 1),则不能到达,方法个数为0。

具体思路:

  • 由上一篇Unique Paths题解可知,实现该状态方程只需要创建一个长度为n的一维数组:step[n]。
  • 初始化:step[0] = 1,step[1~n-1] = 0.
  • 双重循环:
    a. 第一重循环:计算每行首列的值,即step[0]。由于第一列只能由上边一格到达,因此只要上边某格有障碍,下面所有的第一列格子都无法到达,更新公式为:step[0] = step[0] * (1 - obstacleGrid[i, 0])。
    b. 第二重循环:计算每行当前格方法个数公式:step[j] = (step[j-1] + step[j]) * (1 - obstacleGrid[i, j]),当前格有障碍时step[j] = 0。

整个算法时间复杂度为 O ( m ∗ n ) O(m*n) O(mn),空间复杂度为 O ( n ) O(n) O(n)

运行结果:
在这里插入图片描述

Solution (Java)

class Solution {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        int[] step = new int[n];
        step[0] = 1;
        for(int i = 0; i < m; i++){
            step[0] *= (1 - obstacleGrid[i][0]);
            for(int j = 1; j < n; j++){
                step[j] += step[j-1];
                step[j] *= (1 - obstacleGrid[i][j]);
            }
        }
        return step[n-1];
    }
}
影刀初级考试操作2 型E主要考察了Excel的操作,总体上难度不大,流程也很少。有些细节地方比如是追加还是覆盖,多试几次就没问了。 阅读详情

相关推荐

国产麒麟系统下用Rider开发C#桌面应用:从环境配置到Avalonia UI实战

本文详细介绍了在国产麒麟系统上使用JetBrains Rider开发C#桌面应用的完整流程,重点讲解了Avalonia UI框架的实战应用。从环境配置、Rider安装到Avalonia项目创建和UI开发技巧,全面覆盖了在非Windows环境下进行C#开发的关键步骤和优化建议,助力开发者高效构建跨平台桌面应用。

weixin_27791839的博客 345

leetcode——第63——不同路径V2

目: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。 现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径? class Solution { // 这道与上一思路基本一样,只不过就是在处理 dp 数组的时候 // 多了一个判断的条件,思路还是五步走,便不再详述 public: int uniquePathsWithObs

linping_的博客 251

爆肝5万字❤️Open3D 点云数据处理基础(Python版)

本文为Open3D 点云数据处理基础教程(Python版),小白也能轻松上手!还等什么呢,学起来!

孙悟空 13万+

leetcode-63-dp经典算法笔记

leetcode 62状态转移方程是一样的,但是迁入了障碍物的概念,如果需要知道状态转移方程的思路,可以参考https://blog.csdn.net/qq_41936805/article/details/100179828 解出此,我们必须知道对于障碍物的特点如下: 障碍处的dp值=0 我们已经知道了,动态转移方程为 dp[i][j]=dp[i][j-1]+dp[i-1][j] 接下来就要加入限制条件,如果检测到障碍,就把障碍坐标的dp初始化为0,如果起点dp[0][0]那么dp[1][1]=

0117 360

leetcode解题思路分析(九)57-63

插入区间 给出一个无重叠的 ,按照区间起始端点排序的区间列表。 在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。 本质上和上一是一个东西,但是因为给出的已经是无重叠并且排好序的,所以最简单的是一次遍历然后按情况插入新区间 class Solution { public: vector<vector<int>> ...

ty的博客 477

[leetcode][python]63. Unique Paths II

63. Unique Paths II 知识点:dynamic programming 1. 原 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 ...

qq_40670635的博客 332

63. **Unique Paths II

63. **Unique Paths II https://leetcode.com/problems/unique-paths-ii/description/ 目描述 Follow up for 62. Unique Paths**: Now consider if some obstacles are added to the grids. How many unique paths wou...

珍妮的选择的博客 340

LeetCode 63Unique Paths II动态规划

目来源:https://leetcode.com/problems/unique-paths-ii/ 问描述 63.Unique Paths II Medium A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below). The robot can ...

da_kao_la的博客 518

LeetCode:62. Unique Paths63. Unique Paths II

LeetCode:62. Unique Paths Unique Paths链接:https://leetcode.com/problems/unique-paths/description/ Unique Paths II链接:https://leetcode.com/problems/unique-paths-ii/description/ 62. Unique P...

梅森上校的博客 业精于勤荒于嬉,形成于思毁于随。 442

Leetcode63. 不同的路径 IIUnique Paths II

Leetcode - 63 Unique Paths II (Medium) 目描述:和上一道类似,只不过在路径中添加了障碍物,给定路径数组,1 表示障碍物,0 表示正常方块。 Input: [ [0,0,0], [0,1,0], [0,0,0] ] Output: 2 Explanation: There is one obstacle in the middle of the ...

厉兵秣码 203

[leetcode] 63. Unique Paths II 解题报告

目链接: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 an

小榕流光的专栏 646

LeetCode算法63. Unique Paths II(Medium)【Python3解】

目描述: 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 t

DaMoWangZQ的博客 327

leetcode 63.不同路径iiunique paths ii)c语言

leetcode 63.不同路径iiunique paths ii)c语言1.description2.solution 1.description https://leetcode-cn.com/problems/unique-paths-ii/description/ 一个机器人位于一个 obstacleGridSize x *obstacleGridColSize 网格的左上角 (起始点在下图中标记为“Start” )。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为

hsk6543210的博客 574

python写算法leetcode: 63. Unique Paths II

class Solution(object): def __init__(self): self.cnt={} self.cnt[(0,0)]=1 def uniquePathsWithObstacles(self, obstacleGrid): """ :type obstacleGrid: List[Lis...

176

LeetCode解题126:Word Ladder II(BFS算法

LeetCode解题 126:Word Ladder II(BFS算法)Problem 126: Word Ladder II [Hard]解题思路Solution (Java)修改过程 Problem 126: Word Ladder II [Hard] Given two words (beginWord and endWord), and a dictionary’s word list, ...

Fayedily的博客 771

LeetCode解题188:Best Time to Buy and Sell Stock IV(动态规划

LeetCode解题 188:Best Time to Buy and Sell Stock IV (动态规划)Problem 188: Best Time to Buy and Sell Stock IV [Hard]解题思路Solution (Java)修改过程 Problem 188: Best Time to Buy and Sell Stock IV [Hard] Say you hav...

Fayedily的博客 594

LeetCode解题123:Best Time to Buy and Sell Stock III(动态规划

LeetCode解题 123:Best Time to Buy and Sell Stock III (动态规划)Problem 123: Best Time to Buy and Sell Stock III [Hard]解题思路I. O(2n)时间+O(3n)空间II. O(2n)时间+O(n)空间III. O(n)时间+O(4)空间Solution (Java) Problem 123: B...

Fayedily的博客 487

LeetCode解题119:Pascal's Triangle II

LeetCode解题 118:Pascal's Triangle II Problem 118: Pascal's Triangle II [Easy]解题思路Solution (Java) Problem 118: Pascal’s Triangle II [Easy] Given a non-negative index k where k ≤ 33, return the kth index...

Fayedily的博客 444

LeetCode解题78:Subsets

LeetCode解题 78:Subsets Problem 78: Subsets [Medium]解题思路Solution (Java)修改过程 Problem 78: Subsets [Medium] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The so...

Fayedily的博客 444

LeetCode解题44:Wildcard Matching(动态规划解法)

LeetCode解题 44:Wildcard Matching(动态规划解法)Problem 44: Wildcard Matching [Hard]解题思路Solution (Java) Problem 44: Wildcard Matching [Hard] Given an input string (s) and a pattern (p), implement wildcard patt...

Fayedily的博客 429

LeetCode解题10:Regular Expression Matching(动态规划解法)

LeetCode解题 10:Regular Expression Matching(动态规划解法)Problem 10: Regular Expression Matching [Hard]解题思路Solution (Java) Problem 10: Regular Expression Matching [Hard] Given an input string (s) and a patter...

Fayedily的博客 411

LeetCode解题122:Best Time to Buy and Sell Stock II

LeetCode解题 122:Best Time to Buy and Sell Stock II Problem 122: Best Time to Buy and Sell Stock II [Easy]解题思路Solution (Java) Problem 122: Best Time to Buy and Sell Stock II [Easy] Say you have an array...

Fayedily的博客 366

从零开始搭建医药领域知识图谱实现智能问答与分析服务(含码源):含Neo4j基于垂直网站数据的医药知识图谱构建、医药知识图谱的自动

1、本项目完成了从无到有,以垂直网站为数据来源,构建起以疾病为中心的医疗知识图谱,实体规模4.4万,实体关系规模30万。并基于此,搭建起了一个可以回答18类问的自动问答小系统,总共耗时3天。其中,数据采集与整理1天,知识图谱构建与入库0.5天,问答系统组件1.5天。总的来说,还是比较快速。 2、本项目以业务驱动,构建医疗知识图谱,知识schema设计基于所采集的结构化数据生成(对网页结构化数据进行xpath解析)。 3、本项目以neo4j作为存储,并基于传统规则的方式完成了知识问答,并最终以cypher查询语句作为问答搜索sql,支持了问答服务。 4、本项目可以快速部署,数据已经放在data/medical.json当中,本项目的数据,在本项目中的部署上,可以遵循项目运行步骤,完成数据库搭建,并提供搜索服务。

安全+nessus10.9.1无ip限制

安全+nessus无ip限制

上一篇: 【LeetCode】解题62:Unique Paths
下一篇: 【LeetCode】解题64:Minimum Path Sum
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值