Skip to main content
Rich Farmbrough's user avatar
Rich Farmbrough's user avatar
Rich Farmbrough's user avatar
Rich Farmbrough
  • Member for 3 years, 9 months
  • Last seen more than 3 years ago
comment
Time complexity of min() and max() on a list of constant size?
Yes, you need to recalculate (though if we know just one given item has been changed we can do this very fast). The point is that there is an upper limit to calculating max(x1, .... xc) where c is a constant. It may be large, but that's fine from a theoretical point of view. See my comment to the original question for a little more.
comment
Time complexity of min() and max() on a list of constant size?
In your example the complexity is O(m), but m is a constant, namely 1,000,000 so it is O(1000000) ~ O(1).
comment
Time complexity of min() and max() on a list of constant size?
The list is constant size not a list of constants.
comment
Time complexity of min() and max() on a list of constant size?
Because the list is constant size the time complexity of the python min() or max() calls are O(1) - there is no "n". Caveat: if the values are strings, comparing long strings has a worst case O(n) running time, where n is the length of the strings you are comparing, so there's potentially a hidden "n" here. If you are calling the function in a loop, then the call is still O(1) but the contribution to time complexity of the loop is O(n), where n is the number of iterations of the loop. Of course if the loop contains dominant elements the complexity of the loop will be higher. (eg sort)