找到两个暗淡阵列中给出的最高分数或候选分数的最大平均分数

时间:2018-06-06 08:12:29

标签: java arrays arraylist data-structures java-stream

问题:考生可以根据自己的意愿多次尝试入学考试。最终考虑所有尝试的平均分数。给出一个分数列表。找到得分最高的候选人[平均分]。

String scores[][] = {{"Ram","155"}, 
                    {"Shyam","145"},
                    {"Ram","156"},
                    {"Balram","159"},
                    {"Balram","150"},
                    {"Ram","135"},
                    {"Mira","156"},
                    {"Mira","152"},
                    {"Shyam","155"}};

如上所述,分数以二维数组给出。需要建议以有效的方式解决这个问题。

4 个答案:

答案 0 :(得分:1)

减少时间和空间复杂性的原因是什么?我坚信简洁性和可维护性比性能更重要。

由于您标记了,我建议您采用以下方法。

Map<Object, Double> scoreMap = Arrays.stream(scores)
        .collect(
            Collectors.groupingBy(i -> i[0], 
            Collectors.averagingInt(i -> Integer.parseInt(i[1])
        )));

String winner = scoreMap.entrySet().stream()
    .max(Comparator.comparingDouble(e -> e.getValue()))
    .get().getKey().toString();

感谢@AndyTurner提供.max(..)参数建议。

答案 1 :(得分:1)

为了减少时间复杂性,您可以使用特定的集合:

public static String findMaxScore(String[][] scores) {
    final class Candidate implements Comparable<Candidate> {

        private final String name;
        private int scoreSum;
        private int attempts;

        public Candidate(String name) {
            this.name = name;
        }

        public void score(int score) {
            scoreSum += score;
            attempts++;
        }

        public double avg() {
            return (double)scoreSum / attempts;
        }

        @Override
        public int compareTo(Candidate candidate) {
            return Double.compare(candidate.avg(), avg());
        }
    }

    Map<String, Candidate> map = new HashMap<>();

    for (String[] data : scores) {
        map.putIfAbsent(data[0], new Candidate(data[0]));
        map.get(data[0]).score(Integer.parseInt(data[1]));
    }

    return new TreeSet<>(map.values()).iterator().next().name;
}

答案 2 :(得分:0)

public String  findmaxAverage(String[][] Grades) {
    if ( grades == null ) return "";
    Map<String,List<Integer>> map = new HashMap<>();
    for( String[] grade : Grades ) {
        List<Integer> mapList = map.get(grade[0]);
        if ( mapList == null ) mapList = new ArrayList<>();
        mapList.add(Integer.valueOf(grade[1]));
        map.put(grade[0], mapList);
    }

    double maxAverage = Double.MIN_VALUE;
    String winner = "";
    for( String name : map.nameSet()) {
        System.out.println(name+": "+map.get(name));
        List<Integer> mapList = map.get(name);
        double average = getAverage(mapList);
        if ( average > maxAverage) {
            maxAverage = average;
            winner = name;
        }

    }
    return winner; }

答案 3 :(得分:0)

此外,通过这种方式,您可以通过Streams Api做同样的事情,

import java.util.stream.*;
import java.util.*;
public class Solution {
  public static void main (String[] args) {
    String scores[][] = {{"Bob","80"},{"Charles","85"},{"Rob","70"},{"Bob","100"},{"Charles","75"}};
    

 Map<Object,Double> collectValues = Arrays.stream(scores).collect(Collectors.groupingBy(a->a[0],Collectors.averagingInt(a->Integer.parseInt(a[1]))));

    Map.Entry<Object,Double> MaxMarks = collectValues.entrySet().stream().max((e1,e2)->Double.compare(e1.getValue(), e2.getValue())).get();

    System.out.println(MaxMarks.getKey() + " :: " + MaxMarks.getValue());

}