五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

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

LeetCode 2091. Removing Minimum and Maximum From Array

2023-04-19 08:55 作者:您是打尖兒還是住店呢  | 我要投稿

You are given a?0-indexed?array of?distinct?integers?nums.

There is an element in?nums?that has the?lowest?value and an element that has the?highest?value. We call them the?minimum?and?maximum?respectively. Your goal is to remove?both?these elements from the array.

A?deletion?is defined as either removing an element from the?front?of the array or removing an element from the?back?of the array.

Return?the?minimum?number of deletions it would take to remove?both?the minimum and maximum element from the array.

?

Example 1:

Input: nums = [2,10,7,5,4,1,8,6]

Output: 5

Explanation:?

The minimum element in the array is nums[5], which is 1.

The maximum element in the array is nums[1], which is 10.

We can remove both the minimum and maximum by removing 2 elements from the front and 3 elements from the back.?

This results in 2 + 3 = 5 deletions, which is the minimum number possible.

Example 2:

Input: nums = [0,-4,19,1,8,-2,-3,5]

Output: 3

Explanation:?

The minimum element in the array is nums[1], which is -4.?

The maximum element in the array is nums[2], which is 19.?

We can remove both the minimum and maximum by removing 3 elements from the front. This results in only 3 deletions, which is the minimum number possible.

Example 3:

Input: nums = [101]

Output: 1

Explanation: ?

There is only one element in the array, which makes it both the minimum and maximum element. We can remove it with 1 deletion.

?

Constraints:

  • 1 <= nums.length <= 105

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

  • The integers in?nums?are?distinct.

其實就是分3種情況,

[....i....j]方案1的長度是j+1;

[i....j....]方案2的長度是n-i;

[....i]+[j.....]方案3的長度是i+n-j+1;

我們先遍歷整個數(shù)組,找到最大值最小值的位置,一左一右,放好,然后就是根據(jù)這3個方案求出最小值即可;

Runtime:?2 ms, faster than?100.00%?of?Java?online submissions for?Removing Minimum and Maximum From Array.

Memory Usage:?58.5 MB, less than?53.50%?of?Java?online submissions for?Removing Minimum and Maximum From Array.


LeetCode 2091. Removing Minimum and Maximum From Array的評論 (共 條)

分享到微博請遵守國家法律
轮台县| 华亭县| 乌兰浩特市| 湾仔区| 泰州市| 新和县| 连江县| 舟曲县| 疏勒县| 佛教| 兴山县| 晴隆县| 丹巴县| 广南县| 兴国县| 临邑县| 二连浩特市| 朝阳县| 鄂托克前旗| 墨竹工卡县| 侯马市| 梁河县| 宿迁市| 金沙县| 福安市| 泾阳县| 岱山县| 华坪县| 河曲县| 安龙县| 阳西县| 延川县| 伊金霍洛旗| 庄河市| 皋兰县| 信丰县| 奉新县| 潞西市| 宁安市| 宁乡县| 琼结县|