Greedy Java Algorithm