1. 程式人生 > >LintCode_205 Interval Minimum Number

LintCode_205 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]

Challenge 

O(logN) time for each query

線段樹實現:
/**
 * Definition of Interval:
 * classs Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this->start = start;
 *         this->end = end;
 *     }
 */
class Solution { 
public:
    /**
     *@param A, queries: Given an integer array and an query list
     *@return: The result list
     */
    vector<int> intervalMinNumber(vector<int> &A, vector<Interval> &queries) {
        // write your code here
        vector<int> res;
        vector<int> dat = init(A);
        for (int i = 0; i < queries.size(); i++) {
            int min_val = query(dat, queries[i].start, queries[i].end + 1, 0, 0, dat.size() / 2);
            //注意區間是左閉右開;
            res.push_back(min_val);
        }
        return res;
        
    }
    vector<int> init (vector<int> &A) {
        int len = A.size();
        int n = 1;
        while ( n <= len) {
            n*=2;
        }
        vector<int> dat(2 * n, INT_MAX);
        for (int i = 0; i < A.size(); i++) {
            update(dat, i, A[i]);
        }
        // for (int i = 0; i < dat.size(); i++) {
        //     cout<<dat[i]<<" ";
        // }
        return dat;
    }
    void update(vector<int> &dat, int k, int value) {
        int n = dat.size() / 2;
        k += n - 1; //注意要去掉第一個;
        dat[k] = value;
        while (k > 0) {
            k = (k - 1) / 2;
            dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }
    int query (vector<int> &dat, int a, int b, int k, int l, int r) {
        // l 和 r 代表節點k所代表的區間
        //注意區間是左閉右開;
        if (r <= a || b <= l) {
            return INT_MAX;
        }
        if (a <= l && r <= b) {
            return dat[k];
        }else {
            int v1 = query(dat, a, b, k * 2 + 1, l, (l + r) / 2);
            int v2 = query(dat, a, b, k * 2 + 2, (l + r) / 2, r);
            return min(v1, v2);
        }
    }
};


相關推薦

LintCode_205 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

Lintcode205 Interval Minimum Number solution 題解

簡書 calculate ray 描述 ger build calc dex nsh 【題目描述】 Given an integer array (index from 0 to n-1, where n is the size of this array),

codeforce 804B Minimum number of steps

bst 代碼 img 添加 close perf rep spl pri cf勁啊 原題: We have a string of letters ‘a‘ and ‘b‘. We want to perform some operations on it. On each

CF804B Minimum number of steps

ron urn names har ber int cin con href 思路: 找規律。參考了http://blog.csdn.net/harlow_cheng/article/details/71190999。 實現: 1 #include <iostre

[Coding Made Simple] Coin Changes Minimum Number of Coins

sim == pri chan set ble length nco one Given coins of certain denominations and a total, how many minimum coins would you need to make th

CodeForces - 805D Minimum number of steps

mini png turn 增加 include blog 裏的 nbsp style 題目大意:把一個串裏的ab改為bba,求最少改幾次就沒有ab了 具體思路:可以把一次操作看成把a放到b右邊,再增加一個b,要求把所有的a都移到b右邊 發現1個a過一個b要1次操作 2

452. Minimum Number of Arrows to Burst Balloons紮氣球的個數最少

-c 分析 limit 處理 排序 什麽 exactly 循環 ase [抄題]: There are a number of spherical balloons spread in two-dimensional space. For each balloon, pro

Minimum number of swaps required to sort an array

-h continue str num dice fill cond main lse https://www.hackerrank.com/challenges/minimum-swaps-2/problem Minimum Swaps II

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 input is the start and end coordinates of the

LeetCode 452. Minimum Number of Arrows to Burst Balloons & 435. Non-overlapping Intervals

題解 兩題一起寫,都是pair型陣列,都考慮交疊問題,都是貪心。 這類題的核心都在於預先sort一下,以second升序排(若相等再以first升序)。 這樣的好處是,我們再順序遍歷的時候可以很方便地發現交疊情況: 比如用一個 p 變數記載之前的second,新位置比較一下first

Greedy--Minimum Number of Arrows to Burst Balloons (Medium)

原題: Problem Description There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided inp

[leetcode]452. Minimum Number of Arrows to Burst Balloons

[leetcode]452. Minimum Number of Arrows to Burst Balloons Analysis 好冷鴨—— [每天刷題並不難0.0] There are a number of spherical balloons spr

Google - Find minimum number of coins that make a given value

Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins, what is the minimum

leetcode 452 Minimum Number of Arrows to Burst Balloons(射擊氣球最小射擊手數目)貪心

題目 思路: 活動選擇問題,會議室選擇問題開展最多會議數目,其實一一個型別 將開始時間(座標)和結束時間排序(任選一個就行,我看c++做法裡面有按照開始時間排序,本體中是結束時間排序,感覺按照結束時間排序更合理) 將所有的氣球按照終止位置排序,開始從前向後掃描。以第一個氣球

python leetcode 452. Minimum Number of Arrows to Burst Balloons

能重疊的區域能一起射爆 所以先排序 再取重合的區域 看一共有幾個這樣的區域 class Solution: def findMinArrowShots(self, points): """ :type points: List[List[int]]

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 input is the start and end coo

LeetCode 452. Minimum Number of Arrows to Burst Balloons & 435. Non-overlapping Intervals

題解 兩題一起寫,都是pair型陣列,都考慮交疊問題,都是貪心。 這類題的核心都在於預先sort一下,以second升序排(若相等再以first升序)。 這樣的好處是,我們再順序遍歷的時候可以很方便地發

LC 871. Minimum Number of Refueling Stops

posit 是什麽 least ini 什麽 sid cannot for when A car travels from a starting position to a destination which is target miles east of the star

Reorder array to construct the minimum number 解題報告

Reorder array to construct the minimum number Description Construct minimum number by reordering a given non-negative integer array. Arrange

[LeetCode] Minimum Number of Arrows to Burst Balloons 最少數量的箭引爆氣球

There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizo