Understand Find The Number Of Winning Players Problem

Problem Name: Find The Number Of Winning Players
Problem Description:

Problem: Find the Number of Winning Players

Problem Statement:

We are given a 2D array pick where:

  • The first column represents the ball number.
  • The second column represents the color of the ball.

The task is to return the count of balls that satisfy the following condition:

  • For a particular ball number in the first column, it should be less than the count of balls with the same color as indicated by the second column.

Approach:

Step 1: Sorting the Array

  • 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.

Step 2: Initial Setup

  • Base Condition for Ball Number 0:
    • If ball number 0 is present in the array, it automatically satisfies the condition. In this case, increment the count by one.
    • Set prev1 to 0 (representing the ball number) and prev2 to the color of the ball in the last occurrence of ball number 0.
    • Increment the pointer to point to the ball number next to 0.

Step 3: Iterative Process

  • Pointer Initialization:

    • If the base condition is not met, point to the second row with a pointer ti, and assign the current count of balls with the same color as the ti-th ball number to ccount.
    • Set 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:

      • If 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.

Step 4: Updating the Count

  • Check Matching Condition:

    • If the current ball number (pick[i][0]) and ball color (pick[i][1]) match prev1 and prev2, respectively, increment the current count (ccount).
  • Final Condition:

    • If ccount == pick[i][1] + 1, increment the final count.

Step 5: Reset Counters

  • If the current ball number or color does not match prev1 or prev2, update prev1 to the current ball number, prev2 to the current ball color, and reset ccount to 1.

Step 6: Final Step

  • After iterating through the entire array, return the final count of balls that satisfy the condition.

Example Walkthrough

Example 1:

Input:

pick = [
  [0, 3],
  [1, 3],
  [2, 4],
  [3, 2],
  [4, 2]
]

```Output:

1```
Category:
  • Tries
Programming Language:
  • Java
Reference Link:

https://leetcode.com/problems/find-the-number-of-winning-players/

Online IDE

Scroll down for output
Java
Output:

Loading component...

Loading component...

Tracking code (View only. In case you want to track the code, click this button):
Main Function:

Main Function is not defined.

Helper Function:

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 Functions and Global variables:

Utility Function is not required.