Algorithm optimization

shape