-1

I try to resolve this exercise in Hacker Rank:

Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array.

Example:

n = 10
queries = [[1,5,3],[4,8,7],[6,9,1]]

Queries are interpreted as follows:

a b k
1 5 3
4 8 7
6 9 1

Add the values of k between the indices a and b inclusive:

index->  1 2 3  4  5 6 7 8 9 10
        [0,0,0, 0, 0,0,0,0,0, 0]
        [3,3,3, 3, 3,0,0,0,0, 0]
        [3,3,3,10,10,7,7,7,0, 0]
        [3,3,3,10,10,8,8,8,1, 0]

The largest value is after all operations are performed.
Function Description

Complete the function arrayManipulation in the editor below.

arrayManipulation has the following parameters:

int n - the number of elements in the array int queries[q][3] - a two dimensional array of queries where each queries[i] contains three integers, a, b, and k. Returns

int - the maximum value in the resultant array Input Format

The first line contains two space-separated integers and , the size of the array and the number of operations. Each of the next lines contains three space-separated integers , and , the left index, right index and summand.
I used this code:

function arrayManipulation($n, $queries) {
    for($i = 1; $i <= $n; $i++){
        $a[$i] = 0;
    }
    foreach($queries as $index => $query){   
            $e = $query[0];
            $p = $query[1];
            $value = $query[2];
            for($b = $e; $b <= $p; $b++){
                $a[$b] += $value;
            }
    }
    return max($a);
}

All work except for biggest test case (Runtime Error), how i can improve that?
Seems like very easy task but i can't solve it.

The logic i have used:

  • Create an array with index = $n (size of array)
  • Start to foreach $queries and create a tmp variable with:
    • $e min of range

    • $p max of range

    • $value the value need for the sum

  • Use for-loop for sum value into array $a base to index from min and max
  • return max of $a
Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34

1 Answers1

2

I found the way to solve this problem in O(m * log(m)). It passes all test cases.

Look at my solution:

function arrayManipulation($n, $queries)
{
    $arr = [];

    foreach ($queries as $query) {
        $start = $query[0] - 1;
        $end = $query[1];
        $value = $query[2];

        $arr[$start] = ($arr[$start] ?? 0) + $value;

        if ($end < $n) {
            $arr[$end] = ($arr[$end] ?? 0) - $value;
        }
    }

    ksort($arr);

    $answer = 0;
    $currentSum = 0;

    foreach ($arr as $change) {
        $currentSum += $change;
        $answer = max($answer, $currentSum);
    }

    return $answer;
}
  • At first we should avoid using array of size n.
  • Then we construct array of value changes $arr, not of real values (like you've done).
  • And finally we just run through the resulting array and emulate real value for every iteration. And remember max value, of course. This will be our answer.
trckster
  • 430
  • 1
  • 6
  • 11