• + 1 comment

    great solution. thank you.

    Same algorithm on C++ // Complete the arrayManipulation function below. long arrayManipulation(int n, vector> queries) {

    vector<long> v(n, 0);
    for(vector<int> q: queries)
    {
        v[q[0]-1] += q[2];
        v[q[1]] -= q[2];
    }
    long max = 0;
    long sum = 0;
    for(long l: v)
    {
        sum += l;
        if(max < sum)
        {
            max = sum;
        }
    }
    
    return max;
    

    }

    • + 0 comments

      This code has a little bug. This is the code fixed:

      long arrayManipulation(int n, vector> queries) {
        vector<long> v(n, 0);
        for(vector<int> q: queries)
        {
          v[q[0]-1] += q[2];
          if (q[1] < v.size()) v[q[1]] -= q[2];
        }
      
        long max = 0;
        long sum = 0;
      
        for(long l: v)
        {
          sum += l;
          if(max < sum)
          {
              max = sum;
          }
        }
      
        return max;
      }