Quick actions

cmd+k|ctrl+k

Navigation

Languages

Balance investment - Hackerrank

Snippet info

Language

JavaScript

Visibility

public

Author

onifademola

Created

2021-10-22T20:40:02.240099Z

Updated

2021-10-22T20:40:02.240099Z

function maxValue(n, rounds) {
    // Write your code here
    // n is the array size for each contribution
    // a round [ 10, 10,  0,  0,  0]
    // a contrib 3    5     12
    let maxContribution = -Infinity;
    let investmentArray = new Array(n).fill(0);
    console.log(investmentArray)
    const invArray = [];
    let invArrayLength = 0;
    // invArray.push(investmentArray);
    //invArray[rounds[0][0]] = 
    for (let i=0; i < rounds.length; i++) {
        let currentMax = -Infinity;
        let currentIteration = rounds[i][0];
        if (invArrayLength === 0) {
            let thisCurr = rounds[0][0];
            while (thisCurr < rounds[0][1]) {
                investmentArray[thisCurr] += parseInt(rounds[0][2], 10);
                invArray.push(investmentArray);
                thisCurr++;
            }
            
            invArrayLength++;
        }
        console.log(invArray)
        for (j=currentIteration; j <= rounds[i][1]; j++ ) {
            let tempArray = invArray[invArray.length-1];
            
            tempArray[currentIteration] += parseInt(rounds[i][2]);// = currentSum;
            currentMax = tempArray[currentIteration] > currentMax ? tempArray[currentIteration] : currentMax;
            invArray.push(tempArray);
            currentIteration++;
        }
        /*
        while (currentIteration < rounds[i][1]) {
            //const currentSum = investmentArray[currentIteration] + rounds[i][2];
            investmentArray[currentIteration] += rounds[i][2];// = currentSum;
            currentMax = investmentArray[currentIteration] > currentMax ? investmentArray[currentIteration] : currentMax;
            currentIteration++;
        }*/
        // console.log(investmentArray)  
        console.log(invArray)  
        maxContribution = currentMax > maxContribution ? currentMax : maxContribution;
    }
    return maxContribution;
}

const rr = [[2,3,603], [1,1,286], [4,4,882]];
const nn = 4;

console.log(maxValue(nn, rr))
INFO