I am doing a coding test for a software engineering position. It is about adding investments to and array of investments.
investments = [ 0, 0, 0, 0, 0,]
n = length of the investment range
then a 2d array of rounds of investments is input as = [startingIndex, endingIndex, amountToAdd]
loop through the 2d array for the indexes and add the amount to those indexes in the investments array.
Then return the biggest number in the investments array
I think I have a correct answer but my code is running to slowly to test the cases lol. I can do Java and Python3 but would I see an increase in speed. What is the error in my Logic? Im using Javascript for this problem.
function maxValue(n, rounds) {
const investments = []
//create an array of investments size of n
for(let i = 0; i <= n; i++){
investments.push(0)
}
for(let i = 0; i < rounds.length; i++){
let startPos = rounds[i][0] -1;
let endPos = rounds[i][1] -1;
let amount = rounds[i][2];
for(let j = 0; j< investments.length; i++){
if(j >= startPos && j<= endPos){
investments[j] += amount
}
}
}
return Math.max(investments)
}do note the rounds indexes are start at 1 not 0