i solved it but
while...
if sth:
break
this break breaks the while loop and the code below the while block still runs! I wanted to return answer after this if statement but instead of break, u should return ans
class Solution:
def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int:
players.sort()
ans=0
trainers.sort()
idx=0
for player in players:
if idx==len(trainers):
break
while trainers[idx]<player:
idx+=1
if idx==len(trainers):
return ans
# if trainers[idx]>=player:
ans+=1
idx+=1
return ans
u can solve with heap
import heapq
def matchPlayersAndTrainers(players, trainers):
# Create min-heap from players
heap = players[:]
heapq.heapify(heap)
# Sort trainers
trainers.sort()
matches = 0
# Iterate through trainers
for trainer in trainers:
# Try to match with the smallest player who qualifies
if heap and heap[0] <= trainer:
heapq.heappop(heap)
matches += 1
return matches
n^2 time cuz double for loop?
no the pointers run for n+m time, while sorting is n log n
so it is n log n + m long m that is the dominant time
1 space