华为第一题 日志处理 前缀和解法100%通过耗时747ms

自认为最快,希望评论区大神点评下😂
 import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int length = sc.nextInt();
        long[] records = new long[length];
        long[] preSum = new long[length];
        for (int i = 0; i < records.length; i++) {
            records[i] = sc.nextLong();
        }
        long total = sc.nextLong();

        long sum = 0;
        Arrays.sort(records);
        for (int i = 0; i < records.length; i++) {
            preSum[i] = sum;
            sum += records[i];
        }

        if (sum <= total) {
            System.out.println(-1);
        } else {
            long[] temp = new long[length];
            long result = 0;
            for (int i = 0; i < temp.length; i++) {
                temp[i] = preSum[i] + records[i] * (records.length - i);
            }
            for (int i = 0; i < temp.length; i++) {
                if(temp[0]>total){
                    result=total/temp.length;
                    break;
                }
                if (temp[i] <= total && temp[i + 1] > total) {
                    long res = (total - temp[i]) / (temp.length - i - 1);
                    result = records[i] + res;
                    break;
                }
            }
            System.out.println(result);
        }
    }
}

#华为#
全部评论
能大概讲讲思路吗
点赞 回复 分享
发布于 2022-09-22 15:45 上海
能二分吗这个
点赞 回复 分享
发布于 2022-09-21 21:08 北京
思路一样,为啥一直75%
点赞 回复 分享
发布于 2022-09-21 21:05 广东

相关推荐

喜欢核冬天的哈基米很想上市:会爆NullPointerException的
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务