cswork / OA打开练习工作台

AmazonOA #92

Pan Balance — Count Pair Groupings

估计中等评测数据准备中,可先阅读题面

考点(AI 标注)哈希计数贪心调度

A defective pan balance over-reads one side by a fixed amount wt. A pair of parcels (i, j) is "balanced" iff |weight[i] - weight[j]| == wt. Given weight[n] and wt, count the number of valid pairs. (One parcel cannot appear in two pairs; we count unordered pairs (i, j).) Return the count modulo 10⁹ + 7.

Constraints

  • 1 ≤ n ≤ 10⁵
  • 1 ≤ weight[i], wt ≤ 10⁹
Example:
weight = [4, 5, 5, 4, 2, 5, 3, 2, 3], wt = 1
countBalancedPairs(weight, 1) = 11
(every (4,5), (2,3), (3,2), (5,4) index-pair with |diff|==1)