无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

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

LeetCode 2653. Sliding Subarray Beauty

2023-04-23 15:18 作者:您是打尖兒還是住店呢  | 我要投稿

Given an integer array?nums?containing?n?integers, find the?beauty?of each subarray of size?k.

The?beauty?of a subarray is the?xth?smallest integer?in the subarray if it is?negative, or?0?if there are fewer than?x?negative integers.

Return?an integer array containing?n - k + 1?integers, which denote the?beauty?of the subarrays?in order?from the first index in the array.

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

?

Example 1:

Input: nums = [1,-1,-3,-2,3], k = 3, x = 2

Output:?

[-1,-2,-2]

Explanation:?

There are 3 subarrays with size k = 3.?

The first subarray is [1, -1, -3] and the 2nd smallest negative integer is -1.??

The second subarray is [-1, -3, -2] and the 2nd smallest negative integer is -2.??

The third subarray is [-3, -2, 3]?and the 2nd smallest negative integer is -2.

Example 2:

Input: nums = [-1,-2,-3,-4,-5], k = 2, x = 2

Output:?

[-1,-2,-3,-4]

Explanation:?

There are 4 subarrays with size k = 2.?

For [-1, -2], the 2nd smallest negative integer is -1.

For [-2, -3], the 2nd smallest negative integer is -2.?

For [-3, -4], the 2nd smallest negative integer is -3.?

For [-4, -5], the 2nd smallest negative integer is -4.?

Example 3:

Input: nums = [-3,1,2,-3,0,-3], k = 2, x = 1

Output:?

[-3,0,-3,-3,-3]

Explanation:?

There are 5 subarrays with size k = 2.

For [-3, 1], the 1st smallest negative integer is -3.?

For [1, 2], there is no negative integer so the beauty is 0.?

For [2, -3], the 1st smallest negative integer is -3.?

For [-3, 0], the 1st smallest negative integer is -3.?

For [0, -3], the 1st smallest negative integer is -3.

?

Constraints:

  • n == nums.length?

  • 1 <= n <= 105

  • 1 <= k <= n

  • 1 <= x <= k?

  • -50?<= nums[i] <= 50?

有了思路的就很快了,一開始我是直接遍歷的,但是time out了,

所以就用雙指針,每次把數(shù)據(jù)放到map中,然后寫一個函數(shù)去返回第x個數(shù)字,

然后數(shù)字放到數(shù)組中即可;

下面是代碼:



Runtime:?699 ms, faster than?16.67%?of?Java?online submissions for?Sliding Subarray Beauty.

Memory Usage:?59.8 MB, less than?33.33%?of?Java?online submissions for?Sliding Subarray Beauty.


LeetCode 2653. Sliding Subarray Beauty的評論 (共 條)

分享到微博請遵守國家法律
扎赉特旗| 黄龙县| 苗栗县| 邯郸县| 东兰县| 定安县| 璧山县| 高青县| 江山市| 望都县| 彭阳县| 普宁市| 长汀县| 文水县| 庆阳市| 监利县| 临江市| 丹凤县| 光山县| 拜泉县| 同心县| 湖口县| 江城| 冷水江市| 逊克县| 海晏县| 庐江县| 深水埗区| 新安县| 西青区| 彭山县| 瓦房店市| 淮安市| 广汉市| 青龙| 乐清市| 剑川县| 平山县| 青田县| 桃源县| 通海县|