Hot answers tagged

5

Indentation You indentation is inconsistent. int count =0; is not indented the same amount as the following line, despite being logically at the same level. Your indentation amount is not consistent. You should use the same increment for each additional level. Assuming your first indentation was 2 spaces (instead of both 1 & 2), your next indentation ...


4

A couple of comments on your answer: Code style If you've already stored the pattern.length(), why keep calling the method? Just be consistent and use that variable. Same goes for str.length(). I'd just store it and use it. I would store str.length()-(pattern.length()-1 in a variable an name it something like numOfCasesToTest, to make it clearer. Check ...


3

Your algorithm is correct, but the performance can be improved to O(n) avoiding the sorting of the n elements array with O(n log n) cost. The final result is equal to max + 1 - min - n where min and max are respectively the minimum and the maximum element in the array, so your method can be rewritten like below : private static int makeArrayConsecutive2(int[]...


3

You need to be observant of the bounds in the problem description, in particular they allow k=n which means your worst case time complexity is not constant k times O(n), it's actually O(kn), and when k is approximately n, then it turns into O(n^2). I solved a similar problem a while ago and did some interesting benchmarking. I'm not sure why you don't want ...


2

Your indentation has gone wrong here, and it's confusing: if (sl.equalsIgnoreCase("M")) { reliefDays = "SUN MON"; if (rdLength == 3) { reliefDays += " TUE"; } if (rdLength == 4) { reliefDays += " WED"; } ...


1

As far as I can understand, you're a beginner lacking some knowledge. But the job is done well. Some considerations: Comments A perfect code should use identifier names to avoid comments; your code have good names and one unneeded comment at the beginning. But, on the other hand, you have good task description for comments. You can break it into comments, so ...


1

I want to add to the other answers and provide a different more drastic suggestion and a more generic and (I think) clean approach. This is the perfect example where a nice class hierarchy shines. This is also the perfect example where to use visitors. I think in this case everything should be an expression. First, the problem can be broken down in separate ...


1

It is possible to achieve a much better performance in a brutal way result with the use of auxiliary sorted k size array obtained copying and ordering the first k elements from your nums array: int length = nums.length; int lastIndex = k - 1; int[] arr = Arrays.copyOf(nums, k); Arrays.sort(arr); Once you initialized the arr array for every remaining ...


1

Naming MathResult; I was wondering why the prefix Math in MathResult is important. Are there other types of results? If not, we can keep it simple and only name it Result. Operation; As @Ralf Kleberhoff already points out, would Expression a better fit. The grouping of operations and operands is an expression, where an operation is a function such as ...


1

(I've read the book, solved the problems and passed the interview) The purpose of this exercise is to use the properties of lists to your advantage, and show your understanding of them; as well as teaching you to ask the right questions: How big can the numbers be? Will they overflow an int/long? Can they be negative? Will both of the lists fit in memory? Is ...


1

Input doesn't need to be a separate source file when you can use an anonymous inner class that implements KeyListener. When you do this, it becomes easier to put the variable paused where it belongs (i.e. not in the class handling the input). See my edits below for how to do it. Similar thing with Window - it doesn't need to be a separate file, and we don't ...


1

Welcome to the site! This looks good so far, here's a couple of smaller tips: Board.java The cellsize doesn't change, so why not make it a constant like this: public static final int CELLSIZE = 4; This also makes the getter obsolete, as you can access that field using Board.CELLSIZE getNewState() uses a boolean state. Renaming that to something like ...


Only top voted, non community-wiki answers of a minimum length are eligible