Problem statement

https://binarysearch.com/problems/Smallest-Pair-Sum-with-Distance-Constraint/

Solution

Use cumulative minimum of prefixes.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, nums):
        acc = list(accumulate(nums, min))
        ans = float("inf")
        for i in range(2, len(nums)):
            ans = min(ans, nums[i] + acc[i - 2])
        return ans