1714. Sum Of Special Evenly-Spaced Elements In Array

Обновлено: 2024-03-12
1 мин
[]

Содержание

LeetCode problem 1714

class Solution:
    def solve(self, nums: List[int], queries: List[List[int]]) -> List[int]:
        mod = 10**9 + 7
        n = len(nums)
        m = int(sqrt(n))
        suf = [[0] * (n + 1) for _ in range(m + 1)]
        for i in range(1, m + 1):
            for j in range(n - 1, -1, -1):
                suf[i][j] = suf[i][min(n, j + i)] + nums[j]
        res = []
        for x, y in queries:
            if y <= m:
                res.append(suf[y][x] % mod)
            else:
                res.append(sum(nums[x::y]) % mod)
        return res