time-complexity

Complexity of an algorithm

試著忘記壹切 提交于 2019-12-22 08:42:32
问题 I'm studying for a test and saw this question , so I did the following, is it correct? the while loop runs at O(log3n). the for loop runs at about O((n-(some math))*log2n) so because I have the minus symbol which is linear, I say that the whole method runs at O(nlogn), unless I'm wrong and it's something like O((n-(n/log3n))*log2n) <- not exactly but quite, can't really figure it out. is the minus linear here or not? if not what is the correct bigO? public void foo (int n, int m) { int i = m;

algorithm complexity - what double star means

大城市里の小女人 提交于 2019-12-22 08:14:37
问题 Does anybody know what means doubled-star in complexity algorithm like this O(N**3) ? I found that one in PHP's similar_text() function and do not understand it. thx 回答1: ** means power. Hence, n**3 means n^3. Complexity is of the order n^3 or O(n^3) 回答2: This double star is the exponentiation operator in PHP(^ operator in general for exponentiation). As per PHP manual, $a ** $b ---- Exponentiation Operator Result of raising $a to the $b'th power. Introduced in PHP 5.6. hence, here the

Implementation of LinkedList in python __getitem__() method

萝らか妹 提交于 2019-12-22 07:58:27
问题 I am implementing a LinkedList in python(3.7.4) and the code of the module is below :- LinkedList.py class Node: def __init__(self,value): self.value = value self.ref = None class LinkedList(Node): def __init__(self): self.__head = None self.__cur = None self.__count = 0 def add(self,value): if self.__head is None: self.__cur = Node(value) self.__head = self.__cur else: self.__cur.ref = Node(value) self.__cur = self.__cur.ref self.__count += 1 def getList(self): temp = self.__head while temp!

What is time complexity of .NET List.sort()

旧时模样 提交于 2019-12-22 03:35:06
问题 What is time complexity of C#'s List<T>.Sort() I guess it's o(N) But after I searched a lot, I didn't get any accurate result. 回答1: http://msdn.microsoft.com/en-us/library/b0zbh7b6.aspx This method uses Array.Sort, which uses the QuickSort algorithm. This implementation performs an unstable sort; that is, if two elements are equal, their order might not be preserved. In contrast, a stable sort preserves the order of elements that are equal. On average, this method is an O(n log n) operation,

Count number of non-prime pairs that when multiplied form a given number N,

倾然丶 夕夏残阳落幕 提交于 2019-12-22 01:31:42
问题 A non-prime pair which forms N is 2 different non-prime numbers where the product of the numbers is N. 1<=N<=10^6 For example For N = 24 there are 2 good pairs (non-prime pairs that form N) (4,6), (1,24), but (2,12), (3,8) are not good. Note: for any 2 numbers a and b pair(a,b) = pair(b,a). There is another condition which states that if the number is a special number, so output = -1 otherwise count the number of non-primes. Number is called special number if it can be represented as a

Quick sort time complexity [closed]

喜你入骨 提交于 2019-12-22 00:00:35
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I recently read about time complexity and I found out that Quick sort has an average time complexity of O(nlog(n)) . Question 1: What I do not understand is how is the log(n) present in the time complexity

Printing out nodes in a disjoint-set data structure in linear time

我只是一个虾纸丫 提交于 2019-12-21 19:57:05
问题 I'm trying to do this exercise in Introduction to Algorithms by Cormen et al that has to do with the Disjoin Set data structure: Suppose that we wish to add the operation PRINT-SET(x) , which is given a node x and prints all the members of x 's set, in any order. Show how we can add just a single attribute to each node in a disjoint-set forest so that PRINT-SET(x) takes time linear in the number of members of x 's set , and the asymptotic running times of the other operations are unchanged.

Time Complexity of Permutations of a String

耗尽温柔 提交于 2019-12-21 17:28:08
问题 Following example was taken from Cracking the coding interview (version 6) book. As per the book the time complexity of the following code is O(n^2 * n!). (Please refer the example 12. Page 32,33) public static void main(String[] args) { new PermutationsTest().permutations("ABCD"); } void permutations(String string) { permutations(string, ""); } static int methodCallCount = 0; void permutations(String string, String perfix) { if (string.length() == 0) { System.out.println(perfix); } else {

Python list.clear() time and space complexity?

荒凉一梦 提交于 2019-12-21 16:36:06
问题 I am writing a blogpost on Python list.clear() method where I also want to mention about the time and space complexity of the underlying algorithm. I expected the time complexity to be O(N) , iterate over the elements and free the memory? But, I found an article where it is mentioned that it is actually an O(1) operation. Then, I searched the source code of the method in CPython implementation and found a method which I believe is the actual internal implementation of list.clear() , however,

Approach and Code for o(log n) solution

天涯浪子 提交于 2019-12-21 06:45:25
问题 f(N) = 0^0 + 1^1 + 2^2 + 3^3 + 4^4 + ... + N^N. I want to calculate ( f(N) mod M ). These are the constraints. 1 ≤ N ≤ 10^9 1 ≤ M ≤ 10^3 Here is my code test=int(input()) ans = 0 for cases in range(test): arr=[int(x) for x in input().split()] N=arr[0] mod=arr[1] #ret=sum([int(y**y) for y in range(N+1)]) #ans=ret for i in range(1,N+1): ans = (ans + pow(i,i,mod))%mod print (ans) I tried another approach but in vain. Here is code for that from functools import reduce test=int(input()) answer=0