Need help with this recently asked amazon question. Tried being greedy but could not pass all the test cases