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 ...
随机推荐
- nowcoder 202F-平衡二叉树
题目链接 题目描述 平衡二叉树,顾名思义就是一棵“平衡”的二叉树.在这道题中,“平衡”的定义为,对于树中任意一个节点,都满足左右子树的高度差不超过 d. 空树的高度定义为0,单个节点的高度为1,其他情 ...
- JVM学习笔记(二):垃圾收集
程序计数器. 虚拟机栈. 本地方法栈3个区域随线程而生,随线程而灭:栈中的栈帧随着方法的进入和退出而有条不紊地执行着出栈和入栈操作. 每一个栈帧中分配多少内存基本上是在类结构确定下来时就已知的,因此这 ...
- 再谈MySql索引
一.索引简介 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 索引分单列索引(主键索引.唯一索引.普通索引)和组合索引.单列索引,即一个索引只包含单个列 ...
- Make a Crystal UVA - 11014 (容斥定理)
题意:给定一个NxNxN的正方体,求出最多能选几个整数点,使得任意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O),那么所 ...
- c++11 可变参数模板类
c++11 可变参数模板类 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #inc ...
- tokenizer
http://blog.csdn.net/beyond__devil/article/details/52829241
- 关于未来IT职业教育的思考
回首过去20年的IT教育,从基本的办公软件(应用软件)到基础设施培训(网络.ps.3d等)再到软件开发(java等),可以说是见证了中国计算机发展的整个阶段,随着时代的变迁,计算机从最初的普及阶段到深 ...
- MVC4.0中cshtml中怎么解析html编码
http://bbs.csdn.net/topics/391060108?page=1 问题描述: 数据库中存储带有格式的文本,如 <span style="color:#333333 ...
- git<Commit和Push的区别>
git作为支持分布式版本管理的工具,它管理的库(repository)分为本地库.远程库. git commit操作的是本地库,git push操作的是远程库. git commit是将本地修改过的文 ...
- java程序文件读取与保存实例代码
class RadioHere extends JFrame implements ActionListener { private JTextArea ta=new JTextArea(10,20) ...