-
Notifications
You must be signed in to change notification settings - Fork 43
/
path-sum-iv.py
86 lines (81 loc) · 2.55 KB
/
path-sum-iv.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
74
75
76
77
78
79
80
81
82
83
84
85
86
# V0
# V1
# http://bookshadow.com/weblog/2017/08/30/leetcode-path-sum-iv/
class Solution(object):
def pathSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dmap = {1 : 0}
leaves = set([1])
for num in nums:
path, val = num / 10, num % 10
lvl, seq = path / 10, path % 10
parent = (lvl - 1) * 10 + (seq + 1) / 2
dmap[path] = dmap[parent] + val
leaves.add(path)
if parent in leaves: leaves.remove(parent)
return sum(dmap[v] for v in leaves)
# V1'
# https://www.jiuzhang.com/solution/path-sum-iv/#tag-highlight-lang-python
class Solution:
"""
@param nums: the list
@return: the sum of all paths from the root towards the leaves
"""
def pathSumIV(self, nums):
# Write your code here.
if len(nums) == 1:
return nums[0] % 10;
a = [-1 for i in range(100)];
for i in nums:
a[i / 10] = i % 10;
ret = 0;
for i in range(2,5):
for j in range(1,9):
idx = i * 10 + j;
pre = (i - 1) * 10 + (j + 1)/2;
next1 = (i + 1) * 10 + 2 * j;
next2 = (i + 1) * 10 + 2 * j - 1;
if a[idx] != -1 and a[pre] != -1:
a[idx] = a[idx] + a[pre];
if a[next1] == -1 and a[next2] == -1 and a[idx] != -1:
ret = ret + a[idx];
return ret;
# V2
# Time: O(n)
# Space: O(p), p is the number of paths
import collections
class Solution(object):
def pathSum(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
class Node(object):
def __init__(self, num):
self.level = num/100 - 1
self.i = (num%100)/10 - 1
self.val = num%10
self.leaf = True
def isParent(self, other):
return self.level == other.level-1 and \
self.i == other.i/2
if not nums:
return 0
result = 0
q = collections.deque()
dummy = Node(10)
parent = dummy
for num in nums:
child = Node(num)
while not parent.isParent(child):
result += parent.val if parent.leaf else 0
parent = q.popleft()
parent.leaf = False
child.val += parent.val
q.append(child)
while q:
result += q.pop().val
return result