Skip to content

Commit

Permalink
docs: 更新 README
Browse files Browse the repository at this point in the history
  • Loading branch information
lucifer committed Jun 16, 2020
1 parent 95e9dad commit e4394dd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 47 deletions.
24 changes: 22 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@

![](https://tva1.sinaimg.cn/large/007S8ZIlly1gdvenxvjlsj30z90dtdhw.jpg)

## 非科学人士看过来

如果是国内的非科学用户,可以使用 https://lucifer.ren/leetcode ,整站做了静态化,速度贼快!但是阅读体验可能一般,大家也可以访问[力扣加加](http://leetcode-solution.cn/)(暂时没有静态化)获得更好的阅读体验。

## 怎么刷 LeetCode?

- [我是如何刷 LeetCode 的](https://www.zhihu.com/question/280279208/answer/824585814)
- [算法小白如何高效、快速刷 leetcode?](https://www.zhihu.com/question/321738058/answer/1279464192)

## 刷题插件(开发中)

- [刷题效率低?或许你就差这么一个插件](https://lucifer.ren/blog/2020/06/06/algo-chrome-extension/)

## 91 天学算法

- [91 天,遇见不一样的自己](https://lucifer.ren/blog/2020/05/23/91-algo/)

## 介绍

leetcode 题解,记录自己的 leetcode 解题之路。
Expand Down Expand Up @@ -311,12 +328,13 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [《贪婪策略》专题](./thinkings/greedy.md)
- [《深度优先遍历》专题](./thinkings/DFS.md)
- [滑动窗口(思路 + 模板)](./thinkings/slide-window.md)
- [位运算](./thinkings/bit.md) 🆕
- [位运算](./thinkings/bit.md)
- [设计题](./thinkings/design.md) 🆕
- [小岛问题](./thinkings/island.md) 🆕
- [小岛问题](./thinkings/island.md)
- [最大公约数](./thinkings/GCD.md) 🆕
- [并查集](./thinkings/union-find.md) 🆕
- [前缀和](./thinkings/prefix.md) 🆕
- [字典序列删除](https://lucifer.ren/blog/2020/06/13/%E5%88%A0%E9%99%A4%E9%97%AE%E9%A2%98/)🆕

### anki 卡片

Expand Down Expand Up @@ -368,6 +386,8 @@ anki - 文件 - 导入 - 下拉格式选择“打包的 anki 集合”,然后

- 单调栈

- BFS & DFS

## 关注我

点关注,不迷路。如果再给 ➕ 个星标就更棒啦!
Expand Down
92 changes: 47 additions & 45 deletions problems/887.super-egg-drop.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@

## 题目地址

https://leetcode.com/problems/super-egg-drop/description/

## 题目描述

```
You are given K eggs, and you have access to a building with N floors from 1 to N.
You are given K eggs, and you have access to a building with N floors from 1 to N.
Each egg is identical in function, and if an egg breaks, you cannot drop it again.
You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break.
Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).
Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).
Your goal is to know with certainty what the value of F is.
What is the minimum number of moves that you need to know with certainty what F is, regardless of the initial value of F?
Example 1:
Input: K = 1, N = 2
Output: 2
Explanation:
Explanation:
Drop the egg from floor 1. If it breaks, we know with certainty that F = 0.
Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1.
If it didn't break, then we know with certainty F = 2.
Expand All @@ -36,7 +36,7 @@ Example 3:
Input: K = 3, N = 14
Output: 4
Note:
Expand All @@ -52,82 +52,84 @@ Note:

## 思路

> 本题已经重制,重制版更清晰 [《丢鸡蛋问题》重制版来袭~](https://lucifer.ren/blog/2020/06/08/887.super-egg-drop/)
这是一道典型的动态规划题目,但是又和一般的动态规划不一样。

拿题目给的例子为例,两个鸡蛋,六层楼,我们最少扔几次?

![887.super-egg-drop-1](../assets/problems/887.super-egg-drop-1.png)

一个符合直觉的做法是,建立dp[i][j], 代表i个鸡蛋,j层楼最少扔几次,然后我们取dp[K][N]即可。
一个符合直觉的做法是,建立 dp[i][j], 代表 i 个鸡蛋,j 层楼最少扔几次,然后我们取 dp[K][n]即可。

代码大概这样的:

```js
const dp = Array(K + 1);
dp[0] = Array(N + 1).fill(0);
for (let i = 1; i < K + 1; i++) {
dp[i] = [0];
for (let j = 1; j < N + 1; j++) {
// 只有一个鸡蛋
if (i === 1) {
dp[i][j] = j;
continue;
}
// 只有一层楼
if (j === 1) {
dp[i][j] = 1;
continue;
}

// 每一层我们都模拟一遍
const all = [];
for (let k = 1; k < j + 1; k++) {
const brokenCount = dp[i - 1][k - 1]; // 如果碎了
const notBrokenCount = dp[i][j - k]; // 如果没碎
all.push(Math.max(brokenCount, notBrokenCount)); // 最坏的可能
}
dp[i][j] = Math.min(...all) + 1; // 最坏的集合中我们取最好的情况
}
const dp = Array(K + 1);
dp[0] = Array(N + 1).fill(0);
for (let i = 1; i < K + 1; i++) {
dp[i] = [0];
for (let j = 1; j < N + 1; j++) {
// 只有一个鸡蛋
if (i === 1) {
dp[i][j] = j;
continue;
}
// 只有一层楼
if (j === 1) {
dp[i][j] = 1;
continue;
}

return dp[K][N];
// 每一层我们都模拟一遍
const all = [];
for (let k = 1; k < j + 1; k++) {
const brokenCount = dp[i - 1][k - 1]; // 如果碎了
const notBrokenCount = dp[i][j - k]; // 如果没碎
all.push(Math.max(brokenCount, notBrokenCount)); // 最坏的可能
}
dp[i][j] = Math.min(...all) + 1; // 最坏的集合中我们取最好的情况
}
}

return dp[K][N];
```

果不其然,当我提交的时候,超时了。 这个的时复杂度是很高的,可以看到,我们内层暴力的求解所有可能,然后
取最好的,这个过程非常耗时,大概是O(N^2 * K).
取最好的,这个过程非常耗时,大概是 O(N^2 \* K).

然后我看了一位leetcode[网友](https://leetcode.com/lee215/)的回答,
然后我看了一位 leetcode[网友](https://leetcode.com/lee215/)的回答,
他的想法是`dp[M][K]means that, given K eggs and M moves,what is the maximum number of floor that we can check.`

我们按照他的思路重新建模:

![887.super-egg-drop-2](../assets/problems/887.super-egg-drop-2.png)

可以看到右下角的部分根本就不需要计算,从而节省很多时间
## 关键点解析

- dp建模思路要发生变化, 即
`dp[M][K]means that, given K eggs and M moves,what is the maximum number of floor that we can check.`
## 关键点解析

- dp 建模思路要发生变化, 即
`dp[M][K]means that, given K eggs and M moves,what is the maximum number of floor that we can check.`

## 代码


```js
/**
* @param {number} K
* @param {number} N
* @return {number}
*/
var superEggDrop = function(K, N) {
var superEggDrop = function (K, N) {
// 不选择dp[K][M]的原因是dp[M][K]可以简化操作
const dp = Array(N + 1).fill(0).map(_ => Array(K + 1).fill(0))

const dp = Array(N + 1)
.fill(0)
.map((_) => Array(K + 1).fill(0));

let m = 0;
while (dp[m][K] < N) {
m++;
for (let k = 1; k <= K; ++k)
dp[m][k] = dp[m - 1][k - 1] + 1 + dp[m - 1][k];
m++;
for (let k = 1; k <= K; ++k) dp[m][k] = dp[m - 1][k - 1] + 1 + dp[m - 1][k];
}
return m;
};
Expand Down

0 comments on commit e4394dd

Please sign in to comment.