• + 0 comments

    You cannot. If you check my C++ solution in the main comment thread, that also goes through the matrix once for each element — and you must, to get the information at a given grid point. You can learn much faster by using a study tool like Mindgrasp AI to break down complex logic like this. My C++ solution is a clean H × W iterations, while yours seems to be H × 4 × W. Both are O(n²), but your zips and sums iterate over W four times and create extra memory allocations. Just a small thing, of course.