code-complexity

Find the length of the longest valid parenthesis sequence in a string, in O(n) time

不问归期 提交于 2021-02-17 10:30:42
问题 My friend ran into a question in an interview and he was told that there is an O(n) solution. However, neither of us can think it up. Here is the question: There is a string which contains just ( and ) , find the length of the longest valid parentheses substring, which should be well formed. For example ")()())" , the longest valid parentheses is ()() and the length is 4. I figured it out with dynamic programming, but it is not O(n). Any ideas? public int getLongestLen(String s) { if (s ==

C, Time complexity of sigma?

╄→尐↘猪︶ㄣ 提交于 2020-08-08 05:15:32
问题 How may I find the time complexity of the following code: (Sorry for adding image, I will re-edit my question once I have access to the laptop) What I have done so far: The first loop iterates n times, the second i times and the third log(i*j) times, So after simplifying I got: Sigma from i=0 to n for i*log i + n * (Sigma from j=0 to i for log i) But why this is equal to O(n^2 log(n))? 回答1: If we look at the outermost 2 loops we observe that there are 1 + 2 + 3 ... + n - 1 iterations. Using

C, Time complexity of sigma?

白昼怎懂夜的黑 提交于 2020-08-08 05:15:29
问题 How may I find the time complexity of the following code: (Sorry for adding image, I will re-edit my question once I have access to the laptop) What I have done so far: The first loop iterates n times, the second i times and the third log(i*j) times, So after simplifying I got: Sigma from i=0 to n for i*log i + n * (Sigma from j=0 to i for log i) But why this is equal to O(n^2 log(n))? 回答1: If we look at the outermost 2 loops we observe that there are 1 + 2 + 3 ... + n - 1 iterations. Using

What is complexity of this code? (Big O) Is that linear?

99封情书 提交于 2020-06-17 09:41:33
问题 for(int i=0; i<array.length -1; i++){ if(array[i] > array[i+1]){ int temp = array[i]; array[i] = array[i+1]; array[i+1]=temp; i=-1; } } I think the code sorts the input array and that its worst case complexity is O(n). What is the correct big-O complexity of this code? 回答1: It's O(n^3), and it's an inefficient version of bubble sort. The code scans through the array looking for the first adjacent pair of out-of-order elements, swaps them, and then restarts from the beginning of the array. In

What is complexity of this code? (Big O) Is that linear?

荒凉一梦 提交于 2020-06-17 09:40:09
问题 for(int i=0; i<array.length -1; i++){ if(array[i] > array[i+1]){ int temp = array[i]; array[i] = array[i+1]; array[i+1]=temp; i=-1; } } I think the code sorts the input array and that its worst case complexity is O(n). What is the correct big-O complexity of this code? 回答1: It's O(n^3), and it's an inefficient version of bubble sort. The code scans through the array looking for the first adjacent pair of out-of-order elements, swaps them, and then restarts from the beginning of the array. In

What would happen if a control flow graph consists of multiple start and/or stop nodes when calculating Cyclomatic Complexity

守給你的承諾、 提交于 2020-03-26 04:03:23
问题 I want to know how will it affect to the Cyclomatic Complexity when having multiple start or stop nodes in a control flow diagram.If you could explain the relationship between Cyclomatic Complexity and Start/Stop nodes it will be a great help. 回答1: • A control flow graph can consist of many starts and stops. But according to McCabe's theory, if it is consist of multiple starts and stops it doesn't satisfy the formula. 来源: https://stackoverflow.com/questions/60485378/what-would-happen-if-a

What would happen if a control flow graph consists of multiple start and/or stop nodes when calculating Cyclomatic Complexity

允我心安 提交于 2020-03-26 04:02:57
问题 I want to know how will it affect to the Cyclomatic Complexity when having multiple start or stop nodes in a control flow diagram.If you could explain the relationship between Cyclomatic Complexity and Start/Stop nodes it will be a great help. 回答1: • A control flow graph can consist of many starts and stops. But according to McCabe's theory, if it is consist of multiple starts and stops it doesn't satisfy the formula. 来源: https://stackoverflow.com/questions/60485378/what-would-happen-if-a

How to properly use try-with-resources in Database connection?

爷,独闯天下 提交于 2020-01-25 07:52:05
问题 I have looked around, but can't seem to find the answer to my question. Here is the context : I have to connect to a Database in my Java program and execute a SQL request that I have no control over and don't know in advance. To do that I use the code below. public Collection<HashMap<String, String>> runQuery(String request, int maxRows) { List<HashMap<String, String>> resultList = new ArrayList<>(); DataSource datasource = null; try { Context initContext = new InitialContext(); datasource =