forked from lilianweng/LeetcodePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandy.py
More file actions
40 lines (34 loc) · 1.15 KB
/
candy.py
File metadata and controls
40 lines (34 loc) · 1.15 KB
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
#!/usr/bin/env python
'''
Leetcode: Candy
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
'''
from __future__ import division
import random
def candy(scores):
print scores
n = len(scores)
candies = [1]*n
# left --> right
# add one if score > child on the right
for i in range(1,n):
if scores[i] > scores[i-1]:
candies[i] = max(candies[i], candies[i-1]+1)
elif scores[i] == scores[i-1]:
candies[i] = max(candies[i], candies[i-1])
print candies
# right --> left
# add one if score > child on the left
for i in reversed(range(n-1)):
if scores[i] > scores[i+1]:
candies[i] = max(candies[i], candies[i+1]+1)
elif scores[i] == scores[i+1]:
candies[i] = max(candies[i], candies[i+1])
print candies
return candies
if __name__ == '__main__':
candy([1,2,3,7,2,0,0,3,2,10])