Reduce space and time complexity