fix bug, add time complexity and space usage