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的更多相关文章

  1. **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 ...

  2. [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 ...

  3. [Algorithms] Binary Search Algorithm using TypeScript

    (binary search trees) which form the basis of modern databases and immutable data structures. Binary ...

  4. 【437】Binary search algorithm,二分搜索算法

    Complexity: O(log(n)) Ref: Binary search algorithm or 二分搜索算法 Ref: C 版本 while 循环 C Language scripts b ...

  5. js binary search algorithm

    js binary search algorithm js 二分查找算法 二分查找, 前置条件 存储在数组中 有序排列 理想条件: 数组是递增排列,数组中的元素互不相同; 重排 & 去重 顺序 ...

  6. geeksforgeeks@ Largest Number formed from an Array

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=380 Largest Number formed from an Array G ...

  7. 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 ...

  8. 2 - Binary Search & LogN Algorithm

    254. Drop Eggs https://www.lintcode.com/problem/drop-eggs/description?_from=ladder&&fromId=1 ...

  9. [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 ...

随机推荐

  1. IOS-UITableView入门(2)

    1.对于TableView .每一个item的视图基本都是一样的. 不同的仅仅有数据. IOS提供了一种缓存视图跟数据的方法.在 -UITableViewCell *) tableView:cellF ...

  2. 调试工具BTrace 的使用--例子

    http://www.cnblogs.com/serendipity/archive/2012/05/14/2499840.html

  3. mysql大文本数据类型的使用需要考虑实际情况

    mysql数据类型简介(http://news.newhua.com/news1/program_database/2008/618/08618103911CD92HJ6CKI2I9I0AH5CGK1 ...

  4. 在Visual Studio中使用层关系图描述系统架构、技术栈

    当需要描述项目的架构或技术栈的时候,可以考虑使用层关系图. 在解决方案下添加一个名称为"TailspinToys.DesignModel"的建模项目. 在新建的建模项目下添加一个名 ...

  5. RabbitMQ的应用场景以及基本原理介绍(转)

    本文转自https://blog.csdn.net/whoamiyang/article/details/54954780 1.背景 RabbitMQ是一个由erlang开发的AMQP(Advanve ...

  6. 【docker】【Gitlab】gitlab中clone项目时,IP地址是一串数字(内网Gitlab的IP地址不正确)的问题解决

    首次在内网搭建Gitlab环境,在成功后在Gitlab上新建了一个项目. 然而在IDEA上clone项目时发现,项目地址如下: git@0096ce63c43f:root/jump.git 或者这样 ...

  7. C++类静态数据成员与类静态成员函数

    from:://http://blog.csdn.net/taina2008/article/details/1684834 把类中的函数都定义成静态函数,这样相当于在编译时就分配了空间,这样不需要实 ...

  8. 基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏的代码

    在ipad.iphone网页开发中,我们很可能需要判断是横屏或者竖屏.下面就来介绍如何用 jQuery 判断iPad.iPhone.Android是横屏还是竖屏的方法 其实主要是通过window.or ...

  9. 护士当家第一至七季/全集Nurse Jackie迅雷下载

    英文译名Nurse Jackie,第1-7季(2009-2015)Showtime. 本季看点:<护士当家>是一部黑色医务剧,该剧特别邀请到荣获艾美奖和金球奖的Edie Falco出演女主 ...

  10. 关于GreenPlum的一些整理

    Greenplum数据库架构 Greenplum数据库基本由PostgreSQL核心增强数据库实例组合并衔接成的数据库管理系统,即Greenplum数据在PostgreSQL基础上扩展开发,每个Gre ...