-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve: now min & max check returns an std::optional
- Loading branch information
Showing
1 changed file
with
16 additions
and
19 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 |
---|---|---|
@@ -1,42 +1,39 @@ | ||
/*8< | ||
@Title: | ||
@Title: Max \& Min Check | ||
Max \& Min Check | ||
@Description: Returns the min/max value in range | ||
[l, r] that satisfies the lambda function check, | ||
if there is no such value the 'nullopt' is | ||
returned. | ||
@Description: | ||
@Usage: check must be a function that receives | ||
an integer and return a boolean. | ||
Returns the min/max value in range [l, r] that | ||
satisfies the lambda function check, if there | ||
is no such value the max/min possible value | ||
for that type will be returned. | ||
@Time: | ||
$O(\log{l-r+1})$ | ||
@Time: $O(\log{l-r+1})$ | ||
>8*/ | ||
|
||
template <typename T> | ||
T maxCheck(T l, T r, function<bool(T)> check) { | ||
T best = numeric_limits<T>::min(); | ||
optional<T> maxCheck(T l, T r, auto check) { | ||
optional<T> ret; | ||
while (l <= r) { | ||
T m = midpoint(l, r); | ||
if (check(m)) | ||
chmax(best, m), l = m + 1; | ||
ret ? chmax(ret, m) : ret = m, l = m + 1; | ||
else | ||
r = m - 1; | ||
} | ||
return best; | ||
return ret; | ||
} | ||
|
||
template <typename T> | ||
T minCheck(T l, T r, function<bool(T)> check) { | ||
T best = numeric_limits<T>::max(); | ||
optional<T> minCheck(T l, T r, auto check) { | ||
optional<T> ret; | ||
while (l <= r) { | ||
T m = midpoint(l, r); | ||
if (check(m)) | ||
chmin(best, m), r = m - 1; | ||
ret ? chmin(ret, m) : ret = m, r = m - 1; | ||
else | ||
l = m + 1; | ||
} | ||
return best; | ||
return ret; | ||
} |