Problem Statement
Adrian is a runner and every morning he goes for a run with his friends. Every morning, their coach gives them a list of checkpoints from left to right to cover. Each checkpoint has a special value. Now, the coach has a rule such that a runner will only go to checkpoints whose value is strictly higher than the previous checkpoints. Also, all runners are supposed to move strictly towards the right. You need to find out the minimum number of runners it would take to cover all the checkpoints.
The input is taken in the form of an array which denotes the values of checkpoints from left to right.
Sample Input
[12, 15, 18, 17, 20, 25, 27, 19, 20, 21]Sample Output
2Explanation of the Sample Case:
First runner will cover [12, 15, 18, 19, 20, 21] and the second runner will cover [17, 25, 27].
My code
My recursive algorithm gives the correct output but it is not efficient enough.
visited = [0] * len(A)
def ans(A, visited):
n = len(A)
if visited.count(0) == 0:
return 0
num = 0
ind = visited.index(0)
visited[ind] = 1
min_num = A[ind]
for i in range(ind, n):
if A[i] > min_num and visited[i] == 0:
visited[i] = 1
min_num = A[i]
return 1 + ans(A, visited)
Question
What's the way this problem could be solved more efficiently? My code was giving TLE on some test cases. I couldn't figure out how to make it work.
My code was giving the correct output. It is just not efficient enough.