↓Skip to main content

Prefix-Sum

LeetCode 2024: Maximize the Confusion of an Exam
·2 mins
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
LeetCode 2024 note: a window is valid when the rarer of T and F appears at most k times (flip all of them); shrink the left when it is not
LeetCode 1208: Get Equal Substrings Within Budget
·1 min
LeetCode Medium Sliding-Window String Binary-Search Prefix-Sum
LeetCode 1208 note: each position costs |s[i] - t[i]|, so the problem becomes the longest subarray with sum <= maxCost
LeetCode 3788: Maximum Score of a Split
·1 min
LeetCode Medium Prefix-Sum Array
LeetCode 3788 note: build the suffix minimum first, then accumulate the prefix sum from the left and try every split
LeetCode 3694: Distinct Points Reachable After Substring Removal
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table String
LeetCode 3694 note: the endpoint is the total displacement minus the removed segment’s displacement; a length-k window tracks that segment and each endpoint goes into a set
LeetCode 1423: Maximum Points You Can Obtain from Cards
·1 min
LeetCode Medium Sliding-Window Prefix-Sum Array
LeetCode 1423 note: taking k cards from the ends leaves a contiguous middle of n - k cards; find its minimum sum with a fixed-size window
LeetCode 3709: Design Exam Scores Tracker
·2 mins
LeetCode Medium Binary-Search Prefix-Sum Design
LeetCode 3709 note: times are increasing, so store prefix sums and answer queries with two bisects; always parenthesize mid
LeetCode 3707: Equal Score Substrings
·2 mins
LeetCode Easy Prefix-Sum String
LeetCode 3707 note: accumulate the left score while scanning, with the right side as total minus left; scores are alphabet positions 1 to 26, not ord
LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Prefix-Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.
LeetCode 1658: Minimum Operations to Reduce X to Zero
·3 mins
LeetCode Daily Medium Array Sliding-Window Prefix-Sum Reverse-Thinking
LeetCode Problem Solving - Reverse Thinking with Sliding Window
LeetCode 1343: Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
·3 mins
LeetCode Daily Medium Array Sliding-Window Prefix-Sum Average-Calculation
LeetCode Problem Solving - Fixed Size Sliding Window with Sum Calculation
LeetCode 2874: Maximum Value of an Ordered Triplet II
·2 mins
LeetCode Medium Prefix-Sum Array
LeetCode 2874 note: fix the middle j and take the prefix maximum on the left and the suffix maximum on the right, or scan once tracking the maximum and the best difference
LeetCode 1248: Count Number of Nice Subarrays
·3 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 1248 note: “exactly k odd numbers” is not monotonic, so compute “at most k” minus “at most k - 1”; or use prefix sums with a hash map