big-o

What's the computational cost of count operation on strings Python?

雨燕双飞 提交于 2021-01-22 05:41:29
问题 For example: 'hello'.count('e') Is this O(n)? I'm guessing the way it works is it scans 'hello' and increments a counter each time the letter 'e' is seen. How can I know this without guessing? I tried reading the source code here, but got stuck upon finding this: def count(s, *args): """count(s, sub[, start[,end]]) -> int Return the number of occurrences of substring sub in string s[start:end]. Optional arguments start and end are interpreted as in slice notation. """ return s.count(*args)

What's the computational cost of count operation on strings Python?

╄→гoц情女王★ 提交于 2021-01-22 05:41:21
问题 For example: 'hello'.count('e') Is this O(n)? I'm guessing the way it works is it scans 'hello' and increments a counter each time the letter 'e' is seen. How can I know this without guessing? I tried reading the source code here, but got stuck upon finding this: def count(s, *args): """count(s, sub[, start[,end]]) -> int Return the number of occurrences of substring sub in string s[start:end]. Optional arguments start and end are interpreted as in slice notation. """ return s.count(*args)

Fake Coin Problem

不羁岁月 提交于 2021-01-21 05:31:55
问题 Classic problem with 12 coins ( or marbles) one of which is fake. Fake coin assumed to be lighter than real one. Having scales to compare coins (or marbles). One can do comparison one by one and compare all 12 coins. More efficiently one can do it using Decrease By Factor algorithm. Which is divide the coin stack by 2 and compare 2 stacks on the scales. Big O of the decrease by factor 2 is log2n. There is more efficient decrease by factor 3 (log3n) algorithm but I have not yet found it. If

Fake Coin Problem

≯℡__Kan透↙ 提交于 2021-01-21 05:30:11
问题 Classic problem with 12 coins ( or marbles) one of which is fake. Fake coin assumed to be lighter than real one. Having scales to compare coins (or marbles). One can do comparison one by one and compare all 12 coins. More efficiently one can do it using Decrease By Factor algorithm. Which is divide the coin stack by 2 and compare 2 stacks on the scales. Big O of the decrease by factor 2 is log2n. There is more efficient decrease by factor 3 (log3n) algorithm but I have not yet found it. If

Can O(N+NM) be simplified to O(NM)?

江枫思渺然 提交于 2021-01-05 06:48:16
问题 Suppose that N and M are two parameters of an algorithm. Is the following simplification correct? O(N+NM) = O[N(1+M)] = O(NM) In other words, is it allowed to remove the constant in such a context? 回答1: TL;DR Yes Explanation By the definition of the Big-Oh notation, if a term inside the O(.) is provably smaller than a constant times another term for all sufficiently large values of the variable, then you can drop the smaller term. You can find a more precise definition of Big-Oh here, but

Can O(N+NM) be simplified to O(NM)?

。_饼干妹妹 提交于 2021-01-05 06:41:28
问题 Suppose that N and M are two parameters of an algorithm. Is the following simplification correct? O(N+NM) = O[N(1+M)] = O(NM) In other words, is it allowed to remove the constant in such a context? 回答1: TL;DR Yes Explanation By the definition of the Big-Oh notation, if a term inside the O(.) is provably smaller than a constant times another term for all sufficiently large values of the variable, then you can drop the smaller term. You can find a more precise definition of Big-Oh here, but

Is this big O notation correct for this c++ code?

亡梦爱人 提交于 2020-12-30 04:41:12
问题 currently I have best case as o(n), worst case as o(n). I am unsure if this is correct for the following code segment and I would really appreciate if someone could confirm it is correct, or explain why it's wrong. Thanks! template<typename T> void vector_print(const T& vector, bool repeat) { for (typename T::size_type i = 0; i < vector.size(); ++i) { for (typename T::size_type j = 0; j < vector.size(); ++j) { std::cout << vector[j] << ","; } std::cout << std::endl; if (!repeat) { break; }

The Big O on the Dijkstra Fibonacci-heap solution

一个人想着一个人 提交于 2020-12-28 09:31:33
问题 From Wikipedia: O(|E| + |V| log|V|) From Big O Cheat List: O((|V| + |E|) log |V|) I consider there is a difference between E + V log V and (E+V) log V , isn't there? Because, if Wikipedia's one is correct, shouldn't it be shown as O(|V| log |V|) only then (Removing |E| ) for a reason I do not understand?)? What is the Big O of Dijkstra with Fibonacci-Heap? 回答1: The complexity of Dijkstra's shortest path algorithm is: O(|E| |decrease-key(Q)| + |V| |extract-min(Q)|) where Q is the min-priority

The Big O on the Dijkstra Fibonacci-heap solution

我是研究僧i 提交于 2020-12-28 09:31:00
问题 From Wikipedia: O(|E| + |V| log|V|) From Big O Cheat List: O((|V| + |E|) log |V|) I consider there is a difference between E + V log V and (E+V) log V , isn't there? Because, if Wikipedia's one is correct, shouldn't it be shown as O(|V| log |V|) only then (Removing |E| ) for a reason I do not understand?)? What is the Big O of Dijkstra with Fibonacci-Heap? 回答1: The complexity of Dijkstra's shortest path algorithm is: O(|E| |decrease-key(Q)| + |V| |extract-min(Q)|) where Q is the min-priority

The Big O on the Dijkstra Fibonacci-heap solution

泄露秘密 提交于 2020-12-28 09:23:35
问题 From Wikipedia: O(|E| + |V| log|V|) From Big O Cheat List: O((|V| + |E|) log |V|) I consider there is a difference between E + V log V and (E+V) log V , isn't there? Because, if Wikipedia's one is correct, shouldn't it be shown as O(|V| log |V|) only then (Removing |E| ) for a reason I do not understand?)? What is the Big O of Dijkstra with Fibonacci-Heap? 回答1: The complexity of Dijkstra's shortest path algorithm is: O(|E| |decrease-key(Q)| + |V| |extract-min(Q)|) where Q is the min-priority