We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// Thank you, I applied this logic in Javascript, here's the code if anyone's introuble with any test cases.
function candies(n, arr) {
var candiesArray = new Array(n);
//Declare how much the first person starts with.
candiesArray[0]=1;
var sumCandies = 0;
//Loop forward -->
for( var i = 1 ; i < n ; i++ ) {
if( arr[i]>arr[i-1] ){
candiesArray[i]=candiesArray[i-1]+1;
} else {
candiesArray[i]=1;
}
}
//Loop back <--
for (var i = n-2; i>=0; i--){
if(arr[i]>arr[i+1] && candiesArray[i]<=candiesArray[i+1]){
candiesArray[i]=candiesArray[i+1]+1;
}
sumCandies += candiesArray[i];
}
sumCandies += candiesArray[n-1];
return(sumCandies);
}
Candies
You are viewing a single comment's thread. Return to all comments →
// Thank you, I applied this logic in Javascript, here's the code if anyone's introuble with any test cases.