- Related Questions & Answers
- Program to find maximum time to finish K tasks in Python
- Program to schedule tasks to take smallest amount of time in Python
- Find minimum time to finish all jobs with given constraints in Python
- How to run multiple async tasks and waiting for them all to complete in C#?
- Program to Find Out the Minimum Cost to Purchase All in Python
- Program to find minimum swaps needed to group all 1s together in Python
- Program to find minimum bus fare for travelling all days in Python?
- Program to find minimum sum of difficulties to complete jobs over k days in C++
- Find minimum time to finish all jobs with given constraints in C++
- Program to find minimum amount needed to be paid all good performers in Python
- Program to find minimum number of movie theatres required to show all movies in python
- Program to find minimum radius to light up all houses on a street in Python
- Program to find minimum distance that needs to be covered to meet all person in Python
- Program to find in which interval how many tasks are worked on in Python
- Program to find number of tasks can be finished with given conditions in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of numbers called nums where each value is determining a unit of time it takes to finish a task. We can skip any non-consecutive tasks, we have to find the minimum time it takes to finish all tasks.

So, if the input is like nums = [11, 6, 8, 16], then the output will be 14, as we can skip the first and last tasks.

To solve this, we will follow these steps:

- n := size of nums
- table := make a n x 2 matrix and fill this with 0
- table[0, 0] := 0
- table[0, 1] := nums[0]
- for i in range 1 to n - 1, do
- table[i, 0] := table[i - 1, 1]
- table[i, 1] = (minimum of table[i - 1, 0] and table[i - 1][1]) + nums[i]

- return minimum of table row [n - 1]

Let us see the following implementation to get better understanding:

class Solution: def solve(self, nums): n = len(nums) table = [[0] * 2 for _ in range(n)] table[0][0] = 0 table[0][1] = nums[0] for i in range(1, n): table[i][0] = table[i - 1][1] table[i][1] = min(table[i - 1][0], table[i - 1][1]) + nums[i] return min(table[n - 1]) ob = Solution() nums = [11, 6, 8, 16] print(ob.solve(nums))

[11, 6, 8, 16]

14

Advertisements