Add an efficient algorithm

Add a way faster algorithm. But cannot guarantee optimal results.