-
Notifications
You must be signed in to change notification settings - Fork 0
/
HouseRobberIII.py
92 lines (75 loc) · 2.33 KB
/
HouseRobberIII.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
87
88
89
90
91
#!usr/bin/env python3
# -*- coding: utf-8 -*-
' House Robber III - Medium '
__author__ = 'Roger Cui'
'''
The thief has found himself a new place for his thievery again. There is only one
entrance to this area, called the "root." Besides the root, each house has one
and only one parent house. After a tour, the smart thief realized that "all houses
in this place forms a binary tree". It will automatically contact the police if
two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting
the police.
Example 1:
3
/ \
2 3
\ \
3 1
Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
3
/ \
4 5
/ \ \
1 3 1
Maximum amount of money the thief can rob = 4 + 5 = 9.
Results:
Run time: beats 82.45%
Time complex: O()
Space complex: O()
'''
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def rob(self, root):
"""
:type root: TreeNode
:rtype: int
"""
# # version 1
# def sub_rob(sub_root, is_rob):
# if not sub_root:
# return 0
# if is_rob:
# return sub_rob(sub_root.left, False) + sub_rob(sub_root.right, False) + sub_root.val
# else:
# sub_rob1 = sub_rob(sub_root.left, True) + sub_rob(sub_root.right, True)
# sub_rob2 = sub_rob(sub_root.left, False) + sub_rob(sub_root.right, False)
# sub_rob3 = sub_rob(sub_root.left, True) + sub_rob(sub_root.right, False)
# sub_rob4 = sub_rob(sub_root.left, False) + sub_rob(sub_root.right, True)
# return max(sub_rob1, sub_rob2, sub_rob3, sub_rob4)
# return max(sub_rob(root, True), sub_rob(root, False))
# version 2
def sub_rob(sub_root):
if not sub_root:
return (0, 0)
left_rob = sub_rob(sub_root.left)
right_rob = sub_rob(sub_root.right)
rob_now = left_rob[1] + right_rob[1] + sub_root.val
rob_later = max(left_rob) + max(right_rob)
return (rob_now, rob_later)
return max(sub_rob(root))
if __name__ == '__main__':
node1, node2, node3 = TreeNode(3), TreeNode(4), TreeNode(5)
node4, node5, node6 = TreeNode(1), TreeNode(3), TreeNode(1)
node1.left, node1.right = node2, node3
node2.left, node2.right = node4, node5
node3.right = node6
obj = Solution()
result = obj.rob(node1)
print(result)