【算法】淘汰分数-美团2021校招笔试

【算法】淘汰分数-美团2021校招笔试

https://www.nowcoder.com/questionTerminal/9c4a4e879b4f49939dfaebea8948f976


1.题目


2.我的解法

  • 代码

    package 淘汰分数;
    
    import java.util.Arrays;
    import java.util.Scanner;
    
    /*
    6 2 3
    1 2 3 3 3 3
     */
    
    /*
     * 先排序,从最小值开始遍历,判断两者人数是否符合人数区间
     * 注意:考虑存在多个分数线时,要输出最低的,所以要排序
     * */
    public class EliminateScores {
    
        public static int findEliminateScores(int min, int max, int[] scores) {
            if (min > max) return -1;
    
            int rs;//结果分数线
            //分数得先排序
            Arrays.sort(scores);
    
            for (int i = 0; i < scores.length; i++) {
                int temp = scores[i];
                int higherNum = 0;//分高
                int lowerNum = 0;//分低
    
                //统计晋级淘汰人数
                for (int j = 0; j < scores.length; j++) {
                    if (temp < scores[j])
                        higherNum++;
                    else lowerNum++;
    
                    if (higherNum > max || lowerNum > max) break;
                }
    
                if (higherNum <= max && higherNum >= min && lowerNum <= max && lowerNum >= min) {
                    rs = temp;
                    return rs;
                }
            }
    
            return -1;
        }
    
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
    
            int playerNum = s.nextInt();//参赛
            int min = s.nextInt();//晋级淘汰下限
            int max = s.nextInt();//晋级淘汰上限
            int[] scores = new int[playerNum];
            for (int i = 0; i < playerNum; i++) {
                scores[i] = s.nextInt();
            }
    
            int rs = findEliminateScores(min, max, scores);
            System.out.println(rs);
        }
    }
    
posted @ 2021-03-11 16:11  musecho  阅读(345)  评论(0编辑  收藏  举报