Skip to content

Commit b9ae329

Browse files
authored
Create maximum-sum-with-exactly-k-elements.cpp
1 parent 89f257f commit b9ae329

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// math
5+
class Solution {
6+
public:
7+
int maximizeSum(vector<int>& nums, int k) {
8+
return *max_element(cbegin(nums), cend(nums)) * k + k * (k - 1) / 2;
9+
}
10+
};

0 commit comments

Comments
 (0)