Skip to content

Commit 2fce4ee

Browse files
authored
Create greatest-sum-divisible-by-three.py
1 parent 6bdfc0c commit 2fce4ee

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def maxSumDivThree(self, nums):
6+
"""
7+
:type nums: List[int]
8+
:rtype: int
9+
"""
10+
dp = [0, 0, 0]
11+
for num in nums:
12+
for i in [num+x for x in dp]:
13+
dp[i%3] = max(dp[i%3], i)
14+
return dp[0]

0 commit comments

Comments
 (0)