Skip to content

Latest commit

 

History

History
225 lines (173 loc) · 5.71 KB

File metadata and controls

225 lines (173 loc) · 5.71 KB
comments difficulty edit_url rating source tags
true
简单
1217
第 392 场周赛 Q1
数组

English Version

题目描述

给你一个整数数组 nums

返回数组 nums严格递增严格递减 的最长非空子数组的长度。

 

示例 1:

输入:nums = [1,4,3,3,2]

输出:2

解释:

nums 中严格递增的子数组有[1][2][3][3][4] 以及 [1,4]

nums 中严格递减的子数组有[1][2][3][3][4][3,2] 以及 [4,3]

因此,返回 2

示例 2:

输入:nums = [3,3,3,3]

输出:1

解释:

nums 中严格递增的子数组有 [3][3][3] 以及 [3]

nums 中严格递减的子数组有 [3][3][3] 以及 [3]

因此,返回 1

示例 3:

输入:nums = [3,2,1]

输出:3

解释:

nums 中严格递增的子数组有 [3][2] 以及 [1]

nums 中严格递减的子数组有 [3][2][1][3,2][2,1] 以及 [3,2,1]

因此,返回 3

 

提示:

  • 1 <= nums.length <= 50
  • 1 <= nums[i] <= 50

解法

方法一:两次遍历

我们先进行一次遍历,找出严格递增的最长子数组长度,更新答案。然后再进行一次遍历,找出严格递减的最长子数组长度,再次更新答案。

时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$

Python3

class Solution:
    def longestMonotonicSubarray(self, nums: List[int]) -> int:
        ans = t = 1
        for i, x in enumerate(nums[1:]):
            if nums[i] < x:
                t += 1
                ans = max(ans, t)
            else:
                t = 1
        t = 1
        for i, x in enumerate(nums[1:]):
            if nums[i] > x:
                t += 1
                ans = max(ans, t)
            else:
                t = 1
        return ans

Java

class Solution {
    public int longestMonotonicSubarray(int[] nums) {
        int ans = 1;
        for (int i = 1, t = 1; i < nums.length; ++i) {
            if (nums[i - 1] < nums[i]) {
                ans = Math.max(ans, ++t);
            } else {
                t = 1;
            }
        }
        for (int i = 1, t = 1; i < nums.length; ++i) {
            if (nums[i - 1] > nums[i]) {
                ans = Math.max(ans, ++t);
            } else {
                t = 1;
            }
        }
        return ans;
    }
}

C++

class Solution {
public:
    int longestMonotonicSubarray(vector<int>& nums) {
        int ans = 1;
        for (int i = 1, t = 1; i < nums.size(); ++i) {
            if (nums[i - 1] < nums[i]) {
                ans = max(ans, ++t);
            } else {
                t = 1;
            }
        }
        for (int i = 1, t = 1; i < nums.size(); ++i) {
            if (nums[i - 1] > nums[i]) {
                ans = max(ans, ++t);
            } else {
                t = 1;
            }
        }
        return ans;
    }
};

Go

func longestMonotonicSubarray(nums []int) int {
	ans := 1
	t := 1
	for i, x := range nums[1:] {
		if nums[i] < x {
			t++
			ans = max(ans, t)
		} else {
			t = 1
		}
	}
	t = 1
	for i, x := range nums[1:] {
		if nums[i] > x {
			t++
			ans = max(ans, t)
		} else {
			t = 1
		}
	}
	return ans
}

TypeScript

function longestMonotonicSubarray(nums: number[]): number {
    let ans = 1;
    for (let i = 1, t = 1; i < nums.length; ++i) {
        if (nums[i - 1] < nums[i]) {
            ans = Math.max(ans, ++t);
        } else {
            t = 1;
        }
    }
    for (let i = 1, t = 1; i < nums.length; ++i) {
        if (nums[i - 1] > nums[i]) {
            ans = Math.max(ans, ++t);
        } else {
            t = 1;
        }
    }
    return ans;
}