*[Lintcode]Interval Minimum Number 区间最小数
2016-10-11 14:07
337 查看
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
Example
For array
and queries
用线段树直接求解即可。
/**
* 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
*/
public ArrayList<Integer> intervalMinNumber(int[] A,
ArrayList<Interval> queries) {
SegmentTreeNode s = build(A, 0, A.length - 1);
ArrayList<Integer> res = new ArrayList<Integer>();
for(int i = 0; i < queries.size(); i++) {
Interval interval = queries.get(i);
res.add(find(s, interval));
}
return res;
}
private int find(SegmentTreeNode s, Interval i) {
if(s == null) return Integer.MAX_VALUE;
if(i.start > s.end || i.end < s.start) return Integer.MAX_VALUE;
if(i.start <= s.start && i.end >= s.end) return s.min;
else {
int left = find(s.left, i);
int right = find(s.right, i);
return Math.min(left, right);
}
}
private SegmentTreeNode build(int[] A, int start, int end) {
if(start > end) return null;
SegmentTreeNode root = new SegmentTreeNode(start, end);
if(start == end) {
root.min = A[start];
return root;
}
root.left = build(A, start, (start + end) / 2);
root.right = build(A, (start + end) / 2 + 1, end);
root.min = Math.min(root.left.min, root.right.min);
return root;
}
class SegmentTreeNode {
public int start, end, min;
public SegmentTreeNode left, right;
public SegmentTreeNode(int start, int end) {
this.start = start;
this.end = end;
this.left = this.right = null;
}
}
}
[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]
用线段树直接求解即可。
/**
* 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
*/
public ArrayList<Integer> intervalMinNumber(int[] A,
ArrayList<Interval> queries) {
SegmentTreeNode s = build(A, 0, A.length - 1);
ArrayList<Integer> res = new ArrayList<Integer>();
for(int i = 0; i < queries.size(); i++) {
Interval interval = queries.get(i);
res.add(find(s, interval));
}
return res;
}
private int find(SegmentTreeNode s, Interval i) {
if(s == null) return Integer.MAX_VALUE;
if(i.start > s.end || i.end < s.start) return Integer.MAX_VALUE;
if(i.start <= s.start && i.end >= s.end) return s.min;
else {
int left = find(s.left, i);
int right = find(s.right, i);
return Math.min(left, right);
}
}
private SegmentTreeNode build(int[] A, int start, int end) {
if(start > end) return null;
SegmentTreeNode root = new SegmentTreeNode(start, end);
if(start == end) {
root.min = A[start];
return root;
}
root.left = build(A, start, (start + end) / 2);
root.right = build(A, (start + end) / 2 + 1, end);
root.min = Math.min(root.left.min, root.right.min);
return root;
}
class SegmentTreeNode {
public int start, end, min;
public SegmentTreeNode left, right;
public SegmentTreeNode(int start, int end) {
this.start = start;
this.end = end;
this.left = this.right = null;
}
}
}
相关文章推荐
- *[Lintcode]Interval Sum区间最小数
- lintcode-区间最小数-205
- Lintcode---区间最小数
- LintCode_205 Interval Minimum Number
- 区间最小数-LintCode
- LintCode_205 Interval Minimum Number
- Lintcode: Interval Minimum Number
- LintCode:区间最小数
- python_lintcode_177. 把排序数组转换为高度最小的二叉搜索树_156. 合并区间
- lintcode(205)区间最小数
- lintcode-205-区间最小数
- [Lintcode]Interval Sum II区间求和 II
- LintCode 把排序数组转换为高度最小的二叉搜索树
- 笔试算法题(42):线段树(区间树,Interval Tree)
- 【Codeforces Round 336 (Div 2) D】【区间DP 讨论 好题】Zuma 区间取回文串最小操作次数使得取光全串
- lintcode-160-寻找旋转排序数组中的最小值 II
- 区间DP-合并石子(求最小/最代价 )
- STL有限队列的灵活运用+IO优化+区间最大最小——POJ Sliding Window
- LintCode 11-二叉查找树中搜索区间
- CSU-1110 RMQ with Shifts (单点更新+区间最小值 zkw线段树)