LeetCode刷题:845. Longest Mountain in Array

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/seagal890/article/details/89066071

LeetCode刷题:845. Longest Mountain in Array

原题链接:https://leetcode.com/problems/longest-mountain-in-array/

Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold:

  • B.length >= 3、
  • There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1]

(Note that B could be any subarray of A, including the entire array A.)

Given an array A of integers, return the length of the longest mountain. 

Return 0 if there is no mountain.

Example 1:

Input: [2,1,4,7,3,2,5]
Output: 5
Explanation: The largest mountain is [1,4,7,3,2] which has length 5.
Example 2:

Input: [2,2,2]
Output: 0
Explanation: There is no mountain.

Note:

0 <= A.length <= 10000
0 <= A[i] <= 10000

Follow up:

Can you solve it using only one pass?
Can you solve it in O(1) space?

题目的大意是:找出一个数组中最长的山形数组的长度。所谓山形数组就是前半段是单调递增的,后半段是单调递减的。

例如:给定了数组A,有9个元素。

A = [1, 2, 3, 2, 1, 0, 2, 3, 1],如图所示:

在上图中,从A[0] = 1开始,到A[2]=3为止,这时单调递增的,即到达山峰;

此后到A[5]=6为止,又到达山底。子数组为[1,2,3,2,1,0],长度为6

从A[5]=0开始,到A[7]=3为止,又达到山峰;此后从A[7]=3到A[8]=1,又达到山底。

子数组为[0,2,3,1],长度为4

那么第一个子数组 [1,2,3,2,1,0],长度为6 满足题目要求。

算法分析:

开始位置记为base,结束位置记为end。

我们在数组A中计算:A[base], A[base+1], ..., A[end].

如果这样的数字山存在,则下一个可能存在的数字山的起点start就是前一个数字山的end;存在关系base = end;

如果这样的数字山不存在,则存在关系:A[base] > A[base+1];那么我们就从 A[base + 1] 开始寻找。

算法设计:

package com.bean.algorithmbasic;

public class LongestMountainInArray {
	
	public static int longestMountain(int[] A) {
        int N = A.length;
        int ans = 0, base = 0;
        while (base < N) {
            int end = base;
            // if base is a left-boundary
            if (end + 1 < N && A[end] < A[end + 1]) {
                // set end to the peak of this potential mountain
                while (end + 1 < N && A[end] < A[end + 1]) end++;

                // if end is really a peak..
                if (end + 1 < N && A[end] > A[end + 1]) {
                    // set end to the right-boundary of mountain
                    while (end + 1 < N && A[end] > A[end + 1]) end++;
                    // record candidate answer
                    ans = Math.max(ans, end - base + 1);
                }
            }
            base = Math.max(end, base + 1);
        }
        return ans;
    }
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//int[] demo=new int[] {1, 2, 3, 2, 1, 0, 2, 3, 1};
		int[] demo=new int[] {2,1,4,7,3,2,5};	
		int result = longestMountain(demo);
		System.out.println("result = "+result);
	}

}

运行结果为:

result = 5

 第二种算法设计:

	/*
	 * 在一个数组序列中,可能存在3中情况:递增[1,2,3,4],递减[4,3,2,1],还有等值[1,1,1,1]
	 * 为了找到符合题目描述的数字山,我们需要找到类似的子数组[1,2,3,2,1],即先递增,而后递减
	 * 如果是递增的子序列,则存在关系:A[index-1]<A[index]
	 * 如果是等值,则存在关系:A[index - 1] == A[index],需要重新计数
	 * 如果是递减的子序列,则存在关系:A[index-1]>A[index]
	 * 如果能够形成数字山,则最少需要数组子序列的长度为3,即类似[1,2,1],[4,7,3]
	 * */
	public static int longestMountain(int[] A) {
		//如果数组长度小于3,则返回0;不能构成题目要求的数字山
        if (A.length < 3) {
            return 0;
        }
        
        int index = 1;
        int count = 1; 
        int max = 0;
        //遍历数组
        while (index < A.length) {
            if (A[index - 1] < A[index]) {
                if (index > 1 && A[index - 2] > A[index - 1]) {
                    count = 1;
                }
                count++;
            } else if (A[index - 1] == A[index]) {
                count = 1;
            } else {
                if (count != 1) {
                    count++;
                    max = Math.max(max, count);
                }
            }
            
            index++;
        }
        return max;
    }

可以多考虑考虑。

猜你喜欢

转载自blog.csdn.net/seagal890/article/details/89066071