What is the Time Complexity of this code sample? like nested loop, but inner loop is a fixed number

result = [] for i in n: for jj in range(len(m)): if jj < 3: result.append((n,m)) else: jj = len(m) m, n are two python array inner loop is maximum 3 Thinking O(n)? because …

With lowest possible time complexity , subtract every number in a list from the first lower number after it

I have a list of numbers and I want to subtract every number from the smaller value after it but with the lowest possible complexity I have the list [7, 18 ,5 ,5 ,20 ,9 ,14 ,7 ,19] The first lower …

How to calculate time complexity of these two functions? (recursion)

The first function: def f1(n): if n == 1: return 1 return f(f(n-1)) The second function: def f2(n): if n == 1: return 1 return 1 + f(f(n-1)) Now I can see why both of the function’s space …

How to make this greedy function faster?

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 …

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 def csFirstUniqueChar(input_str,letter = 0,num = 1): if letter == len(input_str): …

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 number is divisible by both 3 and 5, print “FizzBuzz”.) I ran two implementations, to compare their speed: Despite the extra if evaluation of Code B, it consistently ran quicker than Code A. Does a larger number result in a

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 …