Skip to content

Commit 03d2ae6

Browse files
committed
Add solution #1262
1 parent d163d8b commit 03d2ae6

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,201 LeetCode solutions in JavaScript
1+
# 1,202 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -960,6 +960,7 @@
960960
1255|[Maximum Score Words Formed by Letters](./solutions/1255-maximum-score-words-formed-by-letters.js)|Hard|
961961
1260|[Shift 2D Grid](./solutions/1260-shift-2d-grid.js)|Easy|
962962
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|
963964
1267|[Count Servers that Communicate](./solutions/1267-count-servers-that-communicate.js)|Medium|
964965
1268|[Search Suggestions System](./solutions/1268-search-suggestions-system.js)|Medium|
965966
1287|[Element Appearing More Than 25% In Sorted Array](./solutions/1287-element-appearing-more-than-25-in-sorted-array.js)|Easy|
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
};

0 commit comments

Comments
 (0)