最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

LeetCode 6911. Continuous Subarrays

2023-07-02 13:10 作者:您是打尖兒還是住店呢  | 我要投稿

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

  • Let?i,?i + 1, ...,?j?be the indices in the subarray. Then, for each pair of indices?i <= i1, i2?<= j,?0 <=?|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 4. Total continuous subarrays = 4 + 3 + 1 = 8. It 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 <= nums.length <= 105

  • 1 <= nums[i] <= 109

這里利用雙指針以及優(yōu)先隊(duì)列來存儲最大值最小值,只是我考慮的時(shí)候,以為存在相同元素的話,不好實(shí)現(xiàn),結(jié)果還是能實(shí)現(xiàn),下面是代碼:(j-i+1是以i為開頭的所有可能的子數(shù)組的數(shù)量)-我有1個(gè)for循環(huán)+while循環(huán)在周賽的時(shí)候也過了呀,過了周賽就TLE了。。。

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

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


LeetCode 6911. Continuous Subarrays的評論 (共 條)

分享到微博請遵守國家法律
资阳市| 玛沁县| 习水县| 桂东县| 万盛区| 康保县| 郧西县| 镇赉县| 德令哈市| 九龙城区| 屏山县| 鹿邑县| 行唐县| 兴仁县| 铜川市| 娄底市| 青铜峡市| 东丰县| 儋州市| 石景山区| 宾川县| 綦江县| 新绛县| 明光市| 清水县| 秭归县| 吕梁市| 顺义区| 荣成市| 临洮县| 甘泉县| 太保市| 遂宁市| 韩城市| 蕲春县| 全南县| 铜陵市| 枝江市| 资阳市| 开原市| 安徽省|