time-complexity

Complexity of len() with regard to sets and lists

独自空忆成欢 提交于 2019-12-20 17:32:39
问题 The complexity of len() with regards to sets and lists is equally O(1). How come it takes more time to process sets? ~$ python -m timeit "a=[1,2,3,4,5,6,7,8,9,10];len(a)" 10000000 loops, best of 3: 0.168 usec per loop ~$ python -m timeit "a={1,2,3,4,5,6,7,8,9,10};len(a)" 1000000 loops, best of 3: 0.375 usec per loop Is it related to the particular benchmark, as in, it takes more time to build sets than lists and the benchmark takes that into account as well? If the creation of a set object

Longest substring computational complexity

拈花ヽ惹草 提交于 2019-12-20 15:35:47
问题 I was wondering about my apparently code for an MIT online edx class practice problem vs the provided answer code. The assignment in question from the class was as follows: Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print Longest substring in alphabetical order is: beggh Our automating testing will provide a value of s for you My successful code: curSlice='z' for b in

Longest substring computational complexity

不打扰是莪最后的温柔 提交于 2019-12-20 15:34:09
问题 I was wondering about my apparently code for an MIT online edx class practice problem vs the provided answer code. The assignment in question from the class was as follows: Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print Longest substring in alphabetical order is: beggh Our automating testing will provide a value of s for you My successful code: curSlice='z' for b in

How to calculate time complexity of backtracking algorithm?

允我心安 提交于 2019-12-20 10:08:28
问题 How to calculate time complexity for these backtracking algorithms and do they have same time complexity? If different how? Kindly explain in detail and thanks for the help. 1. Hamiltonian cycle: bool hamCycleUtil(bool graph[V][V], int path[], int pos) { /* base case: If all vertices are included in Hamiltonian Cycle */ if (pos == V) { // And if there is an edge from the last included vertex to the // first vertex if ( graph[ path[pos-1] ][ path[0] ] == 1 ) return true; else return false; } /

Why does the time complexity of DFS and BFS depend on the way the graph is represented?

怎甘沉沦 提交于 2019-12-20 09:18:59
问题 The site http://web.eecs.utk.edu/~huangj/CS302S04/notes/graph-searching.html describes that when an adjacency list is used then, DFS and BFS have complexity O(V+E), and if an adjacency matrix is used, the complexity is O(V 2 ). Why is this? 回答1: In both cases, the runtime depends on how long it takes to iterate across the outgoing edges of a given node. With an adjacency list, the runtime is directly proportional to the number of outgoing edges. Since each node is visited once, the cost is

Given a list of numbers and a number k, return whether any two numbers from the list add up to k

安稳与你 提交于 2019-12-20 09:08:22
问题 This question was asked in the Google programming interview. I thought of two approaches for the same: Find all the subsequences of length. While doing so compute the sum and of the two elements and check if it is equal to k. If ye, print Yes, else keep searching. This is a brute Force approach. Sort the array in non-decreasing order. Then start traversing the array from its right end. Say we have the sorted array, {3,5,7,10} and we want the sum to be 17. We will start from element 10, index

Given a list of numbers and a number k, return whether any two numbers from the list add up to k

主宰稳场 提交于 2019-12-20 09:06:06
问题 This question was asked in the Google programming interview. I thought of two approaches for the same: Find all the subsequences of length. While doing so compute the sum and of the two elements and check if it is equal to k. If ye, print Yes, else keep searching. This is a brute Force approach. Sort the array in non-decreasing order. Then start traversing the array from its right end. Say we have the sorted array, {3,5,7,10} and we want the sum to be 17. We will start from element 10, index

Algorithm and data structure for solving the game “Globs”/flood fill/“FloodIt”

妖精的绣舞 提交于 2019-12-20 08:59:57
问题 Suggest an algorithm and data structure for solving the game Globs (http://www.deadwhale.com/play.php?game=131). It's pretty fun in a geeky kind of way. State the time-space complexity (big-O) of your approach in terms of N , the size of the grid (N>=14). Good-enough efficient algorithms with low complexity are preferred. (MatrixFrog correctly points out this game is also known as FloodIt, and Smashery gave a solution 3 months ago in the link he cites below. All you dudes suggesting pruning

Breadth First Search time complexity analysis

孤街浪徒 提交于 2019-12-20 08:14:45
问题 Time complexity to go over each adjacent edges of a vertex is say O(N) , where N is number of adjacent edges. So for V number of vertices time complexity becomes O(V*N) = O(E) , where E is the total number of edges in the graph. Since removing and adding a vertex from/to Queue is O(1) , why it is added to the overall time complexity of BFS as O(V+E) . 回答1: Considering the following Graph we see how the time complexity is O(|V|+|E|) but not O(V*E). Adjacency List V E v0:{v1,v2} v1:{v3} v2:{v3}

Breadth First Search time complexity analysis

心不动则不痛 提交于 2019-12-20 08:14:13
问题 Time complexity to go over each adjacent edges of a vertex is say O(N) , where N is number of adjacent edges. So for V number of vertices time complexity becomes O(V*N) = O(E) , where E is the total number of edges in the graph. Since removing and adding a vertex from/to Queue is O(1) , why it is added to the overall time complexity of BFS as O(V+E) . 回答1: Considering the following Graph we see how the time complexity is O(|V|+|E|) but not O(V*E). Adjacency List V E v0:{v1,v2} v1:{v3} v2:{v3}