新农上路
- 积分
- 98
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-3-31
- 最后登录
- 1970-1-1
|
- class Person {
- public String name;
- public int pigNumber;
- public int cowNumber;
- public int finalScore;
- }
- public void caclulateCowsAndPigs(List<Person> personList) {
- HashMap<String, Integer> pigScores = new HashMap<>();
- HashMap<String, Integer> cowScores = new HashMap<>();
- String pigKey = "PIG";
- String cowKey = "COW";
- PriorityQueue<Person> pigQueue = new PriorityQueue<>(new Comparator<Person>() {
- @Override
- public int compare(Person o1, Person o2) {
- return o1.pigNumber - o2.pigNumber;
- }
- });
- handleScore(pigQueue, personList, pigScores, pigKey);
- PriorityQueue<Person> cowQueue = new PriorityQueue<>(new Comparator<Person>() {
- @Override
- public int compare(Person o1, Person o2) {
- return o1.cowNumber - o2.cowNumber;
- }
- });
- handleScore(cowQueue, personList, cowScores, cowKey);
- for(int i = 0 ;i < personList.size() ;i++){
- Person currentPerson = personList.get(i);
- int cowScore = cowScores.get(getUserAnimalScoreKey(currentPerson.name, cowKey));
- int pigScore = pigScores.get(getUserAnimalScoreKey(currentPerson.name, pigKey));
- currentPerson.finalScore = cowScore+pigScore;
- }
- }
- private void handleScore( PriorityQueue<Person> priorityQueue , List<Person> persons, HashMap<String, Integer> scores, String key ){
- priorityQueue.addAll(persons);
- int currentScore = 1;
- while (!priorityQueue.isEmpty()) {
- Person person = priorityQueue.poll();
- scores.put(getUserAnimalScoreKey(person.name, key), currentScore);
- currentScore++;
- }
- }
- private String getUserAnimalScoreKey(String userName, String animalName){
- return userName+"-"+animalName;
- }
复制代码 |
|