Java icon indicating copy to clipboard operation
Java copied to clipboard

Document `CalculateMaxOfMin`

Open mvouzikis opened this issue 1 year ago • 6 comments

…teMaxofMin.java file

  • [ ] I have read CONTRIBUTING.md.
  • [ ] This pull request is all my own work -- I have not plagiarized it.
  • [ ] All filenames are in PascalCase.
  • [ ] All functions and variable names follow Java naming conventions.
  • [ ] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • [ ] All new code is formatted with clang-format -i --style=file path/to/your/file.java

mvouzikis avatar Feb 05 '24 12:02 mvouzikis

@mvouzikis thanks for your contribution, the comments look good. However, there is a bug in the existing/original code. Since you have it fresh - would you like to fix it?

As far as I see, the current code simply returns the maximum of the input array (the test cases are wrong!). Consider the input {1, 1, 5, 1}. The sub arrays are:

{1} -> min: 1,
{1, 5} -> min: 1,
{1, 5, 1} -> min: 1,

So the result should be max{1, 1, 1} = 1.

@siriak, @BamaCharanChhandogi: please have a look

vil02 avatar Feb 05 '24 20:02 vil02

Isn't it always equal to the first element? If not, please provide a counterexample.

siriak avatar Feb 05 '24 21:02 siriak

I probably don't understand the problem statement.

siriak avatar Feb 05 '24 21:02 siriak

If no progress is made, I can check it tomorrow run some examples and see where the bug lies

mvouzikis avatar Feb 05 '24 21:02 mvouzikis

@mvouzikis thanks for your contribution, the comments look good. However, there is a bug in the existing/original code. Since you have it fresh - would you like to fix it?

As far as I see, the current code simply returns the maximum of the input array (the test cases are wrong!). Consider the input {1, 1, 5, 1}. The sub arrays are:

{1} -> min: 1,
{1, 5} -> min: 1,
{1, 5, 1} -> min: 1,

So the result should be max{1, 1, 1} = 1.

@siriak, @BamaCharanChhandogi: please have a look

The code is correct. {1,1,5,1}

[1] -> 1
[1, 1] -> 1
[1, 1, 5]  -> 1
[1, 1, 5, 1]  -> 1
[1, 1, 5, 1]  -> 1
[1, 5]  -> 1
[1, 5, 1] -> 1
[5] -> 5
[5, 1] -> 1
[1] -> 1

Output will be: 5(max)

BamaCharanChhandogi avatar Feb 06 '24 03:02 BamaCharanChhandogi

I think this method is finding the maximum of minima of all subarrays (of any length). In this case it is simply the max (because it is enough to consider arrays of length 1), so @BamaCharanChhandogi is correct.

@siriak: if this problem would only consider arrays of the type (python notation) arr[0:1], arr[0:2], arr[0:3], ..., then the result will be always arr[0] (because min(arr[0:n]) >= min(arr[0:n], arr[n]) = min(arr[0:n+1])).

I think that the real problem should be finding maximum of minima of sub arrays of given length.

I suggest to remove current implementation and create a new PR with a correct one. @mvouzikis would you like to do that?

vil02 avatar Feb 06 '24 06:02 vil02

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution!

github-actions[bot] avatar Mar 08 '24 00:03 github-actions[bot]

Please reopen this pull request once you have made the required changes. If you need help, feel free to ask in our Discord server or ping one of the maintainers here. Thank you for your contribution!

github-actions[bot] avatar Mar 16 '24 00:03 github-actions[bot]