Give me the optimal solution for the LeetCode problem (I will paste the link of the actual problem) (avoid brute force) in GoLang.
Just remove the main() function and only provide the required function.
Explain it step-by-step like a worked example (similar to a math problem).
- Intuition
- Why brute force is inefficient
- The optimal approach
- A full worked example with step-by-step iteration
(track variables/data structures at each step) - Clean code