Skip to content

Commit b05cddf

Browse files
authored
Create largest-element-in-an-array-after-merge-operations.cpp
1 parent eb39640 commit b05cddf

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// greedy
5+
class Solution {
6+
public:
7+
long long maxArrayValue(vector<int>& nums) {
8+
int64_t result = 0;
9+
for (int64_t i = size(nums) - 1, curr = 0; i >= 0; --i) {
10+
if (nums[i] > curr) {
11+
curr = 0;
12+
}
13+
curr += nums[i];
14+
result = max(result, curr);
15+
}
16+
return result;
17+
}
18+
};

0 commit comments

Comments
 (0)