 #1
 246
 78
Homework Statement:
 Starting with a 1indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in your array.
Relevant Equations:
 NA
Starting with a 1indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in your array.
For example, the length of your array of zeros . Your list of queries is as follows:
a b k
1 5 3
4 8 7
6 9 1
Add the values of between the indices and 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 10 after all operations are performed.
Function Description
Complete the function arrayManipulation in the editor below. It must return an integer, the maximum value in the resulting array.
arrayManipulation has the following parameters:
n  the number of elements in your array queries
queries  a two dimensional array of queries where each queries contains three integers, a, b, and k.
Input Format
The first line contains two spaceseparated integers n and m , the size of the array and the number of operations.
Each of the next lines m contains three spaceseparated integers a,b and k, the left index, right index and summand.
Output Format
Return the integer maximum value in the finished array.
Sample Input
5 3
1 2 100
2 5 100
3 4 100
Sample Output
200
Explanation
After the first update list will be 100 100 0 0 0 .
After the second update list will be 100 200 100 100 100 .
After the third update list will be 100 200 200 200 100 . The required answer will be 200.
I have attached a file with the problem statement. Have a look at it if the above is not clear
My solution:
My solution is in javascript.
I have tried to optimize it as much as I can but my code is unable to pass some tests because it is taking too much time, any help will be appreciated.
If interested checkout this website.
https://www.hackerrank.com/challenges/crush/problem
For example, the length of your array of zeros . Your list of queries is as follows:
a b k
1 5 3
4 8 7
6 9 1
Add the values of between the indices and 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 10 after all operations are performed.
Function Description
Complete the function arrayManipulation in the editor below. It must return an integer, the maximum value in the resulting array.
arrayManipulation has the following parameters:
n  the number of elements in your array queries
queries  a two dimensional array of queries where each queries contains three integers, a, b, and k.
Input Format
The first line contains two spaceseparated integers n and m , the size of the array and the number of operations.
Each of the next lines m contains three spaceseparated integers a,b and k, the left index, right index and summand.
Output Format
Return the integer maximum value in the finished array.
Sample Input
5 3
1 2 100
2 5 100
3 4 100
Sample Output
200
Explanation
After the first update list will be 100 100 0 0 0 .
After the second update list will be 100 200 100 100 100 .
After the third update list will be 100 200 200 200 100 . The required answer will be 200.
I have attached a file with the problem statement. Have a look at it if the above is not clear
My solution:
My solution is in javascript.
Code:
// Complete the arrayManipulation function below.
function arrayManipulation(n, queries) {
let arr = new Array(n).fill(0);
for(let i=0; i<queries.length; i++) {
for(let j=queries[i][0]; j<=queries[i][1]; j++) {
arr[j1] = arr[j1] + queries[i][2]
}
}
return Math.max.apply(null, arr);
}
If interested checkout this website.
https://www.hackerrank.com/challenges/crush/problem
Attachments

111.7 KB Views: 18