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

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

Leetcode 1005. Maximize Sum Of Array After K Negations

2023-02-24 14:33 作者:您是打尖兒還是住店呢  | 我要投稿


Given an integer array?nums?and an integer?k, modify the array in the following way:

  • choose an index?i?and replace?nums[i]?with?-nums[i].

You should apply this process exactly?k?times. You may choose the same index?i?multiple times.

Return?the largest possible sum of the array after modifying it in this way.

?

Example 1:

Input: nums = [4,2,3], k = 1Output: 5Explanation: Choose index 1 and nums becomes [4,-2,3].

Example 2:

Input: nums = [3,-1,0,2], k = 3Output: 6Explanation: Choose indices (1, 2, 2) and nums becomes [3,1,0,2].

Example 3:

Input: nums = [2,-3,-1,5,-4], k = 2Output: 13Explanation: Choose indices (1, 4) and nums becomes [2,3,-1,5,4].

?

Constraints:

  • 1 <= nums.length <= 104

  • -100 <= nums[i] <= 100

  • 1 <= k <= 104

    Easy 題目,如果是小于0的,就取絕對值,如果等于0,說明前面小于0的就已經(jīng)遍歷完了,就可以直接求和了。

  • 但是如果所有的都遍歷完了,k還是大于0的話,那么就只能對數(shù)組排序一下,然后對第一個值取反。一直取。即可。

  • 然后求和;


Runtime:?2 ms, faster than?98.19%?of?Java?online submissions for?Maximize Sum Of Array After K Negations.

Memory Usage:?41.8 MB, less than?62.99%?of?Java?online submissions for?Maximize Sum Of Array After K Negations.


Leetcode 1005. Maximize Sum Of Array After K Negations的評論 (共 條)

分享到微博請遵守國家法律
北辰区| 铅山县| 杭锦后旗| 油尖旺区| 渝中区| 上蔡县| 太白县| 阿瓦提县| 娱乐| 郑州市| 咸丰县| 铜山县| 阜宁县| 武山县| 马公市| 兴山县| 鄂托克旗| 巧家县| 开化县| 瓦房店市| 康平县| 鄂托克前旗| 嘉兴市| 乌鲁木齐市| 伊宁县| 平果县| 惠安县| 深州市| 三原县| 永州市| 武功县| 武强县| 赤城县| 昌乐县| 安仁县| 广昌县| 汕头市| 独山县| 秦皇岛市| 讷河市| 阳西县|