#合唱队形(再修改了一下下) 我最后提交的时候是18,后面一直调,调着调着就自动交卷了 现在好像调出来了,请大佬们帮我看看有没有错的地方😥 import java.util.*; public class Main {     public static void main(String[] args) {         ArrayList<Integer> list = new ArrayList();         int N = 0;         Scanner sc = new Scanner(System.in);         N = sc.nextInt();         while(N > 0){             list.add(sc.nextInt());             N--;         }         System.out.println(getGroupNum(list));     }     public static int getGroupNum(ArrayList<Integer> arr){         if(arr.size() <= 1){             return arr.size();         }         HashMap<Integer,Integer> map = new HashMap<>();         int max = arr.get(0);         int min = arr.get(0);         for(int i = 1; i < arr.size(); i++){             int cur = arr.get(i);             if(cur <= min){             //如果后面的值小于等于min,则说明前面最小值为min的组不能组成一队了,要和后面的一起                 deleteMap(map,cur);     //把大于cur的key都移除                 map.put(cur,max);                 min = max;             }else if(cur > max){         //如果遇到大于max的,则修改max的值                 max = cur;             }         }         return map.size();     }     public static void deleteMap(HashMap<Integer,Integer> map,int k){         Set<Integer> set = map.keySet();            //获取keySet         Iterator<Integer> keys = set.iterator();         while(keys.hasNext()){                      //遍历keySet,如果有最小值大于k的组,就把它移除             int key = keys.next();             if(key > k){                 map.remove(key);             }         }     } } 自己测试的一次效果:
点赞 评论

相关推荐

牛客网
牛客企业服务