time-complexity

Determining the big-O runtimes of these different loops?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 07:58:22
问题 I have a series of questions in which I need feedback and answers. I will comment as to what I think, this is not a homework assignment but rather preparation for my exam. My main problem is determining the iterations of a loop for different cases. How would go about attempting to figure that out? Evaluate Running time. Q2. for(int i =0 ; i < =n ; i++) // runs n times for(int j =1; j<= i * i; j++) // same reasoning as 1. n^2 if (j % i == 0) for(int k = 0; k<j; k++) // runs n^2 times? <- same

Maximum Sum of Non-adjacent Elements in 1D array

女生的网名这么多〃 提交于 2019-12-20 06:59:55
问题 Given an array of integers, find a maximum sum of non-adjacent elements. For example, inputs [1, 0, 3, 9, 2,-1] should return 10 (1 + 9). there should be avoid 3,2 since 9 is adjacent for 3,2. maximum in array + maximum in Non adjacent elements of 9(maximum element in array). Since maximum element is 9 and next maximum which should be non-adjacent. resulting this 9+1=10(since 1 is maximum in non adjacent element of maximum). I tried this way in O(n)+O(Max_index-1)+O(Array.length-Max_index+2).

Maximum Sum of Non-adjacent Elements in 1D array

断了今生、忘了曾经 提交于 2019-12-20 06:57:51
问题 Given an array of integers, find a maximum sum of non-adjacent elements. For example, inputs [1, 0, 3, 9, 2,-1] should return 10 (1 + 9). there should be avoid 3,2 since 9 is adjacent for 3,2. maximum in array + maximum in Non adjacent elements of 9(maximum element in array). Since maximum element is 9 and next maximum which should be non-adjacent. resulting this 9+1=10(since 1 is maximum in non adjacent element of maximum). I tried this way in O(n)+O(Max_index-1)+O(Array.length-Max_index+2).

Sorting in O(n) time?

白昼怎懂夜的黑 提交于 2019-12-20 06:42:21
问题 I'm stuck on this problem(2 weeks). Any idea of how to approach it?. Let L be a list of n different integer numbers, assume that the elements of x of L are in the range [1,750]. Design a linear ordering algorithm to order the elements of L I already tried with insertion sort. But i'm not sure if my approach is right: Construct an array of bits. Initialize them to zero. Read the input, for each value you see set the respective bit in the array to 1. Scan the array, for each bit set, output the

Prove that an algorithm has a lower bound

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 05:48:15
问题 I'm trying to prove this problem: if an algorithm exists that can determine if a sorted list of n elements has duplicate elements in it, than the number of comparisons needed has a lower bound of n-1 . I'm not quite familiar with lower and higher bounds and I seem to confuse it, can someone help me with an easy to understand proof? 回答1: The problem statement is not rigorous. It should say "the number of comparisons in the worst case ". In a sorted array, there are n-1 relations between pairs

Is the space complexity of this subset algorithm actually O(n)?

十年热恋 提交于 2019-12-20 05:48:09
问题 This is problem 9.4 from Cracking the Coding Interview 5 th The Problem: Write a method to return all the subsets of a set. Here is my solution in Java.(tested it, it works!!!) public static List<Set<Integer>> subsets(Set<Integer> s) { Queue<Integer> copyToProtectData = new LinkedList<Integer>(); for(int member: s) { copyToProtectData.add(member); } List<Set<Integer>> subsets = new ArrayList<Set<Integer>>(); generateSubsets(copyToProtectData, subsets, new HashSet<Integer>()); return subsets;

Why is the runtime of this code O(n^5)?

妖精的绣舞 提交于 2019-12-20 03:19:51
问题 I have been asked to determine the big-O time complexity of this code: function(int n) { for (int i = 0; i < n; i++) { for (int j = i; j < i * i; j++) { if (j % i == 0) { for (int k = 0; k < j; k++) { printf("*"); } } } } } The answer given is O(n 5 ) . Can anyone explain why, or how to determine this? Do we add the number of times innermost loop works, or do we multiply the complexities of each loop? 回答1: One way to analyze code like this is to start from the innermost loop and work outward.

searching in 2d array as O(n) with unsorted rows

最后都变了- 提交于 2019-12-20 01:36:11
问题 I need to write a method that takes 2d array 'int [][] m' and a value 'val' and check if val is in the array in the complexity of O(n) while n defined as the number of rows and m must be squared The array that can use as a parameter for my method must return true for this method: (if it returns true so the array is as requested) public static boolean test(int[][] m) { int n = m.length; for (int r = 0; r < (n - 1); r++) for (int c = 0; c < n; c++) for (int i = 0; i < n; i++) if (m[r][c] > m[r

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); }