complexity-theory

Prove that n! = O(n^n)

耗尽温柔 提交于 2020-01-01 12:01:22
问题 How can I prove that n! = O(n^n)? 回答1: I assume that you want to prove that the function n! is an element of the set O(n^n) . This can be proven quite easily: Definition: A function f(n) is element of the set O(g(n)) if there exists a c>0 such that there exists a m such that for all k>m we have that f(k)<=c*g(k) . So, we have to compare n! against n^n . Let's write them one under another: n! = n * (n-1) * (n-2) * (n-3) * ... * 3 * 2 * 1 n^n = n * n * n * n * ... * n * n * n As you can see,

Optimum search for k minimum values in unsorted list of integers

时间秒杀一切 提交于 2019-12-31 10:52:06
问题 I was just interviewed with a question, and I'm curious what the answer ought to be. The problem was, essentially: Say you have an unsorted list of n integers. How do you find the k minimum values in this list? That is, if you have a list of [10, 11, 24, 12, 13] and are looking for the 2 minimum values, you'd get [10, 11]. I've got an O(n*log(k)) solution, and that's my best, but I'm curious what other people come up with. I'll refrain from polluting folks brains by posting my solution and

Optimum search for k minimum values in unsorted list of integers

◇◆丶佛笑我妖孽 提交于 2019-12-31 10:51:54
问题 I was just interviewed with a question, and I'm curious what the answer ought to be. The problem was, essentially: Say you have an unsorted list of n integers. How do you find the k minimum values in this list? That is, if you have a list of [10, 11, 24, 12, 13] and are looking for the 2 minimum values, you'd get [10, 11]. I've got an O(n*log(k)) solution, and that's my best, but I'm curious what other people come up with. I'll refrain from polluting folks brains by posting my solution and

What is the time complexity of the following algorithm? [duplicate]

江枫思渺然 提交于 2019-12-31 05:40:31
问题 This question already has answers here : How to find time complexity of an algorithm (9 answers) Closed 2 years ago . can someone tell me what is the time complexity of this algorithm? keep in mind: the second method (findMax) - run on the array based on the index that it gets, means that the method (findMax) doesn't run on all the array every time. I think that the time complexity of this algorithm is O(n) but maybe I'm wrong. public class Q2 { public static int[] replace(int []a) { for(int

Where is the flaw in my algorithm for consolidating gold mines?

随声附和 提交于 2019-12-31 03:47:14
问题 The setup is that, given a list of N objects like class Mine { public int Distance { get; set; } // from river public int Gold { get; set; } // in tons } where the cost of moving the gold from one mine to the other is // helper function for cost of a move Func<Tuple<Mine,Mine>, int> MoveCost = (tuple) => Math.Abs(tuple.Item1.Distance - tuple.Item2.Distance) * tuple.Item1.Gold; I want to consolidate the gold into K mines. I've written an algorithm, thought it over many times, and don't

Java Big O notation of 3 nested loops of log(n)

穿精又带淫゛_ 提交于 2019-12-30 18:43:29
问题 What would the Big O notation be for the following nested loops? for (int i = n; i > 0; i = i / 2){ for (int j = n; j > 0; j = j / 2){ for (int k = n; k > 0; k = k / 2){ count++; } } } My thoughts are: each loop is O(log2(n)) so is it as simple as multiply O(log2(n)) * O(log2(n)) * O(log2(n)) = O(log2(n)^3) 回答1: Yes, that is correct. One way to figure out the big-O complexity of nested loops whose bounds do not immediately depend on one another is to work from the inside out. The innermost

Find the unduplicated element in a sorted array

巧了我就是萌 提交于 2019-12-30 18:26:57
问题 Source : Microsoft Interview Question Given a sorted array, in which every element is present twice except one which is present single time, we need to find that element. Now a standard O(n) solution is to do a XOR of list, which will return the unduplicated element (since all duplicated elements cancel out.) Is it possible to solve this more quickly if we know the array is sorted? 回答1: Yes, you can use the sortedness to reduce the complexity to O(log n) by doing a binary search. Since the

Find the unduplicated element in a sorted array

 ̄綄美尐妖づ 提交于 2019-12-30 18:26:07
问题 Source : Microsoft Interview Question Given a sorted array, in which every element is present twice except one which is present single time, we need to find that element. Now a standard O(n) solution is to do a XOR of list, which will return the unduplicated element (since all duplicated elements cancel out.) Is it possible to solve this more quickly if we know the array is sorted? 回答1: Yes, you can use the sortedness to reduce the complexity to O(log n) by doing a binary search. Since the

Big O Notation for Nested Loop

假如想象 提交于 2019-12-30 07:45:21
问题 I am trying to find time complexity for this Code. for (int i = 0; i <= n - 1; i++) for (int j = i + 1; j <= n - 1; j++) for (int k = j + 1; k <= n - 1; k++) My Attempt: We can write this loop in the form: for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) Now Big Oh of this loop is O(n^5). Am I correct or doing some mistake? 回答1: The first variant of your code with a counter added: int count = 0 for (int i = 0; i <= n - 1; i++) for (int j = i + 1; j <= n -

Big O Notation for Nested Loop

隐身守侯 提交于 2019-12-30 07:45:11
问题 I am trying to find time complexity for this Code. for (int i = 0; i <= n - 1; i++) for (int j = i + 1; j <= n - 1; j++) for (int k = j + 1; k <= n - 1; k++) My Attempt: We can write this loop in the form: for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) Now Big Oh of this loop is O(n^5). Am I correct or doing some mistake? 回答1: The first variant of your code with a counter added: int count = 0 for (int i = 0; i <= n - 1; i++) for (int j = i + 1; j <= n -