-
Notifications
You must be signed in to change notification settings - Fork 1
/
135.candy.py
49 lines (42 loc) · 1.14 KB
/
135.candy.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
#
# @lc app=leetcode id=135 lang=python3
#
# [135] Candy
#
# @lc code=start
class Solution:
def candy(self, ratings: List[int]) -> int:
"""
:type ratings: List[int]
:rtype: int
"""
length = len(ratings)
if length == 0:
return 0
if length == 1:
return 1
# print("length:",length)
ans = [1 for x in range(length)]
minist_rate = ratings[0]
loc = 0
for i, c in enumerate(ratings):
if c < minist_rate:
loc = i
minist_rate = c
ans[loc] = 1
for x in range(loc + 1, length - 1):
left = ratings[x - 1]
if ratings[x] > left:
ans[x] = ans[x - 1] + 1
elif ratings[x] < left:
ans[x] = 1
else:
ans[x] = ans[x - 1]
for x in range(loc - 1, 0, -1):
right = ratings[x + 1]
if ratings[x] > right[]
# if ratings[0] > ratings[1]:
# ans[0] = 2
# if ratings[-1] > ratings[-2]:
# ans[-1] = 2
# @lc code=end