We are given a 2D array pick
where:
The task is to return the count of balls that satisfy the following condition:
Sort by Ball Number (First Column):
First, sort the array based on the first column (ball number) in ascending order.
Sort by Ball Color (Second Column):
Without altering the first column, sort the array again based on the second column (ball color) in ascending order.
0
is present in the array, it automatically satisfies the condition. In this case, increment the count by one.prev1
to 0
(representing the ball number) and prev2
to the color of the ball in the last occurrence of ball number 0
.0
.Pointer Initialization:
ti
, and assign the current count of balls with the same color as the ti-th
ball number to ccount
.prev1
and prev2
to the first and second column values of the 0th row, respectively.Loop through the Array:
Iterate through the array using the ti
pointer.
Skipping Unnecessary Iterations:
pick[i][0] - ccount > (pick.size() - i) - 1
, skip the rest of the loop. This condition checks if the current ball number is greater than the number of remaining rows in the array.Check Matching Condition:
pick[i][0]
) and ball color (pick[i][1]
) match prev1
and prev2
, respectively, increment the current count (ccount
).Final Condition:
ccount == pick[i][1] + 1
, increment the final count.prev1
or prev2
, update prev1
to the current ball number, prev2
to the current ball color, and reset ccount
to 1
.Input:
pick = [
[0, 3],
[1, 3],
[2, 4],
[3, 2],
[4, 2]
]
```Output:
1```
https://leetcode.com/problems/find-the-number-of-winning-players/
Loading component...
Loading component...