元想法:
用雙指標類似雙迴圈去找 -> 超時,可能RE


這題比較像雙指針的同向移動,因為已經排序好了,所以可以不斷比較公寓坪數與顧客預其,太小就+1

n,m,k = map(int,input().split())
cus = sorted(list(map(int,input().split())))#n
apt = sorted(list(map(int,input().split())))#m

left = right = ans = 0
while left<n and right <m:

    if abs(apt[right]-cus[left])<=k:
        left+=1
        ans+=1
        right+=1
    elif apt[right] < cus[left]-k:
        # 公寓太小
        right+=1
    else:
        # 顧客的預期過小
        left+=1

print(ans)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *