I am trying to solve a problem and my code fails at one test case where the list is of length 25000. Is there any way I can make this faster. I tried using functools.lru_cache and I still can not run within the time required to complete. This is the problem from the site Given an array of non-negative integers
Tag: time-complexity
what is the Time Complexity of a nested loop in a recursive function?
I have a recursive function with a nested loop.I’m looking for what is the time complexity? Here is the function Answer Suppose n is the length of input_str. The algorithm can iterate n times recursively in the worst case, i.e., in each recursive call letter will be increased by 1 and it can be continued up to n. In each
Computational complexity of modulos and FizzBuzz
So I don’t want to go into whether this is the most perfect code for the FizzBuzz challenge. For those unfamiliar with FizzBuzz, there are four rules in printing out a range of 1-100: Print out all numbers; If the number is divisible by 3, print “Fizz” instead; If the number is divisible by 5, print “Buzz” instead; If the
Is there a way to print all substrings of a string in O(n) time?
I have an input abcde. I’m trying to output something like this: I can’t make a code which is without nested loops. My question is what is the solution of this problem with O(n) time complexity? My code is given below: Answer Lets do this without a nested loop! This a game with random library, but the execution time is
How to find nth Fibonacci number using Javascript with O(n) complexity
Trying really hard to figure out how to solve this problem. The problem being finding nth number of Fibonacci with O(n) complexity using javascript. I found a lot of great articles how to solve this using C++ or Python, but every time I try to implement the same logic I end up in a Maximum call stack size exceeded. Example
Fast way to remove a few items from a list/queue
This is a follow up to a similar question which asked the best way to write and it seems the consensus was on something like However, I think if you are only removing a few items, most of the items are being copied into the same object, and perhaps that is slow. In an answer to another related question, someone
Finding the most frequent character in a string
I found this programming problem while looking at a job posting on SO. I thought it was pretty interesting and as a beginner Python programmer I attempted to tackle it. However I feel my solution is quite…messy…can anyone make any suggestions to optimize it or make it cleaner? I know it’s pretty trivial, but I had fun writing it. Note: