首页 >市场动态 > > 正文

LeetCode 6911. Continuous Subarrays

2023-07-02 13:03:18 来源:哔哩哔哩

You are given a 0-indexed integer array nums. A subarray of numsis called continuous if:


(资料图片仅供参考)

Let ii + 1, ..., jbe the indices in the subarray. Then, for each pair of indices i <= i1, i2 <= j0 <= |nums[i1] - nums[i2]| <= 2.

Return the total number of continuous subarrays.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [5,4,2,4]

Output: 8

Explanation: 

Continuous subarray of size 1: [5], [4], [2], [4].Continuous subarray of size 2: [5,4], [4,2], [2,4].Continuous subarray of size 3: [4,2,4].Thereare no subarrys of size continuous subarrays = 4 + 3 + 1 = can be shown that there are no more continuous subarrays.

Example 2:

Input: nums = [1,2,3]

Output: 6

Explanation: Continuous subarray of size 1: [1], [2], [3].Continuous subarray of size 2: [1,2], [2,3].Continuous subarray of size 3: [1,2,3].Total continuous subarrays = 3 + 2 + 1 = 6.

Constraints:

1 <= <= 105

1 <= nums[i] <= 109

这里利用双指针以及优先队列来存储最大值最小值,只是我考虑的时候,以为存在相同元素的话,不好实现,结果还是能实现,下面是代码:(j-i+1是以i为开头的所有可能的子数组的数量)-我有1个for循环+while循环在周赛的时候也过了呀,过了周赛就TLE了。。。

Runtime: 296 ms, faster than % of Java online submissions for Continuous Subarrays.

Memory Usage:  MB, less than % of Java online submissions for Continuous Subarrays.

标签:

x 广告
x 广告

Copyright ©   2015-2022 大西洋劳务网版权所有  备案号:沪ICP备2020036824号-2   联系邮箱: 562 66 29@qq.com