Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
That's cool.. but I have the same question that other, why is the reason to sort?
makes sense ty
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
Because now there are better alternatives:
Map.of(...)
. But when that code was written, it wasn't a thing yet.edit: well, at that time, computeIfAbsent would have been an option, tho, iirc.
Could you elaborate? Why shouldn't I used instance initialization blocks?
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
Good, but I don't know why's that considered a good practice. You should not play with IIBs of standart classes
Almost. If itemCount could be zero, then that fails. Use
I don't remember if zero is allowed in the statement, but it should just be a habit in Java, C and C++ to avoid negative arguments to %.
exelent
The best possible time complexity for this task is linear.
This implementation has linear-logarithmic time complexity and it also corrupts the initial data.
Conciseness can never justify poor performance. Sorry, but it's definitely not the best practice.
You are wrong - to sort primitives Java uses the so-called Dual-Pivot Quicksort. This algorithm is based on quicksort, as obvious from its name, but in fact, comprises many technics to ensure linear-logarithmic performance even with the massive data.
Your solution is computationally intensive as well, it performs iteration through the entire array B for every element in array A. That gives O(n * m) time complexity.
And this task can be solved in a linear time (try to refine your algorithm, or you may look at my solution).
Additionally, your code has a side effect - it mutates array B.
this solution returns
pos=[3, 7, 10], peaks=[6, 3, 4]
and notpos=[3, 7], peaks=[6, 3]
, for this input.This solution gives a failure to this arr 3,2,3,6,4,1,2,3,2,1,4,3
it says it should return pos=[3, 7], peaks=[6, 3]
but this actually returns pos=[3, 7, 10], peaks=[6, 3, 4]
which I think is ture because if Iam not wrong 4 here is a peak!!
Loading more items...