- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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

- Related Questions & Answers
- Program to find minimum time required to complete tasks with k time gap between same type tasks in Python
- Program to find minimum time to finish all jobs in Python
- Program to find maximum time to finish K tasks in Python
- Find minimum time to finish all jobs with given constraints in Python
- Program to schedule tasks to take smallest amount of time in Python
- Program to find minimum cost to connect all points 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 out the minimum path to deliver all letters in Python
- Program to find minimum sum of difficulties to complete jobs over k days in C++
- Program to find minimum bus fare for travelling all days in Python?
- Find minimum time to finish all jobs with given constraints in C++
- Program to find minimum number of vertices to reach all nodes using Python
- Program to find minimum amount needed to be paid all good performers in Python

Advertisements