Goldman Sachs interview question

Leetcode question on collecting stones