bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1103. Distribute Candies to People

1103. Distribute Candies to People

Leetcode Solutions

Sum of Arithmetic Progression Approach

  1. Calculate the total number of complete gifts p using the formula p = floor(sqrt(2 * candies + 0.25) - 0.5).
  2. Calculate the remaining candies after giving out complete gifts using remaining = candies - (p * (p + 1) / 2).
  3. Calculate the number of complete turns rows = p // num_people.
  4. Calculate the number of people who received an extra gift in the last incomplete turn cols = p % num_people.
  5. Initialize the distribution array d with zeros.
  6. Distribute the candies for complete turns to each person.
  7. Distribute the extra gifts for the last incomplete turn.
  8. Add the remaining candies to the last person's gift.
  9. Return the distribution array d.
UML Thumbnail

Iterative Distribution Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...