Qualcomm interview question

medium/hard lc question: memory matrix