需要训练且力量和耐力比其他任何球员都低的球员人数
给定具有三个组件的2D阵列播放器[power、urance、id] 。如果一个球员的力量和耐力比任何其他球员都低,那么他就需要训练。任务是用他们的id找出需要训练的玩家数量。
例子:
Input: {{5, 4, 1}, {6, 3, 2}, {3, 5, 3}}
Output: 0
Explanation: There is no player which has strictly greater power and endurance than any other player.
Input: {{1, 1, 0}, {2, 2, 1}, {3, 3, 2}}
Output: 2
0 1
Explanation: Below are the players who need training
Player with id = 0, having power = 1 and endurance = 1 is strictly less than player with id = 2.
Player with id = 1, having power = 2 and endurance = 2 is strictly less than player with id = 2.
Therefore, there are 2 players who need training.
方法:这个问题可以通过使用贪心方法来解决。让X和Y成为两个玩家。如果存在Y使得X 的力量 < Y 的力量并且X 的耐力 < Y 的耐力,则玩家X需要训练。请按照以下步骤解决给定的问题。
- 将根据两个参数对两个玩家进行比较。
- 以非递减的幂顺序对数组player[][]进行排序。
- 如果两个元素具有相同的功率值,则首先考虑耐力值较大的元素。
- 从 player [][]数组的右侧迭代并跟踪最大的先前耐力。
- 一开始,如果任何玩家有资格接受培训,则存储其id 。
- 如果当前玩家耐力小于之前的最大耐力值,则增加玩家计数。
- 否则,更新最大耐力值。
- 返回存储需要训练的玩家的所有id的数组。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
bool compare(vector, vector);
vector CountOfPlayers(vector >& qualities)
{
int count = 0;
int n = qualities.size();
sort(qualities.begin(), qualities.end(),
[](vector& entry1, vector& entry2)
{
// If power value is equal
// for both elements
// Sort in descending order
// according to endurance value
if (entry1[0] == entry2[0])
return entry2[1] < entry1[1];
else
return entry1[0] < entry2[0];
});
// Keep track of maximum
// endurance value in right side
int ma = 0;
vector res;
// Traverse the array players
for (int i = n - 1; i >= 0; i--) {
// If current endurance
// value is smaller than
// max then we will
// increment the count
int id = qualities[i][2];
if (qualities[i][1] < ma) {
// Adding player
// to the final array
res.push_back(id);
int q1 = qualities[i + 1][0] - qualities[i][0];
int q2 = ma - qualities[i][1];
// Increase the count
count++;
}
// Else update max value
ma = max(ma, qualities[i][1]);
}
return res;
}
// Driver Code
int main()
{
vector > qualities
= { { 1, 1, 0 }, { 2, 2, 1 }, { 3, 3, 2 } };
vector ans = CountOfPlayers(qualities);
// Print total number of players
// who need training
cout << ans.size() << "\n";
// Printing id of each player
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << " ";
}
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java program for above approach
import java.io.*;
import java.util.*;
class GFG {
private static Vector
CountOfPlayers(int[][] qualities)
{
int count = 0;
int n = qualities.length;
sort(qualities);
// Keep track of maximum
// endurance value in right side
int max = 0;
Vector res
= new Vector();
// Traverse the array players
for (int i = n - 1; i >= 0; i--) {
// If current endurance
// value is smaller than
// max then we will
// increment the count
int id = qualities[i][2];
if (qualities[i][1] < max) {
// Adding player
// to the final array
res.add(id);
int q1
= qualities[i + 1][0]
- qualities[i][0];
int q2 = max - qualities[i][1];
// Increase the count
count++;
}
// Else update max value
max = Math.max(max, qualities[i][1]);
}
return res;
}
// Sort on the array according
// to the above approach
public static void sort(int arr[][])
{
// Using built-in sort
// function Arrays.sort
Arrays.sort(arr, new Comparator() {
@Override
// Compare values according to columns
public int compare(int[] entry1,
int[] entry2)
{
// If power value is equal
// for both elements
// Sort in descending order
// according to endurance value
if (entry1[0] == entry2[0])
return entry2[1] - entry1[1];
else
return entry1[0] - entry2[0];
}
});
}
// Driver Code
public static void main(String[] args)
{
int[][] qualities
= { { 1, 1, 0 },
{ 2, 2, 1 },
{ 3, 3, 2 } };
Vector ans
= CountOfPlayers(qualities);
// Print total number of players
// who need training
System.out.println(ans.size());
// Printing id of each player
for (int i = 0; i < ans.size(); i++) {
System.out.print(ans.get(i));
System.out.print(" ");
}
}
}
C#
// C# program for above approach
using System;
using System.Collections.Generic;
public class GFG {
private static List
CountOfPlayers(List> qualities)
{
int count = 0;
int n = qualities.Count;
qualities.Sort((entry1,entry2)=>{
if (entry1[0] == entry2[0])
return entry2[1] - entry1[1];
else
return entry1[0] - entry2[0];
});
// Keep track of maximum
// endurance value in right side
int max = 0;
List res
= new List();
// Traverse the array players
for (int i = n - 1; i >= 0; i--) {
// If current endurance
// value is smaller than
// max then we will
// increment the count
int id = qualities[i][2];
if (qualities[i][1] < max) {
// Adding player
// to the readonly array
res.Add(id);
int q1
= qualities[i + 1][0]
- qualities[i][0];
int q2 = max - qualities[i][1];
// Increase the count
count++;
}
// Else update max value
max = Math.Max(max, qualities[i][1]);
}
return res;
}
// Driver Code
public static void Main(String[] args)
{
int[,] qualities
= { { 1, 1, 0 },
{ 2, 2, 1 },
{ 3, 3, 2 } };
List> qualities = new List> ();
for(int i = 0; i < qualities.GetLength(0); i++){
List l = new List();
l.Add(qualities[i, 0]);
l.Add(qualities[i, 1]);
l.Add(qualities[i, 2]);
qualitie.Add(l);
}
List ans
= CountOfPlayers(qualitie);
// Print total number of players
// who need training
Console.WriteLine(ans.Count);
// Printing id of each player
for (int i = 0; i < ans.Count; i++) {
Console.Write(ans[i]);
Console.Write(" ");
}
}
}
// This code contributed by Rajput-Ji
Javascript
2
1 0
时间复杂度: O(NlogN),其中 N 是数组的大小。
空间复杂度: O(1)。