Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhipeng committed Apr 23, 2019
1 parent a6e3a21 commit e091443
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 72 deletions.
71 changes: 0 additions & 71 deletions 62.unique-paths.js

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [11.container-with-most-water](./problems/11.container-with-most-water.md)
- [19. Remove Nth Node From End of List](./problems/removeNthNodeFromEndofList.md)
- [24. Swap Nodes In Pairs](./problems/swapNodesInPairs.md)
- 🆕 [55.jump-game.md](./problems/55.jump-game.md.md)
- 🆕 [55.jump-game.md](./problems/55.jump-game.md)
- 🆕 [62.unique-paths](./problems/62.unique-paths.md)
- [75.sort-colors.md](./problems/75.sort-colors.md)
- [86.partition-list](./problems/86.partition-list.md)
- [92.reverse-linked-list-ii](./problems/92.reverse-linked-list-ii.md)
Expand Down
1 change: 1 addition & 0 deletions assets/drawio/208.implement-trie-prefix-tree.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile modified="2019-04-23T09:48:23.409Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" etag="GTTmjchVZQ0qfG9G-_4Q" version="10.6.3" type="device"><diagram id="tn9JAYlXwyHzK-nR11YM" name="第 1 页">7Ztdj5s4FIZ/jS93hDGfl5Ah24tdtVIq9bJyg0NQAVNwmsz++rXBJIDJJFUTiBoy0gSOD8a8D9jn2ASgRXr4u8D59l8akgToWngA6BXoOkSuw7+E5a22OIZRG6IiDqXTybCK/yPSqEnrLg5J2XFklCYszrvGNc0ysmYdGy4Kuu+6bWjSPWuOI6IYVmucqNYvcci28ip0+2T/QOJo25wZWm5dkuLGWV5JucUh3bdMKABoUVDK6q30sCCJEK/RpT5ueab02LC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94SDh7EHgOCCzgLYGzrDYWwsidnUC4cYvjAj+oDl9WFhP4r8BFlY8LnKpmHwJHr5wXwLEr5wC4njiF41VFtvjvu2KD2x1DNlU426INjt9cOBeyvnZ5TZd7nFsvSNx44QFB8+Jth5r1ot/sKPju6fcWVVnrVyso+B8=</diagram></mxfile>
1 change: 1 addition & 0 deletions assets/drawio/62.unique-paths.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile modified="2019-04-23T11:42:57.322Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" etag="WTlwpOlKDbydg-200h0-" version="10.6.3" type="device"><diagram id="LLvs7Lyrv3_SIfY0iaOW" name="第 1 页">7Vvfj5s4EP5r/NgIMBDzCAnpSe1Je7cPvT6dHHCIewRnwdkk99efDSb8rMrukYSqiVYKHnsG831je2bIArjYnT6meL/9nYUkBoYWngBcAsPQoYPEl5ScCwkyzUIQpTRUgyrBM/2XKKGmpAcakqwxkDMWc7pvCgOWJCTgDRlOU3ZsDtuwuHnXPY5IR/Ac4Lgr/UJDvlVPYcwr+W+ERtvyzrrtFD07XA5WT5JtcciONRH0AVykjPHiandakFiCV+JS6K2+03uZWEoSPkThj7X+khp/0tVrsHI/ul+Wnz55H5SVVxwf1AOryfJziUDKDklIpBENQO+4pZw873Ege4+CcyHb8l0sWrq43NA4XrCYpbkuDC2CQlPIM56yf0itBxlraNuiR02ApJycvvtk+gUv4WiE7QhPz2LIqekrysUMTbWPFWGWEm1rXJUyrFwkuhiuUBQXCsg3gGqMDOoIENkTgwhODyJ9am5kThCjFkTWnSGyJw8RvLcXzacP0b29CE0PovZ+fW+InMlDdPeFVnr1lDG6txvpUzzTpnao6db0Qbr/ahv76G9lLhsUkCDoy1zWyDIt7Uqw3t33BoQLl0RWghDibHuBuAZnE7aEJULZi/GaxE8so5yyRIgDARMR/Z4EkYqk+3NrwJpxzna1AW5MI9nBmaQPq9bFjpjaXs5yd4pkOWK2ywJMZiHZpyTAnISzPcvEyL/zykCXdE3zFitDPhUVCmoOCUslFdcIn8uV/gO2jWuxbQw4sh5sv5ftudVk2+5h27ol2wOKPb8Y2wRn/DpkG92N3OhZ2vBqZA+osDzIfu8+bjTZhhocxLaJrsX2gLj6wfZYbFvGfdmGE8w0odGMbXRtWGxzvQrz2K82xgBpPjWQjOmBZE7Ok3qOUh8BzwKeBnwLIJRfzIG7EH9UiZy5FDkuQCbwbYA04Hl51wq4K6mPIPCWssvJDbVhFwDyAXtzfStUos722t6FdzQM5W16yWzSvWEJVy9ldaTatZ3XmS+1+XykxaG1t9kO7Trq4d2+Gu/dQ1X/OWlaraD4jLXRN2lyujSZt8xhYbda9mBJnDQTY6lbroMPlsypraVu9a8bD5AwIs+qKYslLGIJjv1K2sKzGvOZyfA/J/Yb4fysUMYHzpq0kxPlf0n1maVaX2s9y5OynDfOZSMRCNSUZPNrs1kp5q2G5hNJqQBROs87vEBi8lYfSEmMOX1t6vVRqlSfGBUWqxXumK0wqZVuZOyQBkRpVY7xQ0NOyw7HaUR4x07uYJfH+R8+1/P2VYRJrgkcW154OkC5xPOB68qYCvkqglIRmAk8JOMrEUrJCEwH/gp4CxmDyXBLhF7WG+1YwCnUCztOI5ATcZ2XT0yMdJxyzELaEbd2UR4IClMuMOyYK88B8gdqpfPYLwdWpKZws8ndqCayo/y7jCZrs8rtixspswLswnJL45eKP7X8M1Kab7dKeD3VeVu/5XZs9mRn71garjnO0pB2prY03Mr+Y2lccWm0Xlw53XrnjZdGX04OJZWo7+LBYKvyBM3u+4kbM9jNrpdPD6IuZawyrOt5kXTTnMDs5te2MTsk9OVAPuwx32YDSLulp7erhx349L7qofl2/ESz+vV+EQ5X/wMB/f8A</diagram></mxfile>
Binary file added assets/problems/62.unique-paths-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/problems/62.unique-paths-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/problems/62.unique-paths-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
143 changes: 143 additions & 0 deletions problems/62.unique-paths.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@

## 题目地址
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 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?
```
![62.unique-paths-1](../assets/problems/62.unique-paths-1.png)

```
Above is a 7 x 3 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
Example 1:
Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right
Example 2:
Input: m = 7, n = 3
Output: 28
```

## 思路
这是一道典型的适合使用动态规划解决的题目,它和爬楼梯等都属于动态规划中最简单的题目,
因此也经常会被用于面试之中。

读完题目你就能想到动态规划的话,建立模型并解决恐怕不是难事。其实我们很容易看出,由于机器人只能右移动和下移动,
因此第[i, j]个格子的总数应该等于[i - 1, j] + [i, j -1], 因为第[i,j]个格子一定是从左边或者上面移动过来的。

![62.unique-paths-2](../assets/problems/62.unique-paths-2.png)

代码大概是:

```js
const dp = [];
for (let i = 0; i < m + 1; i++) {
dp[i] = [];
dp[i][0] = 0;
}
for (let i = 0; i < n + 1; i++) {
dp[0][i] = 0;
}
for (let i = 1; i < m + 1; i++) {
for(let j = 1; j < n + 1; j++) {
dp[i][j] = j === 1 ? 1 : dp[i - 1][j] + dp[i][j - 1]; // 转移方程
}
}

return dp[m][n];

```

由于dp[i][j] 只依赖于左边的元素和上面的元素,因此空间复杂度可以进一步优化, 优化到O(n).

![62.unique-paths-3](../assets/problems/62.unique-paths-3.png)

具体代码请查看代码区。

## 关键点

- 空间复杂度可以进一步优化到O(n), 这会是一个考点
- 基本动态规划问题
## 代码

```js
/*
* @lc app=leetcode id=62 lang=javascript
*
* [62] Unique Paths
*
* https://leetcode.com/problems/unique-paths/description/
*
* algorithms
* Medium (46.53%)
* Total Accepted: 277K
* Total Submissions: 587.7K
* Testcase Example: '3\n2'
*
* 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 7 x 3 grid. How many possible unique paths are there?
*
* Note: m and n will be at most 100.
*
* Example 1:
*
*
* Input: m = 3, n = 2
* Output: 3
* Explanation:
* From the top-left corner, there are a total of 3 ways to reach the
* bottom-right corner:
* 1. Right -> Right -> Down
* 2. Right -> Down -> Right
* 3. Down -> Right -> Right
*
*
* Example 2:
*
*
* Input: m = 7, n = 3
* Output: 28
*
* START
*/
/**
* @param {number} m
* @param {number} n
* @return {number}
*/
var uniquePaths = function(m, n) {
const dp = Array(n).fill(1);

for(let i = 1; i < m; i++) {
for(let j = 1; j < n; j++) {
dp[j] = dp[j] + dp[j - 1];
}
}

return dp[n - 1];
};
```

0 comments on commit e091443

Please sign in to comment.