time-complexity

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

How String.charAt(int i) is implemented in Java?

北城余情 提交于 2021-02-07 11:49:13
问题 If I want to check every char in a String using String.charAt(int i) , would it count from start every time or it is converted to an array automatically and get the charAt index directly? Would it be more efficient if I create a char array by String.toCharArray() and then go through the array by index? Can I check this up in JavaDoc? Where? 回答1: The JRE is mostly open source. You can download a zip file here or browse online with websites like grepcode. String.charAt: public char charAt(int

What's the worst-case valid sudoku puzzle for simple backtracking brute force algorithm?

那年仲夏 提交于 2021-02-07 09:48:59
问题 The " simple/naive backtracking brute force algorithm", "Straightforward Depth-First Search" for sudoku is commonly known and implemented. and no different implementation seems to exist. (when i first wrote this question.. i wanted to mean we could completely standardize it, but the wording is bad..) This guy has described the algorithm well i think: https://stackoverflow.com/a/2075498/3547717 Edit: So let me have it more specified with pseudo code... var field[9][9] set the givens in 'field'

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

Count of co-prime pairs from two arrays in less than O(n^2) complexity

£可爱£侵袭症+ 提交于 2021-02-06 03:46:43
问题 I came to this problem in a challenge. There are two arrays A and B both of size of N and we need to return the count of pairs (A[i],B[j]) where gcd(A[i],B[j])==1 and A[i] != B[j] . I could only think of brute force approach which exceeded time limit for few test cases. for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(__gcd(a[i],b[j])==1) { printf("%d %d\n", a[i], b[j]); } } } Can you advice time efficient algorithm to solve this. Edit: Not able to share question link as this was from a

What is the complexity of this function with nested loops?

别说谁变了你拦得住时间么 提交于 2021-02-05 09:36:25
问题 What is the complexity of this code? public class test5{ public static void main(String[] args) { int n = Integer.parseInt(args[0]); for (int i = 1; i<=n; i++) { for (int j = 1; j<=i; j++) { System.out.print ("*"); } System.out.println(); } for (int i = n; i>=1; i--) { for (int j = 1; j<=i; j++) { System.out.print ("*"); } System.out.println(); } } } My assumption is that it will take O(n^2) operations because n*(n/2) + n*(n/2). Am I right? 回答1: You are correct, a tight upper asymptotic bound

What is the complexity of this function with nested loops?

时光总嘲笑我的痴心妄想 提交于 2021-02-05 09:34:49
问题 What is the complexity of this code? public class test5{ public static void main(String[] args) { int n = Integer.parseInt(args[0]); for (int i = 1; i<=n; i++) { for (int j = 1; j<=i; j++) { System.out.print ("*"); } System.out.println(); } for (int i = n; i>=1; i--) { for (int j = 1; j<=i; j++) { System.out.print ("*"); } System.out.println(); } } } My assumption is that it will take O(n^2) operations because n*(n/2) + n*(n/2). Am I right? 回答1: You are correct, a tight upper asymptotic bound

Why is the complexity of simple string concatenation O(n^2)?

自闭症网瘾萝莉.ら 提交于 2021-02-04 21:45:39
问题 I read on several manuals and online sources that the running time of "simple string concatenation" is O(n^2)? The algorithm is this: we take the first 2 strings, create a new string, copy the characters of the 2 original strings in the new string, and repeat this process over and over again until all strings are concatenated. We are not using StringBuilder or similar implementations: just a simple string concatenation. I think the running time should be something like O(kn) where k = number

Why is the complexity of simple string concatenation O(n^2)?

社会主义新天地 提交于 2021-02-04 21:44:07
问题 I read on several manuals and online sources that the running time of "simple string concatenation" is O(n^2)? The algorithm is this: we take the first 2 strings, create a new string, copy the characters of the 2 original strings in the new string, and repeat this process over and over again until all strings are concatenated. We are not using StringBuilder or similar implementations: just a simple string concatenation. I think the running time should be something like O(kn) where k = number