1262. Greatest Sum Divisible by Three
https://leetcode.com/problems/greatest-sum-divisible-by-three/description/
Given an array nums
of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.
Example 1:
Example 2:
Example 3:
Constraints:
1 <= nums.length <= 4 * 10^4
1 <= nums[i] <= 10^4
给定数组nums中满足是三倍数的子集和最大能是多少。优化 + 数组中挑选元素=>DP。分为N步,对于元素i,action分别为选和不选nums[i],选的话nums[i]能和i前最大子集和余数为0,1和2分别组成凑成新的余数为0, 1和2的和。因此维持dp[i][0:2]表示截止到i元素子集和余数分别为0,1和2的最大子集和是多少,它只和i-1有关因此可以用滚动数组节约空间。
Last updated
Was this helpful?