forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distribute-candies-to-people.py
73 lines (66 loc) · 2.31 KB
/
distribute-candies-to-people.py
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
69
70
71
72
73
# Time: O(n + logc), c is the number of candies
# Space: O(1)
class Solution(object):
def distributeCandies(self, candies, num_people):
"""
:type candies: int
:type num_people: int
:rtype: List[int]
"""
# 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)
p = int((2*candies + 0.25)**0.5 - 0.5)
remaining = candies - (p+1)*p//2
rows, cols = divmod(p, num_people)
result = [0]*num_people
for i in xrange(num_people):
result[i] = (i+1)*(rows+1) + (rows*(rows+1)//2)*num_people if i < cols else \
(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(object):
def distributeCandies(self, candies, num_people):
"""
:type candies: int
:type num_people: int
:rtype: List[int]
"""
# find max integer p s.t. sum(1 + 2 + ... + p) <= C
left, right = 1, candies
while left <= right:
mid = left + (right-left)//2
if not ((mid <= candies*2 // (mid+1))):
right = mid-1
else:
left = mid+1
p = right
remaining = candies - (p+1)*p//2
rows, cols = divmod(p, num_people)
result = [0]*num_people
for i in xrange(num_people):
result[i] = (i+1)*(rows+1) + (rows*(rows+1)//2)*num_people if i < cols else \
(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(object):
def distributeCandies(self, candies, num_people):
"""
:type candies: int
:type num_people: int
:rtype: List[int]
"""
result = [0]*num_people
i = 0
while candies != 0:
result[i % num_people] += min(candies, i+1)
candies -= min(candies, i+1)
i += 1
return result