■ 题目描述
给定数组[[2,1],[3 2]],每组表示师徒关系,第一个元素是第二个元素的老师,数字代表排名,现在找出比自己强的徒弟。
输入
[[2,1],[3,2]]
输出
[0,1,2]
第一行数据[2,1]表示排名第 2 的员工是排名第 1 员工的导师,后面的数据以此类推。
说明
第一个元素 0 表示成绩排名第一的导师,没有徒弟考试超过他;
第二个元素 1 表示成绩排名第二的导师,有 1 个徒弟成绩超过他
第三个元素 2 表示成绩排名第二的导师,有 2 个徒弟成绩超过他
输入
[[2,1],[3,2]]
输出
[0,1,2]
import com.alibaba.fastjson.JSONObject;
import java.util.*;
public class FindStrongPeople {
public int[] getStrongPeople(int[][] people){
if (people==null||people.length==0){
return null;
}
Map hashMap = new HashMap<>();
for (int i=0;i
int teacher = people[i][0];
int student = people[i][1];
// teacher大于student,说明该teacher的徒弟考试超过他
if (teacher>student){
hashMap.put(teacher,hashMap.getOrDefault(student,0)+1);
}
if (!hashMap.containsKey(student)){
hashMap.put(student,0);
}
}
int[] ans = new int[hashMap.keySet().size()];
int index = 0;
for (int key:hashMap.keySet()){
ans[index] = hashMap.get(key);
index++;
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()){
int numLen = sc.nextInt();
int[][] arr = new int[numLen][];
int i = 0;
// 放到此处
sc.nextLine();
while(i < numLen){
String[] str = sc.nextLine().split(",");
arr[i] = new int[]{Integer.parseInt(str[0]),Integer.parseInt(str[1])};
i++;
}
int[] ans = new FindStrongPeople().getStrongPeople(arr);
System.out.println(JSONObject.toJSONString(ans));
}
}
}



