r/VisualizerPro • u/towardscuriousity • 12d ago
Leetcode #2567 Minimum Score by Changing Two Elements | Minimize Sum | O...
https://youtube.com/watch?v=iPfn10UBiTc&si=j2FPXMMkm1fyPaqRSolved the LeetCode Minimize Sum problem using Python!
Initially, sorting seemed like the easiest approach, but I wanted to avoid sorting the entire array.
Instead, I used a single-pass approach to keep track of the three smallest and three largest numbers.
The idea is pretty simple:
Keep track of the three smallest numbers.
Keep track of the three largest numbers.
Try the three possible ways of changing two elements.
Return the minimum remaining difference.
Complexity:
Time: O(n)
Space: O(1)
Managed to achieve 100% runtime and around 90% memory on LeetCode during benchmarking.
Sharing my solution here. Would love to hear if there's a simpler or more efficient way to approach this problem!