[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 ...
随机推荐
- 向OSG视图Viewer发送消息
句柄是以下面的方式传递给osgViewer::Viewer的,osgViewer::View.getCamera().setGraphicsContext(osg::GraphicsContext); ...
- C#中泛型容器Stack<T>的用法,以及借此实现”撤销/重做”功能
.Net为我们提供了众多的泛型集合.比如,Stack<T>先进后出,Queue<T>先进先出,List<T>集合元素可排序,支持索引,LinkedList<T ...
- C#编程(四十三)----------Lambda表达式
Lambda表达式 案例: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- WordPress主题开发实例:根据不同分类使用不同模板
分类实现效果: 点击"产品"相关分类,显示 否则显示 创建文件: category.php cat-news.php cat-product.php 由于点击分类时wordpres ...
- WordPress主题开发:数据调用
记录在开发过程中常用的 引入标签:在一个模板文件里引用另外一个文件 get_header() get_footer() get_sidebar() get_template_part() get_se ...
- 清除和新建WordPress数据库
由于在本地已经安装过一次wordpress.所以假设第二次安装的时候 localhost/wp-admin/install.php 会显示你已经安装过了.所以须要把MySQL的数据库信息清除掉,或者另 ...
- Occlusion Culling遮挡剔除理解设置和地形优化应用
这里使用的是unity5.5版本 具体解释网上都有,就不多说了,这里主要说明怎么使用,以及参数设置和实际注意点 在大场景地形的优化上,但也不是随便烘焙就能降低帧率的,必须结合实际情况来考虑,当然还有透 ...
- Java锁的设计
1.自旋锁 自旋锁是采用让当前线程不停地的在循环体内执行实现的,当循环的条件被其他线程改变时 才能进入临界区.如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public ...
- 用UICollectionView实现无限轮播图
用UICollectionView实现无限轮播图 效果 源码 https://github.com/YouXianMing/Animations 细节
- Spring data jpa Specification查询关于日期的范围搜索
代码: 时间格式化类型: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat s ...