Skip to content

Commit

Permalink
Update 高楼扔鸡蛋问题.md
Browse files Browse the repository at this point in the history
将添加的C++代码移动到了文末
  • Loading branch information
tianzhongwei authored and labuladong committed Jun 24, 2020
1 parent e95794b commit a2e0baf
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion 动态规划系列/高楼扔鸡蛋问题.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,43 @@ def superEggDrop(self, K: int, N: int) -> int:

![labuladong](../pictures/labuladong.png)

[renxiaoyao](https://github.com/tianzhongwei) 提供C++解法代码:
1) 状态定义一:动态规划 + 二分查找
```C++
class Solution {
public:
int superEggDrop(int K,int N) {
vector<vector<int>> dp(N + 1,vector<int>(K + 1,0));
for(int j = 1 ; j <= K ; ++j)
dp[1][j] = 1;

for(int i = 1 ; i <= N ; ++i)
dp[i][1] = i;
for(int i = 2 ; i <= N ; ++i)
for(int j = 2 ; j <= K ; ++j)
dp[i][j] = binary_Valley(i,j,dp);
return dp[N][K];
}
private:
int binary_Valley(int floors,int eggs,vector<vector<int>>& dp) {
int l = 1;
int r = floors;
while(l < r) {
int LMid = l + (r - l) / 2;
int broken = dp[LMid - 1][eggs - 1];
int not_broken = dp[floors - LMid][eggs];
if(not_broken > broken)
l = LMid + 1;
else
r = LMid;
}
return max(dp[r - 1][eggs - 1],dp[floors - r][eggs]) + 1;
}
};
```

[上一篇:编辑距离](../动态规划系列/编辑距离.md)

[下一篇:经典动态规划问题:高楼扔鸡蛋(进阶)](../动态规划系列/高楼扔鸡蛋进阶.md)

[目录](../README.md#目录)
[目录](../README.md#目录)

0 comments on commit a2e0baf

Please sign in to comment.