leetcode刷题篇 1109题 航班预订统计 java版

先看题目
在这里插入图片描述
思路分析:
第一种方法就是暴力法,两个for循环,遍历,然后将预定数累加
暴力法比较费时,竟然没有报超时

还能进行优化:
在这里插入图片描述

看代码

class Solution {
    public int[] corpFlightBookings(int[][] bookings, int n) {
        int[] res = new int[n];
        for (int i = 0; i < bookings.length; ++i) {
            for (int j = bookings[i][0] - 1; j < bookings[i][1]; j++) {
                res[j] += bookings[i][2];
            }
        }
        return res;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45806131/article/details/108460851