clueless coding // TODO: be smarter

LeetCode Range Addition: CPP Solution



TL;DR Code Solution


vector<int> getModifiedArray(int length, vector<vector<int>>& updates) {
    vector<int> result(length + 1);
    for (int i = 0; i < updates.size(); i++) {
        int start = updates[i][0];
        int end = updates[i][1];
        int val = updates[i][2];
        result[start] += val;
        result [end + 1] -= val;
    }

    int current = 0;
    for (int i = 0; i < result.size(); i++) {
        current += result[i];
        result[i] = current;
    }

    result.pop_back();
    return result;
}