- 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 and that is showing the bus stops on a line where nums[i] shows the time a bus must arrive at station i. Now that buses can only move forward, we have to find the minimum number of buses that are needed to pass through all the stops.

So, if the input is like nums = [1, 2, 7, 9, 3, 4], then the output will be 2, as one bus can take stops [1, 2, 3, 4] and another can do [7, 9].

To solve this, we will follow these steps−

ans := 0

seen := a list whose length is same as nums and initially filled with false

for each index i and corresponding n in nums, do

if seen[i] is false, then

seen[i] := True

ans := ans + 1

prev := n

for j in range i+1 to size of nums, do

if nums[j] > prev and seen[j] is false, then

seen[j] := True

prev := nums[j]

return ans

Let us see the following implementation to get better understanding −

class Solution: def solve(self, nums): ans = 0 seen = [False] * len(nums) for i, n in enumerate(nums): if not seen[i]: seen[i] = True ans += 1 prev = n for j in range(i+1, len(nums)): if nums[j] > prev and not seen[j]: seen[j] = True prev = nums[j] return ans ob = Solution() nums = [1, 2, 7, 9, 3, 4] print(ob.solve(nums))

[1, 2, 7, 9, 3, 4]

2

- Related Questions & Answers
- Program to find minimum number of movie theatres required to show all movies in python
- Program to find minimum number of pins required to hang all banners in C++
- Program to find number of minimum steps required to meet all person at any cell in Python
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum number of hops required to reach end position in Python
- Program to find minimum number of buses required to reach final target in python
- Program to find minimum number of flips required to have alternating values in Python
- Program to find minimum number of operations required to make lists strictly Increasing in python
- Minimum Number of Refueling Stops in C++
- Program to find minimum number of operations required to make one string substring of other in Python
- Program to find minimum number of bricks required to make k towers of same height in Python
- Program to find minimum number of vertices to reach all nodes using Python
- Program to find minimum number of steps required to catch the opponent in C++
- Program to find minimum number of deletions required from two ends to make list balanced in Python
- Program to count number of minimum swaps required to make it palindrome in Python

Advertisements