715.rangemodule(代码片段)

ruruozhenhao ruruozhenhao     2023-01-28     139

关键词:

A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the following interfaces in an efficient manner.

 

  • addRange(int left, int right) Adds the half-open interval [left, right), tracking every real number in that interval. Adding an interval that partially overlaps with currently tracked numbers should add any numbers in the interval [left, right) that are not already tracked.

 

 

  • queryRange(int left, int right) Returns true if and only if every real number in the interval [left, right) is currently being tracked.

 

 

  • removeRange(int left, int right) Stops tracking every real number currently being tracked in the interval [left, right).

 

Example 1:

addRange(10, 20): null
removeRange(14, 16): null
queryRange(10, 14): true (Every number in [10, 14) is being tracked)
queryRange(13, 15): false (Numbers like 14, 14.03, 14.17 in [13, 15) are not being tracked)
queryRange(16, 17): true (The number 16 in [16, 17) is still being tracked, despite the remove operation)

 

Note:

  • A half open interval [left, right) denotes all real numbers left <= x < right.
  • 0 < left < right < 10^9 in all calls to addRange, queryRange, removeRange.
  • The total number of calls to addRange in a single test case is at most 1000.
  • The total number of calls to queryRange in a single test case is at most 5000.
  • The total number of calls to removeRange in a single test case is at most 1000.

 

 

Approach #1: C++. [Using Vector]

class RangeModule 
public:
    RangeModule() 
        
    
    
    void addRange(int left, int right) 
        vector<pair<int, int>> new_ranges;
        bool inserted = false;
        
        for (const auto& it : ranges_) 
            if (it.first > right && !inserted) 
                new_ranges.emplace_back(left, right);
                inserted = true;
            
            if (it.first > right || it.second < left) 
                new_ranges.push_back(it);
             else 
                left = min(left, it.first);
                right = max(right, it.second);
            
        
        if (!inserted) new_ranges.emplace_back(left, right);
        ranges_.swap(new_ranges);
    
    
    bool queryRange(int left, int right) 
        int l = 0;
        int r = ranges_.size() - 1;
        while (l <= r) 
            int mid = (l + r) / 2;
            if (ranges_[mid].first <= left && ranges_[mid].second >= right) 
                return true;
            else if (ranges_[mid].first > right) 
                r = mid - 1;
             else 
                l = mid + 1;
            
        
        return false;
    
    
    void removeRange(int left, int right) 
        vector<pair<int, int>> new_ranges;
        for (const auto& it : ranges_) 
            if (it.second <= left || it.first >= right) 
                new_ranges.emplace_back(it);
             else 
                if (it.first < left) 
                    new_ranges.emplace_back(it.first, left);
                if (it.second > right)
                    new_ranges.emplace_back(right, it.second);
            
        
        ranges_.swap(new_ranges);
    
    
private:
    vector<pair<int, int>> ranges_;
;

/**
 * Your RangeModule object will be instantiated and called as such:
 * RangeModule obj = new RangeModule();
 * obj.addRange(left,right);
 * bool param_2 = obj.queryRange(left,right);
 * obj.removeRange(left,right);
 */

  

there are some notes about STL.

1. the difference between emplace_back and push_back.

2. emplace vs insert.

3. the method of swap in vector.

 

Approach #2: 

 

csharp代码片段(代码片段)

查看详情

javascript代码片段(代码片段)

查看详情

textvisualbasic代码片段(代码片段)

查看详情

sqloracle代码片段(代码片段)

查看详情

swift代码片段(代码片段)

查看详情

java代码片段【安卓】(代码片段)

查看详情

shbash的代码片段(代码片段)

查看详情

markdownphpexcelnotes和代码片段(代码片段)

查看详情

javaandroid的代码片段(代码片段)

查看详情

javascriptjs-常用代码片段(代码片段)

查看详情

常用代码片段(代码片段)

单例模式privatestaticHttpUtilinstance;publicstaticsynchronizedHttpUtilgetInstance()if(instance==null)instance=newHttpUtil();returninstance; 查看详情

常用代码片段(代码片段)

单例模式privatestaticHttpUtilinstance;publicstaticsynchronizedHttpUtilgetInstance()if(instance==null)instance=newHttpUtil();returninstance; 查看详情

text代码片段很有用(代码片段)

查看详情

vbscript我的代码片段(代码片段)

查看详情

java代码片段【java】(代码片段)

查看详情

rr有用的代码片段(代码片段)

查看详情

常见的代码片段(代码片段)

$(id).select2(placeholder:"--请选择--",allowClear:true,data:list);  查看详情

text代码片段【snl】(代码片段)

查看详情