lintcode :Segmemt Tree Build II
题目
Segmemt Tree Build II
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.
Given [3,2,1,4]
. The segment tree will be:
[0, 3] (max = 4)
/ \
[0, 1] (max = 3) [2, 3] (max = 4)
/ \ / \
[0, 0](max = 3) [1, 1](max = 2)[2, 2](max = 1) [3, 3] (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 Interval Tree
解题
理解题意:根据给的数组构建段树,该节点有区间及其该区间的最大值组成。区间的左右节点利用上面的规则计算。
/**
* Definition of SegmentTreeNode:
* public class SegmentTreeNode {
* public int start, end, max;
* public SegmentTreeNode left, right;
* public SegmentTreeNode(int start, int end, int max) {
* this.start = start;
* this.end = end;
* this.max = max
* this.left = this.right = null;
* }
* }
*/
这个节点定义要好好理解。
/**
* Definition of SegmentTreeNode:
* public class SegmentTreeNode {
* public int start, end, max;
* public SegmentTreeNode left, right;
* public SegmentTreeNode(int start, int end, int max) {
* this.start = start;
* this.end = end;
* this.max = max
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
*@param A: a list of integer
*@return: The root of Segment Tree
*/
public SegmentTreeNode build(int[] A) {
// write your code here
return build(0,A.length-1,A);
}
public SegmentTreeNode build(int start,int end,int[] A){
if(start > end ){
return null;
}
SegmentTreeNode root = new SegmentTreeNode(start,end);
if( start != end){
int mid = (start + end)/2;
root.left = build(start,mid,A);
root.right = build(mid+1,end,A);
root.max = Math.max(root.left.max,root.right.max);
}else{
root.max = A[start];
}
return root;
}
}
Java Code
总耗时: 2532 ms
"""
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:
# @oaram A: a list of integer
# @return: The root of Segment Tree
def build(self, A):
# write your code here
return self.buildX(0,len(A) - 1,A)
def buildX(self,start,end,A):
if start > end:
return None
maxX = 0
root = SegmentTreeNode(start,end)
if start != end:
mid = int((start + end)/2)
root.left = self.buildX(start,mid,A)
root.right = self.buildX(mid+1,end,A)
root.max = max(root.left.max,root.right.max)
else:
root.max = A[start]
return root
Python Code
总耗时: 750 ms
lintcode :Segmemt Tree Build II的更多相关文章
- [LintCode] Segment Tree Build II 建立线段树之二
The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...
- lintcode :Coins in Line II 硬币排成线 II
题目 硬币排成线 II 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币总价值,价值高的人获胜. 请判定 第一个玩家 是 ...
- lintcode:最大子数组II
题目 最大子数组 II 给定一个整数数组,找出两个不重叠子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 样例 给出数组[1, 3, -1, 2, -1, 2], ...
- lintcode:Binary Tree Postorder Traversal 二叉树的后序遍历
题目: 二叉树的后序遍历 给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解题: 递归程序 ...
- lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历
题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递 ...
- 439. Segment Tree Build II
最后更新 08-Jan-2017 开始介绍线段树的主要作用了,可以快速在区间查找极值,我猜是这样的..... 一个NODE的最大值取决于它左边和右边最大值里大 按个,所以,所以什么?对了,我们该用po ...
- Segment Tree Build I & II
Segment Tree Build I The structure of Segment Tree is a binary tree which each node has two attribut ...
- Lintcode247 Segment Tree Query II solution 题解
[题目描述] For an array, we can build a Segment Tree for it, each node stores an extra attribute count t ...
- [Locked] Closest Binary Search Tree Value & Closest Binary Search Tree Value II
Closest Binary Search Tree Value Given a non-empty binary search tree and a target value, find the ...
随机推荐
- vim 安装与运行以及代码的运行
vi功能是最弱的,也是*nix操蛋之后最基本的editor.后来vi被增强加入众多特性,这就是vim.再后来vim加入图形接口,gvim诞生了.功能最强的是gvim,它的很多特性vim并不支持,vi更 ...
- js设计模式(9)---代理模式
0.前言 KG.PP被交易到了布鲁克林篮网,我的心情很复杂,一方面为他们不能终老celtics感到惋惜,另一方面为他们能够再次冲击总冠军感到高兴.从07年以来,作为一个铁杆celtics球迷,他们给我 ...
- JavaScript移除数组元素减少长度的方法
JavaScript移除数组元素减少长度的方法,代码如下: //数组移除长度方法 var array=[]; array[0]="张三"; array[1]="李四& ...
- webstorm 10.0.4 注册码
激活码: UserName:William ===== LICENSE BEGIN ===== 45550-12042010 00001SzFN0n1bPII7FnAxnt0DDOPJA INauvJ ...
- iphone document 图片存储和读取
转载自:http://longquan.iteye.com/blog/1669990 存: //此处首先指定了图片存取路径(默认写到应用程序沙盒 中) NSArray *paths = NSSearc ...
- Python计算斗牛游戏的概率
Python计算斗牛游戏的概率 过年回家,都会约上亲朋好友聚聚会,会上经常会打麻将,斗地主,斗牛.在这些游戏中,斗牛是最受欢迎的,因为可以很多人一起玩,而且没有技术含量,都是看运气(专业术语是概率). ...
- linux c 实现大数相乘
#include <stdio.h> #include <string.h> #include <math.h> #include <stdbool.h& ...
- "=="和equals方法究竟有什么区别
(单独把一个东西说清楚,然后再说清楚另一个,这样,它们的区别自然就出来了,混在一起说,则很难说清楚) ==操作符专门用来比较两个变量的值是否相等,也就是用于比较变量所对应的内存中所存储的数值是否相同, ...
- http概述
HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第 ...
- z470 装黑苹果 10.92
1.分两个区,一个是mac安装区,一个是镜像拷贝区. 2.把镜像压进去. 3.安装好系统. 4.把镜像区的 extent拷贝到安装好的系统盘里去. 5.安装驱动,网盘里有.还有系统也在网盘里. 6.声 ...