Skip to content

2774. Array Upper Bound 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
declare global {
  interface Array<T> {
    upperBound(target: number): number;
  }
}

Array.prototype.upperBound = function (target): number {
  let l = 0;
  let r = this.length - 1;
  while (l < r) {
    const m = (l + r + 1) >> 1;
    if (this[m] > target) {
      r = m - 1;
    } else {
      l = m;
    }
  }
  return this[l] == target ? l : -1;
};