zoukankan      html  css  js  c++  java
  • 436. Find Right Interval

    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:

    1. You may assume the interval's end point is always bigger than its start point.
    2. You may assume none of these intervals have the same srt 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.

    class Solution {
        public int[] findRightInterval(int[][] intervals) {
            int n = intervals.length;
            int[] res = new int[n];
            Arrays.fill(res, -1);
            
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < n; j++) {
                    if(intervals[i][1] <= intervals[j][0]) {
                        if(res[i] == -1) {
                           res[i] = j; 
                        }
                        else if(intervals[j][0] < intervals[res[i]][0]) res[i] = j;
                    }
                }
            }
            return res;
        }
    }

    bruteforce,太brute了

    class Solution {
        public int[] findRightInterval(int[][] intervals) {
            int n = intervals.length;
            int[] res = new int[n];
            TreeMap<Integer, Integer> map = new TreeMap();
            for(int i = 0; i < n; i++) map.put(intervals[i][0], i);
            
            for(int i = 0; i < n; i++) {
                Integer ind = map.ceilingKey(intervals[i][1]);
                res[i] = ind == null ? -1 : map.get(ind);
            }
            return res;
        }
    }

    题目说了所有start point都不一样,而且我们要找的是每个的最小start point,利用这个性质可以搞一个treemap,key是start point,value是index

    存完之后就遍历,找有没有对应这个interval end point的ceilingKey(大于等于),有就get到对应的index存入res中。

    好赖啊

  • 相关阅读:
    Python使用SMTP模块、email模块发送邮件
    harbor搭建及使用
    ELK搭建-windows
    ELK技术栈之-Logstash详解
    【leetcode】1078. Occurrences After Bigram
    【leetcode】1073. Adding Two Negabinary Numbers
    【leetcode】1071. Greatest Common Divisor of Strings
    【leetcode】449. Serialize and Deserialize BST
    【leetcode】1039. Minimum Score Triangulation of Polygon
    【leetcode】486. Predict the Winner
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13575113.html
Copyright © 2011-2022 走看看