That depends what exactly you mean by "constant sized". The time to find the minimum of a list with 917,340 elements is with a very large constant factor. The time to find the minimum of various lists of different constant sizes is
and likely
where
is the size of each list. Finding the minimum of a list of 917,340 elements takes much longer than finding the minimum of a list of 3 elements.
That depends what exactly you mean by "constant sized". The time to find the minimum of a list with 917,340 elements is with a very large constant factor. The time to find the minimum of various lists of different constant sizes is
and likely
where
is the size of each list. Finding the minimum of a list of 917,340 elements takes much longer than finding the minimum of a list of 3 elements.
I found this quote from the Wikipedia article on time complexity helpful:
The time complexity is generally expressed as a function of the size of the input.
So if the size of the input doesn't vary, for example if every list is of 256 integers, the time complexity will also not vary and the time complexity is therefore O(1). This would be true of any algorithm, such as sorting, searching, etc.
performance - How efficient is Python's max function - Stack Overflow
algorithm - Big O of min and max in Python - Stack Overflow
How to Compute Max Value with Linear Time Complexity When 'k' Is Not Fixed?
how to find maximum from a list using a for loop in pythonic way?
Videos
It's O(n), since it must check every element. If you want better performance for max, you can use the heapq module. However, you have to negate each value, since heapq provides a min heap. Inserting an element into a heap is O(log n).
Of course it is O(n) unless you are using a different datastructure supporting the max of a value collection due to some implementation invariant.
It's O(n). It's a general algorithm, you can't find the max/min in the general case without checking all of them. Python doesn't even have a built-in sorted collection type that would make the check easy to specialize.
A for loop would have the same algorithmic complexity, but would run slower in the typical case, since min/max (on CPython anyway) are running an equivalent loop at the C layer, avoiding bytecode interpreter overhead, which the for loop would incur.
To find the maximum or minimum of a sequence, you must look at each element once, thus you can't get better than O(n).
Of course, Python min and max have O(n) too: docs.
You can write your own min/max function with a for loop and it will have the same complexity, but will be slower because it is not optimized in C.
Body: Hello! I'm stuck on a problem and could really use some fresh perspectives. I'm trying to figure out a linear time solution (`Theta(n)`) for a problem that's a bit tricky due to its varying parameters.
Here's the Challenge: Picture a line of creatures, each with its own strength and a unique ability. We have two lists: `x` for their strengths and `k` for the number of creatures in front of each (including itself) they can turn to for help.
Example to Illustrate: Let's say `x = [5, 10, 7, 2, 20]` and `k = [1, 2, 1, 3, 2]`. We need to find the maximum strength each creature can muster. For the fourth creature, it looks at the 3 creatures (`k[3] = 3`) - itself and the two creatures before it, considers the strengths `[10, 7, 2]`, and realizes it can leverage a maximum strength of `10`.
Our goal is to output a list where each element is this maximum accessible strength for each creature.
Where I'm Stuck: Here's my Python attempt so far:
def calculate_ output(x, k):
output = []
for i in range(len(x)):
start_index = max(0, i - k[i])
end_index = i + 1
output.append(max(x[start_index:end_index]))
return outputThis isn't efficient. The nested iterations due to `max` make it O(n^2). For each creature, we slice and dice through the list based on `k`, which isn't ideal.
Looking for Advice: I'm hitting a wall here. Maybe there's a way to do this with a sliding window, but the variable range in `k` throws a wrench in the works. Any thoughts on data structures or algorithms to make this linear?
Thanks in advance! Looking forward to your insights.
lst = [1,2,3,4,5]
basically, I want to find max: max(lst)
but I want to use a syntax like this: var = max(item) for item in lst
first, why does this return a generator object instead of throwing some error?
And second, how to do it properly?
Edit: so basically there is a function. I need to compute the max value returned by the function. The function should calculate values from a list of inputs.
So, I need to de this:
var = max(func(item)) for item in lst
I just wanted to know if we can do this in one or two liner?
even better way ...
I have no idea if it's more performant, but you technically don't need to unpack the list:
mymax = max(mylist)
In your Python interpreter, type help(max), you will get:
Help on built-in function max in module __builtin__:
max(...)
max(iterable[, key=func]) -> value
max(a, b, c, ...[, key=func]) -> value
With a single iterable argument, return its largest item.
With two or more arguments, return the largest argument.
max is written in C, so it should already fast enough, your issue is using it in a not very right way.
Body: Hello! I'm stuck on a problem and could really use some fresh perspectives. I'm trying to figure out a linear time solution (`Theta(n)`) for a problem that's a bit tricky due to its varying parameters.
Here's the Challenge: Picture a line of creatures, each with its own strength and a unique ability. We have two lists: `x` for their strengths and `k` for the number of creatures in front of each (including itself) they can turn to for help.
Example to Illustrate: Let's say `x = [5, 10, 7, 2, 20]` and `k = [1, 2, 1, 3, 2]`. We need to find the maximum strength each creature can muster. For the fourth creature, it looks at the 3 creatures (`k[3] = 3`) - itself and the two creatures before it, considers the strengths `[10, 7, 2]`, and realizes it can leverage a maximum strength of `10`.
Our goal is to output a list where each element is this maximum accessible strength for each creature.
Where I'm Stuck: Here's my Python attempt so far:
def calculate_ output(x, k):
output = []
for i in range(len(x)):
start_index = max(0, i - k[i])
end_index = i + 1
output.append(max(x[start_index:end_index]))
return outputThis isn't efficient. The nested iterations due to `max` make it O(n^2). For each creature, we slice and dice through the list based on `k`, which isn't ideal.
Looking for Advice: I'm hitting a wall here. Maybe there's a way to do this with a sliding window, but the variable range in `k` throws a wrench in the works. Any thoughts on data structures or algorithms to make this linear?
Thanks in advance! Looking forward to your insights.