time-complexity

Time and space complexity of count team question

邮差的信 提交于 2021-02-11 12:44:55
问题 from typing import List def countTeams(num: int, skills: List[int], minAssociates: int, minLevel: int, maxLevel: int) -> int: # WRITE YOUR BRILLIANT CODE HERE qualified = [] possible_teams = [[]] for a in skills: if minLevel <= a <= maxLevel: qualified.append(a) num_teams = 0 while qualified: person = qualified.pop() new_teams = [] for team in possible_teams: print(team) print(person) new_team = [person] + team print(new_team) if len(new_team) >= minAssociates: num_teams += 1 new_teams.append

Worst-case time complexity of Python's int.bit_length()

一笑奈何 提交于 2021-02-10 20:35:04
问题 When we call the function int.bit_length passing an integer n , is the worst-case time complexity O(log(n)) or Python uses some trick to improve it (e.g. storing the position of the most significant bit of n when it is created)? 回答1: In CPython, for values with fewer internal-representation digits than PY_SSIZE_T_MAX/PyLong_SHIFT – i.e. fewer than PY_SSIZE_T_MAX binary digits – it’s calculated from the number of internal digits, yes: msd = ((PyLongObject *)self)->ob_digit[ndigits-1]; msd_bits

Time Complexity for binary permutation representation of n bits

拥有回忆 提交于 2021-02-10 14:48:24
问题 I have return a below code in java to produce the possible binary representation of n digits. public List<String> binaryRepresenation(int n){ List<String> list = new ArrayList<>(); if(n>0){ permuation(n, list, ""); } return list; } private void permuation(int n, List<String> list, String str){ if(n==0){ list.add(str); }else{ permuation(n-1, list, str+"0"); permuation(n-1, list, str+"1"); } } For n=3, it produces 001 001 010 011 100 101 110 111 combinations. Overall this function produces 2^n

Time Complexity for binary permutation representation of n bits

梦想与她 提交于 2021-02-10 14:47:19
问题 I have return a below code in java to produce the possible binary representation of n digits. public List<String> binaryRepresenation(int n){ List<String> list = new ArrayList<>(); if(n>0){ permuation(n, list, ""); } return list; } private void permuation(int n, List<String> list, String str){ if(n==0){ list.add(str); }else{ permuation(n-1, list, str+"0"); permuation(n-1, list, str+"1"); } } For n=3, it produces 001 001 010 011 100 101 110 111 combinations. Overall this function produces 2^n

Python list.pop(i) time complexity?

☆樱花仙子☆ 提交于 2021-02-08 14:43:40
问题 I look up online and know that list.pop() has O(1) time complexity but list.pop(i) has O(n) time complexity. While I am writing leetcode, many people use pop(i) in a for loop and they say it is O(n) time complexity and in fact it is faster than my code, which only uses one loop but many lines in that loop. I wonder why this would happen, and should I use pop(i) instead of many lines to avoid it? Example: Leetcode 26. Remove Duplicates from Sorted Array My code: (faster than 75%) class

Find element in a sorted array

倾然丶 夕夏残阳落幕 提交于 2021-02-08 07:52:44
问题 Given a sorted array A and an element x , I need to find an algorithm that returns the index of x in A or -1 if x is not in A . the time complexity of the algorithm should be Θ(logd) when d is the number of elements that appears before x in A , or if x is not in A , d is the number of elements that were before x if he was in A . Binary search is not good enough because its best case is O(1). I thought of starting from the beginning of the array, and start checking the indexes that are powers

Choice vs. Shuffle, Python

烂漫一生 提交于 2021-02-08 07:49:55
问题 I was wondering which of shuffle() and choice() was more efficient. For example, I have a N element list long_list and need a random item from the list, is it quicker to do: shuffle(long_list) return long_list[0] or return choice(long_list) 回答1: random.choice() will always be faster as it does lookups in constant time O(1) random.shuffle() requires iterating over the full sequence to execute the shuffle and this would be O(n), and then your lookup which is constant time O(1) The source code

How to search a string for multiple substrings

笑着哭i 提交于 2021-02-08 05:24:13
问题 I need to check a short string for matches with a list of substrings. Currently, I do this like shown below (working code on ideone) bool ContainsMyWords(const std::wstring& input) { if (std::wstring::npos != input.find(L"white")) return true; if (std::wstring::npos != input.find(L"black")) return true; if (std::wstring::npos != input.find(L"green")) return true; // ... return false; } int main() { std::wstring input1 = L"any text goes here"; std::wstring input2 = L"any text goes here black";

What is the time complexity of constructing a binary search tree?

安稳与你 提交于 2021-02-08 04:54:29
问题 "Every comparison-based algorithm to sort n elements must take Ω(nlogn) comparisons in the worst case. With this fact, what would be the complexity of constructing a n-node binary search tree and why?" Based on this question, I am thinking that the construction complexity must be at least O(nlogn). That said, I can't seem to figure out how to find the total complexity of construction. 回答1: The title of the question and the text you quote are asking different things. I am going to address what

Time and space complexity for removing duplicates from a list

吃可爱长大的小学妹 提交于 2021-02-08 02:57:08
问题 I've the following code and I'm trying to get the time complexity. seen = set() a=[4,4,4,3,3,2,1,1,1,5,5] result = [] for item in a: if item not in seen: seen.add(item) result.append(item) print (result) As far as my understanding goes as I'm accessing the list the time complexity for that operation would be O(n) . As with the if block each time I've a lookup to the set and that would cost another O(n) . So is the overall time complexity O(n^2) ? Does the set.add() also add to the complexity?