猴子下山摘桃

2017-08-22  本文已影响145人  zlcook
public class Main {

    /** 请完成下面这个函数,实现题目要求的功能 **/
    /** 当然,你也可以不按照这个模板来作答,完全按照自己的想法来 ^-^  **/
    static int pick(int[] peaches) {
        Integer max =0;
        int count = 0;
        Map<Integer ,Integer> map= new LinkedHashMap<>();
        for( int i =peaches.length-1 ;i >=0  ;i -- ){
            count = 0;
            int current= peaches[i];
            for( Map.Entry<Integer ,Integer> entry : map.entrySet()){
                int key = entry.getKey();
                int value = entry.getValue();
                if( current < key ){
                    count = count > value ? count : value ;
                }
            }
            count += 1;
            map.put(current,count);
            max = max >count ?max : count;
        }
        return max;
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int trees = Integer.parseInt(in.nextLine().trim());
        int[] peaches = new int[trees];
        for (int i = 0; i < peaches.length; i++) {
            peaches[i] = Integer.parseInt(in.nextLine().trim());
        }
        System.out.println(pick(peaches));
    }
}

上一篇 下一篇

猜你喜欢

热点阅读