Search code examples
pythonpython-3.xruntime

Counterintuitive LeetCode Runtime difference in slight variable name changes


I am very new to Leetcode, but I know a bit of programming. I thought I knew about what makes runtime slower or faster, but this Leetcode question is making me question everything. Below are 3 slightly different correct answers to the same easy question with the only difference being the name of the variables. I was under the assumption that shorter names would lead to faster runtimes, but not to this level of difference. All answers are the same space complexity. Can somebody explain why there's such a large difference despite the exact same logic?

This is the question: Given an array of integers nums, calculate the pivot index of this array.

The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right.

If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array.

Return the leftmost pivot index. If no such index exists, return -1.


class Solution(object):
    def pivotIndex(self, nums):
left & right respectively...
        leftSum, rightSum = 0, sum(nums)
        # Traverse elements through the loop...
        for idx, ele in enumerate(nums):
            rightSum -= ele
            if leftSum == rightSum:
                return idx      
            leftSum += ele
        return -1      
#Runtime: 103ms and beats 95.77% of all submissions despite being the same with just different variable names
# Space Complexity : O(1)
class Solution(object):
    def pivotIndex(self, nums):
        lS, rS = 0, sum(nums)
        # Traverse elements through the loop...
        for i, e in enumerate(nums):
            rS -= e
            if lS == rS:
                return i      
            lS += e
        return -1

Runtime: 106ms and somehow beats 90.78% of all submissions despite it being identical to the first, just different names

class Solution(object):
    def pivotIndex(self, nums):
        leftS, rightS = 0, sum(nums)
        for index, count in enumerate(nums):
            rightS -= count
            if leftS ==rightS:
                return index
            leftS += count
        return -1

Runtime: 118ms and only beats 52.74% of the other submissions despite identical code, just different names


Solution

  • I believe the run times on LeetCode platform can change depending on load on specific LeetCode server where our code is getting executed at that point in time.

    I think It is important to focus on the algorithmic complexity (the time and space complexity) of your solutions rather than the specific runtimes reported by LeetCode. The runtimes can be affected by factors beyond our control, but the algorithmic complexity is a more reliable measure of the performance of your code. In this case, all three solutions have the same time complexity (O(n)) and space complexity (O(1)), which is a more meaningful metric to compare them.