Skip to content

Instantly share code, notes, and snippets.

@sanan-fataliyev
Created April 25, 2021 23:19
Show Gist options
  • Select an option

  • Save sanan-fataliyev/5bc149b17aa95e3c397bef765bcc823d to your computer and use it in GitHub Desktop.

Select an option

Save sanan-fataliyev/5bc149b17aa95e3c397bef765bcc823d to your computer and use it in GitHub Desktop.
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'maxPairs' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY skillLevel
# 2. INTEGER minDiff
#
def maxPairs(skills, diff):
if len(skills) < 2:
return 0
if len(skills) == 2:
return int(abs(skills[0]-skills[1]) >= diff)
skills.sort()
l, r = 0, 1
while skills[r] - skills[l] < diff:
r+=1
if r == len(skills):
return 0
count = 1
picked = [False] * len(skills)
picked[l] = True
picked[r] = True
l += 1
r += 1
while r < len(skills) and l < len(skills):
if l == r:
r += 1
continue
if picked[r]:
r += 1
continue
if picked[l]:
l += 1
continue
if skills[r] - skills[l] < diff:
r += 1
continue
picked[l] = True
picked[r] = True
count += 1
l += 1
r += 1
return count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment