Domanda di colloquio di DeepL

What is the most optimal algorithm for this problem, and why