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. STM32 TIMER REGISTER

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

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

  3. C#编程(十二)----------函数

    类和结构 类和结构实际上都是创建对象的模板 ,每 个对象都包含数据 ,并 提供了处理和访问数据的方法. 类定义了类的每个对象 (称 为实例 )可 以包含什么数据和功能 . 例如 ,如 果 一 个类表示 ...

  4. 【linux】linux命令grep + awk 详解

    linux命令grep  +  awk 详解 grep:https://www.cnblogs.com/flyor/p/6411140.html awk:https://www.cnblogs.com ...

  5. cocos2d-x系列笔记技巧篇(2)---关于CREATE_FUNC宏的用法

    FROM://http://blog.csdn.net/jinciyulang/article/details/8631889 阅读cocos2d-x demo的代码,我们会看到有些头文件中使用CRE ...

  6. Traceroute(路由追踪)的原理及实现

    现实世界中的网络是由无数的计算机和路由器组成的一张的大网,应用的数据包在发送到服务器之前都要经过层层的路由转发.而Traceroute是一种常规的网络分析工具,用来定位到目标主机之间的所有路由器 原理 ...

  7. python测试开发django-23.admin列表页优化和排序

    前言 列表页优化和排序 ModelAdmin django的options.py里面 ModelAdmin类定义的参数可以设置admin后台列表页面,相关的参数如下 class ModelAdmin( ...

  8. Office Web Apps 错误日志

    前言 最近一直在用Office Web Apps,使用过程会有各种各样的错误,众所周知,sharepoint的错误都在15/Logs下面保存错误日志,那么OWA呢? 经过查找,发现Office Web ...

  9. Eclipse启动时提示Fail to create the Java Virtual Machine的解决方法

    这个错误是Eclipse里面的一个bug,我们通过如下的设置就可以解决它. 打开eclipse安装目录下的eclipse.ini文件: 将其中的256m改为128m,512m改为256m,1024m改 ...

  10. JAVA开发中文乱码的几个解决方案

    一:html乱码或者引入的JS乱码 1:第一步,text file encoding 首先确保文件的保存格式要UTF-8,如在eclipse中,要在文件上点属性,确保这里选择UTF-8 注意,在ecl ...