time-complexity

hashmap containsKey complexity

隐身守侯 提交于 2020-01-01 14:22:37
问题 I have a method i wrote to find the duplicates in a List. It works fine but im concerned about the complexity of using containsKey. When we use containsKey we have to compute a hash function for every key and then compare against each with our search item, right ? So wouldn't the complexity be O(n) ? Here is the function: public void findDup(List<String> list){ HashMap<String,Integer> map = new HashMap<>(); int pos=0; for(String s: list){ if(map.containsKey(s)){ Log.v("myapp","duplicate found

Time Complexity of InOrder Tree Traversal of Binary Tree O(n)?

一笑奈何 提交于 2020-01-01 05:41:27
问题 public void iterativePreorder(Node root) { Stack nodes = new Stack(); nodes.push(root); Node currentNode; while (!nodes.isEmpty()) { currentNode = nodes.pop(); Node right = currentNode.right(); if (right != null) { nodes.push(right); } Node left = currentNode.left(); if (left != null) { nodes.push(left); } System.out.println("Node data: "+currentNode.data); } } Source: Wiki Tree Traversal Is the time complexity going to be O(n)? And is the time complexity going to be the same if it was done

Print all unique combination of factors of a given number

守給你的承諾、 提交于 2020-01-01 02:25:29
问题 What is the most efficient algorithm to print all unique combinations of factors of a positive integer. For example if the given number is 24 then the output should be 24*1 12*2 8*3 6*4 6*2*2 4*3*2 3*2*2*2 Here notice that when 6*4 gets printed then 4*6 doesn't get printed. So basically it's a problem of taking unique subsets without considering the order (one way to look at the problem). But the objective is to have a function that runs the fastest, so storing the factors in a data structure

Find a number with even number of occurrences

↘锁芯ラ 提交于 2019-12-31 22:08:19
问题 Given an array where number of occurrences of each number is odd except one number whose number of occurrences is even. Find the number with even occurrences. e.g. 1, 1, 2, 3, 1, 2, 5, 3, 3 Output should be: 2 The below are the constraints: Numbers are not in range. Do it in-place. Required time complexity is O(N). Array may contain negative numbers. Array is not sorted. With the above constraints, all my thoughts failed: comparison based sorting, counting sort, BST's, hashing, brute-force. I

Find a number with even number of occurrences

淺唱寂寞╮ 提交于 2019-12-31 22:08:01
问题 Given an array where number of occurrences of each number is odd except one number whose number of occurrences is even. Find the number with even occurrences. e.g. 1, 1, 2, 3, 1, 2, 5, 3, 3 Output should be: 2 The below are the constraints: Numbers are not in range. Do it in-place. Required time complexity is O(N). Array may contain negative numbers. Array is not sorted. With the above constraints, all my thoughts failed: comparison based sorting, counting sort, BST's, hashing, brute-force. I

Algorithm with O(n log n) time and O(1) space complexity vs O(n) time and O(n) space complexity

廉价感情. 提交于 2019-12-31 12:50:57
问题 I am curious to know which algorithm is better : Algorithm with O(n log n) time and O(1) space complexity Algorithm with O(n) time and O(n) space complexity Most of the algorithm which are solved in O(n long n) time and constant space can be solved in O(n) time by paying penalty in terms of space. Which algorithm is better ? How do I decide between these two parameters ? Example : Array Pair Sum Can be solved in O(n logn) time by sorting Can be solved using hash maps in O(n) time but with O(n

binary search vs binary search tree

蓝咒 提交于 2019-12-31 08:45:10
问题 What is the benefit of a binary search tree over a sorted array with binary search? Just with mathematical analysis I do not see a difference, so I assume there must be a difference in the low-level implementation overhead. Analysis of average case run time is shown below. Sorted array with binary search search: O(log(n)) insertion: O(log(n)) (we run binary search to find where to insert the element) deletion: O(log(n)) (we run binary search to find the element to delete) Binary search tree

binary search vs binary search tree

喜欢而已 提交于 2019-12-31 08:45:00
问题 What is the benefit of a binary search tree over a sorted array with binary search? Just with mathematical analysis I do not see a difference, so I assume there must be a difference in the low-level implementation overhead. Analysis of average case run time is shown below. Sorted array with binary search search: O(log(n)) insertion: O(log(n)) (we run binary search to find where to insert the element) deletion: O(log(n)) (we run binary search to find the element to delete) Binary search tree

Will Arrays.sort() increase time complexity and space time complexity?

↘锁芯ラ 提交于 2019-12-31 08:38:51
问题 There is an array related problem, the requirement is that time complexity is O(n) and space complexity is O(1). If I use Arrays.sort(arr) , and use a for loop to one pass loop, for example: public static int hello(int[]A){ Arrays.sort(A); for(int i=0;i<A.length;i++){ .................... } return ....; } So the loop will cost O(n) time. My question is: will Arrays.sort() cost more time? If I use Arrays.sort() , will this time complexity still be O(n)? And will Arrays.sort() cost more space?

Time Complexity for an algorithm

帅比萌擦擦* 提交于 2019-12-31 04:40:09
问题 Am I correct in my explanation when calculating the time complexity of the following algorithm? A HashSet, moduleMarksheetFiles, is being used to add the files that contain the moduleName specified. for (File file: marksheetFiles){ while(csvReader.readRecord()){ String moduleName = csvReader.get(ModuleName); if (moduleName.equals(module)){ moduleMarksheetFiles.add(file); } } } Let m be the number of files Let k be the average number of records per file. As each file is added only once because