Facebook面试题 find the peek or valley index in an array

本文介绍了一种在特定类型的数组中查找峰值或谷值的方法。该数组的特点是每个元素与其前一个元素相差1。文中提供了两种解决方案:一种是使用二分搜索实现O(log n)的时间复杂度;另一种是在O(1)时间内直接计算峰值或谷值的位置。

Given an input array in which 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:

    1. If nums[mid] - nums[mid - 1] has a different symbol with nums[mid + 1] - nums[mid], then mid is the index we want.
    2. 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.
    3. 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.

    1. If there is a peek exists: index = (nums.length - 1 + nums[end] - nums[start]) / 2
    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;
        }
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

耀凯考前突击大师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值