370. Range Addition
given entier length и tableau updates, где updates[i] = [startIdxi, endIdxi, inci].
У вас есть tableau arr длины length, заполненный нулями. Вам нужно применить некоторые операции к arr. В i-й операции следует увеличить все elementы arr[startIdxi], arr[startIdxi + 1], ..., arr[endIdxi] на inci.
return arr после Applications всех обновлений.
Exemple:
Input: length = 5, updates = [[1,3,2],[2,4,3],[0,2,-2]]
Output: [-2,0,3,5,3]
C# solution
correspondant/originalpublic int[] GetModifiedArray(int length, int[][] updates) {
int[] result = new int[length];
foreach (var update in updates) {
int start = update[0], end = update[1], val = update[2];
result[start] += val;
if (end + 1 < length) {
result[end + 1] -= val;
}
}
for (int i = 1; i < length; i++) {
result[i] += result[i - 1];
}
return result;
}
C++ solution
brouillon automatique, à relire avant soumission#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
public vector<int>& GetModifiedArray(int length, int[][] updates) {
vector<int>& result = new int[length];
foreach (var update in updates) {
int start = update[0], end = update[1], val = update[2];
result[start] += val;
if (end + 1 < length) {
result[end + 1] -= val;
}
}
for (int i = 1; i < length; i++) {
result[i] += result[i - 1];
}
return result;
}
Java solution
correspondant/originalpublic int[] getModifiedArray(int length, int[][] updates) {
int[] result = new int[length];
for (int[] update : updates) {
int start = update[0], end = update[1], val = update[2];
result[start] += val;
if (end + 1 < length) {
result[end + 1] -= val;
}
}
for (int i = 1; i < length; i++) {
result[i] += result[i - 1];
}
return result;
}
JavaScript solution
correspondant/originalfunction getModifiedArray(length, updates) {
let result = new Array(length).fill(0);
for (let update of updates) {
let [start, end, val] = update;
result[start] += val;
if (end + 1 < length) {
result[end + 1] -= val;
}
}
for (let i = 1; i < length; i++) {
result[i] += result[i - 1];
}
return result;
}
Python solution
correspondant/originaldef getModifiedArray(length, updates):
result = [0] * length
for update in updates:
start, end, val = update
result[start] += val
if end + 1 < length:
result[end + 1] -= val
for i in range(1, length):
result[i] += result[i - 1]
return result
Go solution
correspondant/originalfunc getModifiedArray(length int, updates [][]int) []int {
result := make([]int, length)
for _, update := range updates {
start, end, val := update[0], update[1], update[2]
result[start] += val
if end+1 < length {
result[end+1] -= val
}
}
for i := 1; i < length; i++ {
result[i] += result[i-1]
}
return result
}
Algorithm
Для каждого обновления (start, end, val) выполните две операции:
Увеличьте значение в позиции start на val: arr[start] = arr[start] + val.
Уменьшите значение в позиции end + 1 на val: arr[end + 1] = arr[end + 1] - val.
Примените конечное преобразование: вычислите кумулятивную сумму всего tableauа (с индексами, начиная с 0).
return обновленный tableau arr.
😎
Vacancies for this task
offres actives with overlapping task tags are affichés.