time-complexity

Running Time Complexity of O (n / 2)

耗尽温柔 提交于 2020-06-16 03:15:19
问题 I once understood this but not anymore. Lets say I have an algorithm that will return the number in the middle of an array. for (int i = 0; i < nums.length; i++) { if (i == nums.length / 2) return nums[i]; } The worst case of this will always be O (n / 2) right? There is no worse case than this. But how come we just conclude that it is O(n) ? 回答1: Big O time complexity is not about measuring the actual time an algorithm will take, it instead specifies what variables the time complexity is

Running Time Complexity of O (n / 2)

放肆的年华 提交于 2020-06-16 03:15:18
问题 I once understood this but not anymore. Lets say I have an algorithm that will return the number in the middle of an array. for (int i = 0; i < nums.length; i++) { if (i == nums.length / 2) return nums[i]; } The worst case of this will always be O (n / 2) right? There is no worse case than this. But how come we just conclude that it is O(n) ? 回答1: Big O time complexity is not about measuring the actual time an algorithm will take, it instead specifies what variables the time complexity is

Finding two non-subsequent elements in array which sum is minimal

此生再无相见时 提交于 2020-06-09 11:45:28
问题 Intro: As far as I could search, this question wasn't asked in SO yet. This is an interview question. I am not even specifically looking for a code solution, any algorithm/pseudocode will work. The problem: Given an integer array int[] A and its size N , find 2 non-subsequent (can't be adjacent in the array) elements with minimal sum. Also the answer must not contain the first or last elements (index 0 and n-1 ). Also the solution should be in O(n) time and space complexity. E.g. when A = [5,

Find all possible substring in fastest way [duplicate]

瘦欲@ 提交于 2020-05-25 03:37:09
问题 This question already has answers here : Generate all unique substrings for given string (14 answers) Closed 2 years ago . For String A = "abcd" then answer should be {a,ab,abc,abcd,b,bc,bcd,c,cd,d} To find all the substring I have used following method for (int i = 0; i < A.length(); i++) { for (int j = i+1; j <= A.length(); j++) { System.out.println(A.substring(i,j)); } } But according to my understanding the complexity goes to O(N^2) . Can we make it faster? I referred previous question

Find all possible substring in fastest way [duplicate]

五迷三道 提交于 2020-05-25 03:36:31
问题 This question already has answers here : Generate all unique substrings for given string (14 answers) Closed 2 years ago . For String A = "abcd" then answer should be {a,ab,abc,abcd,b,bc,bcd,c,cd,d} To find all the substring I have used following method for (int i = 0; i < A.length(); i++) { for (int j = i+1; j <= A.length(); j++) { System.out.println(A.substring(i,j)); } } But according to my understanding the complexity goes to O(N^2) . Can we make it faster? I referred previous question

Given a collection of integers and threshold value T, divide the collection into as many groups as possible whose sum >= T

安稳与你 提交于 2020-05-12 07:56:07
问题 Given a collection of integers and threshold value T, divide the collection into as many groups as possible whose sum >= T. The remaining integers (whose sum < T, so another group cannot be formed) should be left outside of the groups. Constraints: length of the list <= 1,000 values and T <= 1,000,000 Is there an algorithm for this problem in polynomial time? For example given [25,25,25,50,50,50,10] and a threshold T = 70 it should return: [25,50] [25,50] [25,50] Remaining: [10] Selecting [25

Knights tour backtracking lasts too long

萝らか妹 提交于 2020-05-08 19:05:06
问题 How long does it last to solve the knights tour problem with backtracking on an 8x8 board? Because my algo allready computes somehow too long and it seems, like it wont finish. But when I try a 6x6, or 5x5 board, it finishes succesfuly. the code: class KnightsTour{ private boolean[][] board; private int count, places; private static final Point[] moves = new Point[]{ new Point(-2, -1), new Point(-2, 1), new Point(2, -1), new Point(2, 1), new Point(-1, -2), new Point(-1, 2), new Point(1, -2),

Java StringBuilder.setLength() - is time complexity O(1)?

被刻印的时光 ゝ 提交于 2020-05-08 03:15:46
问题 I'm planning to perform lots of deletes of the last character in StringBuilders. The solution to use sb.setLength(sb.length() - 1); looks good to me. However, since these deletions will be in a loop, I need to know complexity of it. The way I understand it is that this operation simply decrements some private attribute of my StringBuilder object and does not perform any copying/cloning/duplicating of the characters themselves, thus it is O(1) in time and should work fast. Am I right? 回答1:

Find the median in two AVL BSTs in O(log(n))

只谈情不闲聊 提交于 2020-04-16 04:54:54
问题 So im attempting the classic problem of finding the median in two AVL BST's in O(log(n)) time. Given two AVLs, with a combined size of n (integers are distributed randomly i.e. one may have n-1 elements with the other having 1). I am currently trying to come up with an algorithm to solve this. Each node, besides holding its integer value and pointers to its parent and child nodes, holds the size of the subtree for the subtree rooted at it. I've approached it by initially finding the number of

Find the median in two AVL BSTs in O(log(n))

心不动则不痛 提交于 2020-04-16 04:54:44
问题 So im attempting the classic problem of finding the median in two AVL BST's in O(log(n)) time. Given two AVLs, with a combined size of n (integers are distributed randomly i.e. one may have n-1 elements with the other having 1). I am currently trying to come up with an algorithm to solve this. Each node, besides holding its integer value and pointers to its parent and child nodes, holds the size of the subtree for the subtree rooted at it. I've approached it by initially finding the number of