complexity-theory

Runtime of dynamic programming versus it's naive recursive counterpart

Runtime of dynamic programming versus it's naive recursive counterpart Question: The problem follows: You are playing a game where you start with n sticks in a pile (where n is a positive integer), and each turn you can take away exactly 1, 7, or 9 sticks from the pile (if there are fewer than 9 …

Total answers: 1

How can I reduce time complexity on this algorithm?

How can I reduce time complexity on this algorithm? Question: I have this exercise and the goal is to solve it with complexity less than O(n^2). You have an array with length N filled with event probabilities. Create another array in which for each element i calculate the probability of all event to happen until …

Total answers: 1

Linear time v.s. Quadratic time

Linear time v.s. Quadratic time Question: Often, some of the answers mention that a given solution is linear, or that another one is quadratic. How to make the difference / identify what is what? Can someone explain this, the easiest possible way, for the ones like me who still don’t know? Asked By: DevLounge || …

Total answers: 4

Python dictionary keys. "In" complexity

Python dictionary keys. "In" complexity Question: Quick question to mainly satisfy my curiosity on the topic. I am writing some large python programs with an SQlite database backend and will be dealing with a large number of records in the future, so I need to optimize as much as I can. For a few functions, …

Total answers: 6

What is the runtime complexity of Python's deepcopy()?

What is the runtime complexity of Python's deepcopy()? Question: I’m trying to improve the speed of an algorithm and, after looking at which operations are being called, I’m having difficulty pinning down exactly what’s slowing things up. I’m wondering if Python’s deepcopy() could possibly be the culprit or if I should look a little further …

Total answers: 4

Time complexity of accessing a Python dict

Time complexity of accessing a Python dict Question: I am writing a simple Python program. My program seems to suffer from linear access to dictionaries, its run-time grows exponentially even though the algorithm is quadratic. I use a dictionary to memoize values. That seems to be a bottleneck. The values I’m hashing are tuples of …

Total answers: 6

Cost of len() function

Cost of len() function Question: What is the cost of len() function for Python built-ins? (list/tuple/string/dictionary) Asked By: Imran || Source Answers: It’s O(1) (constant time, not depending of actual length of the element – very fast) on every type you’ve mentioned, plus set and others such as array.array. Answered By: Alex Martelli Calling len() …

Total answers: 5

What is the runtime complexity of python list functions?

What is the runtime complexity of python list functions? Question: I was writing a python function that looked something like this def foo(some_list): for i in range(0, len(some_list)): bar(some_list[i], i) so that it was called with x = [0, 1, 2, 3, … ] foo(x) I had assumed that index access of lists was O(1), …

Total answers: 5