5
4

More than 5 years have passed since last update.

Rubyでlower_bound

Posted at

C++のSTLにある、その値以上の要素のインデックスを見つける関数

lower_bound.rb
def lower_bound(array, value)
  left = -1;
  right = array.length;
  while left + 1 < right
    mid = left + (right - left) / 2;
    if array[mid] >= value
      right = mid
    else
      left = mid
    end
  end
  right
end

参考

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4