Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. Each query has two integers [start, end]. For each query, calculate the minimum number between index start and end in the given array, return the result list.

Example

For array [1,2,7,8,5], and queries [(1,2),(0,4),(2,4)], return [2,1,5]

Analysis:

Use Segment tree to maintain the minimun value from index i to j.

 /**
* Definition of Interval:
* public classs Interval {
* int start, end;
* Interval(int start, int end) {
* this.start = start;
* this.end = end;
* }
*/
public class Solution {
/**
*@param A, queries: Given an integer array and an query list
*@return: The result list
*/
SegmentTreeNode root; public ArrayList<Integer> intervalMinNumber(int[] A,
ArrayList<Interval> queries) {
// write your code here
ArrayList<Integer> res = new ArrayList<Integer>();
root = buildTree(A, , A.length-);
query(res, queries);
return res;
} public void query(ArrayList<Integer> res, ArrayList<Interval> queries) {
for (Interval interval : queries) {
int result = queryTree(root, interval.start, interval.end);
res.add(result);
}
} public int queryTree(SegmentTreeNode cur, int start, int end) {
if (start==cur.start && end==cur.end) {
return cur.min;
}
int mid = (cur.start + cur.end)/;
if (end <= mid) return queryTree(cur.left, start, end);
else if (start > mid) return queryTree(cur.right, start, end);
else return Math.min(queryTree(cur.left, start, mid), queryTree(cur.right, mid+, end));
} public SegmentTreeNode buildTree(int[] A, int start, int end) {
SegmentTreeNode cur = new SegmentTreeNode(start, end);
if (start == end) {
cur.min = A[start];
}
else {
int mid = (start+end)/;
cur.left = buildTree(A, start, mid);
cur.right = buildTree(A, mid+, end);
cur.min = Math.min(cur.left.min, cur.right.min);
}
return cur;
}
} class SegmentTreeNode {
int min;
int start;
int end;
SegmentTreeNode left;
SegmentTreeNode right;
public SegmentTreeNode(int start, int end) {
this.start = start;
this.end = end;
this.min = Integer.MAX_VALUE;
this.left = null;
this.right = null;
}
}

Interval Minimum Number的更多相关文章

  1. Lintcode: Interval Minimum Number

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  2. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  3. [LeetCode] 452 Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  4. Reorder array to construct the minimum number

    Construct minimum number by reordering a given non-negative integer array. Arrange them such that th ...

  5. Leetcode: Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  6. 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  7. Codeforces 279D The Minimum Number of Variables 状压dp

    The Minimum Number of Variables 我们定义dp[ i ][ mask ]表示是否存在 处理完前 i 个a, b中存者 a存在的状态是mask 的情况. 然后用sosdp处 ...

  8. [Swift]LeetCode452. 用最少数量的箭引爆气球 | Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. [Swift]LeetCode995. K 连续位的最小翻转次数 | Minimum Number of K Consecutive Bit Flips

    In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray o ...

随机推荐

  1. 【大数据】Kafka学习笔记

    第1章 Kafka概述 1.1 消息队列 (1)点对点模式(一对一,消费者主动拉取数据,消息收到后消息清除) 点对点模型通常是一个基于拉取或者轮询的消息传送模型,这种模型从队列中请求信息,而不是将消息 ...

  2. Backdooring a OS VM

    Backdooring a OS VM 来源   https://www.cnblogs.com/studyskill/p/6524672.html 提示: 1.经过实验,fortios 5.4 be ...

  3. 主流图片加载框架 ImageLoader、Glide、Picasso、Fresco 对比

    图片缓存库主页: Glidehttps://github.com/bumptech/glide fresco - An Android library for managing images and ...

  4. 【整体二分】【P3834】 【模板】可持久化线段树 1(主席树)

    Description 给定一个长度为 \(n\) 的序列, \(m\) 次操作静态查询区间第 \(k\) 大 Input 第一行是 \(n,m\) 下一行描述这个序列 下面 \(m\) 行描述操作 ...

  5. Java入门:JDK与Eclipse之类的集成开发工具的关系

    JDK是Java Development Kit,也就是说Java开发所需的工具包.有了这个东西,一切Java开发理论上都不是问题了.当然,根据你下载的版本不同,可能擅长的领域不同.通常大家都是用JD ...

  6. error: failed to connect to the hypervisor error: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory 解决办法

    服务器版本:CentOS Linux release 7.4 Linux lb 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x8 ...

  7. Python高手之路【十一】python基础之面向对象

    创建类和对象 面向对象编程是一种编程方式,此编程方式的落地需要使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就是对 “类” 和 “对象” 的使用. 类就是一个模板,模板里可以包含多个函数, ...

  8. 使用uwsgi配置django

    1.uwsgi的安装 pip install uwsgi 2.uwsgi的基本测试: 创建一个test.py文件,内容如下: def application(env, start_response): ...

  9. 「Vue」过滤器

    #全局过滤器要写在var vue之前<td>{{item.time | ctime }}</td>Vue.filter('ctime'(过滤器名),function(data( ...

  10. golang 中的 time 包的 Ticker

    真实的应用场景是:在测试收包的顺序的时候,加了个 tick 就发现丢包了 那么来看一个应用例子: package main import ( "fmt" "runtime ...