Skip to content

feat: add solutions to lc problem: No.370 #3796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
241 changes: 161 additions & 80 deletions solution/0300-0399/0370.Range Addition/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,16 @@ class Solution {
public:
vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
vector<int> d(length);
for (auto& e : updates) {
for (const auto& e : updates) {
int l = e[0], r = e[1], c = e[2];
d[l] += c;
if (r + 1 < length) d[r + 1] -= c;
if (r + 1 < length) {
d[r + 1] -= c;
}
}
for (int i = 1; i < length; ++i) {
d[i] += d[i - 1];
}
for (int i = 1; i < length; ++i) d[i] += d[i - 1];
return d;
}
};
Expand All @@ -131,6 +135,24 @@ func getModifiedArray(length int, updates [][]int) []int {
}
```

#### TypeScript

```ts
function getModifiedArray(length: number, updates: number[][]): number[] {
const d: number[] = Array(length).fill(0);
for (const [l, r, c] of updates) {
d[l] += c;
if (r + 1 < length) {
d[r + 1] -= c;
}
}
for (let i = 1; i < length; ++i) {
d[i] += d[i - 1];
}
return d;
}
```

#### JavaScript

```js
Expand All @@ -140,7 +162,7 @@ func getModifiedArray(length int, updates [][]int) []int {
* @return {number[]}
*/
var getModifiedArray = function (length, updates) {
const d = new Array(length).fill(0);
const d = Array(length).fill(0);
for (const [l, r, c] of updates) {
d[l] += c;
if (r + 1 < length) {
Expand Down Expand Up @@ -177,82 +199,74 @@ var getModifiedArray = function (length, updates) {

```python
class BinaryIndexedTree:
def __init__(self, n):
__slots__ = "n", "c"

def __init__(self, n: int):
self.n = n
self.c = [0] * (n + 1)

@staticmethod
def lowbit(x):
return x & -x

def update(self, x, delta):
def update(self, x: int, delta: int) -> None:
while x <= self.n:
self.c[x] += delta
x += BinaryIndexedTree.lowbit(x)
x += x & -x

def query(self, x):
def query(self, x: int) -> int:
s = 0
while x:
s += self.c[x]
x -= BinaryIndexedTree.lowbit(x)
x -= x & -x
return s


class Solution:
def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]:
tree = BinaryIndexedTree(length)
for start, end, inc in updates:
tree.update(start + 1, inc)
tree.update(end + 2, -inc)
for l, r, c in updates:
tree.update(l + 1, c)
tree.update(r + 2, -c)
return [tree.query(i + 1) for i in range(length)]
```

#### Java

```java
class Solution {
public int[] getModifiedArray(int length, int[][] updates) {
BinaryIndexedTree tree = new BinaryIndexedTree(length);
for (int[] e : updates) {
int start = e[0], end = e[1], inc = e[2];
tree.update(start + 1, inc);
tree.update(end + 2, -inc);
}
int[] ans = new int[length];
for (int i = 0; i < length; ++i) {
ans[i] = tree.query(i + 1);
}
return ans;
}
}

class BinaryIndexedTree {
private int n;
private int[] c;

public BinaryIndexedTree(int n) {
this.n = n;
c = new int[n + 1];
this.c = new int[n + 1];
}

public void update(int x, int delta) {
while (x <= n) {
for (; x <= n; x += x & -x) {
c[x] += delta;
x += lowbit(x);
}
}

public int query(int x) {
int s = 0;
while (x > 0) {
for (; x > 0; x -= x & -x) {
s += c[x];
x -= lowbit(x);
}
return s;
}
}

public static int lowbit(int x) {
return x & -x;
class Solution {
public int[] getModifiedArray(int length, int[][] updates) {
var tree = new BinaryIndexedTree(length);
for (var e : updates) {
int l = e[0], r = e[1], c = e[2];
tree.update(l + 1, c);
tree.update(r + 2, -c);
}
int[] ans = new int[length];
for (int i = 0; i < length; ++i) {
ans[i] = tree.query(i + 1);
}
return ans;
}
}
```
Expand All @@ -261,46 +275,43 @@ class BinaryIndexedTree {

```cpp
class BinaryIndexedTree {
public:
private:
int n;
vector<int> c;

BinaryIndexedTree(int _n)
: n(_n)
, c(_n + 1) {}
public:
BinaryIndexedTree(int n)
: n(n)
, c(n + 1) {}

void update(int x, int delta) {
while (x <= n) {
for (; x <= n; x += x & -x) {
c[x] += delta;
x += lowbit(x);
}
}

int query(int x) {
int s = 0;
while (x > 0) {
for (; x > 0; x -= x & -x) {
s += c[x];
x -= lowbit(x);
}
return s;
}

int lowbit(int x) {
return x & -x;
}
};

class Solution {
public:
vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
BinaryIndexedTree* tree = new BinaryIndexedTree(length);
for (auto& e : updates) {
int start = e[0], end = e[1], inc = e[2];
tree->update(start + 1, inc);
tree->update(end + 2, -inc);
for (const auto& e : updates) {
int l = e[0], r = e[1], c = e[2];
tree->update(l + 1, c);
tree->update(r + 2, -c);
}
vector<int> ans;
for (int i = 0; i < length; ++i) ans.push_back(tree->query(i + 1));
for (int i = 0; i < length; ++i) {
ans.push_back(tree->query(i + 1));
}
return ans;
}
};
Expand All @@ -314,46 +325,116 @@ type BinaryIndexedTree struct {
c []int
}

func newBinaryIndexedTree(n int) *BinaryIndexedTree {
c := make([]int, n+1)
return &BinaryIndexedTree{n, c}
func NewBinaryIndexedTree(n int) *BinaryIndexedTree {
return &BinaryIndexedTree{n: n, c: make([]int, n+1)}
}

func (this *BinaryIndexedTree) lowbit(x int) int {
return x & -x
}

func (this *BinaryIndexedTree) update(x, delta int) {
for x <= this.n {
this.c[x] += delta
x += this.lowbit(x)
func (bit *BinaryIndexedTree) update(x, delta int) {
for ; x <= bit.n; x += x & -x {
bit.c[x] += delta
}
}

func (this *BinaryIndexedTree) query(x int) int {
func (bit *BinaryIndexedTree) query(x int) int {
s := 0
for x > 0 {
s += this.c[x]
x -= this.lowbit(x)
for ; x > 0; x -= x & -x {
s += bit.c[x]
}
return s
}

func getModifiedArray(length int, updates [][]int) []int {
tree := newBinaryIndexedTree(length)
func getModifiedArray(length int, updates [][]int) (ans []int) {
bit := NewBinaryIndexedTree(length)
for _, e := range updates {
start, end, inc := e[0], e[1], e[2]
tree.update(start+1, inc)
tree.update(end+2, -inc)
l, r, c := e[0], e[1], e[2]
bit.update(l+1, c)
bit.update(r+2, -c)
}
ans := make([]int, length)
for i := range ans {
ans[i] = tree.query(i + 1)
for i := 1; i <= length; i++ {
ans = append(ans, bit.query(i))
}
return ans
return
}
```

#### TypeScript

```ts
class BinaryIndexedTree {
private n: number;
private c: number[];

constructor(n: number) {
this.n = n;
this.c = Array(n + 1).fill(0);
}

update(x: number, delta: number): void {
for (; x <= this.n; x += x & -x) {
this.c[x] += delta;
}
}

query(x: number): number {
let s = 0;
for (; x > 0; x -= x & -x) {
s += this.c[x];
}
return s;
}
}

function getModifiedArray(length: number, updates: number[][]): number[] {
const bit = new BinaryIndexedTree(length);
for (const [l, r, c] of updates) {
bit.update(l + 1, c);
bit.update(r + 2, -c);
}
return Array.from({ length }, (_, i) => bit.query(i + 1));
}
```

#### JavaScript

```js
/**
* @param {number} length
* @param {number[][]} updates
* @return {number[]}
*/
var getModifiedArray = function (length, updates) {
class BinaryIndexedTree {
constructor(n) {
this.n = n;
this.c = Array(n + 1).fill(0);
}

update(x, delta) {
while (x <= this.n) {
this.c[x] += delta;
x += x & -x;
}
}

query(x) {
let s = 0;
while (x > 0) {
s += this.c[x];
x -= x & -x;
}
return s;
}
}

const bit = new BinaryIndexedTree(length);
for (const [l, r, c] of updates) {
bit.update(l + 1, c);
bit.update(r + 2, -c);
}
return Array.from({ length }, (_, i) => bit.query(i + 1));
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Loading
Loading