Search code examples
pythoncomplexity-theoryspace-complexity

Both of the functions' space complexity is O(1) but the one is better than other one?_?


I have solved leetcode 1658. problem in two ways which are very similar. However, one of them space complexity result %36, the second one is %90. Why are these too different? Can you guys help me to understand the difference?

Code 1: Space Complexity Result %36

` def minOperations(self, nums: List[int], x: int) -> int:

    length=len(nums)
    if length==1: return 1 if nums[0]==x else -1
    if sum(nums)<x: return -1

    start,end,maximum=0,1,-1                               
    currsum=nums[0]                                        
    target=sum(nums)-x

    while start<=end and end<length:
        while currsum<target and end<length:
            currsum+=nums[end]
            end+=1
        
        while currsum>target:
            currsum-=nums[start]
            start+=1
        
        if currsum==target:
            maximum=max(end-start,maximum)
            currsum-=nums[start]
            start+=1
    
    return length-maximum if maximum!=-1 else -1`

Code 2: Space Complexity Result :%90

` class Solution: def minOperations(self, nums: List[int], x: int) -> int:

    length=len(nums)
    if length==1: return 1 if nums[0]==x else -1
    if sum(nums)<x: return -1

    start,end,maximum=0,1,-1
    currsum=nums[0]
    target=sum(nums)-x
    if nums[0]==target:maximum=1

    while end<length:
        currsum+=nums[end]
        end+=1
        
        while currsum>target:
            currsum-=nums[start]
            start+=1
        
        if currsum==target:
            maximum=max(end-start,maximum)

    return length-maximum if maximum!=-1 else -1`

Both functions have O(1) space complexity but there are difference.


Solution

  • The space complexity of both solutions is indeed O(1) as you mentioned, because they both use a constant amount of space regardless of the input size. The difference in the space complexity results you're seeing (%36 vs %90) is not due to the actual space complexity of the solutions, but rather due to the specific implementation details of the LeetCode platform.

    LeetCode measures the memory usage of your solution relative to other submissions for the same problem. The percentage you see is not the actual space complexity of your solution, but rather how your solution compares to others in terms of memory usage. A lower percentage means that your solution uses more memory than a higher percentage of submissions, and vice versa.

    In practice, the difference in memory usage between the two solutions is likely very small and should not significantly impact the performance of your code. Both solutions are efficient in terms of space complexity. Leetcode complexity result can vary across reruns especially when the difference is very small, and should not be seen as a measure of your code performance, understanding the actual complexity is more important.