-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path439.segment-tree-build-ii.py
77 lines (69 loc) · 2.4 KB
/
439.segment-tree-build-ii.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
# Tag: Segment Tree
# Time: O(N)
# Space: O(1)
# Ref: -
# Note: -
# The structure of Segment Tree is a binary tree which each node has two attributes `start` and `end` denote an segment / interval.
#
# `start` and `end` are both integers, they should be assigned in following rules:
#
# - The root's `start` and `end` is given by `build` method.
# - The left child of node A has `start=A.left, end=(A.left + A.right) / 2`.
# - The right child of node A has `start=(A.left + A.right) / 2 + 1, end=A.right`.
# - if `start` equals to `end`, there will be no children for this node.
#
# Implement a `build` method with a given array, so that we can create a corresponding segment tree with every node value represent the corresponding interval max value in the array, return the root of this segment tree.
#
# ```
# Input: [3,2,1,4]
# Explanation:
# The segment tree will be
# [0,3](max=4)
# / \
# [0,1] [2,3]
# (max=3) (max=4)
# / \ / \
# [0,0] [1,1] [2,2] [3,3]
# (max=3)(max=2) (max=1)(max=4)
# ```
#
# Segment Tree (a.k.a Interval Tree) is an advanced data structure which can support queries like:
#
# - which of these intervals contain a given point
# - which of these points are in a given interval
#
# See wiki:
# [Segment Tree](https://en.wikipedia.org/wiki/Segment_tree "Segment Tree")
# [Interval Tree](https://en.wikipedia.org/wiki/Interval_tree "Interval Tree")
from typing import (
List,
)
from lintcode import (
SegmentTreeNode,
)
"""
Definition of SegmentTreeNode:
class SegmentTreeNode:
def __init__(self, start, end, max):
self.start, self.end, self.max = start, end, max
self.left, self.right = None, None
"""
class Solution:
"""
@param a: a list of integer
@return: The root of Segment Tree
"""
def build(self, a: List[int]) -> SegmentTreeNode:
# write your code here
return self.helper(a, 0, len(a) - 1)
def helper(self, a: list, start: int, end: int) -> SegmentTreeNode:
if start > end:
return None
node = SegmentTreeNode(start, end, a[start])
if start == end:
return node
mid = (start + end) // 2
node.left = self.helper(a, start, mid)
node.right = self.helper(a, mid + 1, end)
node.max = max(node.left.max, node.right.max)
return node