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. 设置快捷键 让word轻松实现无格式粘贴

    设置快捷键 让word轻松实现无格式粘贴使用word时,我们经常会遇到需要将网页上的内容复制到word进行编辑的情况,但是通常这样复制进来的内容都是带有格式的,编辑起来非常不便.虽然我们可以利用“记事 ...

  2. hdu 5475 (线段树)

    An easy problem Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  3. 浅谈kmp

    简介: 一种由Knuth(D.E.Knuth).Morris(J.H.Morris)和Pratt(V.R.Pratt)三人设计的线性时间字符串匹配算法.这个算法不用计算变迁函数δ,匹配时间为Θ(n), ...

  4. 【题解】玲珑杯河南专场17B

    容斥大法妙~其实网上很多的题解虽然给出了容斥系数,但是并没有说明为什么是这个样子的.在这里解释一下好了. 考虑用容斥,实际上就是让 \(ans = \sum_{T\subseteq S}^{\ }f_ ...

  5. 洛谷 P3190 [HNOI2007]神奇游乐园 解题报告

    P3190 [HNOI2007]神奇游乐园 Description 给你一个 \(m * n\) 的矩阵,每个矩阵内有个权值\(V(i,j)\) (可能为负数),要求找一条回路,使得每个点最多经过一次 ...

  6. 【bzoj2594】 Wc2006—水管局长数据加强版

    http://www.lydsy.com/JudgeOnline/problem.php?id=2594 (题目链接) 题意 给出一个带边权的无向简单,要求维护两个操作,删除${u,v}$之间的连边: ...

  7. linux内核分析综合总结

    linux内核分析综合总结 zl + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 Linux内核分析 ...

  8. 20165218 《网络对抗技术》Exp1 逆向及Bof基础

    Exp1 逆向及Bof基础 基础知识 1. NOP, JNE, JE, JMP, CMP汇编指令的机器码 指令 机器码 NOP NOP指令即"空指令",在x86的CPU中机器码为0 ...

  9. 前端学习 -- Css -- 伪元素

    :first-letter 表示第一个字符 :first-line 表示文字的第一行 :before 选中元素的最前边,一般该伪类都会结合content一起使用,通过content可以向指定位置添加内 ...

  10. android 布局的两个属性 dither 和 tileMode

    tileMode(平铺)tileMode(平铺) 的效果类似于 让背景小图不是拉伸而是多个重复(类似于将一张小图设置电脑桌面时的效果) dither(抖动) Dither(图像的抖动处理,当每个颜色值 ...