Pergunta de entrevista da empresa Amazon

Can you first explain your brute-force solution, and then optimize it step by step while discussing the time and space complexity?