Given a sorted array, for example:

// [2,5,6,8,11,12,15,18]

Then we rotated it 1 time, it becomes:

// [18, 2,5,6,8,11,12,15]

2 times:

// [15,182,5,6,8,11,12]

So now given you an array which is rotated N times based on an sorted array, try to find the what is the N?

Key point is, the smallest value in the array (if rotated happened), it must smaller than its previous and next element.  Using binary search to reduce numbers of elements we are searching each time.

function countRotated (ary) {
let N = ary.length,
low = ,
high = N - ; while (low <= high) {
// case 1: ary is sorted already, no rotated
if (ary[low] < ary[high]) {return low;}
let mid = Math.floor((low + high) / 2);
// if mid is the last element, then we need to go to first element in the array, %N does that
let next = (mid + ) % N;
// if mid is the first element, prevent -1 index
let prev = (mid - + N) % N;
// case 2: if mid is smaller than next and prev element, then it must be the smallest item in the array
if (ary[mid] < ary[next] && ary[mid] < ary[prev]) {
return mid;
}
// case 3: if mid is smaller than high, then it means pivot element is not on the right side
else if (ary[mid] < ary[high]) {
high = mid - ;
}
// if mid is larger than low, then it means pivot element is not on the left side
else if (ary[mid] > ary[low]) {
low = mid + ;
}
} return -;
} const data = [,,,,,,,,]; //
const data2 = [,,,,,,,]; //
const data3 = [,,,,,,,,,]; //
const data4 = [,,,,,,]; //
const res = countRotated(data);
console.log(res);
const res2 = countRotated(data2);
console.log(res2);
const res3 = countRotated(data3);
console.log(res3);
const res4 = countRotated(data4);
console.log(res4);

[Algorithm] How many times is a sorted array rotated?的更多相关文章

  1. [Algorithm] Search element in a circular sorted array

    function findInCircularlySortedAry (ary = [], target) { ) { ; } ) { ] === target ? : -; } let , high ...

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

  3. [LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  4. No.026:Remove Duplicates from Sorted Array

    问题: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  5. lintcode 447 Search in a Big Sorted Array

    Given a big sorted array with positive integers sorted by ascending order. The array is so big so th ...

  6. Almost Sorted Array

    http://acm.hdu.edu.cn/contests/contest_showproblem.php?cid=646&pid=1006 #include<iostream> ...

  7. Why is processing a sorted array faster than an unsorted array?

    这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这 ...

  8. 2015ACM/ICPC亚洲区长春站 F hdu 5533 Almost Sorted Array

    Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  9. 【题解】【数组】【查找】【Leetcode】Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. javaDoc 注释规范

    Javadoc虽然是Sun公司为Java文档自动生成设计的,可以从程序源代码中抽取类.方法.成员等注释形成一个和源代码配套的API帮助文档.但是Javadoc的注释也符合C的注释格式,而且doxyen ...

  2. [Go] Template 使用简介

    Golang 提供了两个标准库用来处理模板 text/template 和 html/template.我们使用 html/template 格式化 html 字符. 模板引擎 模板引擎很多,Pyth ...

  3. RDMA over TCP的协议栈工作过程浅析

    http://blog.chinaunix.net/uid-140205-id-2849342.html

  4. C#轻量级高性能日志组件EasyLogger

    一.课程介绍 本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的第六部分,阿笨后续会计划将实际项目中的一些比较实用的关于C#高级编程的技巧分享出来给大家进行学习,不断的收集.整理 ...

  5. X.509 数字证书结构和实例

    http://www.cppblog.com/sleepwom/archive/2010/07/08/119746.html 一. X.509数字证书的编码 X.509证书的结构是用ASN1(Abst ...

  6. Flume 1.5.0简单部署试用

    ================================================================================ 一.Flume简介 ========= ...

  7. Java 下载文件

    public @ResponseBody void exportExcel(HttpServletRequest request, HttpServletResponse response, Khxx ...

  8. 【mysql】update的in的嵌套查询更新,如果字段中包含字符串A,统一替换为字符串B

    对于select的in嵌套子查询[DQL]: select en_name from goods where uid in( select uid from goods where goods_typ ...

  9. 反击黑客之对网站攻击者的IP追踪

    ip追踪是一件比较难实现的,因为我只有一个ip,而且在没有任何技术支持下对该ip追踪,同时我在公司也没有服务器权限,仅有后台,一般的ip追踪技术分类,反应式ip追踪,主动式的追踪,分享的只是一个过程, ...

  10. cocos2d-x 保持屏幕点亮及自动变灰

    很早之前遇到的问题,现在记录一下.有一家Android渠道(抱歉,时间太长了已经记不大清楚是哪一家了 oppo/联想/酷派?)在我们提交新版本时拒绝了,理由是:手机背光状态下,屏幕不会自动变灰. 这里 ...