cs: add «1800. Maximum Ascending Subarray Sum»
URL: https://leetcode.com/problems/maximum-ascending-subarray-sum/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
5048736010
commit
747f728dc9
1 changed files with 15 additions and 0 deletions
15
cs/maximum-ascending-subarray-sum.cs
Normal file
15
cs/maximum-ascending-subarray-sum.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
public class Solution {
|
||||
private record MaxAscendingAcc(int Sum, int Max) {
|
||||
public MaxAscendingAcc Add(int num) => new(Sum + num, Math.Max(Max, Sum + num));
|
||||
public MaxAscendingAcc Reset(int num) => new(num, Math.Max(Max, num));
|
||||
}
|
||||
|
||||
public int MaxAscendingSum(int[] nums) =>
|
||||
nums.Zip(nums.Skip(1)).Aggregate(
|
||||
new MaxAscendingAcc(nums[0], nums[0]),
|
||||
(acc, pair) => pair switch {
|
||||
_ when pair.First < pair.Second => acc.Add(pair.Second),
|
||||
_ => acc.Reset(pair.Second),
|
||||
}
|
||||
).Max;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue