forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distribute-candies-to-people.cpp
68 lines (63 loc) · 2.32 KB
/
distribute-candies-to-people.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Time: O(n + logc), c is the number of candies
// Space: O(1)
class Solution {
public:
vector<int> distributeCandies(int candies, int num_people) {
// find max integer p s.t. sum(1 + 2 + ... + p) <= C
// => remaining : 0 <= C-(1+p)*p/2 < p+1
// => -2p-2 < p^2+p-2C <= 0
// => 2C+1/4 < (p+3/2)^2 and (p+1/2)^2 <= 2C+1/4
// => sqrt(2C+1/4)-3/2 < p <= sqrt(2C+1/4)-1/2
// => p = floor(sqrt(2C+1/4)-1/2)
int p = int(sqrt(2 * candies + 0.25) - 0.5);
int remaining = candies - (p + 1) * p / 2;
int rows = p / num_people, cols = p % num_people;
vector<int> result(num_people);
for (int i = 0; i < num_people; ++i) {
result[i] = (i < cols) ? (i + 1) * (rows + 1) + (rows * (rows + 1) / 2) * num_people
: (i + 1) * rows + ((rows - 1) * rows / 2) * num_people;
}
result[cols] += remaining;
return result;
}
};
// Time: O(n + logc), c is the number of candies
// Space: O(1)
class Solution2 {
public:
vector<int> distributeCandies(int candies, int num_people) {
// find max integer p s.t. sum(1 + 2 + ... + p) <= C
int left = 1, right = candies;
while (left <= right) {
const auto& mid = left + (right - left) / 2;
if (!(mid <= candies * 2 / (mid + 1))) {
right = mid - 1;
} else {
left = mid + 1;
}
}
int p = right;
int remaining = candies - (p + 1) * p / 2;
int rows = p / num_people, cols = p % num_people;
vector<int> result(num_people);
for (int i = 0; i < num_people; ++i) {
result[i] = (i < cols) ? (i + 1) * (rows + 1) + (rows * (rows + 1) / 2) * num_people
: (i + 1) * rows + ((rows - 1) * rows / 2) * num_people;
}
result[cols] += remaining;
return result;
}
};
// Time: O(sqrt(c)), c is the number of candies
// Space: O(1)
class Solution3 {
public:
vector<int> distributeCandies(int candies, int num_people) {
vector<int> result(num_people);
for (int i = 0; candies; ++i) {
result[i % num_people] += min(candies, i + 1);
candies -= min(candies, i + 1);
}
return result;
}
};