[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 ...
随机推荐
- AI 实验--v_JULY_v
http://blog.csdn.net/v_JULY_v http://www.julyedu.com/
- Win10年度更新开发必备:VS2015 正式版下载汇总
============================================================================ 微软在07月20日发布了Visual Stud ...
- MySQL主从复制的原理及配置方法(比较详细)
MySQL 的数据库的高可用性的架构大概有以下几种:集群,读写分离,主备.而后面两种都是通过复制来实现的.下面将简单介绍复制的原理及配置,以及一些常见的问题 一.复制的原理 MySQL 复制基于主服务 ...
- javascript: break跳出多重循环以及退出each循环
先来看一个小例子: <html> <body> <script type="text/javascript"> for(j=0;j<2;j ...
- android 玩转ContentProvider之二--实现多个ContentProvider对多张表进行操作
SQLite数据库直接操作类: DatabaseHelper.java package com.jacp.database; import android.content.Context; impor ...
- ClipDrawable属性介绍
ClipDrawable代表从其它位图上截取一个"图片片段",XML中的根元素为<clip.../>,截取的方向由clipOrientation控制 <?xml ...
- .NET培训 | JAVA培训 | 最课程
最课程(www.zuikc.com) 软件开发培训,在线软件培训的创新者!我们的创新在于: 1:一次购买,终身服务.每个最课程学员都会分配一位专职教师及一位监管教师,点对点跟进课程进度,直到您学会课程 ...
- js混淆加密,通过混淆Js代码让别人(很难)无法还原
js混淆加密,通过混淆Js代码让别人(很难)无法还原 使用js的混淆加密,其目的是为了保护我们的前端代码逻辑,对应一些搞技术吃饭的公司来说,为了防止被竞争对手抓取或使用自己的代码,就会考虑如何加密 ...
- 奇怪吸引子---Bouali
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...
- Asp.net WebAPI Ioc
网上关于webapi Ioc 的东西很多,如http://efmvc.codeplex.com/SourceControl/latest#MyFinance.Web/Global.asax.cs 这是 ...