最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

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

LeetCode 2656. Maximum Sum With Exactly K Elements

2023-05-02 08:21 作者:您是打尖兒還是住店呢  | 我要投稿

You are given a?0-indexed?integer array?nums?and an integer?k. Your task is to perform the following operation?exactly?k?times in order to maximize your score:

  1. Select an element?m?from?nums.

  2. Remove the selected element?m?from the array.

  3. Add a new element with a value of?m + 1?to the array.

  4. Increase your score by?m.

Return?the maximum score you can achieve after performing the operation exactly?k?times.

?

Example 1:

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

Output: 18

Explanation:?

We need to choose exactly 3 elements from nums to maximize the sum.?

For the first iteration, we choose 5. Then sum is 5 and nums = [1,2,3,4,6]?

For the second iteration, we choose 6. Then sum is 5 + 6 and nums = [1,2,3,4,7]?

For the third iteration, we choose 7. Then sum is 5 + 6 + 7 = 18 and nums = [1,2,3,4,8]?

So, we will return 18. It can be proven,?

that 18 is the maximum answer that we can achieve.

Example 2:

Input: nums = [5,5,5], k = 2

Output: 11

Explanation: We need to choose exactly 2 elements from nums to maximize the sum.?

For the first iteration, we choose 5. Then sum is 5 and nums = [5,5,6]?

For the second iteration, we choose 6. Then sum is 5 + 6 = 11 and nums = [5,5,7]

So, we will return 11.?

It can be proven, that 11 is the maximum answer that we can achieve.

?

Constraints:

  • 1 <= nums.length <= 100

  • 1 <= nums[i] <= 100

  • 1 <= k <= 100

?easy題目,不多解釋,就是先找到最大值,然后根據(jù)等差數(shù)列求和的公式計算出結(jié)果,

返回即可;


Runtime:?7 ms, faster than?56.91%?of?Java?online submissions for?Maximum Sum With Exactly K Elements?.

Memory Usage:?42.5 MB, less than?92.06%?of?Java?online submissions for?Maximum Sum With Exactly K Elements?.


LeetCode 2656. Maximum Sum With Exactly K Elements的評論 (共 條)

分享到微博請遵守國家法律
文登市| 厦门市| 陇西县| 无棣县| 韶山市| 溆浦县| 柘荣县| 逊克县| 双辽市| 济南市| 五大连池市| 石嘴山市| 金阳县| 庄浪县| 桑植县| 贵港市| 奉新县| 新营市| 平乐县| 昌都县| 巴中市| 那坡县| 岫岩| 南郑县| 南投市| 丰镇市| 同仁县| 绥阳县| 上饶市| 哈密市| 淮阳县| 通化市| 化德县| 阿拉尔市| 凤庆县| 渑池县| 巴东县| 诸暨市| 如皋市| 永福县| 开封县|