You are given n days and for each day (di) you could either perform a high effort tasks (hi) or a low effort tasks (li) or no task with the constraint that you can choose a high-effort tasks only if you chose no task on the previous day. Write a program to find the maximum amount of tasks you can perform within these n days.
No. of days (n) = 5 Day L.E. H.E 1 1 3 2 5 6 3 4 8 4 5 7 5 3 6 Maximum amount of tasks = 3 + 5 + 4 + 5 + 3 = 20
To find the maximum amount of tasks done till i’th day, we need to compare 2 choices:
- Go for high effort tasks on that day, then find the maximum amount of tasks done till (i – 2) th day.
- Go for low effort task on that day and find the maximum amount of tasks done till (i – 1) th day.
Let high [1…n] be the input array for high effort task amount on i’th day and low [1…n] be the input array for low effort task amount on ith day.
Let max_task (high , low , i) be the function that returns maximum amount of task done till ith day, so it will return max(high[i] + max_task(high, low, (i – 2)), low [i] + max_task (high, low, (i – 1)))
Therefore, the problem has optimal substructure property as the problem can be solved using solutions to subproblems.
Following is a simple recursive implementation of the High-effort vs. Low-effort task problem. The implementation simply follows the recursive structure mentioned above. So, High-effort vs. Low-effort Task Problem has both properties of a dynamic programming problem.
It should be noted that the above function computes the same subproblems again and again.
Therefore, this problem has Overlapping Subproblems Property. So the High-effort vs. Low-effort Task Problem has both the properties of a dynamic programming problem.
Dynamic Programming Solution
Time Complexity : O(n)
This article is contributed by Akash Aggarwal .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Tile Stacking Problem
- Minimum cost to fill given weight in a bag
- Find number of times a string occurs as a subsequence in given string
- Find Jobs involved in Weighted Job Scheduling
- Printing Longest Bitonic Subsequence
- Find minimum adjustment cost of an array
- Maximize arr[j] - arr[i] + arr[l] - arr[k], such that i < j < k < l
- Construction of Longest Increasing Subsequence using Dynamic Programming
- Find if string is K-Palindrome or not | Set 2
- Printing Longest Common Subsequence | Set 2 (Printing All)
- Longest Common Increasing Subsequence (LCS + LIS)
- Wildcard Pattern Matching
- Weighted Job Scheduling
- Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
- Longest Palindromic Subsequence | DP-12