Dynamic Programming | High-effort vs. Low-effort Tasks Problem

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.

Examples:

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

Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

Optimal Substructure
To find the maximum amount of tasks done till i’th day, we need to compare 2 choices:

1. Go for high effort tasks on that day, then find the maximum amount of tasks done till (i – 2) th day.
2. 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.

Overlapping 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.

C

 // A naive recursive C program to find maximum // tasks. #include    // Returns the maximum among the 2 numbers int max(int x, int y) {     return (x > y ? x : y); }    // Returns maximum amount of task that can be // done till day n int maxTasks(int high[], int low[], int n) {     // If n is less than equal to 0, then no     // solution exists     if (n <= 0)         return 0;        /* Determines which task to choose on day n,        then returns the maximum till that day */     return max(high[n-1] + maxTasks(high, low, (n-2)),               low[n-1] + maxTasks(high, low, (n-1))); }    // Driver program to test above function int main() {     int n = 5;     int high[] = {3, 6, 8, 7, 6};     int low[] = {1, 5, 4, 5, 3};     printf("%dn", maxTasks(high, low, n));     return 0; }

Java

 // A naive recursive Java program  // to find maximum tasks.    class GFG{            // Returns maximum amount of task      // that can be done till day n     static int maxTasks(int high[], int low[], int n)     {                    // If n is less than equal to 0,         // then no solution exists         if (n <= 0)             return 0;            /* Determines which task to choose on day n,             then returns the maximum till that day */         return Math.max(high[n - 1] + maxTasks(high, low, (n - 2)),                 low[n - 1] + maxTasks(high, low, (n - 1)));     }        // Driver code     public static void main(String []args)     {         int n = 5;         int high[] = {3, 6, 8, 7, 6};         int low[] = {1, 5, 4, 5, 3};         System.out.println( maxTasks(high, low, n));     } }    // This code is contributed by Ita_c.

Python3

 # A naive recursive Python3 program to  # find maximum tasks.     # Returns maximum amount of task  # that can be done till day n  def maxTasks(high, low, n) :            # If n is less than equal to 0,      # then no solution exists      if (n <= 0) :         return 0        # Determines which task to choose on day n,      # then returns the maximum till that day      return max(high[n - 1] + maxTasks(high, low, (n - 2)),                 low[n - 1] + maxTasks(high, low, (n - 1)));     # Driver Code if __name__ == "__main__" :         n = 5;      high = [3, 6, 8, 7, 6]      low = [1, 5, 4, 5, 3]     print(maxTasks(high, low, n));     # This code is contributed by Ryuga

C#

 // A naive recursive C# program  // to find maximum tasks. using System;     class GFG {            // Returns maximum amount of task      // that can be done till day n     static int maxTasks(int[] high,                     int[] low, int n)     {                    // If n is less than equal to 0,         // then no solution exists         if (n <= 0)             return 0;            /* Determines which task to choose on day n,             then returns the maximum till that day */         return Math.Max(high[n - 1] +              maxTasks(high, low, (n - 2)), low[n - 1] +              maxTasks(high, low, (n - 1)));     }        // Driver code     public static void Main()     {         int n = 5;         int[] high = {3, 6, 8, 7, 6};         int[] low = {1, 5, 4, 5, 3};         Console.Write( maxTasks(high, low, n));     } }    // This code is contributed by Ita_c.

PHP



Output :

20

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

PHP

Output:

20

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 contribute@geeksforgeeks.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.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

2

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.