题解 | #最大乘积#

最大乘积

https://www.nowcoder.com/practice/5f29c72b1ae14d92b9c3fa03a037ac5f

代码实现:
import java.util.*;
public class Main{
    Scanner scan=new Scanner(System.in);
    public static void main(String[] args) {
       Main maxProduct=new Main();
       maxProduct.max_product();
    }
    static Comparator<Integer> cmp = new Comparator<Integer>() {
        public int compare(Integer e1, Integer e2) {
            return e2 - e1;
        }
    };
    private void max_product(){
        int n=scan.nextInt();
        int [] arr=new int[n];
        PriorityQueue<Integer> Descending=new PriorityQueue<>(cmp);//降序
        PriorityQueue<Integer> Ascending=new PriorityQueue<>();
        for(int i=0;i<n;i++){
            int temp=scan.nextInt();
            Ascending.add(temp);
            Descending.add(temp);
        }
        long max1=Descending.poll();
        long max2=Descending.poll();
        long max3=Descending.poll();
        long min1=Ascending.poll();
        long min2=Ascending.poll();
        long res=Math.max(max1*max2*max3,max1*(min1*min2));
        System.out.println(res);

    }
}


全部评论

相关推荐

09-18 20:41
百度_Java
要个offer怎么这...:哈哈哈哈哈哈,我也拿了0x10000000个offer,秋招温啦啦啦,好开心
我的秋招日记
点赞 评论 收藏
分享
10-10 00:14
门头沟学院 Java
程序员小白条:20年架构师,无工资
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务