Can somebody explain the time complexity of the problem?

Given an m*n matrix with each row representing a person's task status. Task status can be A, B

A -> Completed tasks
B -> Not started

The person finishes tasks in sequence so there can't be an A after a B. So all the As are contiguous.

Now we need to find the person with most number of completed tasks.

[[A,B,B,B],
[A,A,B,B],
[B,B,B,B],
[A,A,A,B]]

Now in this scenario the last person has most number of completed task.

We can solve it by going over all the cells of the matrix making it O(m*n).

We can do better by doing a binary search on each rows to find the last 'A' per row which gives O(m*log(n)).

We can still speed this up. Lets say on ith row the last A is at position k. For the next rows we will be looking for As from k+1 position till the end where the last A maybe. If the k+1 position of the next row is a B then just skip that row as there can't be any A after the B.

I am trying to find out what would be the complexity in this scenario. Its definitely better than m*log(n) as we will be skipping cells per row depending on where the A was in the previous row but how do I formulate the big O complexity.

Comments (1)