• + 0 comments

    I spent the night managing linked lists, and a headache! Your solution is so simple and so clear. Here is my small addition:

    // credit amansbhandari for his crystal clear solution in C++
    // The beauty of JavaScript Object/Set/Arrays is an improvement
    // space = O(k)  time = O(k)
    function arrayManipulation(n, queries) {
      let arro = {}
      let a,b,k,i,x=0,imax=0
      for(i=0;i<queries.length;i++) {
          a = queries[i][0]
          b = queries[i][1]
          k = queries[i][2]
          arro[a] = (arro[a]) ? arro[a]+k : k
          arro[b+1] = (arro[b+1]) ? arro[b+1]-k : 0-k
      }
      for(i in arro) {
        x += arro[i]
        if(x > imax) imax = x
      }
      return imax
    }