zoukankan      html  css  js  c++  java
  • 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. 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 }
  • 相关阅读:
    机器视觉资料整理
    《用TCP/IP进行网络互连》读书笔记
    Win Form不能响应键盘事件
    C语言 字符串前加L的意义 如:L“A”
    UniCode 下 CString 转 char* 的方法(转)
    BATCH
    HALCON不支持的设备中,获取图像
    关于FragmentManager动态管理Fragment时Fragment生命周期的探究
    关于如何惟一地标识一台Android设备的综合性讨论
    如何使ActionBar不那么单调
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/5175033.html
Copyright © 2011-2022 走看看