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

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

Leetcode 2195. Append K Integers With Minimal Sum

2023-02-26 15:58 作者:您是打尖兒還是住店呢  | 我要投稿

You are given an integer array?nums?and an integer?k. Append?k?unique positive?integers that do?not?appear in?nums?to?nums?such that the resulting total sum is?minimum.

Return?the sum of the?k?integers appended to?nums.

?

Example 1:

Input: nums = [1,4,25,10,25], k = 2Output: 5Explanation: The two unique positive integers that do not appear in nums which we append are 2 and 3. The resulting sum of nums is 1 + 4 + 25 + 10 + 25 + 2 + 3 = 70, which is the minimum. The sum of the two integers appended is 2 + 3 = 5, so we return 5.

Example 2:

Input: nums = [5,6], k = 6Output: 25Explanation: The six unique positive integers that do not appear in nums which we append are 1, 2, 3, 4, 7, and 8. The resulting sum of nums is 5 + 6 + 1 + 2 + 3 + 4 + 7 + 8 = 36, which is the minimum. The sum of the six integers appended is 1 + 2 + 3 + 4 + 7 + 8 = 25, so we return 25.

?

Constraints:

  • 1 <= nums.length <= 10(5)

  • 1 <= nums[i] <= 10(9)

  • 1 <= k <= 10(8)

第一個(gè)方法就是常規(guī)思路,但是TLE(TIME LIMIT EXCEED)超時(shí)了,

所以 只能轉(zhuǎn)換思路,因?yàn)镵會(huì)很大,為了減少遍歷k內(nèi)部的數(shù)字,于是先把k以內(nèi)的數(shù)字求和,

然后去判斷數(shù)字在數(shù)組中是否存在(為了方便,把數(shù)組放在set-集合中),如果存在了,則sum-i, cnt++(這里cnt就要去用大于k的數(shù)字去添加了)。

遍歷后再去根據(jù)cnt的大小去遍歷大于k的數(shù)字,這時(shí)候還是要判斷在不在set中,

遍歷完就可以return了。


Runtime:?37 ms, faster than?60.47%?of?Java?online submissions for?Append K Integers With Minimal Sum.

Memory Usage:?59.2 MB, less than?75.35%?of?Java?online submissions for?Append K Integers With Minimal Sum.


Leetcode 2195. Append K Integers With Minimal Sum的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國家法律
罗源县| 柞水县| 巴青县| 海宁市| 普洱| 门头沟区| 思茅市| 丰宁| 长治市| 砀山县| 双桥区| 南木林县| 潼关县| 梅州市| 阳高县| 任丘市| 祥云县| 晴隆县| 青龙| 沂源县| 贵港市| 泗阳县| 古浪县| 英吉沙县| 河西区| 贺兰县| 岳池县| 普兰店市| 手游| 聂荣县| 大名县| 彝良县| 寿宁县| 贵溪市| 漳平市| 南陵县| 柘城县| 翼城县| 万盛区| 当雄县| 灌南县|