-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
"Maximum Element After Decreasing and Rearranging" solution
- Loading branch information
Showing
2 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution: | ||
def maximumElementAfterDecrementingAndRearranging( | ||
self, arr: list[int] | ||
) -> int: | ||
arr = sorted(arr) | ||
|
||
last = 0 | ||
|
||
for x in arr: | ||
if x > last + 1: | ||
last = last + 1 | ||
else: | ||
last = x | ||
|
||
return last |
18 changes: 18 additions & 0 deletions
18
tests/test_maximum_element_after_decreasing_and_rearranging.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pytest | ||
|
||
from src.maximum_element_after_decreasing_and_rearranging import Solution | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"arr,expected", | ||
( | ||
([2, 2, 1, 2, 1], 2), | ||
([100, 1, 1000], 3), | ||
([1, 2, 3, 4, 5], 5), | ||
), | ||
) | ||
def test_solution(arr, expected): | ||
assert ( | ||
Solution().maximumElementAfterDecrementingAndRearranging(arr) | ||
== expected | ||
) |