Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def search(mas, Id, pos):
- l = 0
- r = len(mas)
- while (r - l > 1):
- m = (r + l) // 2
- if (mas[m][0] < Id or (mas[m][0] == Id and mas[m][1] < pos)):
- l = m
- else:
- r = m
- for i in range(l, r + 1):
- if (mas[i][0] == Id and mas[i][1] == pos):
- return i
- return -1
- def doctorAndPatients(patientId, query):
- N = len(patientId)
- Q = len(query)
- assert(1 <= N and N <= 100000)
- assert(1 <= Q and Q <= 100000)
- for i in range(N):
- assert(1 <= patientId[i] and patientId[i] <= 100000)
- for i in range(Q):
- assert(0 <= query[i] and query[i] < N)
- mas = []
- res = []
- for i in range(N):
- mas.append((patientId[i], i))
- mas.sort()
- print(mas)
- for i in range(Q):
- pos = search(mas, patientId[query[i]], query[i])
- if (pos + 1 < N and mas[pos + 1][0] == mas[pos][0]):
- res.append(mas[pos + 1][1] - mas[pos][1])
- else:
- res.append(-1)
- return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement