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 ...
随机推荐
- MySql安装时在Start Service处失败
安装MySql时在最后Start Service时失败: 首先先卸载掉MySql. 查看MySql服务有没有启动,若启动,则先停止MySql服务. 打开注册表,到HKEY_LOCAL_MACHIN ...
- HTML5 INPUT新增属性
HTML5的input标签新增了很多属性,也是让大家非常兴奋的一件事,用简单的一个属性搞定以前复杂的JS验证.input新增的这些属性,使得html和js的分工更明确了,使用起来十分舒畅.我们先看下i ...
- 【转】理解JavaScript之闭包
闭包(closure)是掌握Javascript从人门到深入一个非常重要的门槛,它是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现.下面写下我的学习笔记~ 闭包-无处不 ...
- Hbase 0.95.2介绍及下载地址
HBase是一个分布式的.面向列的开源数据库,该技术来源于Google论文“Bigtable:一个结构化数据的分布式存储系统”.就像Bigtable利用了Google文件系统(File System) ...
- SequoiaDB数据库的一般概念介绍
SequoiaDB数据库的主要对象包括文档.集合.集合空间与索引等. 文档 SequoiaDB中的文档为JSON格式,一般又被称为记录.在数据库内部使用BSON,即二进制的方式存放JSON数据.一般情 ...
- linux命令详解之chkconfig命令使用方法
介绍一个linux常用命令,chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法:c ...
- R简易入门(二)
本文内容来源:https://www.dataquest.io/mission/128/working-with-data-frames 本文摘要:简单介绍一下用R处理数据 原始数据展示(这是一份 ...
- python学习第四天第一部分
1.字典的特性:无序.去重.查询速度快.比list占用内存多. 2.字典查询速度快的原因:因为他是哈希类型的. 3.什么是(hash)哈希? hash把任意长度的二进制映射为较短的固定长度的二进制,这 ...
- Python脚本控制的WebDriver 常用操作 <十四> 处理button dropdown 的定位
测试用例场景 模拟选择下拉菜单中数据的操作 Python脚本 测试用HTML代码: <html> <body> <form> <select name=&qu ...
- Java中resourceBundle和Properties的区别
第一种办法InputStream is = Test.class.getResourceAsStream("DbConfig.properties");Properties p = ...