-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcandy.py
58 lines (43 loc) · 1.85 KB
/
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
50
51
52
53
54
55
56
57
58
from typing import List, Tuple
class Solution:
def candy(self, ratings: List[int]) -> int:
max_candies = [1] * len(ratings)
cur_candies = 1
for child in range(1, len(ratings)):
if ratings[child - 1] < ratings[child]:
cur_candies += 1
max_candies[child] = max(max_candies[child], cur_candies)
else:
cur_candies = 1
cur_candies = 1
for child in reversed(range(len(ratings) - 1)):
if ratings[child + 1] < ratings[child]:
cur_candies += 1
max_candies[child] = max(max_candies[child], cur_candies)
else:
cur_candies = 1
return sum(max_candies)
def candy_topological_sort(self, ratings: List[int]) -> int:
def build_adj_list() -> Tuple[List[List[int]], List[int]]:
adj_list: List[List[int]] = [[] for _ in range(len(ratings))]
indegrees = [0] * len(ratings)
for child in range(len(ratings)):
if child > 0 and ratings[child - 1] < ratings[child]:
adj_list[child].append(child - 1)
indegrees[child - 1] += 1
if child < len(ratings) - 1 and ratings[child + 1] < ratings[child]:
adj_list[child].append(child + 1)
indegrees[child + 1] += 1
return adj_list, indegrees
adj_list, indegrees = build_adj_list()
depths = [1] * len(ratings)
def dfs(child: int) -> int:
max_depth = 1
for adj_child in adj_list[child]:
max_depth = max(max_depth, dfs(adj_child) + 1)
depths[child] = max_depth
return max_depth
for child in range(len(ratings)):
if indegrees[child] == 0:
dfs(child)
return sum(depths)