File tree 2 files changed +33
-1
lines changed
2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,116 LeetCode solutions in JavaScript
1
+ # 1,117 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
850
850
1040|[ Moving Stones Until Consecutive II] ( ./solutions/1040-moving-stones-until-consecutive-ii.js ) |Medium|
851
851
1041|[ Robot Bounded In Circle] ( ./solutions/1041-robot-bounded-in-circle.js ) |Medium|
852
852
1042|[ Flower Planting With No Adjacent] ( ./solutions/1042-flower-planting-with-no-adjacent.js ) |Medium|
853
+ 1043|[ Partition Array for Maximum Sum] ( ./solutions/1043-partition-array-for-maximum-sum.js ) |Medium|
853
854
1047|[ Remove All Adjacent Duplicates In String] ( ./solutions/1047-remove-all-adjacent-duplicates-in-string.js ) |Easy|
854
855
1051|[ Height Checker] ( ./solutions/1051-height-checker.js ) |Easy|
855
856
1071|[ Greatest Common Divisor of Strings] ( ./solutions/1071-greatest-common-divisor-of-strings.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1043. Partition Array for Maximum Sum
3
+ * https://leetcode.com/problems/partition-array-for-maximum-sum/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an integer array arr, partition the array into (contiguous) subarrays of length at most k.
7
+ * After partitioning, each subarray has their values changed to become the maximum value of that
8
+ * subarray.
9
+ *
10
+ * Return the largest sum of the given array after partitioning. Test cases are generated so that
11
+ * the answer fits in a 32-bit integer.
12
+ */
13
+
14
+ /**
15
+ * @param {number[] } arr
16
+ * @param {number } k
17
+ * @return {number }
18
+ */
19
+ var maxSumAfterPartitioning = function ( arr , k ) {
20
+ const dp = new Array ( arr . length + 1 ) . fill ( 0 ) ;
21
+
22
+ for ( let i = 1 ; i <= arr . length ; i ++ ) {
23
+ let maxVal = 0 ;
24
+ for ( let j = 1 ; j <= Math . min ( k , i ) ; j ++ ) {
25
+ maxVal = Math . max ( maxVal , arr [ i - j ] ) ;
26
+ dp [ i ] = Math . max ( dp [ i ] , dp [ i - j ] + maxVal * j ) ;
27
+ }
28
+ }
29
+
30
+ return dp [ arr . length ] ;
31
+ } ;
You can’t perform that action at this time.
0 commit comments