complexity-theory

What is the time complexity of indexing, inserting and removing from common data structures?

喜夏-厌秋 提交于 2019-12-20 07:58:39
问题 There is no summary available of the big O notation for operations on the most common data structures including arrays, linked lists, hash tables etc. 回答1: Information on this topic is now available on Wikipedia at: Search data structure +----------------------+----------+------------+----------+--------------+ | | Insert | Delete | Search | Space Usage | +----------------------+----------+------------+----------+--------------+ | Unsorted array | O(1) | O(1) | O(n) | O(n) | | Value-indexed

Complexity greater than authorized in AngularJS Controller (SonarLint issue)

纵然是瞬间 提交于 2019-12-20 01:58:23
问题 I use SonarLint with Eclipse , and I'm codding an application using AngularJS . I had a problem with a controller so I was trying to clean it a bit to see clearer, and then SonarLint popped me up an issue : Function has a complexity of 11 which is greater than 10 authorized. And here's the code of my controller : app.controller('LauncherCtrl', function ($scope, $http) { $scope.genStatus = "stopped"; $scope.startgenerator = function() { $http.get('/start').success(function () { $scope

Dictionary Lookup (O(1)) vs Linq where

核能气质少年 提交于 2019-12-19 19:42:15
问题 What is faster and should I sacrifice the Linq standard to achieve speed (assuming Dictionary lookup is truly faster)? So let me elaborate: I have the following: List<Product> products = GetProductList(); I have a need to search for a product based on some attribute, for example, the serial number. I could first create a dictionary, and then populate it as follow: Dictionary<string, Product> dict = new Dictionary<string, Product>(); foreach(Product p in products) { dict.Add(p.serial, p); }

Dictionary Lookup (O(1)) vs Linq where

。_饼干妹妹 提交于 2019-12-19 19:42:15
问题 What is faster and should I sacrifice the Linq standard to achieve speed (assuming Dictionary lookup is truly faster)? So let me elaborate: I have the following: List<Product> products = GetProductList(); I have a need to search for a product based on some attribute, for example, the serial number. I could first create a dictionary, and then populate it as follow: Dictionary<string, Product> dict = new Dictionary<string, Product>(); foreach(Product p in products) { dict.Add(p.serial, p); }

Why can't the median-of-medians algorithm use block size 3?

↘锁芯ラ 提交于 2019-12-19 16:57:35
问题 I am Working through the analysis of deterministic median finding under the assumption that the input is divided into 3 parts rather than 5 and the question is Where does it break down? the deterministic median finding algorithm: SELECT(i, n) Divide the n elements into groups of 5. Find the median of each 5-element group by rote. Recursively SELECT the median x of the ⎣n/5⎦ group medians to be the pivot. Partition around the pivot x. Let k = rank(x) 4.if i = k then return x elseif i < k then

Complexity in using Binary search and Trie

匆匆过客 提交于 2019-12-19 04:45:23
问题 given a large list of alphabetically sorted words in a file,I need to write a program that, given a word x, determines if x is in the list. Preprocessing is ok since I will be calling this function many times over different inputs. priorties: 1. speed. 2. memory I already know I can use (n is number of words, m is average length of the words) 1. a trie, time is O(log(n)), space(best case) is O(log(n m)), space(worst case) is O(n m). 2. load the complete list into memory, then binary search,

Complexity of inserting n numbers into a binary search tree

风格不统一 提交于 2019-12-18 17:29:47
问题 I have got a question, and it says "calculate the tight time complexity for the process of inserting n numbers into a binary search tree". It does not denote whether this is a balanced tree or not. So, what answer can be given to such a question? If this is a balanced tree, then height is logn, and inserting n numbers take O(nlogn) time. But this is unbalanced, it may take even O(n 2 ) time in the worst case. What does it mean to find the tight time complexity of inserting n numbers to a bst?

What is the complexity of this code whose nested for loop repeatedly doubles its counter?

ⅰ亾dé卋堺 提交于 2019-12-18 16:21:33
问题 In the book Programming Interviews Exposed it says that the complexity of the program below is O(N), but I don't understand how this is possible. Can someone explain why this is? int var = 2; for (int i = 0; i < N; i++) { for (int j = i+1; j < N; j *= 2) { var += var; } } 回答1: You need a bit of math to see that. The inner loop iterates Θ(1 + log [N/(i+1)]) times (the 1 + is necessary since for i >= N/2 , [N/(i+1)] = 1 and the logarithm is 0, yet the loop iterates once). j takes the values (i

What is the complexity of this code whose nested for loop repeatedly doubles its counter?

不羁岁月 提交于 2019-12-18 16:21:30
问题 In the book Programming Interviews Exposed it says that the complexity of the program below is O(N), but I don't understand how this is possible. Can someone explain why this is? int var = 2; for (int i = 0; i < N; i++) { for (int j = i+1; j < N; j *= 2) { var += var; } } 回答1: You need a bit of math to see that. The inner loop iterates Θ(1 + log [N/(i+1)]) times (the 1 + is necessary since for i >= N/2 , [N/(i+1)] = 1 and the logarithm is 0, yet the loop iterates once). j takes the values (i

Complexity for towers of Hanoi?

跟風遠走 提交于 2019-12-18 15:09:09
问题 I was recently solving Towers of Hanoi problem. I used a "Divide and Conquer" Strategy to solve this problem. I divided the main problem into three smaller sub problems and thus following recurrence was generated. T(n)=2T(n-1)+1 Solving this leads to O(2^n) [exponential time] Then i tried to use memoization technique to solve it, but here too the space complexity was exponential and heap space exhausted very soon and problem was still unsolvable for larger n. Is there a way to solve the