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.
what i did was first create a list of troughs in the rankings array (a trough is an item which is less than or equal to both its right and left neighbors) , then for each trought i do the following :
set the number of candies at the trough to 1.
go right incrementing the number of candies by one each time until i find a ranking crest.
repeat step 2 but to the left.
its not exactly dynamic programming , but its O(N) time complexity.
but i am wandering whats the dynamic programming version of the solution.
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 →
what i did was first create a list of troughs in the rankings array (a trough is an item which is less than or equal to both its right and left neighbors) , then for each trought i do the following :
set the number of candies at the trough to 1.
go right incrementing the number of candies by one each time until i find a ranking crest.
repeat step 2 but to the left.
its not exactly dynamic programming , but its O(N) time complexity.
but i am wandering whats the dynamic programming version of the solution.