Skip to content

Commit 4d74fb9

Browse files
committed
fix cheatsheet/difference_array.md
1 parent 5fcae7a commit 4d74fb9

File tree

1 file changed

+78
-28
lines changed

1 file changed

+78
-28
lines changed

doc/cheatsheet/difference_array.md

Lines changed: 78 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
- [fucking algorithm - Difference Array](https://labuladong.online/algo/data-structure/diff-array/)
77

88
### 0-1) Types
9-
- LC 1109
10-
- LC 370
11-
- LC 1094
9+
- LC 1109
10+
- LC 370
11+
- LC 1094
1212

1313
### 0-2) Pattern
1414

@@ -46,6 +46,7 @@ class PrefixSum {
4646
// https://labuladong.online/algo/data-structure/diff-array/
4747

4848
// 差分数组工具类
49+
// V1
4950
class Difference {
5051
// 差分数组
5152
private int[] diff;
@@ -82,6 +83,46 @@ class Difference {
8283
}
8384
```
8485

86+
```java
87+
// V2
88+
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_java/src/main/java/AlgorithmJava/DifferenceArray.java
89+
90+
91+
// method
92+
public int[] getDifferenceArray(int[][] input, int n) {
93+
94+
/** LC 1109. Corporate Flight Bookings input : [start, end, seats]
95+
*
96+
* NOTE !!!
97+
*
98+
* in java, index start from 0;
99+
* but in LC 1109, index start from 1
100+
*
101+
*/
102+
int[] tmp = new int[n + 1];
103+
for (int[] x : input) {
104+
int start = x[0];
105+
int end = x[1];
106+
int seats = x[2];
107+
108+
// add
109+
tmp[start] += seats;
110+
111+
// subtract
112+
if (end + 1 <= n) {
113+
tmp[end + 1] -= seats;
114+
}
115+
}
116+
117+
for (int i = 1; i < tmp.length; i++) {
118+
//tmp[i] = tmp[i - 1] + tmp[i];
119+
tmp[i] += tmp[i - 1];
120+
}
121+
122+
return Arrays.copyOfRange(tmp, 1, n+1);
123+
}
124+
```
125+
85126
### 1-1) Basic OP
86127

87128
## 2) LC Example
@@ -116,6 +157,40 @@ class Solution {
116157
// java
117158
// LC 1109
118159

160+
// V0
161+
// IDEA : DIFFERENCE ARRAY
162+
// LC 1109
163+
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_java/src/main/java/LeetCodeJava/Array/CorporateFlightBookings.java
164+
public static int[] getModifiedArray(int length, int[][] updates) {
165+
166+
int[] tmp = new int[length + 1]; // or new int[length]; both works
167+
for (int[] x : updates) {
168+
int start = x[0];
169+
int end = x[1];
170+
int amount = x[2];
171+
172+
// add
173+
tmp[start] += amount;
174+
175+
// subtract (remove the "adding affect" on "NEXT" element)
176+
/**
177+
* NOTE !!!
178+
*
179+
* <p>we remove the "adding affect" on NEXT element (e.g. end + 1)
180+
*/
181+
if (end + 1 < length) { // NOTE !!! use `end + 1`
182+
tmp[end + 1] -= amount;
183+
}
184+
}
185+
186+
// prepare final result
187+
for (int i = 1; i < tmp.length; i++) {
188+
tmp[i] += tmp[i - 1];
189+
}
190+
191+
return Arrays.copyOfRange(tmp, 0, length); // return the sub array between 0, lengh index
192+
}
193+
119194
// V1
120195
class Solution {
121196
public int[] corpFlightBookings(int[][] bookings, int n) {
@@ -138,31 +213,6 @@ class Solution {
138213
}
139214
```
140215

141-
```java
142-
// java
143-
144-
// V2
145-
public int[] corpFlightBookings_2(int[][] bookings, int n) {
146-
int[] ans = new int[n];
147-
for (int[] booking : bookings) {
148-
int i = booking[0] - 1;
149-
int j = booking[1];
150-
int seats = booking[2];
151-
ans[i] += seats;
152-
if (j != n)
153-
ans[j] -= seats;
154-
}
155-
156-
int count = 0;
157-
for (int i = 0; i < ans.length; i++) {
158-
ans[i] += count;
159-
count = ans[i];
160-
}
161-
return ans;
162-
}
163-
```
164-
165-
166216
### Car Pooling
167217

168218
```java

0 commit comments

Comments
 (0)