LeetCode 1262. Greatest Sum Divisible by Three
考点 | 难度 |
---|---|
Greedy | Easy |
题目
Given an integer array nums, return the maximum possible sum of elements of the array such that it is divisible by three.
思路
dp[0]:除以三余0
dp[1]:除以三余1
dp[2]:除以三余1
答案
class Solution(object):
def maxSumDivThree(self, A):
dp = [0, 0, 0]
for a in A:
for i in dp[:]:
dp[(i + a) % 3] = max(dp[(i + a) % 3], i + a)
return dp[0]