Reputation: 49
I found SortedSet#max
, but it seems to be O(N). For now I would do something like this:
s = SortedSet.new((1..100000).to_a.reverse)
where reverse
is just to make sure.
min_element = nil; s.each { |x| min_element = x; break }
For maximum element, I would build another SortedSet
with all values multiplied by -1
and do the same thing. Is there a more conventional way to do this?
Upvotes: 2
Views: 344
Reputation: 8898
Monkey patch SortedSet
.
class SortedSet
def max
@keys[-1]
end
def min
@keys[0]
end
end
That's O(1).
Upvotes: 1