forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
...ramming/2403.Minimum-Time-to-Kill-All-Monsters/2403.Minimum-Time-to-Kill-All-Monsters.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}; |
16 changes: 16 additions & 0 deletions
Dynamic_Programming/2403.Minimum-Time-to-Kill-All-Monsters/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.