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.
publicstaticlongcandies(intn,List<Integer>arr){long[]candies=newlong[n];Arrays.fill(candies,1);// Left to rightfor(inti=1;i<n;i++){if(arr.get(i)>arr.get(i-1)){candies[i]=candies[i-1]+1;}}// Right to leftfor(inti=n-2;i>=0;i--){if(arr.get(i)>arr.get(i+1)){candies[i]=Math.max(candies[i],candies[i+1]+1);}}returnArrays.stream(candies).sum();}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Candies
You are viewing a single comment's thread. Return to all comments →
Java: