forked from Mohammed-Shoaib/Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC1103.cpp
More file actions
executable file
·30 lines (25 loc) · 727 Bytes
/
LC1103.cpp
File metadata and controls
executable file
·30 lines (25 loc) · 727 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
Problem Statement: https://leetcode.com/problems/distribute-candies-to-people/
Time: O(n)
Space: O(n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
vector<int> distributeCandies(int candies, int n) {
int full, turns, last;
full = sqrt(2 * candies + 0.25) - 0.5;
turns = full / n;
last = full % n;
vector<int> people(n);
// distribute for full turns
for (int i = 0; i < n; i++)
people[i] = turns * (i + 1) + n * turns * (turns - 1) / 2;
// distribute for leftover turns
for (int i = 0; i < last; i++)
people[i] += turns * n + (i + 1);
// distribute remaining to the last person
people[last] += candies - full * (full + 1) / 2;
return people;
}
};