Skip to content

Commit

Permalink
go
Browse files Browse the repository at this point in the history
  • Loading branch information
robot committed Jan 7, 2023
1 parent 57e2986 commit 499b9ef
Show file tree
Hide file tree
Showing 44 changed files with 487 additions and 680 deletions.
2 changes: 1 addition & 1 deletion 91/season2.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

力扣加加,一个努力做西湖区最好的算法题解的团队。就在今天它给大家带来了《91 天学算法》,帮助大家摆脱困境,征服算法。

<img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gf2atkdikgj30u70u0tct.jpg" width="50%">
<img src="https://p.ipic.vip/befr2w.jpeg" width="50%">

## 初衷

Expand Down
4 changes: 2 additions & 2 deletions 91/two-pointers.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# 91算法-基础篇】05.双指针
# 91 算法-基础篇】05.双指针

力扣加加,一个努力做西湖区最好的算法题解的团队。就在今天它给大家带来了《91 天学算法》,帮助大家摆脱困境,征服算法。

<img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gf2atkdikgj30u70u0tct.jpg" width="50%">
<img src="https://p.ipic.vip/befr2w.jpeg" width="50%">

## 什么是双指针

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

可以去我的公众号《力扣加加》后台回复电子书获取!

<img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg" width="100%">
<img src="https://p.ipic.vip/n8gbxo.jpg" width="100%">

> epub 还是有动图的
Expand Down
2 changes: 1 addition & 1 deletion backlog/精彩预告.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[0042.trapping-rain-water](./problems/42.trapping-rain-water.md):

<img width="300" src="https://tva1.sinaimg.cn/large/007S8ZIlly1ghlueqdwy3j30bg04hmx3.jpg">
![](https://p.ipic.vip/9twl4j.jpg)

[0547.friend-circles](./problems/547.friend-circles-en.md):

Expand Down
58 changes: 23 additions & 35 deletions daily/2019-08-13.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# 毎日一题 - 417. 太平洋大西洋水流问题
# 毎日一题 - 417. 太平洋大西洋水流问题

## 信息卡片

* 时间:2019-08-13
* 题目链接:https://leetcode-cn.com/problems/pacific-atlantic-water-flow
- tag:`Backtracking` `DFS`
- 时间:2019-08-13
- 题目链接:https://leetcode-cn.com/problems/pacific-atlantic-water-flow

* tag:`Backtracking` `DFS`

## 题目描述

给定一个 m x n 的非负整数矩阵来表示一片大陆上各个单元格的高度。“太平洋”处于大陆的左边界和上边界,而“大西洋”处于大陆的右边界和下边界。
Expand All @@ -15,15 +17,14 @@

提示:

输出坐标的顺序不重要
m 和 n 都小于150
输出坐标的顺序不重要 m 和 n 都小于 150

示例:

```
给定下面的 5x5 矩阵:
太平洋 ~ ~ ~ ~ ~
太平洋 ~ ~ ~ ~ ~
~ 1 2 2 3 (5) *
~ 3 2 3 (4) (4) *
~ 2 4 (5) 3 1 *
Expand All @@ -33,25 +34,18 @@ m 和 n 都小于150
返回:
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]] (上图中带括号的单元).
[[0, 4], [1, 3], [1, 4], [2, 2], [3, 0], [3, 1], [4, 0]](上图中带括号的单元).
```



## 参考答案

- 方法1:直接采用回溯法 超时
- 方法 1:直接采用回溯法 超时

直接判断 水流既可以流动到“太平洋”,又能流动到“大西洋”的陆地单元的坐标
采用方法是
回溯法(英语:backtracking)是暴力搜索法中的一种。
在最坏的情况下,回溯法会导致一次复杂度为指数时间的计算。
在这个题目中,这个题目中正好就是如此。
因为需要等到上下左右全部计算完毕才有确定答案。
直接判断 水流既可以流动到“太平洋”,又能流动到“大西洋”的陆地单元的坐标采用方法是回溯法(英语:backtracking)是暴力搜索法中的一种。在最坏的情况下,回溯法会导致一次复杂度为指数时间的计算。在这个题目中,这个题目中正好就是如此。因为需要等到上下左右全部计算完毕才有确定答案。

m 和 n =150,肯定超时。

- 方法2:动态规划+回溯法
- 方法 2:动态规划+回溯法

思路:

Expand All @@ -61,8 +55,7 @@ m 和 n =150,肯定超时。

最后将探测结果进行合并即可。合并的条件就是当前单元既能流入太平洋又能流入大西洋。

![集合](https://p.ipic.vip/r02fm7.jpg)
扩展:
![集合](https://p.ipic.vip/r02fm7.jpg) 扩展:

如果题目改为能够流入大西洋或者太平洋,我们只需要最后合并的时候,条件改为求或即可

Expand Down Expand Up @@ -124,7 +117,6 @@ var pacificAtlantic = function(matrix) {
};
```


- C++ Code

```c++
Expand All @@ -139,12 +131,12 @@ var pacificAtlantic = function(matrix) {
int col = matrix[0].size();
if ( 0 == col )
return(out);

/* 能流动到“太平洋"的陆地 */
vector<vector<bool> > dp1( row, vector<bool>( col, false ) );
/* 能流动到“大西洋"的陆地 */
vector<vector<bool> > dp2( row, vector<bool>( col, false ) );

/* 从第一行/最后一行出发寻找连同节点,不变的x坐标 */
for ( int j = 0; j < col; j++ )
{
Expand All @@ -157,7 +149,7 @@ var pacificAtlantic = function(matrix) {
dfs( i, 0, INT_MIN, matrix, dp1 );
dfs( i, col - 1, INT_MIN, matrix, dp2 );
}

vector<int> temp( 2 );
for ( int i = 0; i < row; i++ )
{
Expand All @@ -174,9 +166,9 @@ var pacificAtlantic = function(matrix) {
}
return(out);
}
void dfs( int row, int col, int height,


void dfs( int row, int col, int height,
vector<vector<int> > & matrix, vector<vector<bool> > & visited )
{
if ( row < 0 || row >= matrix.size() ||
Expand All @@ -185,19 +177,19 @@ var pacificAtlantic = function(matrix) {
{
return;
}

if ( visited[row][col] == true )
{
return;
}

if ( height > matrix[row][col] )
{
return;
}

visited[row][col] = true;

dfs( row + 1, col, matrix[row][col], matrix, visited );
dfs( row - 1, col, matrix[row][col], matrix, visited );
dfs( row, col + 1, matrix[row][col], matrix, visited );
Expand All @@ -206,10 +198,6 @@ var pacificAtlantic = function(matrix) {
};
```
## 其他优秀解答
> ##### 暂缺
2 changes: 1 addition & 1 deletion introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

有些动图,在做成电子书(比如 pdf)的时候自然就变没了,如果需要看动图的, 可以去我的公众号《力扣加加》或者我的 leetcode 题解仓库看。

<img src="https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg" width="100%">
<img src="https://p.ipic.vip/n8gbxo.jpg" width="100%">

> epub 还是有动图的
Expand Down
63 changes: 28 additions & 35 deletions problems/1011.capacity-to-ship-packages-within-d-days-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ https://leetcode-cn.com/problems/capacity-to-ship-packages-within-d-days

A conveyor belt has packages that must be shipped from one port to another within D days.

The i-th package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.
The i-th package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.

Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within D days.

Expand All @@ -15,23 +15,23 @@ Return the least weight capacity of the ship that will result in all the package
```
Input: weights = [1,2,3,4,5,6,7,8,9,10], D = 5
Output: 15
Explanation:
Explanation:
A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
1st day: 1, 2, 3, 4, 5
2nd day: 6, 7
3rd day: 8
4th day: 9
5th day: 10
Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
```

**Example 2:**

```
Input: weights = [3,2,2,4,1,4], D = 3
Output: 6
Explanation:
Explanation:
A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
1st day: 3, 2
2nd day: 2, 4
Expand All @@ -43,49 +43,42 @@ A ship capacity of 6 is the minimum to ship all the packages in 3 days like this
```
Input: weights = [1,2,3,1,1], D = 4
Output: 3
Explanation:
Explanation:
1st day: 1
2nd day: 2
3rd day: 3
4th day: 1, 1
```



**Note:**
**Note:**

1. `1 <= D <= weights.length <= 50000`
2. `1 <= weights[i] <= 500`



## Solution

The problem is same as [**LeetCode 875 koko-eating-bananas**] (https://github.com/azl397985856/leetcode/blob/master/problems/875.koko-eating-bananas-en.md) practically.
The problem is same as [**LeetCode 875 koko-eating-bananas**](https://github.com/azl397985856/leetcode/blob/master/problems/875.koko-eating-bananas-en.md) practically.

It is easy to solve this kind of problems if you take a closer look into it.
It is easy to solve this kind of problems if you take a closer look into it.


The essence is to search a given number in finite discrete data like [ 1,2,3,4, ... , total ].
The essence is to search a given number in finite discrete data like [ 1,2,3,4, ... , total ].

However, We should find the cargo that can be shipped in D days rather than look for the target directly.


Consider the following questions:

- Can it be shipped if the capacity is 1?
- Can it be shipped if the capacity is 2?
- Can it be shipped if the capacity is 3?
- ...
- Can it be shipped if the capacity is total ? ( Yeap we can, D is greater than or equal to 1)
- Can it be shipped if the capacity is total ? ( Yeap we can, D is greater than or equal to 1)

During the process, we directly `return` if the answer is *yes*.
During the process, we directly `return` if the answer is _yes_.

If the answer is *no*, just keep asking.
If the answer is _no_, just keep asking.

This is a typical binary search problem, the only difference is the judgement condition:


```python
def canShip(opacity):
# Whether the capacity of the specified ship can be shipped in D days
Expand Down Expand Up @@ -146,38 +139,38 @@ class Solution:
* @param {number} D
* @return {number}
*/
var shipWithinDays = function(weights, D) {
let high = weights.reduce((acc, cur) => acc + cur)
let low = 0
var shipWithinDays = function (weights, D) {
let high = weights.reduce((acc, cur) => acc + cur);
let low = 0;

while(low < high) {
let mid = Math.floor((high + low) / 2)
while (low < high) {
let mid = Math.floor((high + low) / 2);
if (canShip(mid)) {
high = mid
high = mid;
} else {
low = mid + 1
low = mid + 1;
}
}

return low
return low;

function canShip(opacity) {
let remain = opacity
let count = 1
let remain = opacity;
let count = 1;
for (let weight of weights) {
if (weight > opacity) {
return false
return false;
}
remain -= weight
remain -= weight;
if (remain < 0) {
count++
remain = opacity - weight
count++;
remain = opacity - weight;
}
if (count > D) {
return false
return false;
}
}
return count <= D
return count <= D;
}
};
```
Expand Down
Loading

0 comments on commit 499b9ef

Please sign in to comment.