Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions Python/Power_Of_Heros.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
from typing import List

class Solution:
def sumOfPower(self, nums: List[int]) -> int:
# Initialize the result variable 'ans' to store the final sum of power
# 't' is a temporary variable to accumulate the sum of powers
# 'base' is used to perform modulo 10^9 + 7 to prevent integer overflow
ans, t, base = 0, 0, 10**9 + 7

# Iterate over the sorted 'nums' list (sorting ensures elements are processed in ascending order)
# Iterate over the sorted 'nums' list
for c in sorted(nums):
# Update 'ans' by adding the current power contribution for this element 'c'
# Power contribution formula: (previous_sum + current_element) * current_element^2
# This ensures we are calculating the power for each element and previous sums
# Calculate the power contribution of the current element 'c'
ans = (ans + (t + c) * c * c) % base

# Update 't' which is used in the next iteration
# 't' keeps track of the accumulated sum from previous iterations
# Formula: 2 * previous_sum + current_element (to account for future contributions)
# Update 't' for the next iteration
t = (2 * t + c) % base

# Return the final sum of powers modulo 10^9 + 7
return ans
return ans # Return the final sum of powers modulo 10^9 + 7