{"id":9954,"date":"2023-02-13T16:19:54","date_gmt":"2023-02-14T00:19:54","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9954"},"modified":"2023-02-13T16:28:30","modified_gmt":"2023-02-14T00:28:30","slug":"leetcode-2554-maximum-number-of-integers-to-choose-from-a-range-i","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2554-maximum-number-of-integers-to-choose-from-a-range-i\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2554.\u00a0Maximum Number of Integers to Choose From a Range I"},"content":{"rendered":"\n<p>You are given an integer array&nbsp;<code>banned<\/code>&nbsp;and two integers&nbsp;<code>n<\/code>&nbsp;and&nbsp;<code>maxSum<\/code>. You are choosing some number of integers following the below rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The chosen integers have to be in the range&nbsp;<code>[1, n]<\/code>.<\/li><li>Each integer can be chosen&nbsp;<strong>at most once<\/strong>.<\/li><li>The chosen integers should not be in the array&nbsp;<code>banned<\/code>.<\/li><li>The sum of the chosen integers should not exceed&nbsp;<code>maxSum<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;number of integers you can choose following the mentioned rules<\/em>.<\/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> banned = [1,6,5], n = 5, maxSum = 6\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> You can choose the integers 2 and 4.\n2 and 4 are from the range [1, 5], both did not appear in banned, and their sum is 6, which did not exceed maxSum.\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> banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> You cannot choose any integer while following the mentioned conditions.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> banned = [11], n = 7, maxSum = 50\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> You can choose the integers 1, 2, 3, 4, 5, 6, and 7.\nThey are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= banned.length &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= banned[i], n &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= maxSum &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Greedy + HashSet<\/strong><\/h2>\n\n\n\n<p>We would like to use the smallest numbers possible. Store all the banned numbers into a hashset, and enumerate numbers from 1 to n and check whether we can use that number.<\/p>\n\n\n\n<p>Time complexity: O(m + n)<br>Space complexity: O(m)<\/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  int maxCount(vector<int>& banned, int n, int maxSum) {\n    unordered_set<int> m(begin(banned), end(banned));\n    int ans = 0;    \n    for (int i = 1, j = 0, sum = 0; i <= n; ++i) {\n      if (sum + i > maxSum) break;\n      if (m.count(i)) continue;\n      sum += i;\n      ++ans;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Two Pointers<\/strong><\/h2>\n\n\n\n<p>Sort the banned numbers. Use one pointer j and compare with the current number i.<\/p>\n\n\n\n<p>Time complexity: O(mlogm + n)<br>Space complexity: O(1)<\/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  int maxCount(vector<int>& banned, int n, int maxSum) {\n    sort(begin(banned), end(banned));\n    int ans = 0;    \n    for (int i = 1, j = 0, sum = 0; i <= n; ++i) {\n      if (sum + i > maxSum) break;\n      while (j < banned.size() &#038;&#038; banned[j] < i) ++j;\n      if (j < banned.size() &#038;&#038; i == banned[j]) continue;\n      sum += i;\n      ++ans;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer array&nbsp;banned&nbsp;and two integers&nbsp;n&nbsp;and&nbsp;maxSum. You are choosing some number of integers following the below rules: The chosen integers have to be&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,177],"class_list":["post-9954","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9954","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=9954"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9954\/revisions"}],"predecessor-version":[{"id":9959,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9954\/revisions\/9959"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}