【leetcode】1608. Special Array With X Elements Greater Than or Equal X
2021/5/21 18:29:31
本文主要是介绍【leetcode】1608. Special Array With X Elements Greater Than or Equal X,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
题目如下:
You are given an array
nums
of non-negative integers.nums
is considered special if there exists a numberx
such that there are exactlyx
numbers innums
that are greater than or equal tox
.Notice that
x
does not have to be an element innums
.Return
x
if the array is special, otherwise, return-1
. It can be proven that ifnums
is special, the value forx
is unique.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:
Input: nums = [0,0] Output: -1 Explanation: No numbers fit the criteria for x. If x = 0, there should be 0 numbers >= x, but there are 2. If x = 1, there should be 1 number >= x, but there are 0. If x = 2, there should be 2 numbers >= x, but there are 0. x cannot be greater since there are only 2 numbers in nums.Example 3:
Input: nums = [0,4,3,0,4] Output: 3 Explanation: There are 3 values that are greater than or equal to 3.Example 4:
Input: nums = [3,6,7,7,0] Output: -1Constraints:
1 <= nums.length <= 100
0 <= nums[i] <= 1000
解题思路:送分题。先对数组排序,然后从0~1001依次尝试就行了。
代码如下:
class Solution(object): def specialArray(self, nums): """ :type nums: List[int] :rtype: int """ nums.sort() for i in range(0,nums[-1] + 1): while i > nums[0]: nums.pop(0) if len(nums) == i:return i return -1
这篇关于【leetcode】1608. Special Array With X Elements Greater Than or Equal X的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-26终极指南:Scrum中如何设置需求优先级
- 2024-06-26AI大模型企业应用实战(25)-为Langchain Agent添加记忆功能
- 2024-06-26小白家庭 nas 搭建方案-icode9专业技术文章分享
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding
- 2024-06-23AI大模型企业应用实战(15)-langchain核心组件
- 2024-06-23AI大模型企业应用实战(16)-langchain核心组件
- 2024-06-23AI 大模型企业应用实战(06)-初识LangChain
- 2024-06-19EntBot.ai: AI Website Chatbot for Product Guides and Development Doc
- 2024-06-17zero-shot-learning-definition-examples-comparison
- 2024-06-06Package Easy(基于 NSIS 的打包exe安装包工具)使用方法-icode9专业技术文章分享