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. Object [object Object] has no method 'live'

    用了2个jquery的2个文件: <script src="~/Scripts/jquery-1.10.2.js"></script> <script ...

  2. Windows Phone本地数据库(SQLCE):8、DataContext(翻译)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第八篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的知 ...

  3. Facebook工程师是如何改进他们Android客户端的

    from://http://greenrobot.me/devnews/facebook-engineer-improve-android-app/ Facebook工程师是如何改进他们Android ...

  4. Unity动画知识之二:Animator动画状态机

    上次我们讲过Unity游戏动画从入门到住院,今天我们来讲一下动画状态机. 好了,现在我们已经成功的导入了动画.接下来要玩的东西就很装13啦.因为大部分动画师是用不到这家伙的,需要掌握这个技能的,至少也 ...

  5. 手机端可以和PC端同时在线-java QRCode 实现网站扫码登录(即支持同帐号多设备同时登录)

    微信扫码测试地址:: http://sms.reyo.cn 用户名:aa 密码:123456 扫码登录实现方式很多,比如ajax轮询,http长连接(comet...),websocket,event ...

  6. 对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾

    今天在springmvc集成mybatis时,遇到一个错误 "characterEncoding" 的引用必须以 ';' 分隔符结尾. 这是“&”定义与解析的原因,需要对& ...

  7. Flash对象插入到网页中的3px问题

    我记得我已经遇到过,不过今天又遇到了,而且浪费了大量的时候在上面,甚至怀疑自己写的脚本有问题,花了几乎一个下午来调试这个问题.最后发现是样式导致的- 公司里有很多网页游戏,之前是项目多,抄来抄去,JS ...

  8. Android使用代码模拟HOME键的功能

    Intent intent= new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.addF ...

  9. python 直方图hist

    import sys sys.path.append('/usr/local/lib/python2.7/site-packages') sys.path.append('/usr/lib/pytho ...

  10. java 常用集合list与Set、Map区别及适用场景总结

     转载请备注出自于:http://blog.csdn.net/qq_22118507/article/details/51576319                  list与Set.Map区别及 ...