Alibaba Group Interview Question

Why do you solve this problem in this way?