[Algorithm] How many times is a sorted array rotated?
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?的更多相关文章
- [Algorithm] Search element in a circular sorted array
function findInCircularlySortedAry (ary = [], target) { ) { ; } ) { ] === target ? : -; } let , high ...
- [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 ...
- [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 ...
- No.026:Remove Duplicates from Sorted Array
问题: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 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 ...
- Almost Sorted Array
http://acm.hdu.edu.cn/contests/contest_showproblem.php?cid=646&pid=1006 #include<iostream> ...
- Why is processing a sorted array faster than an unsorted array?
这是我在逛 Stack Overflow 时遇见的一个高分问题:Why is processing a sorted array faster than an unsorted array?,我觉得这 ...
- 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 ...
- 【题解】【数组】【查找】【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 ...
随机推荐
- MongoDB简单使用 —— 驱动
C#中可以通过官方的驱动MongoDB.Drvier来使用,使用Nuget安装即可. Install-Package MongoDB.Driver Bson文档操作: using MongoDB.Bs ...
- Timer-triggered memory-to-memory DMA transfer demonstrator
http://www.efton.sk/STM32/bt.c // Timer-triggered memory-to-memory DMA transfer demonstrator for STM ...
- Windows 2008 R2防火墙设置运行被ping通
参考文献: http://huobumingbai.blog.51cto.com/1196746/323896/
- linux无锁化编程--__sync_fetch_and_add系列原子操作函数
linux支持的哪些操作是具有原子特性的?知道这些东西是理解和设计无锁化编程算法的基础. 下面的东西整理自网络.先感谢大家的分享! __sync_fetch_and_add系列的命令,发现这个系列命令 ...
- GitHub 第一坑:换行符自动转换
源起 一直想在 GitHub 上发布项目.参与项目,但 Git 这货比较难学啊.买了一本<Git 权威指南>,翻了几页,妈呀,那叫一个复杂,又是 Cygwin 又是命令行的,吓得我不敢学了 ...
- 对一个前端使用AngularJS后端使用ASP.NET Web API项目的理解(4)
chsakell分享了一个前端使用AngularJS,后端使用ASP.NET Web API的项目. 源码: https://github.com/chsakell/spa-webapi-angula ...
- CE找基址
- 关于流媒体(m3u8)的下载与播放
求助:关于流媒体(m3u8)的下载与播放 http://www.cocoachina.com/bbs/read.php?tid-93389.html 此文有相关讨论.demo等,可关注.
- C#编程(四十六)----------正则表达式
正则表达式 1.定义一个Regex类的实例 Regex regex=new Regex(“”); 这里初始化参数就是一个正则表达式,”\d”表示配置数字 2.判断是否匹配 判断一个字符串,是否匹配一个 ...
- 【linux】find命令仅返回文件名 不用返回完整的文件路径
正常查询 find /apps/swapping -name '*swapping*.jar' 在/apps/swapping 目录下 查找 文件名为 '包含swapping的并且以.java结尾的文 ...