|
1 | 1 | # Difference Array |
2 | 2 |
|
| 3 | +- The main applicable scenario is to frequently query the cumulative sum of a certain interval when the original array will not be modified |
| 4 | + |
3 | 5 | ## 0) Concept |
4 | | -- [fucking algorithm - Difference Array](https://labuladong.gitee.io/algo/2/18/22/) |
| 6 | +- [fucking algorithm - Difference Array](https://labuladong.online/algo/data-structure/diff-array/) |
5 | 7 |
|
6 | 8 | ### 0-1) Types |
| 9 | + - LC 1109 |
| 10 | + - LC 370 |
| 11 | + - LC 1094 |
7 | 12 |
|
8 | 13 | ### 0-2) Pattern |
9 | 14 |
|
| 15 | +```java |
| 16 | + |
| 17 | +// java |
| 18 | +// https://labuladong.online/algo/data-structure/diff-array/ |
| 19 | + |
| 20 | +class PrefixSum { |
| 21 | + // 前缀和数组 |
| 22 | + private int[] preSum; |
| 23 | + |
| 24 | + // 输入一个数组,构造前缀和 |
| 25 | + public PrefixSum(int[] nums) { |
| 26 | + // preSum[0] = 0,便于计算累加和 |
| 27 | + preSum = new int[nums.length + 1]; |
| 28 | + // 计算 nums 的累加和 |
| 29 | + for (int i = 1; i < preSum.length; i++) { |
| 30 | + preSum[i] = preSum[i - 1] + nums[i - 1]; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // 查询闭区间 [left, right] 的累加和 |
| 35 | + public int sumRange(int left, int right) { |
| 36 | + return preSum[right + 1] - preSum[left]; |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | + |
10 | 41 | ## 1) General form |
11 | 42 |
|
| 43 | +```java |
| 44 | + |
| 45 | +// java |
| 46 | +// https://labuladong.online/algo/data-structure/diff-array/ |
| 47 | + |
| 48 | +// 差分数组工具类 |
| 49 | +class Difference { |
| 50 | + // 差分数组 |
| 51 | + private int[] diff; |
| 52 | + |
| 53 | + // 输入一个初始数组,区间操作将在这个数组上进行 |
| 54 | + public Difference(int[] nums) { |
| 55 | + assert nums.length > 0; |
| 56 | + diff = new int[nums.length]; |
| 57 | + // 根据初始数组构造差分数组 |
| 58 | + diff[0] = nums[0]; |
| 59 | + for (int i = 1; i < nums.length; i++) { |
| 60 | + diff[i] = nums[i] - nums[i - 1]; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // 给闭区间 [i, j] 增加 val(可以是负数) |
| 65 | + public void increment(int i, int j, int val) { |
| 66 | + diff[i] += val; |
| 67 | + if (j + 1 < diff.length) { |
| 68 | + diff[j + 1] -= val; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // 返回结果数组 |
| 73 | + public int[] result() { |
| 74 | + int[] res = new int[diff.length]; |
| 75 | + // 根据差分数组构造结果数组 |
| 76 | + res[0] = diff[0]; |
| 77 | + for (int i = 1; i < diff.length; i++) { |
| 78 | + res[i] = res[i - 1] + diff[i]; |
| 79 | + } |
| 80 | + return res; |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
12 | 85 | ### 1-1) Basic OP |
13 | 86 |
|
14 | 87 | ## 2) LC Example |
| 88 | + |
| 89 | +## Range Addition |
| 90 | + |
| 91 | +```java |
| 92 | +// java |
| 93 | +// LC 370 |
| 94 | +class Solution { |
| 95 | + public int[] getModifiedArray(int length, int[][] updates) { |
| 96 | + // nums 初始化为全 0 |
| 97 | + int[] nums = new int[length]; |
| 98 | + // 构造差分解法 |
| 99 | + Difference df = new Difference(nums); |
| 100 | + |
| 101 | + for (int[] update : updates) { |
| 102 | + int i = update[0]; |
| 103 | + int j = update[1]; |
| 104 | + int val = update[2]; |
| 105 | + df.increment(i, j, val); |
| 106 | + } |
| 107 | + |
| 108 | + return df.result(); |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +### Corporate Flight Bookings |
| 114 | + |
| 115 | +```java |
| 116 | +// java |
| 117 | +// LC 1109 |
| 118 | + |
| 119 | +class Solution { |
| 120 | + public int[] corpFlightBookings(int[][] bookings, int n) { |
| 121 | + // nums 初始化为全 0 |
| 122 | + int[] nums = new int[n]; |
| 123 | + // 构造差分解法 |
| 124 | + Difference df = new Difference(nums); |
| 125 | + |
| 126 | + for (int[] booking : bookings) { |
| 127 | + // 注意转成数组索引要减一哦 |
| 128 | + int i = booking[0] - 1; |
| 129 | + int j = booking[1] - 1; |
| 130 | + int val = booking[2]; |
| 131 | + // 对区间 nums[i..j] 增加 val |
| 132 | + df.increment(i, j, val); |
| 133 | + } |
| 134 | + // 返回最终的结果数组 |
| 135 | + return df.result(); |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | + |
| 141 | +### Car Pooling |
| 142 | + |
| 143 | +```java |
| 144 | +// java |
| 145 | +// LC 1094 |
| 146 | +// https://leetcode.com/problems/car-pooling/description/ |
| 147 | + |
| 148 | +class Solution { |
| 149 | + public boolean carPooling(int[][] trips, int capacity) { |
| 150 | + // 最多有 1001 个车站 |
| 151 | + int[] nums = new int[1001]; |
| 152 | + |
| 153 | + // 构造差分解法 |
| 154 | + Difference df = new Difference(nums); |
| 155 | + |
| 156 | + for (int[] trip : trips) { |
| 157 | + // 乘客数量 |
| 158 | + int val = trip[0]; |
| 159 | + |
| 160 | + // 第 trip[1] 站乘客上车 |
| 161 | + int i = trip[1]; |
| 162 | + |
| 163 | + // 第 trip[2] 站乘客已经下车, |
| 164 | + // 即乘客在车上的区间是 [trip[1], trip[2] - 1] |
| 165 | + int j = trip[2] - 1; |
| 166 | + |
| 167 | + // 进行区间操作 |
| 168 | + df.increment(i, j, val); |
| 169 | + } |
| 170 | + |
| 171 | + int[] res = df.result(); |
| 172 | + |
| 173 | + // 客车自始至终都不应该超载 |
| 174 | + for (int i = 0; i < res.length; i++) { |
| 175 | + if (capacity < res[i]) { |
| 176 | + return false; |
| 177 | + } |
| 178 | + } |
| 179 | + return true; |
| 180 | + } |
| 181 | +} |
| 182 | +``` |
0 commit comments