Editing
2020 Jan Gold Problem 1 Time is Mooney
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Solution == The solution to this problem can be solved using dynamic programming. We create an array dp[N+1] where dp[i] represents the minimum amount of time needed to complete the first i tasks. We can then fill the array using the following recurrence relation: dp[i] = min(dp[i], dp[i-1] + time[i]) Where time[i] is the time required to complete the i-th task. We can then iterate through the array and find the minimum amount of time needed to complete all tasks. The following is a C++ implementation of the solution: <pre> int N; // number of tasks int T; // total time available int time[N+1]; // time required to complete each task int dp[N+1]; // dp[i] represents the minimum amount of time needed to complete the first i tasks dp[0] = 0; for (int i = 1; i <= N; i++) { dp[i] = T + 1; // set dp[i] to a value greater than T for (int j = 0; j < i; j++) { dp[i] = min(dp[i], dp[j] + time[i]); } } int ans = T + 1; for (int i = 0; i <= N; i++) { ans = min(ans, dp[i]); } if (ans > T) { cout << "Not possible" << endl; } else { cout << ans << endl; } </pre> [[Category:Yearly_2019_2020]] [[Category:Gold]] [[Category:Dynamic Programming]] [[Category:Graph]] [[Category:DFS]] [[Category:Memoization]]
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information