Implemented greedy search finding optimal solution