classSolution{public:longlongmaxAlternatingSum(vector<int>&nums){longeven=0;// the maximum alternating sum ending in an even indexlongodd=0;// the maximum alternating sum ending in an odd indexfor(constintnum:nums){even=max(even,odd+num);odd=even-num;}returneven;}};
1 2 3 4 5 6 7 8 910111213
classSolution{publiclongmaxAlternatingSum(int[]nums){longeven=0;// the maximum alternating sum ending in an even indexlongodd=0;// the maximum alternating sum ending in an odd indexfor(finalintnum:nums){even=Math.max(even,odd+num);odd=even-num;}returneven;}}
1 2 3 4 5 6 7 8 910
classSolution:defmaxAlternatingSum(self,nums:list[int])->int:even=0# the maximum alternating sum ending in an even indexodd=0# the maximum alternating sum ending in an odd indexfornuminnums:even=max(even,odd+num)odd=even-numreturneven