avatar
b*y
1
A sorted array is rotated(rotate position is unknown), find the index for
the smallest element.
Example: 1 2 3 4 5 6 7, after rotated to the right becomes 4 5 6 7 1 2 3
avatar
a*y
2
每次比较中间和两头的值吧
跟rotated array的二分查找类似

【在 b******y 的大作中提到】
: A sorted array is rotated(rotate position is unknown), find the index for
: the smallest element.
: Example: 1 2 3 4 5 6 7, after rotated to the right becomes 4 5 6 7 1 2 3

avatar
B*g
3
怎么知道中间?
One stupid algorithm I can think of:
int smallestIndex(int arr[], int size)
{
int smallest = arr[0];
int index = 0;
for( int i = 1; i < size; ++i ){
if( arr[i] < arr[i-1] && smallest > arr[i] ){
smallest = arr[i];
index = i;
}
}
return index;
}

【在 a*****y 的大作中提到】
: 每次比较中间和两头的值吧
: 跟rotated array的二分查找类似

avatar
c*e
4
The trick of the question is that, in the worst scenario, the approach can
only be O(n). If the array has all a[i]=1, you have to go through every
element. If you do not, one of it (if only one of it) could be 0. But for
general case, it should be O(logn) (the binary search approach).
With that the approach is clear. Use recursion. For an array a[i] to a[j]
with i)/2 or k=(i+j+1)/2. If a[k]>a[j], then call the subroutine for

【在 b******y 的大作中提到】
: A sorted array is rotated(rotate position is unknown), find the index for
: the smallest element.
: Example: 1 2 3 4 5 6 7, after rotated to the right becomes 4 5 6 7 1 2 3

avatar
w*y
5
only rotate once??
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。