[Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search
Let's say we are going to find out number of occurrences of a number in a sorted array using binary search in O(log n) time.
For example the given array is:
[1,1,3,5,5,5,5,5,9,11],
the number 5 appears 5 times;
the number 3 appears 1 time;
2 appears 0 times.
The idea:
we can use binary search twice, first time is to find first index of target number in the array; second is to find last index of given number in the array.
function count_numbers(ary, target) {
function helper(ary, target, isFirst) {
let start = ;
let end = ary.length - ;
let result = -;
while (start <= end) {
let mid = Math.floor((start + end) / );
if (ary[mid] === target) {
result = mid;
isFirst ? (end = mid - ) : (start = mid + );
} else {
ary[mid] > target ? (end = mid - ) : (start = mid + );
}
}
return result;
}
const first = helper(ary, target, true);
const last = helper(ary, target, false);
if (first === - || last === -) {
return ;
}
return last - first + ;
}
console.log(count_numbers([, , , , , , , , , , ], )); //
[Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search的更多相关文章
- **611. Valid Triangle Number three pointer O(n^3) -> square(binary search larget number smaller than target)
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- [geeksforgeeks] Count the number of occurrences in a sorted array
Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a ...
- [Algorithms] Binary Search Algorithm using TypeScript
(binary search trees) which form the basis of modern databases and immutable data structures. Binary ...
- 【437】Binary search algorithm,二分搜索算法
Complexity: O(log(n)) Ref: Binary search algorithm or 二分搜索算法 Ref: C 版本 while 循环 C Language scripts b ...
- js binary search algorithm
js binary search algorithm js 二分查找算法 二分查找, 前置条件 存储在数组中 有序排列 理想条件: 数组是递增排列,数组中的元素互不相同; 重排 & 去重 顺序 ...
- geeksforgeeks@ Largest Number formed from an Array
http://www.practice.geeksforgeeks.org/problem-page.php?pid=380 Largest Number formed from an Array G ...
- 2 - Binary Search & LogN Algorithm - Apr 18
38. Search a 2D Matrix II https://www.lintcode.com/problem/search-a-2d-matrix-ii/description?_from=l ...
- 2 - Binary Search & LogN Algorithm
254. Drop Eggs https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1 ...
- [LeetCode] 374. Guess Number Higher or Lower_Easy tag: Binary Search
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
随机推荐
- Answer Sheet - Both, Either & Neither
http://www.usingenglish.com/quizzes/answers.php?quiz_id=44 This page displays the answers to the 'Bo ...
- 汽车之家店铺数据抓取 DotnetSpider实战
一.背景 春节也不能闲着,一直想学一下爬虫怎么玩,网上搜了一大堆,大多都是Python的,大家也比较活跃,文章也比较多,找了一圈,发现园子里面有个大神开发了一个DotNetSpider的开源库,很值得 ...
- Reactor与Proactor比较
from http://www.cnblogs.com/dawen/archive/2011/05/18/2050358.html 1.标准定义 两种I/O多路复用模式:Reactor和Proacto ...
- Unity3D实践系列10, Canvas画布的创建和使用
Canvas是所有ui元素的父物体. 当添加一个Button类型的GameObject后,在"Hierarch"窗口中自动添加了一个Canvas,以及EventSystem. 在C ...
- iPhone系统常用文件夹位置
1. [/Applications] 常用软件的安装目录 2. [/private /var/ mobile/Media /iphone video Recorder] iphone v ...
- C#编程(三十一)----------泛型总结
C#泛型总结 C#中的所谓的泛型程序设计和C++中相应的模版类似. 泛型方法 C#中的泛型方法是指使用了类型参数的方法成员,案例: static void Main(string[] args) { ...
- 【Devops】【docker】【CI/CD】Jenkins源代码管理 添加gitlab项目地址,报错Failed to connect to repository : Error performing command: ls-remote -h git@192.168.92.130:8090/root/swapping.git HEAD
Jenkins源代码管理 添加gitlab项目地址 报错如下: Failed to connect to repository : Error performing command: ls-remot ...
- 一文犀利看懂中美贸易战 z
如今的中国面对着前所未有的经济全球化的大环境.面对着如何成为创新性国家的重任. 钛媒体注:美国东部时间 7 月 6 日凌晨0:01 分,美国正式开始对 340 亿美元的中国产品加征 25% 的关税,这 ...
- http抓包以及网速限定
由于我是MAC, 举荐一个Charles工具 限速选择在:可以打开Proxy –> Throttle Setting 设置. 附多篇介绍:http://www.36ria.com/6278 ht ...
- Loader的初步学习笔记
Loader是一个异步加载数据的类,它和AsyncTask有类似也有不同,今天我们就先来学习下它.由于是对比学习,所以我们先来复习下AsyncTask的使用和特点. 一.AsyncTask 参考自:h ...