Upper Bound
Given a non-empty integer array nums sorted in non-decreasing order and an integer target, return the smallest index i such that nums[i] > target. If no such…
Given a non-empty integer array `nums` sorted in non-decreasing order and an integer `target`, return the smallest index `i` such that `nums[i] > target`. If no such index exists, return `nums.length`.
- `1 <= nums.length <= 10^4` - `-10^4 <= nums[i], target <= 10^4` - `nums` is sorted in non-decreasing order.
Example 1: input nums = [1,2,4,4,5], target = 4; output 4. Index 4 contains the first value strictly greater than 4.
Example 2: input nums = [1,2,4,4,5], target = 3; output 2. Value 4 at index 2 is the first value greater than 3.
Example 3: input nums = [1,2,4,4,5], target = 5; output 5. No array value is greater than 5, so the answer is the array length 5.
Category: Binary Search. Topic: binary-search. Difficulty: Easy. Patterns: binary-search.