Radoslav Petkov’s Post

Today I solved LeetCode 3321 - Find X-Sum of All K-Long Subarrays II (Hard) #day14 of #1001DaysOfCode The challenge is to compute the “x-sum” for every subarray of length k. Instead of simply summing values, we first: Count occurrences of each number in the subarray Select the top x most frequent elements (breaking ties by choosing the larger value) Then sum only those selected elements with all their occurrences A direct recalculation for every sliding window would be too slow, so the key idea was to maintain frequency counts incrementally and keep the top-x elements efficiently balanced using two heaps. To solve it, I used: A sliding window to move across the array efficiently A frequency map to track counts Two heaps (in for top-x elements, out for remaining elements) A dynamic rebalancing step to ensure correctness as counts change Detailed explanation + code here: https://lnkd.in/dD4e5WNh #LeetCode #Python #DataStructures #Heaps #Algorithms #CodingChallenge #SoftwareEngineering #CompetitiveProgramming

  • text

To view or add a comment, sign in

Explore content categories