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

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

Leetcode 2598. Smallest Missing Non-negative Integer After Opera

2023-03-19 14:18 作者:您是打尖兒還是住店呢  | 我要投稿

You are given a?0-indexed?integer array?nums?and an integer?value.

In one operation, you can add or subtract?value?from any element of?nums.

  • For example,

  • if?nums = [1,2,3]?and?value = 2,

  • you can choose to subtract?value?from?nums[0]?to make?nums = [-1,2,3].

The MEX (minimum excluded) of an array is the smallest missing?non-negative?integer in it.

  • For example, the MEX of?[-1,2,3]?is?0?while the MEX of?[1,0,3]?is?2.

Return?the maximum MEX of?nums?after applying the mentioned operation?any number of times.

?

Example 1:

Input: nums = [1,-10,7,13,6,8],?

value = 5

Output: 4

Explanation: One can achieve this result by applying the following operations:?

- Add value to nums[1] twice to make nums = [1,0,7,13,6,8]

- Subtract value from nums[2] once to make nums = [1,0,2,13,6,8]

- Subtract value from nums[3] twice to make nums = [1,0,2,3,6,8]

The MEX of nums is 4. It can be shown that 4 is the maximum MEX?

we can achieve.

Example 2:

Input: nums = [1,-10,7,13,6,8], value = 7

Output: 2

Explanation: One can achieve this result by applying the following operation:

- subtract value from nums[2] once to make nums = [1,-10,0,13,6,8]

The MEX of nums is 2. It can be shown that 2 is the maximum MEX?

we can achieve.

?

Constraints:

  • 1 <= nums.length, value <= 105

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

  • 一直沒思路,然后看了lee的思路,但是沒看代碼,于是自己就按照思路,寫出了代碼,居然100%,難以置信,方法如下:

  • A 將所有的數(shù)字都對(duì)value求余,如果是負(fù)數(shù),則求余后,+value 然后再求余,這樣保證所有的數(shù)字都是正數(shù)

  • B 將所有的數(shù)字放到hashmap中,計(jì)算每個(gè)數(shù)字出現(xiàn)的次數(shù),然后按照次數(shù)就行排序,

  • C最后返回的結(jié)果一定是次數(shù)最少的那個(gè)乘以value加上對(duì)應(yīng)的余數(shù)。。

  • 好廢腦子啊。。。

Runtime:?72 ms, faster than?100.00%?of?Java?online submissions for?Smallest Missing Non-negative Integer After Operations.

Memory Usage:?61.8 MB, less than?100.00%?of?Java?online submissions for?Smallest Missing Non-negative Integer After Operations.


Leetcode 2598. Smallest Missing Non-negative Integer After Opera的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國家法律
乐业县| 保靖县| 当雄县| 鄯善县| 自治县| 巴彦淖尔市| 黔南| 唐山市| 黎平县| 酉阳| 济南市| 呈贡县| 仁寿县| 信丰县| 大化| 北京市| 绍兴县| 隆回县| 昌图县| 陆丰市| 霍邱县| 寿光市| 绥宁县| 建昌县| 韶山市| 陕西省| 湾仔区| 墨竹工卡县| 吉木萨尔县| 昌平区| 宁国市| 峨眉山市| 武平县| 松原市| 巴南区| 犍为县| 淮滨县| 桦南县| 九龙坡区| 福泉市| 周口市|