Facebook面试题 find the peek or valley index in an array
2017-01-08 08:21
423 查看
Given an input array in which
Analysis:
The
A more efficient solution would be to use Binary Search. Observing that each number is different from its previous one with only 1, we could utilize this in the binary search. The ides is like following:
If
If
Other wise peek or valley is in subarray from start to mid.
However, after a careful thinking, this question is solvable in
If there is a peek exists:
If there is a valley exists:
In order to know if there is a peek or a valley, we could use nums[1] - nums[0] to see the initial slope.
Thus we can solve it in O(1) time.
Java implementation of Binary Search is showed as following:
Java implementation of the
A[i] = A[i - 1] -1/+1. And there is only one peek or one valley in the array. Find out the index of the peek or the valley.
Analysis:
The
O(n)solution is rather easy to think of. Just traverse the whole array and find out the index of the peek or valley.
A more efficient solution would be to use Binary Search. Observing that each number is different from its previous one with only 1, we could utilize this in the binary search. The ides is like following:
If
nums[mid] - nums[mid - 1]has a different symbol with
nums[mid + 1] - nums[mid], then
midis the index we want.
If
Math.abs(nums[mid] - nums[start]) == mid - start, that means subarray from start to mid is monotonical, peek or valley is in subarray from mid to end.
Other wise peek or valley is in subarray from start to mid.
However, after a careful thinking, this question is solvable in
O(1)time. Still, considering that each number is different from its previous one with only 1, we could get following conditions for the question.
If there is a peek exists:
index = (nums.length - 1 + nums[end] - nums[start]) / 2
If there is a valley exists:
index = (nums.length - 1 + nums[start] - nums[end]) / 2
In order to know if there is a peek or a valley, we could use nums[1] - nums[0] to see the initial slope.
Thus we can solve it in O(1) time.
Java implementation of Binary Search is showed as following:
public class Solution { public int find(int[] nums) { if (nums == null || nums.length == 0) return -1; int len = nums.length; if (Math.abs(nums[len - 1] - nums[0]) == len - 1) return -1 //there is no peek or valley exists return binarySearch(nums, 0, len - 1); } private int binarySearch(int[] nums, int start, int end) { int mid = start + (end - start) / 2; if ((nums[mid] - nums[mid - 1]) * (nums[mid + 1] - nums[mid] < 0)) { return mid; } int diffOfIndex = mid - start; int diffOfValue = Math.abs(nums[mid] - nums[start]); if (diffOfIndex == diffOfValue) start = mid; else end = mid; return binarySearch(nums, start, mid); } }
Java implementation of the
O(1)solution is showed as following:
public class Solution { public int find(int[] nums) { if (nums == null || nums.length == 0) return -1; int len = nums.length; if (Math.abs(nums[len - 1] - nums[0]) == len - 1) return -1; //no peek or valley exists int slope = nums[1] - nums[0]; if (slope > 0) { //first increase then decrease int diff = nums[len - 1] - nums[0]; return (len - 1 + diff) / 2; } else { //first decrease then increase int diff = nums[0] - nums[len - 1]; return (len - 1 + diff) / 2; } } }
相关文章推荐
- Find the Smallest K Elements in an Array
- Find the repeated item in an array
- Data Structure Array: Find the two numbers with odd occurrences in an unsorted array
- Err:The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array
- Find the 5 repeated items in an array
- stackover flow 上的一个问题:Find the most common entry in an array
- Each child in an array or iterator should have a unique "key" prop. Check the render method of `Tabl
- given an array of integers Find the index of values that satisfy A+B = C + D
- [TypeScript] Find the repeated item in an array using TypeScript
- Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method
- Warning: Each child in an array or iterator should have a unique "key" prop. Check the render ...
- react Warning: Each child in an array or iterator should have a unique "key" prop. Check the render
- Facebook面试题 Moving positive, negative and zeros in an array
- Find the kth largest element in an unsorted array
- Find the two non-repeating elements in an array of repeating elements
- Find the first repeating element in an array of integers
- [LeetCode] 无序数组中的最长连续数列 The Longest Consecutive Sequence in an unsorted array
- You’re given an array containing both positive and negative integers and required to find the sub-a
- Queue an action event or invoking the button action in programmatic way
- Use find the search an element in deque