[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 ...
随机推荐
- uva 10154 - Weights and Measures【dp】qi
题意:uva 10154 - Weights and Measures 题意:有一些乌龟有一定的体重和力量,求摞起来的最大高度.力量必须承受其上面包含自己的所有的重量. 分析:先按其能举起来的力量从小 ...
- Driving proportional valves from microcontroller
Driving proportional valves from microcontroller I am looking to drive a current regulated proportio ...
- 使用filezilla server搭建FTP服务器
参考文献 http://www.pc6.com/infoview/Article_51961_all.html 背景 需要在内网环境下搭建一个FTP服务器,查阅相关资料发现使用filezilla se ...
- showplan_text查询计划查询 sql执行顺序 时间 IO
http://www.cnblogs.com/happyday56/archive/2009/09/10/1564144.html set showplan_text ongoselect exp ...
- delphi shr和shl的作用
x:=x shl 1 二进制数向左移1位,尾部补1个零,相当于x:=x*2;x:=x shl 2 二进制数向左移2位,尾部补2个零,相当于x:=x*4;...x:=x shl n 二进制数向左移n位, ...
- WebLogic使用总结(五)——Web项目使用Sigar在WebLogic服务器部署遇到的问题
今天在WebLogic 12c服务器上部署Web项目时,碰到了一个问题.项目中使用到了"Sigar.jar"监控Window平台下的cpu使用率.内存使用率和硬盘信息,sigar. ...
- 基于设备树的TQ2440的中断(2)
下面以按键中断为例看看基于设备数的中断的用法: 设备树: tq2440_key { compatible = "tq2440,key"; interrupt-parent = &l ...
- java容器的总结
1.什么是容器? 在程序中,容器是一种用来容纳对象的数据结构,比如说list.set .map.queue. 2.为什么需要容器? 我们为什么需要容器呢?因为在程序中,我们会在任意时刻和任意位置创建任 ...
- 将Linux默认的OpenJDK替换为Oracle JDK
在使用Logstash安装插件的时候,发生了一个错误,如下: ERROR: Something went wrong when installing logstash-input-jdbc, mess ...
- 让Orchard支持多个Layout
默认Orchard只有一个Layout,有的时候,我们的站点往往需要多个母版页.那么,如果要让Orchard支持多个Layout,以下是一种解决方案. 一:创建LayoutFilter using S ...