题解 | #最大乘积#

最大乘积

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);

    }
}


全部评论

相关推荐

野猪不是猪🐗:把你的学校加黑,加粗,斜体,下划线,描边,内阴影,内发光,投影,外发光,再上渐变色,居中,放大到最大字号,再把简历里其它内容删了,就行了
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客企业服务