Implemented greedy solution