Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 4 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using LL = long long;
class Solution {
public:
long long minimumTime(vector<int>& power)
{
int n = power.size();
vector<LL>dp(1<<n, LLONG_MAX/2);
dp[0] = 0;
for (int state = 1; state < (1<<n); state++)
{
int k = __builtin_popcount(state);
for (int i=0; i<n; i++)
{
if ((state>>i)&1)
dp[state] = min(dp[state], dp[state- (1<<i)] + (power[i]-1)/k+1);
}
}
return dp[(1<<n)-1];
}
};
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### 2403.Minimum-Time-to-Kill-All-Monsters

本题首先需要知道,并不是将monster按照从小到大排序就是最优解。Example 1 就是一个反例。考虑到monster的个数只有17,说明所有的状态数是`2^17=131072`,使用状态压缩dp的复杂度是可以接受的。

我们令二进制数state表示哪些怪兽被消灭,dp[state]表示消灭这些怪兽所需要的最少天数,因此状态转移方程取决于state里面哪一个是最后一只被消灭的怪兽,穷举一下即可:
```cpp
int k = __builtin_popcount(state); // state所对应的怪兽数目
for (int i=0; i<n; i++)
{
if (bit i属于state的子集)
dp[state] = min{ dp[state - (1<<i)] + 怪兽i作为第k个目标所需要花费的时间}
}
```
最后返回`dp[(1<<n)-1]`.

整体的时间复杂度是`2^17*17 = 2e6`。
1 change: 1 addition & 0 deletions Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@
[1931.Painting-a-Grid-With-Three-Different-Colors](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1931.Painting-a-Grid-With-Three-Different-Colors) (M+)
[1994.The-Number-of-Good-Subsets](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1994.The-Number-of-Good-Subsets) (H)
[2184.Number-of-Ways-to-Build-Sturdy-Brick-Wall](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2184.Number-of-Ways-to-Build-Sturdy-Brick-Wall) (H-)
[2403.Minimum-Time-to-Kill-All-Monsters](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2403.Minimum-Time-to-Kill-All-Monsters) (M+)
* ``枚举集合的子集``
[1494.Parallel-Courses-II](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1494.Parallel-Courses-II) (H)
[1655.Distribute-Repeating-Integers](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1655.Distribute-Repeating-Integers) (H)
Expand Down

AltStyle によって変換されたページ (->オリジナル) /