code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

02 Find Smallest Letter Greater Than Target

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const findSmallestLetterGreaterThanTarget = (letters, target) => {
  if (letters[0] > target || letters[letters.length - 1] <= target) {
    return letters[0]
  }
  let start = 0
  let end = letters.length - 1
  while (start < end) {
    const mid = Math.trunc((end + start) / 2)
    console.log(start, end, mid, letters[mid])
    if (letters[mid] > target) {
      end = mid
    } else {
      start = mid + 1
    }
  }
  return letters[start]
}

console.log(findSmallestLetterGreaterThanTarget(['c', 'f', 'j'], 'k'))

Created 2022-03-18T07:03:34+00:00 · Edit