作者oin1104 (是oin的說)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Mon May 27 11:24:53 2024
※ 引述 《sustainer123 (caster)》 之銘言:
:
:
: https://reurl.cc/gGYKGL
:
: 1608. Special Array With X Elements Greater Than or Equal X
:
: 給你一數列 此數列只有非負整數 假設有一整數x 數列中恰巧有x個元素大於等於x
:
: 請回傳x 假設無x 則回傳-1
:
: x不必是存在於數列的元素
:
: Example 1:
:
: Input: nums = [3,5]
: Output: 2
: Explanation: There are 2 values (3 and 5) that are greater than or equal to 2.
: Example 2:
:
思路:
排序 數數字
然後看數到的地方跟>=res的數量一不一樣
就可以知道要不要回傳-1了
屁眼
class Solution {
public:
int specialArray(vector<int>& nums)
{
int res = 0;
int len = nums.size();
sort(nums.begin(),nums.end());
for(int i = len-1 ; i >= 0 ; i --)
{
if(nums[i] >= len-i)res++;
else break;
}
int c = 0;
for(int i = len-1 ; i >= 0 ; i --)
{
if(nums[i] >= res)c ++;
else break;
}
if(res == 0)return -1;
if(res != c)return -1;
return res;
}
};
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.162.10.229 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1716780295.A.7CB.html
推 sustainer123: 大師 05/27 11:40
→ JIWP: 大師 05/27 13:41