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.
- Prepare
- Algorithms
- Greedy
- Candies
- Discussions
Candies
Candies
Sort by
recency
|
674 Discussions
|
Please Login in order to post a comment
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)
My Java 8 solution, passing all test cases, for a Max Score of 50:
The code is fairly simple & self-explanatory.
public static long candies(int n, List arr) { // Write your code here
Java:
C++ Solution