How would you analyze the time and space complexity of your solution and optimize it for performance-critical environments?