Created
April 23, 2021 17:50
-
-
Save Fredpwol/61fea0b58c5e31f4702a4b3c7bbc887e to your computer and use it in GitHub Desktop.
Find num start and end position in a list after sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def findSortedPosition(nums, val): | |
if nums == None: return [-1, -1] | |
if val == None: return [-1, -1] | |
prev_num = None | |
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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment