question/search-last-index-with-binary-search-possible-duplicate-array #44
Replies: 1 comment
-
/**
* @param {number[]} arr - ascending array with duplicates
* @param {number} target
* @return {number}
*/
function lastIndex(arr, target) {
// your code here
let left = 0, right = arr.length - 1;
while (left < right) {
const mid = (left + right + 1) >> 1;
if (arr[mid] <= target) {
left = mid;
} else {
right = mid - 1;
}
}
return arr[left] == target ? left : -1;
}
console.log(lastIndex([1, 2, 2, 2, 3], 2) == 3); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
question/search-last-index-with-binary-search-possible-duplicate-array
北美前端面试攻略
https://us-fe.github.io/question/search-last-index-with-binary-search-possible-duplicate-array.html
Beta Was this translation helpful? Give feedback.
All reactions