data-structures

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

How can I implement a generic struct that manages key-value pairs for UserDefaults in Swift?

十年热恋 提交于 2021-02-08 04:44:30
问题 How would one implement a struct that manages UserDefaults mappings in Swift? Right now I have some computed properties a, b, c, d of different types and corresponding keys that look like this: enum UserDefaultsKeys { a_key b_key ... } var a: String { get { UserDefaults.standard.string(forKey: UserDefaultsKeys.a_key.rawValue) } set { UserDefaults.standard.set(newValue, forKey: UserDefaultsKeys.a_key.rawValue) } } var b: Int { get { UserDefaults.standard.integer(forKey: UserDefaultsKeys.b_key

Print ways to reach the n’th stair

让人想犯罪 __ 提交于 2021-02-07 20:32:31
问题 I recently encountered this problem in an interview There are n stairs, a person standing at the bottom wants to reach the top. The person can climb either 1 stair or 2 stairs at a time. Print all possible ways person can reach the top. For example, n=4 Output: 1 2 3 4 1 2 4 1 3 4 2 3 4 2 4 But I couldn't code this properly. How to code up solution for this? 回答1: To print the number of ways, you can first understand how to calculate the number of ways, and adjust it so each "count" will print

Numbers of common distinct difference

拟墨画扇 提交于 2021-02-07 20:22:13
问题 Given two array A and B. Task to find the number of common distinct (difference of elements in two arrays). Example : A=[3,6,8] B=[1,6,10] so we get differenceSet for A differenceSetA=[abs(3-6),abs(6-8),abs(8-3)]=[3,5,2] similiarly differenceSetB=[abs(1-6),abs(1-10),abs(6-10)]=[5,9,4] Number of common elements=Intersection :{differenceSetA,differenceSetB}={5} Answer= 1 My approach O(N^2) int commonDifference(vector<int> A,vector<int> B){ int n=A.size(); int m=B.size(); unordered_set<int>

Complexity of different operations on different data structures according to the Big-O notation

落爺英雄遲暮 提交于 2021-02-07 17:11:00
问题 I was reading about big O notation in java programming. I found the following table it shows different big O for different data structures. http://bigocheatsheet.com/ My questions are: If I want to delete an item in an array, is it O(n^2) ? (search and delete) If I want to delete an item in a stack, is it O(n) ? Which one is more effective, is it a single linked list or double single list? In what case is that the insert operation is O(1) or O(n) in a hash table? If I want to delete an item

Complexity of different operations on different data structures according to the Big-O notation

时光毁灭记忆、已成空白 提交于 2021-02-07 17:05:56
问题 I was reading about big O notation in java programming. I found the following table it shows different big O for different data structures. http://bigocheatsheet.com/ My questions are: If I want to delete an item in an array, is it O(n^2) ? (search and delete) If I want to delete an item in a stack, is it O(n) ? Which one is more effective, is it a single linked list or double single list? In what case is that the insert operation is O(1) or O(n) in a hash table? If I want to delete an item

Fastest algorithm for Kth smallest Element (or median) finding on 2 Dimensional Array?

筅森魡賤 提交于 2021-02-07 14:38:54
问题 I see a lot of SO topics on related topics but none of them provides the efficient way. I want to find the k-th smallest element (or median) on 2D array [1..M][1..N] where each row is sorted in ascending order and all elements are distinct. I think there is O(M log MN) solution, but I have no idea about implementation. (Median of Medians or Using Partition with Linear Complexity is some method but no idea any more...). This is an old Google interview question and can be searched on Here. But

Fastest algorithm for Kth smallest Element (or median) finding on 2 Dimensional Array?

こ雲淡風輕ζ 提交于 2021-02-07 14:38:36
问题 I see a lot of SO topics on related topics but none of them provides the efficient way. I want to find the k-th smallest element (or median) on 2D array [1..M][1..N] where each row is sorted in ascending order and all elements are distinct. I think there is O(M log MN) solution, but I have no idea about implementation. (Median of Medians or Using Partition with Linear Complexity is some method but no idea any more...). This is an old Google interview question and can be searched on Here. But

Specific Graph and need to more Creative solution

巧了我就是萌 提交于 2021-02-07 08:55:37
问题 Directed Graph (|V|=a, |E|=b) is given. each vertexes has specific weight. we want for each vertex (1..a) find a vertex with maximum weight that can be reachable from that vertex. Update 1: one nice answer is prepare by @Paul in O(b + a log a). but I search for O(a + b) algorithms, if any? Is there any different efficient or fastest any other ways for doing it? 回答1: Yes, it's possible to modify Tarjan's SCC algorithm to solve this problem in linear time. Tarjan's algorithm uses two node

Data structure with amortized O(1) delete and O(log n) search

萝らか妹 提交于 2021-02-07 08:29:29
问题 I need a data structure that supports two operations - delete and search. Now, the delete operation should run in amortized O(1) time, while search should run in O(log n) time. Search operation should work as follows: look for value specified and if it is here, return value itself. Otherwise, return the nearest greater value (return inorder successor). What could this data structure be? 回答1: It could be a pair of data structures: binary search tree, holding values hash table, holding pointers