Day04 前缀和&差分 1109. 航班预订统计 、304. 二维区域和检索 - 矩阵不可变

一、304. 二维区域和检索 - 矩阵不可变
1、思路(二维前缀和)

使用二维前缀合

2、代码
JAVA 复制代码
class NumMatrix {
    int[][] sum;

    public NumMatrix(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        sum = new int[m + 1][n + 1];
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                // 注意索引是从1开始,当前值索引记得 -1
                sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + matrix[i - 1][j - 1];
            }
        }
    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
        //注意题目索引从0开始,公式索引是从1开始
        row1++;
        col1++;
        row2++;
        col2++;
        return sum[row2][col2] - sum[row2][col1 - 1] - sum[row1 - 1][col2] + sum[row1 - 1][col1 - 1];
    }
}

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */
二、1109. 航班预订统计
1、思路(差分&前缀和)

1、差分数组B的前缀和数组就是原数组A

java 复制代码
B1+B2+....+ Bn = A1+ A2 -A1 + A3-A2+A4-A3... +An - An-1 =  An


B = delta(A);
A = sum(B)
即:A =sum(delta(A));

2、差分+前缀和 善于处理问题,对A数组 某连续端统一加 一个数字的问题

这类问题可以先得出差分,对差分计算,再求前缀和即可以得到答案

图中例子1拆解:

原数组A为0,1,-1,-1,0 需要 对 2 到 4 个数+1 最终应该为:【0,2,0,0,0】

复制代码
1> 先计算出原数组A差分数组B为:【0,1,-2,0,1】,再对差分B相同位置 +1 

B2 = A2 -A1 ; ------------>A1没变,A2本身+1了,即b2+1

B3 = A3-A2;------------> A2和A3都加1,抵消,不变

B4 = A4-A3;------------>A4和A3都加1,抵消,不变

B5 = A5-A4;------------>A5没变,A4本身+1了,即B5 -1

(L为2,r为4,1为d)得出公式:	**Bₗ** + d  ,B(r+1) - d 

2> 按上面得出公式对差分数组B操作得到:【0,2,-2,0,0】

3>最后对差分数组B求前缀和得到结果:【0,2,0,0,0】
2、代码
JAVA 复制代码
class Solution {
    public int[] corpFlightBookings(int[][] bookings, int n) {
        int[] delta = new int[n + 2];  // 差分要开0~n+1
        Arrays.fill(delta, 0);
        for (int[] booking : bookings) {
            int fir = booking[0];
            int last = booking[1];
            int seats = booking[2];
            // 差分模板
            delta[fir] += seats;
            delta[last + 1] -= seats;
        }

        int[] sum = new int[n + 1]; // 0~n
        for (int i = 1; i <= n; i++) {
            sum[i] = sum[i - 1] + delta[i];
        }
        // 往前移一位,前缀和求出来的是索引为1开始的
        int[] result = new int[n];
        for (int i = 1; i <= n; i++) {
            result[i - 1] = sum[i];
        }
        return result;
    }
}
相关推荐
头发还在的女程序员9 小时前
医院陪诊管理系统怎么选择?——2026 年选型避坑与架构参考
java·开发语言·陪诊系统·陪诊app·医院陪诊陪护
CodeStats9 小时前
【Spring事务】Spring事务注解 @Transactional 完整体系:从 MySQL 隔离级别到 MyBatis 原理详解
java·spring·mybatis·事务·transactional
我命由我1234510 小时前
Android 开发问题:为 PDFView 设置一个带有黑色边框的背景 drawable,但边框没有生效
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
拳里剑气11 小时前
C++算法:BFS解决FloodFill算法
c++·算法·bfs·宽度优先
都叫我大帅哥12 小时前
从Python到Java:为什么企业级Agent最终会选择Java?
java·ai编程
wanderist.12 小时前
Lambda表达式在算法竞赛中的应用
java·开发语言·算法
稚南城才子,乌衣巷风流13 小时前
支配树(Dominator Tree)详解:概念、算法与应用
算法
稚南城才子,乌衣巷风流13 小时前
动态开点:原理、实现与应用场景
数据结构·算法
yyds_yyd_1008613 小时前
1464. 数组中两元素的最大乘积(2026.07.27)
数据结构·c++·算法·leetcode