370. Range Addition

LeetCode medium original: C# #array #csharp #leetcode #medium
선택한 UI 언어에 맞게 문제 텍스트를 러시아어에서 번역합니다. 코드는 변경하지 않습니다.

given 정수 length и 배열 updates, где updates[i] = [startIdxi, endIdxi, inci].

У вас есть 배열 arr длины length, заполненный нулями. Вам нужно применить некоторые операции к arr. В i-й операции следует увеличить все elementы arr[startIdxi], arr[startIdxi + 1], ..., arr[endIdxi] на inci.

return arr после Applications всех обновлений.

예제:

Input: length = 5, updates = [[1,3,2],[2,4,3],[0,2,-2]]

Output: [-2,0,3,5,3]

C# 해법

매칭됨/원본
public 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++ 해법

자동 초안, 제출 전 검토
#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 해법

매칭됨/원본
public 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 해법

매칭됨/원본
function 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 해법

매칭됨/원본
def 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 해법

매칭됨/원본
func 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.

Примените конечное преобразование: вычислите кумулятивную сумму всего 배열а (с индексами, начиная с 0).

return обновленный 배열 arr.

😎

Vacancies for this task

활성 채용 with overlapping task tags are 표시됨.

전체 채용
아직 활성 채용이 없습니다.