Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f9c758a

Browse files
committed
Add solution #1712
1 parent 5f7c4d1 commit f9c758a

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-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,494 LeetCode solutions in JavaScript
1+
# 1,495 LeetCode solutions in JavaScript
22

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

@@ -1319,6 +1319,7 @@
13191319
1707|[Maximum XOR With an Element From Array](./solutions/1707-maximum-xor-with-an-element-from-array.js)|Hard|
13201320
1710|[Maximum Units on a Truck](./solutions/1710-maximum-units-on-a-truck.js)|Easy|
13211321
1711|[Count Good Meals](./solutions/1711-count-good-meals.js)|Medium|
1322+
1712|[Ways to Split Array Into Three Subarrays](./solutions/1712-ways-to-split-array-into-three-subarrays.js)|Medium|
13221323
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13231324
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13241325
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* 1712. Ways to Split Array Into Three Subarrays
3+
* https://leetcode.com/problems/ways-to-split-array-into-three-subarrays/
4+
* Difficulty: Medium
5+
*
6+
* A split of an integer array is good if:
7+
* - The array is split into three non-empty contiguous subarrays - named left, mid, right
8+
* respectively from left to right.
9+
* - The sum of the elements in left is less than or equal to the sum of the elements in mid,
10+
* and the sum of the elements in mid is less than or equal to the sum of the elements in right.
11+
*
12+
* Given nums, an array of non-negative integers, return the number of good ways to split nums.
13+
* As the number may be too large, return it modulo 109 + 7.
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @return {number}
19+
*/
20+
var waysToSplit = function(nums) {
21+
const MOD = 1e9 + 7;
22+
const n = nums.length;
23+
const prefixSum = new Array(n).fill(0);
24+
25+
prefixSum[0] = nums[0];
26+
for (let i = 1; i < n; i++) {
27+
prefixSum[i] = prefixSum[i - 1] + nums[i];
28+
}
29+
30+
let splitCount = 0;
31+
for (let i = 0; i < n - 2; i++) {
32+
let left = i + 1;
33+
let right = n - 2;
34+
let validStart = -1;
35+
let validEnd = -1;
36+
37+
while (left <= right) {
38+
const mid = Math.floor((left + right) / 2);
39+
const leftSum = prefixSum[i];
40+
const midSum = prefixSum[mid] - prefixSum[i];
41+
const rightSum = prefixSum[n - 1] - prefixSum[mid];
42+
43+
if (leftSum <= midSum && midSum <= rightSum) {
44+
validStart = mid;
45+
right = mid - 1;
46+
} else if (leftSum > midSum) {
47+
left = mid + 1;
48+
} else {
49+
right = mid - 1;
50+
}
51+
}
52+
53+
left = validStart === -1 ? n - 1 : validStart;
54+
right = n - 2;
55+
56+
while (left <= right) {
57+
const mid = Math.floor((left + right) / 2);
58+
const leftSum = prefixSum[i];
59+
const midSum = prefixSum[mid] - prefixSum[i];
60+
const rightSum = prefixSum[n - 1] - prefixSum[mid];
61+
62+
if (leftSum <= midSum && midSum <= rightSum) {
63+
validEnd = mid;
64+
left = mid + 1;
65+
} else {
66+
right = mid - 1;
67+
}
68+
}
69+
70+
if (validStart !== -1 && validEnd !== -1 && validStart <= validEnd) {
71+
splitCount = (splitCount + validEnd - validStart + 1) % MOD;
72+
}
73+
}
74+
75+
return splitCount;
76+
};

0 commit comments

Comments
 (0)