📜  等级等于或小于给定截止等级的玩家数量

📅  最后修改于: 2022-05-13 01:56:09.573000             🧑  作者: Mango

等级等于或小于给定截止等级的玩家数量

给定一个由N个整数和一个整数R组成的数组arr[] ,表示截止秩,任务是计算秩最多为 R的数组元素的数量,使得相等的数组元素排名相同,不同的数组元素是根据它们在数组arr[]中的位置进行排名。

例子:

方法:给定的问题可以通过使用排序的概念来解决。请按照以下步骤解决此问题:

  • 按降序对给定数组arr[]进行排序。
  • 初始化两个变量,将 rank设为1来存储数组元素的等级,将count设为0来存储所需的结果。
  • 使用变量i遍历给定的数组arr[] ,并执行以下步骤:
    • 如果arr[i]等于前一个元素,则将与前一个等级相同的等级分配给当前元素。
    • 否则,将第(count + 1)等级的值分配给当前元素。
    • 如果大于R则中断。否则,将计数增加1
  • 完成上述步骤后,打印count的值作为答案。

下面是上述方法的实现:

C++
// C++  program for above approach
#include 
#include 
using namespace std;
 
// Function to find the count of array
// elements having rank at most R
int countElements(int R, int N, int arr[])
{
 
  // Sort the array arr[] in the
  // decreasing order
  sort(arr, arr + N, greater());
 
  // Stores the rank and required
  // count of array elements
  int rank = 1, count = 0;
 
  // store the previou element
  int prevScore = arr[0], score;
 
  // Traverse the array
  for (int i = 0; i < N; i++) {
    score = arr[i];
 
    // If score is less than the
    // prevScore
    if (score < prevScore) {
      rank = count + 1;
    }
 
    // If the rank is greater than R
    if (rank > R) {
      break;
    }
 
    // Increment count by 1
    count++;
 
    // update prevscore
    prevScore = score;
  }
 
  // return count
  return count;
}
 
// Driver code
int main()
{
  int arr[] = { 100, 50, 50, 25 };
  int R = 2;
  int N = sizeof(arr) / sizeof(arr[0]);
  cout << countElements(R, N, arr);
  return 0;
}
 
// This code is contributed by Parth Manchanda


Java
// Java program for the above approach
import java.util.*;
 
class GFG
{
  static void reverse(int a[])
  {
    int n = a.length;
    int[] b = new int[n];
    int j = n;
    for (int i = 0; i < n; i++) {
      b[j - 1] = a[i];
      j = j - 1;
    }
  }
 
  // Function to find the count of array
  // elements having rank at most R
  static int countElements(int R, int N, int[] arr)
  {
 
    // Sort the array arr[] in the
    // decreasing order
    Arrays.sort(arr);
    reverse(arr);
 
    // Stores the rank and required
    // count of array elements
    int rank = 1;
    int count = -1;
 
    // Stores the previous element
    int prevScore = arr[0];
 
    // Traverse the array
    for(int score : arr)
    {
 
      // If score is less than the
      // prevScore
      if (score < prevScore)
        rank = count + 1;
 
      // If the rank is greater than R
      if (rank > R)
        break;
 
      // Increment count by 1
      count = count + 1;
 
      // Update prevScore
      prevScore = score;
    }
 
    // Return the result
    return count;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int[] arr = { 100, 50, 50, 25 };
    int R = 2;
    int N = arr.length;
 
    // Function Call
    System.out.println(countElements(R, N, arr));
  }
}
 
// This code is contributed by sanjoy_62.


Python3
# Python program for the above approach
 
# Function to find the count of array
# elements having rank at most R
def countElements(R, N, arr):
 
    # Sort the array arr[] in the
    # decreasing order
    arr.sort(reverse = True)
 
    # Stores the rank and required
    # count of array elements
    rank = 1
    count = 0
 
    # Stores the previous element
    prevScore = arr[0]
 
    # Traverse the array
    for score in arr:
 
        # If score is less than the
        # prevScore
        if score < prevScore:
            rank = count + 1
 
        # If the rank is greater than R
        if rank > R:
            break
             
        # Increment count by 1
        count += 1
 
        # Update prevScore
        prevScore = score
 
    # Return the result
    return count
 
 
# Driver Code
arr = [100, 50, 50, 25]
R = 2
N = len(arr)
 
# Function Call
print(countElements(R, N, arr))


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the count of array
// elements having rank at most R
static int countElements(int R, int N, int[] arr)
{
     
    // Sort the array arr[] in the
    // decreasing order
    Array.Sort(arr);
    Array.Reverse(arr);
 
    // Stores the rank and required
    // count of array elements
    int rank = 1;
    int count = 0;
 
    // Stores the previous element
    int prevScore = arr[0];
 
    // Traverse the array
    foreach(int score in arr)
    {
         
        // If score is less than the
        // prevScore
        if (score < prevScore)
            rank = count + 1;
 
        // If the rank is greater than R
        if (rank > R)
            break;
 
        // Increment count by 1
        count = count + 1;
 
        // Update prevScore
        prevScore = score;
    }
     
    // Return the result
    return count;
}
 
 
// Driver code
static public void Main()
{
    int[] arr = { 100, 50, 50, 25 };
    int R = 2;
    int N = arr.Length;
 
    // Function Call
    Console.WriteLine(countElements(R, N, arr));
}
}
 
// This code is contributed by target_2.


Javascript


输出:
3

时间复杂度: O(N*log N)
辅助空间: O(1)