FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codility-python/04_missing_integer.py at main · jsueling/codility-python · GitHub
jsueling
/
codility-python
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
codility-python
/
04_missing_integer.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
23 lines (21 loc) · 724 Bytes
Breadcrumbs
codility-python
/
04_missing_integer.py
Copy path
File metadata and controls
23 lines (21 loc) · 724 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""https://app.codility.com/programmers/lessons/4-counting_elements/missing_integer/"""
# Time complexity:
# Init array of size N is O(N)
# 2 single passes with O(1) array ops (set/get) is O(N),
# Overall: O(N)
def
solution
(
a
:
list
[
int
])
->
int
:
"""
Return the smallest positive integer that does not occur in A.
A is an array of N integers.
"""
n
=
len
(
a
)
# We observe that the missing integer must be in the range [1..N+1],
# since there are only N numbers
present
=
[
False
]
*
(
n
+
1
)
for
val
in
a
:
if
1
<=
val
<=
n
:
present
[
val
]
=
True
for
i
in
range
(
1
,
n
+
1
):
if
not
present
[
i
]:
return
i
return
n
+
1
# all integers 1..N are present
Back
|
FazBrowse Home
|
New Git URL