Skip to content

Instantly share code, notes, and snippets.

@Fredpwol

Fredpwol/findpos.py

Last active Apr 27, 2021
Embed
What would you like to do?
Find num start and end position in a list after sorting
def findSortedPosition(nums, val):
"This solution takes O(n) time complexity and O(1) space complexity"
if nums == None: return [-1, -1]
if val == None or type(val) != int: return [-1, -1]
nums_less = 0
num_val = 0
for num in nums:
if num < val:
nums_less += 1
elif num == val:
num_val += 1
if num_val > 0:
return [nums_less, (nums_less+num_val)-1] #subtract one here because of 0 based indexing
return [-1, -1]
@meekg33k

This comment has been minimized.

Copy link

@meekg33k meekg33k commented Apr 27, 2021

Hello @Fredpwol, congratulations, you are one of the winners of the $20 award 🎉🎉for Week 3 of #AlgorithmFridays.

Your solution was selected because it is most optimal in terms of time complexity. You were able to find a way to solve the problem without sorting the array. Smart!

I have made a blog post here about the different solutions and also announcing you as one of the winners for Week 3 of #AlgorithmFridays.

We will contact you in less than 24 hours for your award.

Congratulations once again and thanks for participating!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment