- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 maximum number of courses we can take based on interval time in Python?
Suppose we have a list of intervals in the form [start, end], this is representing the start and end time of a course. We have to find the maximum number of courses we can take, assuming we can only take one course at a time and the start of a course needs to be later than the end of the last course.
So, if the input is like times = [[3, 6],[6, 9],[7, 8],[9, 11]], then the output will be 3, as we can take courses [[3, 6], [7, 8], [9, 11]]
To solve this, we will follow these steps:
sort time based on ending time
counter := 0, end := -1
for i in range 0 to size of times, do
if times[i, 0] > end is non-zero, then
counter := counter + 1
end := times[i, 1]
return counter
Let us see the following implementation to get better understanding:
Example
class Solution: def solve(self, times): times.sort(key=lambda x: x[1]) counter = 0 end = -1 for i in range(len(times)): if times[i][0] > end: counter += 1 end = times[i][1] return counter ob = Solution() times = [ [3, 6], [6, 9], [7, 8], [9, 11] ] print(ob.solve(times))
Input
[ [3, 6],[6, 9],[7, 8],[9, 11]]
Output
3
- Related Articles
- Program to check whether we can take all courses or not in Python
- Program to find maximum number of coins we can collect in Python
- Program to find maximum number of coins we can get using Python
- Program to find maximum number of people we can make happy in Python
- Program to find maximum number of boxes we can fit inside another boxes in python
- Program to find the maximum profit we can get by buying on stock market once in Python
- Program to find maximum number of consecutive values you can make in Python
- Program to find maximum score we can get in jump game in Python
- Program to find maximum how many water bottles we can drink in Python
- Program to find the maximum profit we can get by buying on stock market multiple times in Python
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python
- Program to find maximum credit we can get by finishing some assignments in python
- Program to find maximum coins we can get from disappearing coins matrix in Python
- Program to find maximum value of k for which we can maintain safe distance in Python

Advertisements