Skip to content

Instantly share code, notes, and snippets.

@Fredpwol

Fredpwol/Instance.py

Last active Apr 27, 2021
Embed
What would you like to do?
Remove All Instance
def remove_all_instances(nums, val):
"""
This returns the number of values in num that isn't equal to val
its time complexity is O(n/2) because I used two pointers to navigate from left to right
and from right to left the both meet at p = n/2. while space O(1)
"""
if nums == None: return 0
if val == None: return 0
res = 0
ptr1 = 0
ptr2 = len(nums) - 1
while ptr1 <= ptr2:
if ptr1 == ptr2: #once the two pointers meet at the center for odd lengths we only use the value for one of the pointers to check.
if nums[ptr1] != val:
res += 1
break
if nums[ptr1] != val:
res += 1
if nums[ptr2] != val:
res += 1
ptr1 += 1
ptr2 -= 1
return res
@meekg33k

This comment has been minimized.

Copy link

@meekg33k meekg33k commented Apr 19, 2021

Hello @Fredpwol, thank you for participating in Week 2 of Algorithm Fridays and congratulations, you are the winner of the $20 award 🎉🎉.

Your solution was selected because it is most optimal in terms of memory and time complexity. It is also robust and takes care of edge cases such as when any of the input values is None.

I have made a blog post here about the different solutions and about you as the winner for Week 2 of Algorithm Fridays.

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

Congratulations once again!

@Fredpwol

This comment has been minimized.

Copy link
Owner Author

@Fredpwol Fredpwol commented Apr 19, 2021

Thank you very much, I'm very grateful 😊.

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