Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 49dac9b

Browse files
committedMar 6, 2025
Add solution #2270
1 parent faba347 commit 49dac9b

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@
704704
2215|[Find the Difference of Two Arrays](./2215-find-the-difference-of-two-arrays.js)|Easy|
705705
2235|[Add Two Integers](./2235-add-two-integers.js)|Easy|
706706
2244|[Minimum Rounds to Complete All Tasks](./2244-minimum-rounds-to-complete-all-tasks.js)|Medium|
707+
2270|[Number of Ways to Split Array](./2270-number-of-ways-to-split-array.js)|Medium|
707708
2300|[Successful Pairs of Spells and Potions](./2300-successful-pairs-of-spells-and-potions.js)|Medium|
708709
2336|[Smallest Number in Infinite Set](./2336-smallest-number-in-infinite-set.js)|Medium|
709710
2342|[Max Sum of a Pair With Equal Sum of Digits](./2342-max-sum-of-a-pair-with-equal-sum-of-digits.js)|Medium|
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 2270. Number of Ways to Split Array
3+
* https://leetcode.com/problems/number-of-ways-to-split-array/
4+
* Difficulty: Medium
5+
*
6+
* You are given a 0-indexed integer array nums of length n.
7+
*
8+
* nums contains a valid split at index i if the following are true:
9+
* - The sum of the first i + 1 elements is greater than or equal to the sum of the
10+
* last n - i - 1 elements.
11+
* - There is at least one element to the right of i. That is, 0 <= i < n - 1.
12+
*
13+
* Return the number of valid splits in nums.
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @return {number}
19+
*/
20+
var waysToSplitArray = function(nums) {
21+
const total = nums.reduce((a, b) => a + b);
22+
let result = 0;
23+
24+
for (let i = 0, sum = 0; i < nums.length - 1; i++) {
25+
sum += nums[i];
26+
if (sum >= total - sum) {
27+
result++;
28+
}
29+
}
30+
31+
return result;
32+
};

0 commit comments

Comments
 (0)
Please sign in to comment.