No. 22 - Turning Number in an Array
来源:互联网 发布:淘宝代写征文靠谱 编辑:程序博客网 时间:2024/06/06 03:56
No. 22 - Turning Number in an Array
Problem: Turning number is the maximum number in an array whichincreases and then decreases. This kind of array is also named unimodal array.Please write a function which gets the index of the turning number in such anarray.
For example, theturning number in array {1, 2, 3, 4, 5, 10, 9, 8, 7, 6} is 10, so its index 5is the expected output.
Analysis: As we know, the binary search algorithmis suitable to search a number in a sorted array. Since the input array forthis problem is partially sorted, we may also have a try with binary search.
Let us try toget the middle number in an array. The middle number of array {1, 2, 3, 4, 5,10, 9, 8, 7, 6} is 5 (the fourth number). It is greater than its previousnumber 4, and less than its next number 10, so it is in the increasingsub-array. Therefore, numbers before 5 can be discarded in the next round ofsearch.
The remainingnumbers for the next round of search are {5, 10, 9, 8, 7, 6}, and the number 9is in the middle of them. Since 9 is less than is previous number 10 andgreater than its next number 8, it is in the decreasing sub-array. Therefore,numbers after 9 can be discarded in the next round of search.
The remainingnumbers for the next round of search are {5, 10, 9}, and the number 10 is inthe middle. It is noticeable that number 10 is greater than its previous number5 and greater than its next number 9, so it is the maximum number. That is tosay, the number 10 is the turning number in the input array.
We can see theprocess above is actually a classic binary search. Therefore, we can implementthe required function based on binary search algorithm, as listed below:
int TurningNumberIndex(int* numbers, int length)
{
if(numbers == NULL || length <= 2)
return -1;
int left = 0;
int right = length - 1;
while(right > left + 1)
{
int middle = (left + right) / 2;
if(middle == 0 || middle == length - 1)
return -1;
if(numbers[middle] > numbers[middle - 1] &&
numbers[middle] > numbers[middle + 1])
return middle;
else if(numbers[middle] > numbers[middle - 1] &&
numbers[middle] < numbers[middle + 1])
left = middle;
else
right = middle;
}
return -1;
}
The author Harry Heowns all the rights of this post. If you are going to use part of or the wholeof this ariticle in your blog or webpages, please add a reference to http://codercareer.blogspot.com/. If you are going to use it in your books,please contact me (zhedahht@gmail.com) . Thanks.
- No. 22 - Turning Number in an Array
- find kth largest number in an array
- Get nth Max number in an array of unsorted integers.
- LeetCode 448.Find All Number Disappeared in an Array
- No. 47 - Search in a Rotation of an Array
- LeetCode No.448 Find All Numbers Disappeared in an Array
- NO.5_448. Find All Numbers Disappeared in an Array
- How to find number of a specific integer in an array of Fixnums ruby
- Distinguish between index of a decimal number and integer inside an array in Ruby?
- 448. Find All Numbers Disappeared in an Array \ 414. Third Maximum Number
- Rotating an array in place
- Rotating an array in place
- Find duplicates in an array
- PIQ60: Leaders in an array
- Closest Number in Sorted Array
- closest-number-in-sorted-array
- LeetCode No.384 Shuffle an Array
- POJ 1991 Turning in Homework
- ffmpeg网络流解码
- 我的第一个游戏FoodieThebug完成之后的心得体会
- No. 21 - Push and Pop Sequences ofStacks
- js window.event对象解析
- JSF的学习参考地址
- No. 22 - Turning Number in an Array
- No. 23 - Palindrome Numbers
- 关键词的选取注意事项
- No. 24 - Intersection of Sorted Arrays
- No. 25 - Edit Distance
- No. 26 - Minimal Number of Coins for Change
- No. 27 - Area of Rectangles
- 类名.this 的使用
- No. 28 - A Pair with the Maximal Difference