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. 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.
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的更多相关文章
- 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. ...
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [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 ...
- Reorder array to construct the minimum number
Construct minimum number by reordering a given non-negative integer array. Arrange them such that th ...
- Leetcode: Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- Codeforces 279D The Minimum Number of Variables 状压dp
The Minimum Number of Variables 我们定义dp[ i ][ mask ]表示是否存在 处理完前 i 个a, b中存者 a存在的状态是mask 的情况. 然后用sosdp处 ...
- [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 ...
- [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 ...
随机推荐
- Android 5.0 Activity切换动画
在Androiod5.0中,Google定义了Material Design的规范.而动画切换,能给用户直观的连贯性的体验,也是Google推崇的. 为此,在Android5.0中,Android新支 ...
- 【BZOJ 3326】[Scoi2013]数数 数位dp+矩阵乘法优化
挺好的数位dp……先说一下我个人的做法:经过观察,发现这题按照以往的思路从后往前递增,不怎么好推,然后我就大胆猜想,从前往后推,发现很好推啊,维护四个变量,从开始位置到现在有了i个数 f[i]:所有数 ...
- 【刷题】BZOJ 1565 [NOI2009]植物大战僵尸
Description Plants vs. Zombies(PVZ)是最近十分风靡的一款小游戏.Plants(植物)和Zombies(僵尸)是游戏的主角,其中Plants防守,而Zombies进攻. ...
- 【codeforces 528D】 Fuzzy Search
http://codeforces.com/problemset/problem/528/D (题目链接) 题意 给定母串和模式串,字符集大小为${4}$,给定${k}$,模式串在某个位置匹配当且仅当 ...
- debian安装filebeat5.5收集nginx日志
先贴一下我的BELK架构 1.Download and install the Public Signing Key: # wget -qO - https://artifacts.elastic.c ...
- DataTables实现rowspan思路
直接看例子吧 <table id="example" class="display table table-bordered" cellspacing=& ...
- C++ 文本查询2.0(逻辑查询)
代码实例实现了继承和友元之间的关系,以及为了隐藏实际继承实现,而实现的接口类,代码偏乱,楼主- -自看的(提醒作用) TextQuery.h // // Created by 徐爱东 on 17/7/ ...
- SQL复杂语句查询练习
--复杂查询练习 -- 1 .列出所有员工的年工资,按年薪从低到高排序. SELECT (SAL+NVL(COMM,0))*12 INCOME FROM EMP ORDER BY INCOME; -- ...
- Docker图形界面管理之DockerUI
DockerUI DockerrUI是一个基于Docker API提供图形化页面简单的容器管理系统,支持容器管理.镜像管理. 1.1 下载镜像 docker pull abh1nav/dockerui ...
- python基础4--文件操作
文件操作 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 1.open()打开文件 #获取文件句柄 f = open("yesterday" ...