成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

leetcode436. Find Right Interval

robin / 1851人閱讀

摘要:題目要求假設(shè)一個(gè)二維的整數(shù)數(shù)組中每一行表示一個(gè)區(qū)間,每一行的第一個(gè)值表示區(qū)間的左邊界,第二個(gè)值表示區(qū)間的右邊界。

題目要求
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.

For any interval i, you need to store the minimum interval j"s index, which means that the interval j has the minimum start point to build the "right" relationship for interval i. If the interval j doesn"t exist, store -1 for the interval i. Finally, you need output the stored value of each interval as an array.

Note:

You may assume the interval"s end point is always bigger than its start point.
You may assume none of these intervals have the same start point.
 

Example 1:

Input: [ [1,2] ]

Output: [-1]

Explanation: There is only one interval in the collection, so it outputs -1.
 

Example 2:

Input: [ [3,4], [2,3], [1,2] ]

Output: [-1, 0, 1]

Explanation: There is no satisfied "right" interval for [3,4].
For [2,3], the interval [3,4] has minimum-"right" start point;
For [1,2], the interval [2,3] has minimum-"right" start point.
 

Example 3:

Input: [ [1,4], [2,3], [3,4] ]

Output: [-1, 2, -1]

Explanation: There is no satisfied "right" interval for [1,4] and [3,4].
For [2,3], the interval [3,4] has minimum-"right" start point.
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.

假設(shè)一個(gè)二維的整數(shù)數(shù)組中每一行表示一個(gè)區(qū)間,每一行的第一個(gè)值表示區(qū)間的左邊界,第二個(gè)值表示區(qū)間的右邊界?,F(xiàn)在要求返回一個(gè)整數(shù)數(shù)組,用來(lái)記錄每一個(gè)邊界右側(cè)最鄰近的區(qū)間。

[ [1,4], [2,3], [3,4] ]表示三個(gè)區(qū)間,[1,4]不存在最鄰近右區(qū)間,因此[1,4]的最鄰近右區(qū)間的位置為-1。[2,3]最鄰近右區(qū)間為[3,4],因此返回2,[3,4]也不存在最臨近右區(qū)間,因此也返回-1。最終函數(shù)返回?cái)?shù)組[-1,2,-1]。

思路1:二分法

如果我們將區(qū)間按照左邊界進(jìn)行排序,則針對(duì)每一個(gè)區(qū)間的右邊界,只要找到左邊界比這個(gè)值大的最小左邊界所在的區(qū)間即可。這里不能夠直接對(duì)原來(lái)的二維數(shù)組進(jìn)行排序,因?yàn)闀?huì)丟失每一個(gè)區(qū)間的原始下標(biāo)位置。代碼中采用了內(nèi)部類Node來(lái)記錄每一個(gè)區(qū)間的左邊界以及每一個(gè)區(qū)間的原始下標(biāo),并對(duì)Node進(jìn)行排序和二分法查找。代碼如下:

    public int[] findRightInterval(int[][] intervals) {        
        int[] result = new int[intervals.length];
        Arrays.fill(result, -1);
        
        Node[] leftIndex = new Node[intervals.length];
        for(int i = 0 ; i rightIndex) {
                    right = mid - 1;
                }else {
                    left = mid + 1;
                }
            }
            if(leftIndex[right].value == rightIndex) {
                result[i] = leftIndex[right].index;
            }else if(right{
        int value;
        int index;
        @Override
        public int compareTo(Node o) {
            // TODO Auto-generated method stub
            return this.value - o.value;
        }
    }
思路二:桶排序

換一種思路,有沒(méi)有辦法將所有的區(qū)間用一維數(shù)組的方式展開,一維數(shù)組的每一個(gè)下標(biāo)上的值,都記錄了該位置所在的右側(cè)第一個(gè)區(qū)間。具體流程如下:

假設(shè)輸入為[3,4], [2,3], [1,2]的三個(gè)區(qū)間,展開來(lái)后我們知道這里的三個(gè)區(qū)間橫跨了[1,4]這么一個(gè)區(qū)間

我們可以用長(zhǎng)度為4的一維數(shù)組bucket來(lái)表示這個(gè)完整的區(qū)間,其中每一個(gè)下標(biāo)代表的位置都以左邊界作為偏移值,如數(shù)組的0下標(biāo)其實(shí)代表位置1,數(shù)組的1下標(biāo)代表位置2。

先根據(jù)所有區(qū)間的左值更新區(qū)間數(shù)組,如[3,4]代表著區(qū)間數(shù)組中位置為3-1=2的位置的第一個(gè)右側(cè)區(qū)間為[3,4], 因此bucket[2]=0, 同理bucket[1]=0, bucket[0]=1

開始查詢時(shí),如果當(dāng)前桶下標(biāo)上沒(méi)有記錄右側(cè)的第一個(gè)區(qū)間,則不停的向右遍歷,直到找到第一個(gè)值。如果沒(méi)找到,則說(shuō)明其右側(cè)不存在區(qū)間了。反過(guò)來(lái),也可以從右往左遍歷bucket數(shù)組,每遇到一個(gè)沒(méi)有填充的位置,就將右側(cè)的值賦予當(dāng)前位置。

代碼如下:

    public int[] findRightInterval(int[][] intervals) {
        int[] result = new int[intervals.length];
        int min = intervals[0][0], max = intervals[0][1];
        
        for(int i = 1 ; i=0 ; i--) {
            if(buckets[i] == -1) buckets[i] = buckets[i+1]; 
        }
        
        for(int i = 0 ; i

這里的核心思路在于,如何理解bucket數(shù)組,這個(gè)bucket數(shù)組本質(zhì)上是將所有的區(qū)間以最左邊界和最右邊界展開,數(shù)組的每一個(gè)下標(biāo)對(duì)應(yīng)著區(qū)間中的相對(duì)位置,并記錄了這個(gè)下標(biāo)右側(cè)的第一個(gè)區(qū)間的位置。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/75332.html

相關(guān)文章

  • [LeetCode] 689. Maximum Sum of 3 Non-Overlapping S

    摘要: Problem In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be of size k, and we want to maximize the sum of all 3*k entries. R...

    dailybird 評(píng)論0 收藏0
  • SVG做圓環(huán)進(jìn)度

    摘要:哈哈動(dòng)態(tài)效果是用來(lái)實(shí)現(xiàn)的,,至于進(jìn)度則是用控制屬性實(shí)現(xiàn)的。。代碼如下首先看下結(jié)構(gòu)加油開始濾鏡效果對(duì)于不熟悉的,先看下基本教程接下來(lái)看下樣式最后就腳本了,代碼不多,也很簡(jiǎn)單,就是設(shè)置屬性圓環(huán)進(jìn)度條謝謝關(guān)注 我們要實(shí)現(xiàn)的效果:showImg(https://segmentfault.com/img/bVJyQ0?w=440&h=392); 是不是有點(diǎn)感覺(jué)。。。哈哈 動(dòng)態(tài)效果是用css3來(lái)實(shí)...

    Elle 評(píng)論0 收藏0
  • [Leetcode] Meeting Rooms 會(huì)議室

    摘要:排序法復(fù)雜度時(shí)間空間思路這題和很像,我們按開始時(shí)間把這些都給排序后,就挨個(gè)檢查是否有沖突就行了。有沖突的定義是開始時(shí)間小于之前最晚的結(jié)束時(shí)間。這里之前最晚的結(jié)束時(shí)間不一定是上一個(gè)的結(jié)束時(shí)間,所以我們更新的時(shí)候要取最大值。 Meeting Rooms Given an array of meeting time intervals consisting of start and end...

    jubincn 評(píng)論0 收藏0
  • [LeetCode] 253. Meeting Rooms II

    Problem Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. Example 1: Input: [[0, 30],[5,...

    mengera88 評(píng)論0 收藏0
  • [LeetCode] 435. Non-overlapping Intervals

    Problem Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Note:You may assume the intervals end point is alway...

    mrcode 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<