big-o

What is the time complexity of deleting a node in a binary tree

坚强是说给别人听的谎言 提交于 2020-08-06 14:54:06
问题 For deleting a node in the binary tree, we have to search the node. That is possible in minimum O(log N) and max O(N). Depending on the node, we have to rearrange the pointers. How do we calculate the time complexity of that. 回答1: That depends on how you're doing the deletion. The most common way involves finding the successor of the node, then replacing the node with that successor. This can be done in O(h), where h is the height of the tree. In the worst case this is O(n), but in a balanced

Big O Notation - Correct definition for a loop with HashSet lookup

二次信任 提交于 2020-08-05 06:12:22
问题 From my understanding, a simple for loop will have a complexity of O(n). foreach(var record in records) { // ... } If I introduce a Hash lookup within the foreach, will that keep the complexity to O(n)? var ids = new HashSet<int>(); foreach(var record in records) { bool isRecordInIdSet = ids.Contains(record.id); } Likewise, if the HashSet was instead a list, would that make the complexity to O(n^2)? var ids = new List<int>(); foreach(var record in records) { bool isRecordInIdSet = ids

What is the time complexity of Javascript Array.reduce() and Array.find()?

♀尐吖头ヾ 提交于 2020-07-29 13:08:52
问题 I am trying to return an array of indexes of values that add up to a given target. I am trying to solve it the fastest way I can! Examples: sumOfTwo([1, 2, 4, 4], 8) // => [2, 3] sumOfTwo([1, 2, 3, 9], 8) // => [] So first I tried a simple brute-force. (Time complexity: O(n^2) ) function sumOfTwo(arr, target) { for (let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) { if (arr[i] + arr[j] === target) { return [i, j]; } } } return []; } Then I tried: (Time complexity:

What is the BigO of Swift's String.count?

瘦欲@ 提交于 2020-07-29 03:19:45
问题 When swift uses String.count is it: O(n) where each time we call it we iterate through the entire String in order to count it or O(1) where swift has previously stored the size of this array and simply accesses it. 回答1: It is definitely O(n) . From the Swift Book: As a result, the number of characters in a string can't be calculated without iterating through the string to determine its extended grapheme cluster boundaries. If you are working with particularly long string values, be aware that

How to find kth smallest integer in an unsorted array without sorting the array?

心已入冬 提交于 2020-07-05 03:34:06
问题 So I am given an (unsorted) array A of N distinct integers, I am trying to implement a divide-and-conquer algorithm to find the Kth smallest element (K ≤ N) in the array (i.e. it would be the overall smallest if K=1). The algorithm returns the value of the Kth smallest element in the array. I need it to run in O(N) time in the average case. Could anyone give me some hints? 回答1: Sephy, I'm going to walk this through very carefully. You always have to be careful when helping people with

Time Complexity in singly link list

女生的网名这么多〃 提交于 2020-06-24 14:45:06
问题 I am studying data-structure: singly link list. The website says singly linked list has a insertion and deletion time complexity of O(1) . Am I missing something? website link I do this in C++, and I only have a root pointer . If I want to insert at the end, then I have to travel all the way to the back, which means O(n) . 回答1: The explanation for this is, that the big O notation in the linked table refers to the function implementation itself, not including the list traversal to find the

Time Complexity in singly link list

霸气de小男生 提交于 2020-06-24 14:44:49
问题 I am studying data-structure: singly link list. The website says singly linked list has a insertion and deletion time complexity of O(1) . Am I missing something? website link I do this in C++, and I only have a root pointer . If I want to insert at the end, then I have to travel all the way to the back, which means O(n) . 回答1: The explanation for this is, that the big O notation in the linked table refers to the function implementation itself, not including the list traversal to find the

What is complexity of this code? (Big O) Is that linear?

99封情书 提交于 2020-06-17 09:41:33
问题 for(int i=0; i<array.length -1; i++){ if(array[i] > array[i+1]){ int temp = array[i]; array[i] = array[i+1]; array[i+1]=temp; i=-1; } } I think the code sorts the input array and that its worst case complexity is O(n). What is the correct big-O complexity of this code? 回答1: It's O(n^3), and it's an inefficient version of bubble sort. The code scans through the array looking for the first adjacent pair of out-of-order elements, swaps them, and then restarts from the beginning of the array. In

What is complexity of this code? (Big O) Is that linear?

荒凉一梦 提交于 2020-06-17 09:40:09
问题 for(int i=0; i<array.length -1; i++){ if(array[i] > array[i+1]){ int temp = array[i]; array[i] = array[i+1]; array[i+1]=temp; i=-1; } } I think the code sorts the input array and that its worst case complexity is O(n). What is the correct big-O complexity of this code? 回答1: It's O(n^3), and it's an inefficient version of bubble sort. The code scans through the array looking for the first adjacent pair of out-of-order elements, swaps them, and then restarts from the beginning of the array. In

What is constant factors and low-order term in algorithms?

谁都会走 提交于 2020-05-23 02:53:49
问题 In the following video, there is an explanation of asymptotic analysis: https://class.coursera.org/algo-004/lecture/169 But I can't understand what is "low-order term" and "constant factor" itself? (it is at the 4th minute of the video). The merge sort is 6n*log2(n)+6n . Why 6n is the low-order term and 6 is constant factor in here? Do these terms have concrete definition? 回答1: Lower-order term: "Order" here refers to the order of magnitude. The concept is easy to understand and explain when