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.
My solution is based on observation that each student should get at least d candies, where d is the length of the longest decreasing subsequence in arr starting from this student. E.g. if scores are 8 6 3, then first student should get at least 3 candies, second should get at least 2, etc.
On the other hand, each student should get at least one more candy then their left colleague if they have a higher score. That is why we have the formula:
Candies
You are viewing a single comment's thread. Return to all comments →
My solution is based on observation that each student should get at least d candies, where d is the length of the longest decreasing subsequence in arr starting from this student. E.g. if scores are 8 6 3, then first student should get at least 3 candies, second should get at least 2, etc.
On the other hand, each student should get at least one more candy then their left colleague if they have a higher score. That is why we have the formula:
candies[i] = max(longest_decreasing_subsequence[i], candies[i - 1] + 1)