• + 0 comments

    My original solution was:

    function arrayManipulation(n, queries) {
      let array = []
      let maxArrayValue = 0
      let indices = queries
        .reduce((_indices, query) => {
          _indices.start = (_indices.start === undefined)
            ? query[0] - 1
            : (query[0] - 1 < _indices.start)
              ? query[0] - 1
              : _indices.start
          _indices.stop = (_indices.stop === undefined)
            ? query[1] - 1
            : (query[0] - 1 > _indices.stop)
              ? query[1] - 1
              : _indices.stop
          return _indices
        }, {
          start: undefined,
          stop: undefined,
        })
      for(var i = 0; i <= queries.length; i++) {
        let subArray = []
        for(var j = indices.start; j <= indices.stop; j++) {
          if(i === 0) {
            subArray[j - indices.start] = 0
          } else {
            if(
              j >= queries[i - 1][0] - 1 &&
              j <= queries[i - 1][1] - 1
            ) {
              let additiveValue = queries[i - 1][2]
              subArray[j - indices.start] = array[j - indices.start] + additiveValue
            } else {
              subArray[j - indices.start] = array[j - indices.start]
            }
          }
          maxArrayValue = (maxArrayValue < subArray[j - indices.start])
            ? subArray[j - indices.start]
            : maxArrayValue
        }
        array = subArray
      }
      return maxArrayValue
    }
    

    It returns the correct answers, but times out on half of the tests.

    I did not consider that I could form an array with empty entries and populate it with the additive value at start/stop indices. That sped the processing time up enough to pass the timing tests. I ended up studying your solution the producing the same thing after understanding how it works. Thanks!

    With HackerRank solutions there is often an easy-to-read stepped solution that will pass tests with correct answers, but fail on timeouts with large numbers or sets of data. To avoid timeouts it is necessary to author solutions that are more considerate of data regardless of how easy-to-read.

    `