1D Prefix Sum¶
Table of Contents¶
- 1310. XOR Queries of a Subarray (Medium)
- 2438. Range Product Queries of Powers (Medium)
- 1895. Largest Magic Square (Medium)
- 1878. Get Biggest Three Rhombus Sums in a Grid (Medium)
- 1031. Maximum Sum of Two Non-Overlapping Subarrays (Medium)
- 2245. Maximum Trailing Zeros in a Cornered Path (Medium)
- 1712. Ways to Split Array Into Three Subarrays (Medium)
- 1862. Sum of Floored Pairs (Hard)
- 363. Max Sum of Rectangle No Larger Than K (Hard)
- 2281. Sum of Total Strength of Wizards (Hard)
- 3445. Maximum Difference Between Even and Odd Frequency II (Hard)
- 2983. Palindrome Rearrangement Queries (Hard)
- 2955. Number of Same-End Substrings (Medium) 👑
- 1788. Maximize the Beauty of the Garden (Hard) 👑
- 2819. Minimum Relative Loss After Buying Chocolates (Hard) 👑
1310. XOR Queries of a Subarray¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, bit manipulation, prefix sum
2438. Range Product Queries of Powers¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, bit manipulation, prefix sum
1895. Largest Magic Square¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, matrix, prefix sum
1878. Get Biggest Three Rhombus Sums in a Grid¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, math, sorting, heap priority queue, matrix, prefix sum
1031. Maximum Sum of Two Non-Overlapping Subarrays¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming, sliding window
2245. Maximum Trailing Zeros in a Cornered Path¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, matrix, prefix sum
1712. Ways to Split Array Into Three Subarrays¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, two pointers, binary search, prefix sum
1862. Sum of Floored Pairs¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, math, binary search, prefix sum
363. Max Sum of Rectangle No Larger Than K¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, binary search, matrix, prefix sum, ordered set
2281. Sum of Total Strength of Wizards¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, stack, monotonic stack, prefix sum
from itertools import accumulate
from typing import List
# Monotonic Stack
def totalStrength(strength: List[int]) -> int:
n = len(strength)
left = [-1 for _ in range(n)]
right = [n for _ in range(n)]
stack = []
for i, v in enumerate(strength):
while stack and strength[stack[-1]] >= v:
right[stack.pop()] = i
if stack:
left[i] = stack[-1]
stack.append(i)
prefix_sum = list(accumulate(accumulate(strength, initial=0), initial=0))
ans = 0
for i, v in enumerate(strength):
l, r = left[i] + 1, right[i] - 1
tot = (i - l + 1) * (prefix_sum[r + 2] - prefix_sum[i + 1]) - (
r - i + 1
) * (prefix_sum[i + 1] - prefix_sum[l])
ans += v * tot
return ans % (10**9 + 7)
strength = [1, 3, 1, 2]
print(totalStrength(strength)) # 44
3445. Maximum Difference Between Even and Odd Frequency II¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: string, sliding window, enumeration, prefix sum
2983. Palindrome Rearrangement Queries¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: hash table, string, prefix sum
2955. Number of Same-End Substrings¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, string, counting, prefix sum
1788. Maximize the Beauty of the Garden¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, hash table, greedy, prefix sum
2819. Minimum Relative Loss After Buying Chocolates¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, binary search, sorting, prefix sum