# Bus Stop - Microsoft Top Interview Questions

### Problem Statement :

```You are given a list of integers nums representing bus stops on a line where nums[i] represents the time a bus must arrive at stop i.

Given that buses can only move forward, return the minimum number of buses that are needed to pass through all the stops.

Constraints

n ≤ 1,000 where n is length of nums.

Example 1

Input

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

Output

2

Explanation

One bus can take this route [1, 2, 3, 4] and another can do [7, 9].

Example 2

Input

nums = [5, 5]

Output

2

Explanation

One bus can't be at two places at once so we need 2 busses.```

### Solution :

```                        ```Solution in C++ :

int solve(vector<int>& nums) {
multiset<int> buses;

for (int num : nums)
if (buses.empty() or num <= *buses.begin())
buses.insert(num);
else
buses.erase(prev(buses.lower_bound(num))), buses.insert(num);

return buses.size();
}```
```

```                        ```Solution in Java :

import java.util.*;

class Solution {
public int solve(int[] nums) {
int ans = 0;
TreeMap<Integer, Integer> set = new TreeMap<>();
for (int i : nums) {
Integer floor = set.floorKey(i);
if (floor != null && floor.intValue() == i)
floor = set.floorKey(i - 1);
if (floor == null) {
ans++;
} else {
if (set.get(floor) == 1)
set.remove(floor);
else
set.put(floor, set.getOrDefault(floor, 0) - 1);
}
set.put(i, set.getOrDefault(i, 0) + 1);
}
return ans;
}
}```
```

```                        ```Solution in Python :

from bisect import insort, bisect_right

class Solution:
def solve(self, a):
n = len(a)
buses = [a[n - 1]]
for i in range(n - 2, -1, -1):
idx = bisect_right(buses, a[i])
if idx == len(buses):
buses.append(a[i])
else:
buses[idx] = a[i]
return len(buses)```
```

## View More Similar Problems

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

## Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

## Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

## Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

## Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

## Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share