{"id":5878,"date":"2019-11-26T20:49:44","date_gmt":"2019-11-27T04:49:44","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5878"},"modified":"2019-11-26T20:51:21","modified_gmt":"2019-11-27T04:51:21","slug":"leetcode-1262-greatest-sum-divisible-by-three","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1262-greatest-sum-divisible-by-three\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1262. Greatest Sum Divisible by Three"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>nums<\/code>&nbsp;of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [3,6,5,1,8]\n<strong>Output:<\/strong> 18\n<strong>Explanation:<\/strong> Pick numbers 3, 6, 1 and 8 their sum is 18 (maximum sum divisible by 3).<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [4]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> Since 4 is not divisible by 3, do not pick any number.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4,4]\n<strong>Output:<\/strong> 12\n<strong>Explanation:<\/strong> Pick numbers 1, 3, 4 and 4 their sum is 12 (maximum sum divisible by 3).\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 4 * 10^4<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10^4<\/code><\/li><\/ul>\n\n\n\n<p><strong>Solution: DP<\/strong><\/p>\n\n\n\n<p>dp[i] := max sum that has a remainder  i when mod 3.<br><br>dp[(i + num) % 3]  = max( dp[(i + num) % 3] , dp[i] + num)<\/p>\n\n\n\n<p>ans: dp[0]<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxSumDivThree(vector<int>& nums) {\n    vector<int> dp(3);    \n    for (int num : nums) {\n      vector<int> tmp(dp);\n      for (int s : tmp)\n        dp[(s + num) % 3] = max(dp[(s + num) % 3], s + num);      \n    }\n    return dp[0];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;nums&nbsp;of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three. Example&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,177],"class_list":["post-5878","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5878"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5878\/revisions"}],"predecessor-version":[{"id":5880,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5878\/revisions\/5880"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}