educative.io

Complexity of Restore IP Addresses

The naive solution reports O(n^3) time while the optimal solution reports O(1) time. The former uses variable notation and assumes an infinitely large input, while the latter assumes an input of constant size, so the comparison doesn’t strike me as meaningful. To create a meaningful comparison, I recommend assuming infinitely large input and using variables in the complexity analysis. Here we could use n to describe the total input length, or even better, break things down into the max length of an octet (x) and number of octets (y) s.t. xy = n.


Course: Grokking Coding Interview Patterns in Python - Learn Interactively
Lesson: Solution: Restore IP Addresses - Grokking Coding Interview Patterns in Python

1 Like

Hello @Isaac_Haseley,

The “Restore IP Addresses” problem presents a unique case where the input size is inherently constrained (a string of maximum 12 characters for a valid IP address). Therefore, the complexities of both approaches are essentially evaluated within a bounded context. While the notion of breaking down the analysis into the max length of an octet (x) and the number of octets (y), such that xy=n, offers a structured approach, it might not entirely fit this specific problem’s context due to the fixed maximum length of the input string.

Thank you for your feedback. Engaging with users like you helps improve the quality and accuracy of the content, fostering a better understanding of complex topics.

Happy learning!

Hey @Dian_Us_Suqlain! If you think it’s wiser to stay true to the fixed input length in the complexity analysis, then I recommend changing the complexity of the naive solution to O(1) to reflect that, so that we aren’t assuming an infinite length in the naive analysis and a fixed length in the optimized analysis. In this case, our naive and optimized solutions would have the same complexity, which is accurate to the problem statement exactly as it’s written, if less intellectually motivating :wink:

1 Like

@Isaac_Haseley sure, we’ll work on this. Thank you for responding back!

1 Like