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: 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 & 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: Segment Tree Modify

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

  5. [LintCode] Segment Tree Build II 建立线段树之二

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

  6. [LintCode] Segment Tree Build 建立线段树

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

  7. Lintcode: Segment Tree Build

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

  8. 247. Segment Tree Query II

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

  9. 202. Segment Tree Query

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

随机推荐

  1. laravel elixir

    npm install npm install jquery --save //bootsrap3.36 与 jquery3不兼容,下载jquery时可能需要确定版本号 npm install jqu ...

  2. C#数组的排序

    对于数组的排序有好多种方法,上面这种是最常规的方法,当然在Array类中有两个方法就是专门来完成排序的,一会我们再来看这两方法,下面我们还是来看一下语法吧,只要搞懂语法了,就可以自己随便排序了. 冒泡 ...

  3. Tomcat7 安装StartSSL证书笔记

    1.Tomcat-Native安装 使用StartSSL,Tomcat必须用apr方式启动(apr方式对于静态的内容,比默认的bio效率要高很多倍) Windows下tomcat-native安装 直 ...

  4. linux指定目录安装软件后,程序找不到共享库问题

    以svn为例,64位centos yum install subversion --installroot=/usr/svn/后 执行svn命令,报错svn: error while loading ...

  5. USBDongle及Btool使用说明

    BLE 模块可使用开发套件中的 USB Dongle 模拟手机APP配合Btool.exe 进行蓝牙通讯测试. >连接 BLE 模块 USB Dongle 和模块的连接是通讯的基础,扫描连接的操 ...

  6. 通过runtime替换系统类实现的代码(从github开源库fdstackview中摘录)

    其中部分代码为汇编:由此可见oc的runtime的灵活性和能力.此代码仅供参考 // ---------------------------------------------------- // R ...

  7. int a=5,则 ++(a++)的值是?

    编译出错:++(a++)先计算的是括号里的(a++),返回的结果是一个表达式,其值是5,不能对表达式进行赋值

  8. php---初学者git使用

    1.git自学网站 廖雪峰的官方网站 2.安装一个简单的git 创建用户名.邮箱 git config --global user.name "your name" git con ...

  9. 远程通知APNs(Apple Push Notification Server)

    推送通知是由应用服务提供商发起的,通过苹果的APNs(Apple Push Notification Server)发送到应用客户端.下面是苹果官方关于推送通知的过程示意图: 推送通知的过程可以分为以 ...

  10. ActiveMQ发布订阅模式

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...