[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 ...
随机推荐
- CAS服务器配置
参考文献: http://sucre.blog.51cto.com/1084905/683624 1.安装部署CAS Server 从官网下载CAS Server,今天发现CAS Server的官网居 ...
- ajax jquery 异步表单验证
文件目录: html代码: <html> <head> <title>异步表单验证</title> <script type='text/java ...
- Android 数据存储03之SQLite
SQLite数据存储 Android 集成了 SQLite 数据库.它存储在 /data/data/< 项目文件夹 >/databases/ 下.Android 开发中使用 SQLite ...
- ifeq endif
ifeq ($(PLATFORM_VERSION),4.4)$(info "________________________4.4"); LOCAL_CFLAGS += -DPLU ...
- sqlite3 插入数据的时候,返回SQLITE_CONSTRAINT
sqlite3 插入数据的时候.返回SQLITE_CONSTRAINT 原因是:数据库的表的名字是纯数字. 大改这个原因太诡异了.创建的时候能够创建成功. 插入数据的时候就失败,由于表名是纯数字. 附 ...
- 每天一个linux命令-wc命令
语法:wc [选项] 文件… 说明:该命令统计给定文件中的字节数.字数.行数.如果没有给出文件名,则从标准输入读取.wc同时也给出所有指定文件的总统计数.字是由空格字符区分开的最大字符串. 该命令各选 ...
- Svg.Js 父类的基础操作
一.SVG.Doc 创建SVG文档 var draw = SVG('drawing') <div id="svg1"></div> <script&g ...
- git如何删除远端不存在的本地分支?
问题:远端分支删除后,如何删除之前拉取的本地分支? 答案: git fetch -p git remote show origin 可以查看remote地址,远程分支,还有本地分支与之相对应关系等信息 ...
- Guava ClassToInstanceMap
概述 ClassToInstanceMap提供了一种是用Class作为Key, 对应实例作为Value的途径.他定义了T getInstance(Class<T>)和T putInstan ...
- [Windows_Server_2012优化V1.1_20140425]
[Windows_Server_2012优化V1.1_20140425] Winsows Server 2012 Datacenter Evaluation Build 9200数据中心评估版GUI ...