{"id":8549,"date":"2021-08-09T19:26:48","date_gmt":"2021-08-10T02:26:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8549"},"modified":"2021-08-09T19:26:58","modified_gmt":"2021-08-10T02:26:58","slug":"leetcode-1893-check-if-all-the-integers-in-a-range-are-covered","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1893-check-if-all-the-integers-in-a-range-are-covered\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1893. Check if All the Integers in a Range Are Covered"},"content":{"rendered":"\n<p>You are given a 2D integer array&nbsp;<code>ranges<\/code>&nbsp;and two integers&nbsp;<code>left<\/code>&nbsp;and&nbsp;<code>right<\/code>. Each&nbsp;<code>ranges[i] = [start<sub>i<\/sub>, end<sub>i<\/sub>]<\/code>&nbsp;represents an&nbsp;<strong>inclusive<\/strong>&nbsp;interval between&nbsp;<code>start<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>end<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;<em>if each integer in the inclusive range<\/em>&nbsp;<code>[left, right]<\/code>&nbsp;<em>is covered by&nbsp;<strong>at least one<\/strong>&nbsp;interval in<\/em>&nbsp;<code>ranges<\/code>. Return&nbsp;<code>false<\/code>&nbsp;<em>otherwise<\/em>.<\/p>\n\n\n\n<p>An integer&nbsp;<code>x<\/code>&nbsp;is covered by an interval&nbsp;<code>ranges[i] = [start<sub>i<\/sub>, end<sub>i<\/sub>]<\/code>&nbsp;if&nbsp;<code>start<sub>i<\/sub>&nbsp;&lt;= x &lt;= end<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Every integer between 2 and 5 is covered:\n- 2 is covered by the first range.\n- 3 and 4 are covered by the second range.\n- 5 is covered by the third range.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> ranges = [[1,10],[10,20]], left = 21, right = 21\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> 21 is not covered by any range.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= ranges.length &lt;= 50<\/code><\/li><li><code>1 &lt;= start<sub>i<\/sub>&nbsp;&lt;= end<sub>i<\/sub>&nbsp;&lt;= 50<\/code><\/li><li><code>1 &lt;= left &lt;= right &lt;= 50<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Hashtable<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n * (right &#8211; left))<br>Space complexity: O(right &#8211; left)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:  \n  bool isCovered(vector<vector<int>>& ranges, int left, int right) {\n    unordered_set<int> s;\n    for (const auto& range : ranges)\n      for (int i = max(left, range[0]); i <= min(right, range[1]); ++i)\n        s.insert(i);    \n    return s.size() == right - left + 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a 2D integer array&nbsp;ranges&nbsp;and two integers&nbsp;left&nbsp;and&nbsp;right. Each&nbsp;ranges[i] = [starti, endi]&nbsp;represents an&nbsp;inclusive&nbsp;interval between&nbsp;starti&nbsp;and&nbsp;endi. Return&nbsp;true&nbsp;if each integer in the inclusive range&nbsp;[left, right]&nbsp;is covered by&nbsp;at&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[222,82,139],"class_list":["post-8549","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-easy","tag-hashtable","tag-range","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8549"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8549\/revisions"}],"predecessor-version":[{"id":8551,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8549\/revisions\/8551"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}