Sorted integer array uses binary search O(logn)
int foo(int* a, int low, int up) //note the index is from 1 to N
{
if(low=up-1) return a[low]+1;
int mid=(low+up)/2;
if(a[mid]>mid) up=mid;
else low=mid;
foo(a,low,up);
}
DFS the life without backtracking
int foo(int* a, int low, int up) //note the index is from 1 to N
{
if(low=up-1) return a[low]+1;
int mid=(low+up)/2;
if(a[mid]>mid) up=mid;
else low=mid;
foo(a,low,up);
}
No comments:
Post a Comment