- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find minimum time to complete all tasks in python
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:
Example
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))
Input
[11, 6, 8, 16]
Output
14
Advertisements