给你一个整数数组 nums,请你找出并返回能被三整除的元素最大和。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/greatest-sum-divisible-by-three
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public int maxSumDivThree(int[] nums) {
int[] dp = {0, Integer.MIN_VALUE, Integer.MIN_VALUE};
for (int num : nums) {
int[] helper = new int[3];
for (int i = 0; i < 3; i++) {
helper[i] = Math.max(dp[i], dp[(i + num % 3) % 3] + num);
}
dp = helper;
}
return dp[0];
}
}
标签:最大,helper,nums,int,1262,num,整除,dp
From: https://www.cnblogs.com/tianyiya/p/17491049.html