{"id":7456,"date":"2020-10-04T10:33:56","date_gmt":"2020-10-04T17:33:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7456"},"modified":"2020-10-04T10:43:19","modified_gmt":"2020-10-04T17:43:19","slug":"leetcode-1608-special-array-with-x-elements-greater-than-or-equal-x","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1608-special-array-with-x-elements-greater-than-or-equal-x\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1608. Special Array With X Elements Greater Than or Equal X"},"content":{"rendered":"\n<p>You are given an array&nbsp;<code>nums<\/code>&nbsp;of non-negative integers.&nbsp;<code>nums<\/code>&nbsp;is considered&nbsp;<strong>special<\/strong>&nbsp;if there exists a number&nbsp;<code>x<\/code>&nbsp;such that there are&nbsp;<strong>exactly<\/strong>&nbsp;<code>x<\/code>&nbsp;numbers in&nbsp;<code>nums<\/code>&nbsp;that are&nbsp;<strong>greater than or equal to<\/strong>&nbsp;<code>x<\/code>.<\/p>\n\n\n\n<p>Notice that&nbsp;<code>x<\/code>&nbsp;<strong>does not<\/strong>&nbsp;have to be an element in&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<code>x<\/code>&nbsp;<em>if the array is&nbsp;<strong>special<\/strong>, otherwise, return&nbsp;<\/em><code>-1<\/code>. It can be proven that if&nbsp;<code>nums<\/code>&nbsp;is special, the value for&nbsp;<code>x<\/code>&nbsp;is&nbsp;<strong>unique<\/strong>.<\/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> nums = [3,5]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> There are 2 values (3 and 5) that are greater than or equal to 2.\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> nums = [0,0]\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong> No numbers fit the criteria for x.\nIf x = 0, there should be 0 numbers &gt;= x, but there are 2.\nIf x = 1, there should be 1 number &gt;= x, but there are 0.\nIf x = 2, there should be 2 numbers &gt;= x, but there are 0.\nx cannot be greater since there are only 2 numbers in nums.\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> nums = [0,4,3,0,4]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> There are 3 values that are greater than or equal to 3.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [3,6,7,7,0]\n<strong>Output:<\/strong> -1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 100<\/code><\/li><li><code>0 &lt;= nums[i] &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force<\/strong><\/h2>\n\n\n\n<p>Try all possible x from 0 to n.<\/p>\n\n\n\n<p>Time complexity: O(n^2)<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 specialArray(vector<int>& nums) {\n    for (int x = 0; x <= nums.size(); ++x)\n      if (x == count_if(begin(nums), end(nums), [x](int num) { \n            return num >= x; }))\n        return x;\n    return -1;    \n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def specialArray(self, nums: List[int]) -> int:\n    for x in range(len(nums) + 1):\n      if x == sum([n >= x for n in nums]): return x\n    return -1\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Counting Sort<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<p>f[i] := sum(nums >= i)<\/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 specialArray(vector<int>& nums) {\n    const int n = nums.size();\n    vector<int> f(n + 2);\n    for (int v : nums) ++f[min(v, n)];\n    for (int i = n; i >= 0; --i)\n      if ((f[i] += f[i + 1]) == i) return i;\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array&nbsp;nums&nbsp;of non-negative integers.&nbsp;nums&nbsp;is considered&nbsp;special&nbsp;if there exists a number&nbsp;x&nbsp;such that there are&nbsp;exactly&nbsp;x&nbsp;numbers in&nbsp;nums&nbsp;that are&nbsp;greater than or equal to&nbsp;x. Notice that&nbsp;x&nbsp;does not&nbsp;have to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,106,222],"class_list":["post-7456","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-brute-force","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7456","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=7456"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7456\/revisions"}],"predecessor-version":[{"id":7459,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7456\/revisions\/7459"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}