-
Notifications
You must be signed in to change notification settings - Fork 2
/
1608.py
39 lines (33 loc) · 967 Bytes
/
1608.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# [ LeetCode ] 1608. Special Array With X Elements Greater Than or Equal X
def solution(nums: list[int]) -> int:
start, end = 0, max(nums)
while start <= end:
count: int = 0
middle: int = start + (end - start) // 2
for num in nums:
if num >= middle:
count += 1
if middle > count:
end = middle - 1
elif middle < count:
start = middle + 1
else:
return middle
return -1
if __name__ == "__main__":
cases: list[dict[str, dict[str, list[int]] | int]] = [
{
"input": {"nums": [3, 5]},
"output": 2
},
{
"input": {"nums": [0, 0]},
"output": -1
},
{
"input": {"nums": [0, 4, 3, 0, 4]},
"output": 3
}
]
for case in cases:
assert case["output"] == solution(**case["input"])