LeetCode - Unique Paths

Unique Paths

2013.12.21 02:43

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.

Solution:

  It's obviously a dynamic programming problem, with the recurrence relations expressed as: a[x][y] = a[x - 1][y] + a[x][y - 1], a[0][0] = 1;

  Using this formula will give you a solution in O(m * n) time. If you think one step further, you'll see this is the Pascal Triangle, thus each term in the triangle can be calculated with combinatorials, namely C(m + n - 2, m - 1).

  To travel from (0, 0) to (m - 1, n - 1), you'll have to move (m - 1) steps down and (n - 1) steps right. Then comes the question: in the (m + n - 2) moves you make, which of them are down and which of them are right. The number of ways to choose is the answer we're looking for.

  Time complexity is O(m) or O(n) or O(min(m, n)), depending on how you write the code to calculate the combinatorial. Space compelxity is O(1).

Accepted code:

 1 class Solution {
 2 public:
 3     int uniquePaths(int m, int n) {
 4         // IMPORTANT: Please reset any member data you declared, as
 5         // the same Solution instance will be reused for each test case.
 6         if(m > n){
 7             return uniquePaths(n, m);
 8         }
 9         
10         // 1WA here, integer overflow
11         long long int sum, res;
12         
13         // 1WA here, m and n start from 0, not 1
14         --m;
15         --n;
16         sum = res = 1;
17         // 1CE here, int i is not declared
18         for(int i = 1; i <= m; ++i){
19             sum *= i;
20             res *= (m + n + 1 - i);
21             if(res % sum == 0){
22                 res /= sum;
23                 sum = 1;
24             }
25         }
26         
27         return res;
28     }
29 };

 

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3484767.html

本项目是一个基于SSM(Spring+SpringMVC+MyBatis)后端框架与Vue.js前端框架开发的疫情居家办公系统。该系统旨在为居家办公的员工提供一个高效、便捷的工作环境,同时帮助企业更好地管理远程工作流程。项目包含了完整的数据库设计、前后端代码实现以及详细的文档说明,非常适合计算机相关专业的毕设学生和需要进行项目实战练习的Java学习者。 系统的核心功能包括用户管理、任务分配、进度跟踪、文件共享和在线沟通等。用户管理模块允许管理员创建和管理用户账户,分配不同的权限。任务分配模块使项目经理能够轻松地分配任务给团队成员,并设置截止日期。进度跟踪模块允许员工实时更新他们的工作状态,确保项目按计划进行。文件共享模块提供了一个安全的平台,让团队成员可以共享和协作处理文档。在线沟通模块则支持即时消息和视频会议,以增强团队之间的沟通效率。 技术栈方面,后端采用了Spring框架来管理业务逻辑,SpringMVC用于构建Web应用程序,MyBatis作为ORM框架简化数据库操作。前端则使用Vue.js来实现动态用户界面,搭配Vue Router进行页面导航,以及Vuex进行状态管理。数据库选用MySQL,确保数据的安全性和可靠性。 该项目不仅提供了一个完整的技术实现示例,还为开发者留下了扩展和改进的空间,可以根据实际需求添加新功能或优化现有功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值