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...
Main Function is not defined.
INPUT: n = 4 , pick = [[0 , 0] , [1,0] ,[1,0] , [2,1] , [2 ,1], [2, 0] ]
OUTPUT: 2
public static int winningPlayerCount(int n , vector<vector<int>& a , const vector<int>& b){
sort(pick.begin(), pick.end(), sortByFirstAnd2ndElement);
int count = 0;
int ccount =0 ;
int ti = 0;
int prev1 = pick[0][0];
int prev2 = pick[0][1];
while(ti < pick.size() && pick[ti][0] == 0){
count = 1;
prev1 = pick[ti][0];
prev2 = pick[ti][1];
ti++;
}
if(ti == 0){
ti =1;
}
for(int i = ti; i < pick.size(); i++){
if(pick[i][0] -ccount > (pick.size()-i)-1){
continue;
}
if(prev1 == pick[i][0] && prev2 == pick[i][1]){
ccount++;
if(ccount == pick[i][0]+1){
count++;
}
}
else{
prev1 = pick[i][0];
prev2 = pick[i][1];
ccount =1;
}
}
return count;
}//function end
Utility Function is not required.