Qualcomm Interview Question

medium/hard lc question: memory matrix