LeetCode 62. Unique Paths

【算法】【动态规划】Leetcode62 Unique Paths & Codeforces D. Red-Green Towers 先碰到Leetcode62这题,Discuss里有个大佬给了非常详细的思路解答,三段解题代码层层递进,依次从维护二维数组到维护两个一维数组再到只需要维护一个一维数组,印象深刻。 后来碰到跟Codeforces中D. Red-Green Towers差不多的一道题,当时没思路,甚至都没想到要用动规(啊,我好菜)。后来搜了下别人列的状态转移方程,发现可以用类似的方法简化解题过程,所以列一下这两道题... 阅读详情

Question:

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?


This is a classical DP problem. The most important part is to initialize the status correctly.

  int uniquePaths(int m, int n) {
    vector< vector<int> > map(m, vector<int>(n, 0));

    // initialize the matrix
    for(int i = 0; i < m; ++i) {
        map[i][0] = 1;  // there is one one way to go right straightly.
    }

    for(int j = 0; j < n; ++j) {
        map[0][j] = 1;  // there is one way to go down straightly
    }

    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];  // the most recent cube path is from right top and left down.
        }
    }
    return map[m-1][n-1];  // the right down conner one is the sum of the whole path.
 }

Further optimize.... Space becomes O(N) using rolling array.

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


Variation:

 Given a N * M matrix, and the point in the rectangle (x, y). search from (x, y) to (0, 0) at K steps. Every point can go for 4 directions.
 go left, right, up, down. For example, in the following matrix

  0, 0, 0, 0, 0
  0, 0, 0, 0, 0
  0, 0, 0, 0, 0
  0, 0, 0, 0, 0
  Given (x, y) = (1, 1), k = 2, return 2.

// dfs
void pathNumber(vector< vector<int> >& matrix, int x, int y, int &count, int k) {
  if(x < 0 || x >= matrix.size() || y >= matrix[0].size() || y < 0 || matrix[x][y] == -1 || k < 0) return;
  if((x == 0) && (y == 0) && (k == 0)) {
    count++;
    return;
  }
  matrix[x][y] = -1;
  pathNumber(matrix, x - 1, y, count, k - 1);
  matrix[x][y] = 0;
  pathNumber(matrix, x + 1, y, count, k - 1);
  matrix[x][y] = 0;
  pathNumber(matrix, x, y + 1, count, k - 1);
  matrix[x][y] = 0;
  pathNumber(matrix, x, y - 1, count, k - 1);
  matrix[x][y] = 0;
}

// first thought is to use dfs.
int pathNumber(vector< vector<int> >& matrix, int x, int y, int steps) {
  if(x == 0 || y == 0) return 1;
  int count = 0;
  pathNumber(matrix, x, y, count, steps);
  return count;
}

To further optimize it, we can use 3D dynamic programming.

dp[i][j][k] = dp[i-1][j][k-1] + dp[i+1][j][k-1] + dp[i][j-1][k-1]+dp[i][j+1][k-1]. initialize: dp[0][y][k] --- 1, dp[x][0][k] = 1


Maybe, can be further optimized using 2D array.



半导体EAP系统入门指南:从零开始理解设备自动化程序(附实战案例) 本文深入浅出地介绍了半导体制造中EAP(设备自动化程序)系统的核心作用与工作原理。EAP作为连接MES与生产设备的桥梁,负责指令翻译、数据采集与实时控制,是实现晶圆厂全自动化的关键。文章通过对比手动与自动模式,阐述了EAP在提升效率、保障良率方面的价值,并提供了一个实战案例,帮助读者从零构建对半导体CIM体系中这一关键组件的理解。 阅读详情

相关推荐

量化实战:如何用Barra多因子模型给你的投资组合做一次‘风险体检’?

本文详细介绍了如何利用Barra多因子模型对投资组合进行全面的风险体检,帮助投资者识别行业和风格因子的风险暴露。通过实战案例和Python代码示例,展示了数据采集、风险归因和组合优化的完整流程,提升投资组合的风险调整收益。

weixin_27215337的博客 323

LeetCode刷题:62. Unique Paths 和 63. 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...

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

智能车八邻域图像算法

智能车八邻域图像算法

技术缝合怪的博客 4万+

python写算法题:leetcode: 62. Unique Paths

class Solution(object): def __init__(self): self.cnt={} self.cnt[(0,0)]=1 def uniquePaths(self, m, n): """ :type m: int :type n: int :rtype: ...

253

STM32----SPI+DMA高效读写外部Flash的配置与实战

本文详细介绍了STM32通过SPI+DMA高效读写外部Flash的配置与实战方法。针对大量数据传输场景,使用DMA技术可显著提升系统效率,释放CPU资源。文章包含完整的代码实现、常见问题调试技巧以及性能优化建议,帮助开发者快速掌握这一关键技术。

pea55的博客 1030

组态王7.5和SQLsever数据库交互

摘要:本文详细介绍了组态王7.5与SQL Server 2016数据库交互的实现流程,包括建立连接、数据存储与查询操作。主要内容涵盖ODBC数据源配置、数据库表格创建、数据插入及查询功能实现(含日期控件查询)

gsjkwg的博客 1577

LeetCode62——Unique Paths

LeetCode62——Unique Paths 《组合数学》一道题。由于有直接的方法,就没怎么思考。 答案是从m+n中选出m或者n个结果的组合数,虽然结果是一样的,这里为了减少迭代次数,取m和n中的较小值进行计算 代码: class Solution { public: int uniquePaths(int m, int n) { m--;

上善若水 845

OpenClaw的引导配置(onboard)流程

脚本安装的直接安装完到引导配置界面,其他安装方式还可以直接命令到配置。

supersolon的专栏 1323

Leetcode 62. Unique Paths 不同路径

Leetcode 62. Unique Paths 不同路径 标签: Leetcode 题目地址:https://leetcode-cn.com/problems/unique-paths/ 题目描述 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。 问总共...

CoderWangSon 346

用R语言做WGCNA分析全步骤一(附代码解读)【转载】

代码逐句分析一、文章来源二、基因共表达网络构建及模块识别1.数据导入、清洗及预处理2.检查过度缺失值和离群样本3.聚类做离群样本检测4.载入临床特征数据三、自动构建网络及识别模块1.确定合适的软阈值:网络拓扑分析2.一步构建网络和识别模块 一、文章来源 初学WGCNA,觉得博主的代码写的很不错,但其中很多代码在第一遍看的时候有很多地方不理解,后查阅了很多资料,终于看明白了,于是写了一篇笔记,记录自己的学习心得,有不准确的地方,还望各位大佬们不吝赐教~ 原文链接:WGCNA简明指南 二、基因共表达网络构建及模

qq_43138237的博客 2万+

7.91版本unidbg调用方式

【代码】xhs最新7.91版本unidbg调用方式。

Codeooo 博客 1106

leetcode 62. Unique Paths

dp路径即可累加 ,明天考算法感觉要g了呜呜呜。

weixin_50310972的博客 157

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

LeetCode 62 Unique Paths

LeetCode 62 Unique Paths题目链接:https://leetcode.com/problems/unique-paths/description/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 m

编程小白的进阶之路 234

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

LeetCode 62 --- Unique Paths

题目链接: LeetCode 62 — Unique Paths问题分析: 简单dp。AC代码:public class Problem62 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(uniquePaths(5

调包侠 316

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Leetcode-62. Unique Paths and 63. Unique Paths II

Leetcode-62. 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 tim

caoyan_12727的博客 508

LeetCode OJ 62. Unique Paths

LeetCode OJ 62. Unique PathsDescriptionA 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.

jan_deng的博客 287

LeetCode 62.unique-paths

【代码】LeetCode 62.unique-paths。

eee08201030的博客 210

【Leetcode】62. 不同的路径(Unique Paths)

Leetcode - 62 Unique Paths (Medium) 题目描述:机器人只能向右或向下移动,给定矩阵的长和宽,求从左上角移动到右下角共有几种不同的路径。 Input: m = 3, n = 2 Output: 3 Explanation: From the top-left corner, there are a total of 3 ways to reach the b...

厉兵秣码 279

LeetCode 62. Unique Paths(动态规划)

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

da_kao_la的博客 417

【LeetCode】62. Unique Paths 解题报告(Python)

【LeetCode】62. Unique Paths 解题报告(Python)题目描述解法1:组合数直接求解防溢出(python大数不会溢出,给一个思路)类似动态规划,不可直接递归,有重复子问题解法2:动态规划 题目地址:https://leetcode.com/problems/unique-paths/ 题目描述 A robot is located at the top-left corne...

ttinch的博客 215

[Leetcode]62. Unique Paths

62. Unique Paths 本题难度: Easy Topic: Dynamic Programming Description 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 dow...

weixin_30888027的博客 88

【Leetcode】62. Unique Paths

每次可以向左或者向下移动一位,求从左上角到右下角最多有多少种走法 方法1 经典DP法,每一格可以由其左边和上面的格子到大,所以我们很容易写出递推方程 dp[i][j] = dp[i-1][j] + dp[i][j-1] 注意边界条件,第一行和第一列的初始值都应该是1,因为遍历是从左往右从下往上并且python 中数组的[-1]位置表示数组的末尾,所以第一行和第一列越界的元素正好为0 class ...

很吵请安静 569

[leetcode][python] 62. Unique Paths

62. Unique Paths 知识点: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 poi...

qq_40670635的博客 361

Leetcode 62. Unique Paths & 63. Unique Paths II

好久没刷题了,今天来俩道简单题。 Leetcode 62. Unique Paths Leetcode 63. Unique Paths IIA 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

xindoo 1764

【LeetCode】解题62:Unique Paths

LeetCode解题 62:Unique Paths Problem 62: Unique Paths [Medium]解题思路Solution (Java) Problem 62: Unique Paths [Medium] A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagr...

Fayedily的博客 244
上一篇: LeetCode 137. Single Number II
下一篇: LeetCode 63. Unique Paths II
ActiveCoder
博客等级 码龄11年 4粉丝 345原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值