Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 088bf63

Browse files
feat: add solutions to lc problem: No.370 (#3796)
1 parent 54778c9 commit 088bf63

File tree

11 files changed

+491
-246
lines changed

11 files changed

+491
-246
lines changed

‎solution/0300-0399/0370.Range Addition/README.md‎

Lines changed: 161 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,16 @@ class Solution {
101101
public:
102102
vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
103103
vector<int> d(length);
104-
for (auto& e : updates) {
104+
for (const auto& e : updates) {
105105
int l = e[0], r = e[1], c = e[2];
106106
d[l] += c;
107-
if (r + 1 < length) d[r + 1] -= c;
107+
if (r + 1 < length) {
108+
d[r + 1] -= c;
109+
}
110+
}
111+
for (int i = 1; i < length; ++i) {
112+
d[i] += d[i - 1];
108113
}
109-
for (int i = 1; i < length; ++i) d[i] += d[i - 1];
110114
return d;
111115
}
112116
};
@@ -131,6 +135,24 @@ func getModifiedArray(length int, updates [][]int) []int {
131135
}
132136
```
133137

138+
#### TypeScript
139+
140+
```ts
141+
function getModifiedArray(length: number, updates: number[][]): number[] {
142+
const d: number[] = Array(length).fill(0);
143+
for (const [l, r, c] of updates) {
144+
d[l] += c;
145+
if (r + 1 < length) {
146+
d[r + 1] -= c;
147+
}
148+
}
149+
for (let i = 1; i < length; ++i) {
150+
d[i] += d[i - 1];
151+
}
152+
return d;
153+
}
154+
```
155+
134156
#### JavaScript
135157

136158
```js
@@ -140,7 +162,7 @@ func getModifiedArray(length int, updates [][]int) []int {
140162
* @return {number[]}
141163
*/
142164
var getModifiedArray = function (length, updates) {
143-
const d = newArray(length).fill(0);
165+
const d = Array(length).fill(0);
144166
for (const [l, r, c] of updates) {
145167
d[l] += c;
146168
if (r + 1 < length) {
@@ -177,82 +199,74 @@ var getModifiedArray = function (length, updates) {
177199

178200
```python
179201
class BinaryIndexedTree:
180-
def __init__(self, n):
202+
__slots__ = "n", "c"
203+
204+
def __init__(self, n: int):
181205
self.n = n
182206
self.c = [0] * (n + 1)
183207

184-
@staticmethod
185-
def lowbit(x):
186-
return x & -x
187-
188-
def update(self, x, delta):
208+
def update(self, x: int, delta: int) -> None:
189209
while x <= self.n:
190210
self.c[x] += delta
191-
x += BinaryIndexedTree.lowbit(x)
211+
x += x &-x
192212

193-
def query(self, x):
213+
def query(self, x: int) -> int:
194214
s = 0
195215
while x:
196216
s += self.c[x]
197-
x -= BinaryIndexedTree.lowbit(x)
217+
x -= x &-x
198218
return s
199219

200220

201221
class Solution:
202222
def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]:
203223
tree = BinaryIndexedTree(length)
204-
for start, end, inc in updates:
205-
tree.update(start + 1, inc)
206-
tree.update(end + 2, -inc)
224+
for l, r, c in updates:
225+
tree.update(l + 1, c)
226+
tree.update(r + 2, -c)
207227
return [tree.query(i + 1) for i in range(length)]
208228
```
209229

210230
#### Java
211231

212232
```java
213-
class Solution {
214-
public int[] getModifiedArray(int length, int[][] updates) {
215-
BinaryIndexedTree tree = new BinaryIndexedTree(length);
216-
for (int[] e : updates) {
217-
int start = e[0], end = e[1], inc = e[2];
218-
tree.update(start + 1, inc);
219-
tree.update(end + 2, -inc);
220-
}
221-
int[] ans = new int[length];
222-
for (int i = 0; i < length; ++i) {
223-
ans[i] = tree.query(i + 1);
224-
}
225-
return ans;
226-
}
227-
}
228-
229233
class BinaryIndexedTree {
230234
private int n;
231235
private int[] c;
232236

233237
public BinaryIndexedTree(int n) {
234238
this.n = n;
235-
c = new int[n + 1];
239+
this.c = new int[n + 1];
236240
}
237241

238242
public void update(int x, int delta) {
239-
while (x <= n) {
243+
for (; x <= n; x += x &-x) {
240244
c[x] += delta;
241-
x += lowbit(x);
242245
}
243246
}
244247

245248
public int query(int x) {
246249
int s = 0;
247-
while (x > 0) {
250+
for (; x > 0; x -= x &-x) {
248251
s += c[x];
249-
x -= lowbit(x);
250252
}
251253
return s;
252254
}
255+
}
253256

254-
public static int lowbit(int x) {
255-
return x & -x;
257+
class Solution {
258+
public int[] getModifiedArray(int length, int[][] updates) {
259+
var tree = new BinaryIndexedTree(length);
260+
for (var e : updates) {
261+
int l = e[0], r = e[1], c = e[2];
262+
tree.update(l + 1, c);
263+
tree.update(r + 2, -c);
264+
}
265+
int[] ans = new int[length];
266+
for (int i = 0; i < length; ++i) {
267+
ans[i] = tree.query(i + 1);
268+
}
269+
return ans;
256270
}
257271
}
258272
```
@@ -261,46 +275,43 @@ class BinaryIndexedTree {
261275

262276
```cpp
263277
class BinaryIndexedTree {
264-
public:
278+
private:
265279
int n;
266280
vector<int> c;
267281

268-
BinaryIndexedTree(int _n)
269-
: n(_n)
270-
, c(_n + 1) {}
282+
public:
283+
BinaryIndexedTree(int n)
284+
: n(n)
285+
, c(n + 1) {}
271286

272287
void update(int x, int delta) {
273-
while (x <= n) {
288+
for (; x <= n; x += x & -x) {
274289
c[x] += delta;
275-
x += lowbit(x);
276290
}
277291
}
278292

279293
int query(int x) {
280294
int s = 0;
281-
while (x > 0) {
295+
for (; x > 0; x -= x & -x) {
282296
s += c[x];
283-
x -= lowbit(x);
284297
}
285298
return s;
286299
}
287-
288-
int lowbit(int x) {
289-
return x & -x;
290-
}
291300
};
292301

293302
class Solution {
294303
public:
295304
vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
296305
BinaryIndexedTree* tree = new BinaryIndexedTree(length);
297-
for (auto& e : updates) {
298-
int start = e[0], end = e[1], inc = e[2];
299-
tree->update(start + 1, inc);
300-
tree->update(end + 2, -inc);
306+
for (const auto& e : updates) {
307+
int l = e[0], r = e[1], c = e[2];
308+
tree->update(l + 1, c);
309+
tree->update(r + 2, -c);
301310
}
302311
vector<int> ans;
303-
for (int i = 0; i < length; ++i) ans.push_back(tree->query(i + 1));
312+
for (int i = 0; i < length; ++i) {
313+
ans.push_back(tree->query(i + 1));
314+
}
304315
return ans;
305316
}
306317
};
@@ -314,46 +325,116 @@ type BinaryIndexedTree struct {
314325
c []int
315326
}
316327
317-
func newBinaryIndexedTree(n int) *BinaryIndexedTree {
318-
c := make([]int, n+1)
319-
return &BinaryIndexedTree{n, c}
328+
func NewBinaryIndexedTree(n int) *BinaryIndexedTree {
329+
return &BinaryIndexedTree{n: n, c: make([]int, n+1)}
320330
}
321331
322-
func (this *BinaryIndexedTree) lowbit(x int) int {
323-
return x & -x
324-
}
325-
326-
func (this *BinaryIndexedTree) update(x, delta int) {
327-
for x <= this.n {
328-
this.c[x] += delta
329-
x += this.lowbit(x)
332+
func (bit *BinaryIndexedTree) update(x, delta int) {
333+
for ; x <= bit.n; x += x & -x {
334+
bit.c[x] += delta
330335
}
331336
}
332337
333-
func (this *BinaryIndexedTree) query(x int) int {
338+
func (bit *BinaryIndexedTree) query(x int) int {
334339
s := 0
335-
for x > 0 {
336-
s += this.c[x]
337-
x -= this.lowbit(x)
340+
for ; x > 0; x -= x & -x {
341+
s += bit.c[x]
338342
}
339343
return s
340344
}
341345
342-
func getModifiedArray(length int, updates [][]int) []int {
343-
tree := newBinaryIndexedTree(length)
346+
func getModifiedArray(length int, updates [][]int) (ans []int) {
347+
bit := NewBinaryIndexedTree(length)
344348
for _, e := range updates {
345-
start, end, inc := e[0], e[1], e[2]
346-
tree.update(start+1, inc)
347-
tree.update(end+2, -inc)
349+
l, r, c := e[0], e[1], e[2]
350+
bit.update(l+1, c)
351+
bit.update(r+2, -c)
348352
}
349-
ans := make([]int, length)
350-
for i := range ans {
351-
ans[i] = tree.query(i + 1)
353+
for i := 1; i <= length; i++ {
354+
ans = append(ans, bit.query(i))
352355
}
353-
return ans
356+
return
354357
}
355358
```
356359

360+
#### TypeScript
361+
362+
```ts
363+
class BinaryIndexedTree {
364+
private n: number;
365+
private c: number[];
366+
367+
constructor(n: number) {
368+
this.n = n;
369+
this.c = Array(n + 1).fill(0);
370+
}
371+
372+
update(x: number, delta: number): void {
373+
for (; x <= this.n; x += x & -x) {
374+
this.c[x] += delta;
375+
}
376+
}
377+
378+
query(x: number): number {
379+
let s = 0;
380+
for (; x > 0; x -= x & -x) {
381+
s += this.c[x];
382+
}
383+
return s;
384+
}
385+
}
386+
387+
function getModifiedArray(length: number, updates: number[][]): number[] {
388+
const bit = new BinaryIndexedTree(length);
389+
for (const [l, r, c] of updates) {
390+
bit.update(l + 1, c);
391+
bit.update(r + 2, -c);
392+
}
393+
return Array.from({ length }, (_, i) => bit.query(i + 1));
394+
}
395+
```
396+
397+
#### JavaScript
398+
399+
```js
400+
/**
401+
* @param {number} length
402+
* @param {number[][]} updates
403+
* @return {number[]}
404+
*/
405+
var getModifiedArray = function (length, updates) {
406+
class BinaryIndexedTree {
407+
constructor(n) {
408+
this.n = n;
409+
this.c = Array(n + 1).fill(0);
410+
}
411+
412+
update(x, delta) {
413+
while (x <= this.n) {
414+
this.c[x] += delta;
415+
x += x & -x;
416+
}
417+
}
418+
419+
query(x) {
420+
let s = 0;
421+
while (x > 0) {
422+
s += this.c[x];
423+
x -= x & -x;
424+
}
425+
return s;
426+
}
427+
}
428+
429+
const bit = new BinaryIndexedTree(length);
430+
for (const [l, r, c] of updates) {
431+
bit.update(l + 1, c);
432+
bit.update(r + 2, -c);
433+
}
434+
return Array.from({ length }, (_, i) => bit.query(i + 1));
435+
};
436+
```
437+
357438
<!-- tabs:end -->
358439

359440
<!-- solution:end -->

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /