Lintcode: Segment Tree Query

时间:2022-09-03 08:43:53
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding SegmentTree, each node stores an extra attribute max to denote the maximum number in the interval of the array (index from start to end).

Design a query method with three parameters root, start and end, find the maximum number in the interval [start, end] by the given root of segment tree.

Have you met this question in a real interview? Yes
Example
For array [1, 4, 2, 3], the corresponding Segment Tree is: [0, 3, max=4]
/ \
[0,1,max=4] [2,3,max=3]
/ \ / \
[0,0,max=1] [1,1,max=4] [2,2,max=2], [3,3,max=3]
query(root, 1, 1), return 4 query(root, 1, 2), return 4 query(root, 2, 3), return 3 query(root, 0, 2), return 4

这道题的启示是:Segment Tree 不光是可以找线段和,还可以找线段max, min等各种各样的指标

 /**
* 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 root, start, end: The root of segment tree and
* an segment / interval
*@return: The maximum number in the interval [start, end]
*/
public int query(SegmentTreeNode root, int start, int end) {
// write your code here
if (root.start==start && root.end==end) return root.max;
int mid = (root.start+root.end)/2;
if (end <= mid) return query(root.left, start, end);
else if (start > mid) return query(root.right, start, end);
else return Math.max(query(root.left, start, mid), query(root.right, mid+1, end));
}
}

Lintcode: Segment Tree Query的更多相关文章

  1. Lintcode&colon; Segment Tree Query II

    For an array, we can build a SegmentTree for it, each node stores an extra attribute count to denote ...

  2. 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 ...

  3. Segment Tree Query I &amp&semi; II

    Segment Tree Query I For an integer array (index from 0 to n-1, where n is the size of this array), ...

  4. Lintcode&colon; Segment Tree Modify

    For a Maximum Segment Tree, which each node has an extra value max to store the maximum value in thi ...

  5. &lbrack;LintCode&rsqb; Segment Tree Build II 建立线段树之二

    The structure of Segment Tree is a binary tree which each node has two attributes startand end denot ...

  6. &lbrack;LintCode&rsqb; Segment Tree Build 建立线段树

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  7. Lintcode&colon; Segment Tree Build

    The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...

  8. 247&period; Segment Tree Query II

    最后更新 二刷 09-Jna-2017 利用线段树进行区间查找,重点还是如何判断每一层的覆盖区间,和覆盖去见与当前NODE值域的关系. public class Solution { public i ...

  9. 202&period; Segment Tree Query

    最后更新 二刷 09-Jan-17 正儿八经线段树的应用了. 查找区间内的值. 对于某一个Node,有这样的可能: 1)需要查找区间和当前NODE没有覆盖部分,那么直接回去就行了. 2)有覆盖的部分, ...

随机推荐

  1. 支付宝PC即时到账和手机网站支付同步

    前几个月做了一个旅游网站,有PC站和手机站,涉及支付宝支付功能. 要求:PC站下的单,用户用手机登录也能支付;同理,手机站下的单,PC端登录也能支付. 附支付宝开放平台网址:即时到账 ,手机网站支付. ...

  2. qbxt十一系列四

    关于考试:题目很难,T1和T3都失误,爆零orz 更正:第三组:不存在相同的字符|str|=26,26<=n<=100 [题目分析] 第一反应,组合数学:第二反应,有端倪:jn给了一道题G ...

  3. &lbrack;terry笔记&rsqb;ora-00904 invalid identifier—同义词

    今天遇到一个问题,说起来也简单,但是困扰我半天. 升级数据库后,一个功能无法运行,在后台观察到其sql,发现sql中包含一个包执行不了,报错ora-00904 invalid identifier w ...

  4. 基于ant的jmeter自动化性能测试

    准备工作: 1.java的运行环境正常,及运行java -version.javac -version能正常输出java版本: 2.ant的运行环境正常,使用ant需要配置环境变量,编辑/etc/pr ...

  5. flex 4 transition

    <s:transitions> <s:Transition fromState="default"> <s:Parallel> <mx:R ...

  6. SQL学习:查询的用法(1)

    在SQL servre的使用中,查询的用法是最多的.最重要的,也是最难学习的,因此掌握查询的用法很重要. 先将表的示例上图 员工表: 部门表:                             ...

  7. Win7 64位安装MySQL

    1.Win7 64位 安装MySQL5.5版本 安装文件的执行:会提示“已经停止工作”: 2.我下载了mysql-installer-community-5.7.11.0.msi,可以安装成功,中途需 ...

  8. MYSQL 体系结构图-LRU

  9. verilog HDL-参数型数据对像 与&OpenCurlyQuote;define

    参数新数据对象是用来定义常量的,它可以提升verilog hdl代码的可读性和维护性. verilog hdl支持参数有两种,普通参数和局部参数.普通参数在模块例化时可以从新赋值,局部参数在模块例化时 ...

  10. Linux用过的命令集合

    1,查看是否安装过openssl:(openssl version -a)(rpm -qa|grep -i openssl) 2,安装gcc:(yum install gcc-c++) 3,查看主机名 ...