Skip to content

Latest commit

 

History

History
22 lines (20 loc) · 561 Bytes

2681.md

File metadata and controls

22 lines (20 loc) · 561 Bytes

2681. Power of Heroes

Solution 1 (time O(nlongn), space O(n))

class Solution(object):
    def sumOfPower(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        dp = [0] * len(nums)
        pre_sum = [0] * (len(nums) + 1)
        ans = 0
        mod = 10 ** 9 + 7
        for i in range(len(nums)):
            dp[i] = (nums[i] + pre_sum[i]) % mod
            pre_sum[i + 1] = (pre_sum[i] + dp[i]) % mod
            ans = (ans + nums[i] * nums[i] * dp[i]) % mod
        return ans