DowngradedOur downstream service providers are currently experiencing outages, and our engineering team is actively working on a resolution. Some services—including the Solver, Partner, and Tools—are temporarily degraded with higher latency and lower bandwidth. Rest assured, Intervipedia, Solutions, and the Question Bank features are not impacted and remain fully operational.DowngradedOur downstream service providers are currently experiencing outages, and our engineering team is actively working on a resolution. Some services—including the Solver, Partner, and Tools—are temporarily degraded with higher latency and lower bandwidth. Rest assured, Intervipedia, Solutions, and the Question Bank features are not impacted and remain fully operational.DowngradedOur downstream service providers are currently experiencing outages, and our engineering team is actively working on a resolution. Some services—including the Solver, Partner, and Tools—are temporarily degraded with higher latency and lower bandwidth. Rest assured, Intervipedia, Solutions, and the Question Bank features are not impacted and remain fully operational.DowngradedOur downstream service providers are currently experiencing outages, and our engineering team is actively working on a resolution. Some services—including the Solver, Partner, and Tools—are temporarily degraded with higher latency and lower bandwidth. Rest assured, Intervipedia, Solutions, and the Question Bank features are not impacted and remain fully operational.
DowngradedOur downstream service providers are currently experiencing outages, and our engineering team is actively working on a resolution. Some services—including the Solver, Partner, and Tools—are temporarily degraded with higher latency and lower bandwidth. Rest assured, Intervipedia, Solutions, and the Question Bank features are not impacted and remain fully operational.DowngradedOur downstream service providers are currently experiencing outages, and our engineering team is actively working on a resolution. Some services—including the Solver, Partner, and Tools—are temporarily degraded with higher latency and lower bandwidth. Rest assured, Intervipedia, Solutions, and the Question Bank features are not impacted and remain fully operational.DowngradedOur downstream service providers are currently experiencing outages, and our engineering team is actively working on a resolution. Some services—including the Solver, Partner, and Tools—are temporarily degraded with higher latency and lower bandwidth. Rest assured, Intervipedia, Solutions, and the Question Bank features are not impacted and remain fully operational.DowngradedOur downstream service providers are currently experiencing outages, and our engineering team is actively working on a resolution. Some services—including the Solver, Partner, and Tools—are temporarily degraded with higher latency and lower bandwidth. Rest assured, Intervipedia, Solutions, and the Question Bank features are not impacted and remain fully operational.
The Question
Coding

Candy Distribution Minimization

There are n children standing in a line, each assigned a specific rating value. You need to distribute candies to these children under two conditions: 1. Every child must receive at least one candy. 2. Children with a higher rating than their immediate neighbors (left or right) must receive more candies than those neighbors. Write a function to calculate the minimum total number of candies required to satisfy these conditions. Constraints: - 1 <= n <= 2 10^4 - 0 <= ratings[i] <= 2 10^4
Java
Greedy
Questions & Insights

Clarifying Questions

What is the maximum value for $n$? (Assuming n \le 2 \times 10^4 for a standard O(n) solution).
How should we handle equal ratings for adjacent children? (The rule states "higher rating gets more"; it does not imply that equal ratings must get the same number. If two adjacent children have equal ratings, one can have more candies than the other, or they can have the same, as long as the "higher rating" rule isn't violated. To minimize, we usually give the child with the same rating as their neighbor the minimum possible: 1 candy).
Are ratings always non-negative? (Assuming yes, though the algorithm works for any integer ratings).
What are the space constraints? (Can we use O(n) extra space, or is O(1) required?)

Assumptions

n \ge 1.
If ratings[i] == ratings[i-1], child i does not need more candies than child i-1; they only need at least 1 candy.
Time complexity should be O(n).

Thinking Process

Greedy Strategy: The problem presents local constraints (left neighbor and right neighbor) that must be satisfied globally. A greedy approach can solve this by looking at one direction at a time.
Forward Pass (Left-to-Right): Iterate through the children. If a child has a higher rating than the child to their left, they must have at least one more candy than that child. This ensures the "left-neighbor" constraint is satisfied.
Backward Pass (Right-to-Left): Iterate through the children in reverse. If a child has a higher rating than the child to their right, they must have more candies than that child. We update the current count to be the maximum of its existing value (from the forward pass) and candies[right_neighbor] + 1. This satisfies the "right-neighbor" constraint while preserving the "left-neighbor" one.
Summation: The result is the sum of the elements in the candies array after both passes.
Implementation Breakdown

Problem Set

Functional Requirements:
Assign at least 1 candy to every child.
If ratings[i] > ratings[i-1], candies[i] > candies[i-1].
If ratings[i] > ratings[i+1], candies[i] > candies[i+1].
Minimize the total sum of candies.
Constraints:
1 \le n \le 2 \times 10^4.
0 \le ratings[i] \le 2 \times 10^4.

Approach

Algorithm: Two-pass Greedy.
Data Structure: Integer Array.
Complexity:
Time: O(n) because we traverse the array twice.
Space: O(n) to store the candies count for each child.

Implementation

Wrap Up

Advanced Topics

Space Optimization ($O(1)$ space): It is possible to solve this in O(1) extra space (excluding the output) by using the Peak and Valley approach. You track the length of the current upward slope and downward slope. When you reach the end of a downward slope, you can calculate the candies needed for that "mountain" using the arithmetic series formula, taking care to handle the peak correctly (it must be the height of the taller of the two slopes).
Readability vs. Performance: While O(1) space is more efficient, the two-pass O(n) approach is significantly more readable and less error-prone during a 45-minute interview.
Parallelization: The forward pass and backward pass are inherently sequential. However, if the ratings array was massive and stored across multiple nodes, you could use a "Divide and Conquer" approach where you solve chunks and then reconcile the boundaries, similar to how parallel prefix sums are calculated.