您的当前位置:首页正文

LeetCode LCP 28. 采购方案

2024-12-03 来源:个人技术集锦

双指针

class Solution:
    def purchasePlans(self, nums: List[int], target: int) -> int:
        nums.sort()
        n = len(nums)
        j = n - 1
        res = 0
        for i in range(n):
            while nums[i] + nums[j] > target and i < j:
                j -= 1
            if i < j:
                res = (res + j - i) % int((1e9 + 7))
            else:
                break
        return res
显示全文