File tree 2 files changed +27
-1
lines changed
2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,201 LeetCode solutions in JavaScript
1
+ # 1,202 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
960
960
1255|[ Maximum Score Words Formed by Letters] ( ./solutions/1255-maximum-score-words-formed-by-letters.js ) |Hard|
961
961
1260|[ Shift 2D Grid] ( ./solutions/1260-shift-2d-grid.js ) |Easy|
962
962
1261|[ Find Elements in a Contaminated Binary Tree] ( ./solutions/1261-find-elements-in-a-contaminated-binary-tree.js ) |Medium|
963
+ 1262|[ Greatest Sum Divisible by Three] ( ./solutions/1262-greatest-sum-divisible-by-three.js ) |Medium|
963
964
1267|[ Count Servers that Communicate] ( ./solutions/1267-count-servers-that-communicate.js ) |Medium|
964
965
1268|[ Search Suggestions System] ( ./solutions/1268-search-suggestions-system.js ) |Medium|
965
966
1287|[ Element Appearing More Than 25% In Sorted Array] ( ./solutions/1287-element-appearing-more-than-25-in-sorted-array.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1262. Greatest Sum Divisible by Three
3
+ * https://leetcode.com/problems/greatest-sum-divisible-by-three/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an integer array nums, return the maximum possible sum of elements of the array such
7
+ * that it is divisible by three.
8
+ */
9
+
10
+ /**
11
+ * @param {number[] } nums
12
+ * @return {number }
13
+ */
14
+ var maxSumDivThree = function ( nums ) {
15
+ const dp = [ 0 , - Infinity , - Infinity ] ;
16
+
17
+ for ( const num of nums ) {
18
+ const prev = [ ...dp ] ;
19
+ for ( let remainder = 0 ; remainder < 3 ; remainder ++ ) {
20
+ dp [ ( remainder + num ) % 3 ] = Math . max ( dp [ ( remainder + num ) % 3 ] , prev [ remainder ] + num ) ;
21
+ }
22
+ }
23
+
24
+ return dp [ 0 ] ;
25
+ } ;
You can’t perform that action at this time.
0 commit comments