题目
!传送门题目太长就不描述了
解题思路
双指针(最好理解),对向双指针,取一个数组做完全遍历,关键步骤的思路在注释中。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public int breakfastNumber(int[] staple, int[] drinks, int x) { int ps = 0, pd = drinks.length - 1, count = 0; Arrays.sort(staple); Arrays.sort(drinks); while (ps < staple.length && pd >= 0) { if (staple[ps] + drinks[pd] <= x) { count += pd + 1; count = count % 1000000007; ps++; } else { pd--; } } return count; } }
|