forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #329
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
5 commits
Select commit
Hold shift + click to select a range
ec02f92
Create 3592.Inverse-Coin-Change.cpp
wisdompeak 67c5ed5
Update Readme.md
wisdompeak e918891
Create Readme.md
wisdompeak 97f67fa
Create 3593.Minimum-Increments-to-Equalize-Leaf-Paths.cpp
wisdompeak 2f2a9ad
Update Readme.md
wisdompeak 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
37 changes: 37 additions & 0 deletions
...imum-Increments-to-Equalize-Leaf-Paths/3593.Minimum-Increments-to-Equalize-Leaf-Paths.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,37 @@ | ||
class Solution { | ||
vector<int>adj[100005]; | ||
public: | ||
pair<long long, int>dfs(int u, int p, vector<int>&cost) { | ||
long long maxPath = 0; | ||
int totalChanged = 0; | ||
vector<long long>paths; | ||
|
||
for (int v: adj[u]) { | ||
if (v==p) continue; | ||
auto [path, changed] = dfs(v, u, cost); | ||
maxPath = max(maxPath, path); | ||
totalChanged += changed; | ||
paths.push_back(path); | ||
} | ||
|
||
int count = 0; | ||
for (long long p: paths) { | ||
if (p < maxPath) | ||
count++; | ||
} | ||
|
||
return {cost[u]+maxPath, totalChanged + count}; | ||
} | ||
|
||
int minIncrease(int n, vector<vector<int>>& edges, vector<int>& cost) { | ||
for (auto& edge: edges) { | ||
int u = edge[0], v = edge[1]; | ||
adj[u].push_back(v); | ||
adj[v].push_back(u); | ||
} | ||
|
||
auto [_, ret ] = dfs(0, -1, cost); | ||
|
||
return ret; | ||
} | ||
}; |
26 changes: 26 additions & 0 deletions
Dynamic_Programming/3592.Inverse-Coin-Change/3592.Inverse-Coin-Change.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,26 @@ | ||
class Solution { | ||
public: | ||
vector<int> findCoins(vector<int>& numWays) { | ||
int n = numWays.size(); | ||
numWays.insert(numWays.begin(), 0); | ||
|
||
vector<int>rets; | ||
vector<int>dp(n+1); | ||
dp[0] = 1; | ||
for (int c=1; c<=n; c++) { | ||
if (numWays[c] == dp[c]) | ||
continue; | ||
rets.push_back(c); | ||
for (int i=c; i<=n; i++) { | ||
dp[i] += dp[i-c]; | ||
} | ||
} | ||
|
||
for (int i=1; i<=n; i++) { | ||
if (dp[i]!=numWays[i]) | ||
return {}; | ||
} | ||
|
||
return rets; | ||
} | ||
}; |
26 changes: 26 additions & 0 deletions
Dynamic_Programming/3592.Inverse-Coin-Change/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,26 @@ | ||
### 3592.Inverse-Coin-Change | ||
|
||
这是一道非常有意思的"反向重构"的DP题。 | ||
|
||
首先,从题境上来看这类似一道经典的背包问题。给出一系列硬币面值coins可以无限重复使用,问有多少种构造方法能拼出面值n来?对于这个问题,我们有如下动态规划解法。特别注意最外层的循环是硬币面额 | ||
``` | ||
for (int c: coins) | ||
for (int i=c; i<=n; i++) | ||
dp[i] += dp[i-c]; | ||
``` | ||
状态转移的思想就是,每轮引入一种新的硬币面额c:遍历所有面值i,考察最后一个硬币是否使用c。如果不使用,那么dp[i]依然是前一轮的数值;如果使用c,那么就取决于dp[i-c]。 | ||
|
||
我们可以沿用同样的思路,来思考numWays(也就是dp)是怎么计算出来的 | ||
``` | ||
for (int c: coins) { | ||
if c 不存在 | ||
continue; | ||
else if c 存在 | ||
for (int i=c; i<=n; i++) | ||
dp[i] += dp[i-c]; | ||
} | ||
``` | ||
|
||
那么如何判断c是否存在呢?突破点就是面额c的构造方法。如果numWays[c]等于前一轮(还没有引入面额c的时候)的dp[c],那么就意味着面额c一定不存在。否则c的构造必然至少还能增加一种方法(即只使用一个c硬币)。反之,面额c必须存在,否则无法弥补这个差额。 | ||
|
||
当然,我们仅凭numWays[c]的分析来决定面额的种类还是比较片面的,它不能保证最终据此计算得到的dp都与所有的numsWays一致。所以我们还要再次校验一下。 |
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.