time-complexity

What is the time complexity of this custom function?

What is the time complexity of this custom function? Question: Is it correct to say that the time complexity of the following code snippet is O(n^2)? My main doubt is whether I can consider triplets.append(sorted([array[i], array[j], twoSum – array[j]])) as constant time since it does not depend on N and is always sorting an array …

Total answers: 1

Understanding time/space complexity for Leetcode 1366

Understanding time/space complexity for Leetcode 1366 Question: I’ve solved leetcode 1366 but having some trouble approximating the proper time/space complexity. I’ve looked at the solutions tab but answers vary and I’m not sure if I buy some of them. Here is my code and the general algorithm – class Solution: def rankTeams(self, votes: List[str]) -> …

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

Time complexity and number of calls

Time complexity and number of calls Question: I have trouble finding the time complexity of my program and the exact formula to compute the number of calls (represented by the length of the list). Here is my python program that I wrote: from math import * def calc(n): i = n li = [] while …

Total answers: 2

CPython list.insert(index) real time complexity, when all inserts occur at same index?

CPython list.insert(index) real time complexity, when all inserts occur at same index? Question: My noob question is in the title I have some code that iteratively builds a list. All inserts are always at the second-to-last position. Is list.insert(-1, value) truly an O(1) operation in the CPython implementation? O(?) -> what I want to do …

Total answers: 1

Time complexity question: why is it O(n log n)?

Time complexity question: why is it O(n log n)? Question: Can someone explain why this is O(n log n) and not O(n^2)? I had the idea that the if statement is n times and the else is log n so you pick the worst case between the two in this case is O(n) so multiply …

Total answers: 5

Generate Permutation With Minimum Guaranteed Distance from Elements in Source

Generate Permutation With Minimum Guaranteed Distance from Elements in Source Question: Given a sequence a with n unique elements, I want to create a sequence b which is a randomly selected permutation of a such that there is at least a specified minimum distance d between duplicate elements of the sequence which is b appended …

Total answers: 1

Distances between same numbers in list using single for loop

Distances between same numbers in list using single for loop Question: I have an assignment to get distances of every occurrence of same number in string and its time complexity should be O(n) so it shouldn’t use nested for loops. For example if my string contains "100101" and I need to get distances between ones …

Total answers: 3