码迷,mamicode.com
首页 > 其他好文 > 详细

Lintcode: Interval Minimum Number

时间:2016-02-01 15:35:22      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

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.

Have you met this question in a real interview? Yes
Example
For array [1,2,7,8,5], and queries [(1,2),(0,4),(2,4)], return [2,1,5]

Note
We suggest you finish problem Segment Tree Build, Segment Tree Query and Segment Tree Modify first.

Challenge
O(logN) time for each query

SegmentTree time complexity: build O(N), query O(logN), update O(logN),      space cost O(NlogN)

 1 /**
 2  * Definition of Interval:
 3  * public classs Interval {
 4  *     int start, end;
 5  *     Interval(int start, int end) {
 6  *         this.start = start;
 7  *         this.end = end;
 8  *     }
 9  */
10 public class Solution {
11     /**
12      *@param A, queries: Given an integer array and an query list
13      *@return: The result list
14      */
15     public class SegmentTreeNode {
16         int min;
17         int start;
18         int end;
19         SegmentTreeNode left;
20         SegmentTreeNode right;
21         public SegmentTreeNode(int start, int end) {
22             this.start = start;
23             this.end = end;
24             this.min = Integer.MAX_VALUE;
25             this.left = null;
26             this.right = null;
27         }
28     } 
29     
30     SegmentTreeNode root;
31      
32     public ArrayList<Integer> intervalMinNumber(int[] A, 
33                                                 ArrayList<Interval> queries) {
34         // write your code here
35         ArrayList<Integer> res = new ArrayList<Integer>();
36         root = buildTree(A, 0, A.length-1);
37         query(res, queries);
38         return res;
39     }
40     
41     public SegmentTreeNode buildTree(int[] A, int start, int end) {
42         SegmentTreeNode cur = new SegmentTreeNode(start, end);
43         if (start == end) {
44             cur.min = A[start];
45         }
46         else {
47             int mid = (start+end)/2;
48             cur.left = buildTree(A, start, mid);
49             cur.right = buildTree(A, mid+1, end);
50             cur.min = Math.min(cur.left.min, cur.right.min);
51         }
52         return cur;
53     }
54     
55     public void query(ArrayList<Integer> res, ArrayList<Interval> queries) {
56         for (Interval interval : queries) {
57             int result = queryTree(root, interval.start, interval.end);
58             res.add(result);
59         }
60     }
61     
62     public int queryTree(SegmentTreeNode cur, int start, int end) {
63         if (start==cur.start && end==cur.end) {
64             return cur.min;
65         }
66         int mid = (cur.start + cur.end)/2;
67         if (end <= mid) return queryTree(cur.left, start, end);
68         else if (start > mid) return queryTree(cur.right, start, end);
69         else return Math.min(queryTree(cur.left, start, mid), queryTree(cur.right, mid+1, end));
70     }
71 }

 

Lintcode: Interval Minimum Number

标签:

原文地址:http://www.cnblogs.com/EdwardLiu/p/5175033.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!