
You are given 'N' pairs of integers representing the scores of Bob and Noddy at different points during their rugby match. Each pair ( A[i], B[i] ) represents Bob's score and Noddy's score respectively at the i-th observation.
Find the maximum possible number of times Bob and Noddy could have had equal scores (ties) during the entire match, assuming they started from (0, 0).
Let N = 3, and the score pairs be: (1, 2), (3, 2), (3, 4).
One possible sequence of goals could be:
(0,0) -> (1,0) -> (1,1) -> (1,2) -> (2,2) -> (3,2) -> (3,3) -> (3,4)
The ties occur at: (0,0), (1,1), (2,2), (3,3) = 4 times.
Therefore, the answer is '4'.
The first line contains an integer 'N'.
The next 'N' lines each contain two integers, representing Bob's and Noddy's scores.
Output Format :
Return the maximum possible number of ties between Bob and Noddy's scores.
Note :
You don't need to print anything. Just implement the given function.
1 <= N <= 10^5
0 <= A[i], B[i] <= 10^9
All score pairs are given in non-decreasing order
The last score pair denotes the final score of both players.
Time Limit: 1 sec
4
2 1
2 3
4 3
5 5
6
Starting from (0,0), the score pairs are in non-decreasing order: (2,1), (2,3), (4,3), (5,5).
One optimal path could be:
(0,0) -> (1,0) -> (1,1) -> (2,1) -> (2,2) -> (2,3) -> (3,3) -> (4,3) -> (4,4) -> (5,4) -> (5,5)
The ties occur at: (0,0), (1,1), (2,2), (3,3), (4,4), (5,5) = 6 times.
Therefore, the answer is 6.
2
3 0
3 4
2
Focus on the difference between consecutive scores
Approach:
Algorithm:
O(N), where 'N' is the number of score observations.
We iterate through all 'N' score pairs once to calculate the maximum ties. Thus, the overall time complexity is of the order O(N).
O(1).
We use only a constant amount of extra space. Thus, the overall space complexity is of the order O(1).