LintCode "Find Peak Element II"

Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind!

class Solution {
public:
    /**
     * @param A: An integer matrix
     * @return: The index of the peak
     */
    vector<int> findPeakII(vector<vector<int> > A)
    {
        int n = A.size();
        int m = A[0].size();

        int i = 1, j = 1;
        while(i < m - 1 && j < n - 1)
        {
            bool up   = A[j - 1][i] < A[j][i];
            bool down = A[j + 1][i] < A[j][i];
            bool left = A[j][i - 1] < A[j][i];
            bool right= A[j][i + 1] < A[j][i];

            if(up && down && left && right)
            {
                return {j, i};
            }
            if(!down && j < n - 2)
            {
                j ++;
            }
            else if(!right && i < m - 2)
            {
                i ++;
            }
        }

        return {-1, -1};
    }
};
时间: 2024-12-07 06:21:01

LintCode "Find Peak Element II"的相关文章

[LintCode] Find Peak Element 求数组的峰值

There is an integer array which has the following features: The numbers in adjacent positions are different. A[0] < A[1] && A[A.length - 2] > A[A.length - 1]. We define a position P is a peek if: A[P] > A[P-1] && A[P] > A[P+1]

Find Peak Element II

There is an integer matrix which has the following features: The numbers in adjacent positions are different. The matrix has n rows and m columns. For all i < m, A[0][i] < A[1][i] && A[n - 2][i] > A[n - 1][i]. For all j < n, A[j][0] &l

[Leetcode + Lintcode] 162. Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi

[Lintcode]75. Find Peak Element

75. Find Peak Element Description There is an integer array which has the following features: The numbers in adjacent positions are different. A[0] < A[1] && A[A.length - 2] > A[A.length - 1]. We define a position P is a peak if: A[P] > A

LeetCode OJ 162. Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi

162. Find Peak Element (Array; Divide-and-Conquer)

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi

Find Peak Element

问题: A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks i

[LeetCode]Find Peak Element

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi

Leetcode 162 Find Peak Element (二分查找思想)

A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its index. The array may contain multiple peaks, in that case return the index to any one of the peaks is fi