-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d2a602
commit 1b4208a
Showing
2 changed files
with
26 additions
and
2 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
2025-01-January-LeetCoding-Challenge/Number of Ways to Split Array.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def waysToSplitArray(self, nums: List[int]) -> int: | ||
prefix_sum, total_sum = 0, sum(nums) | ||
valid_splits = 0 | ||
for num in nums[:-1]: | ||
prefix_sum += num | ||
if prefix_sum >= total_sum - prefix_sum: | ||
valid_splits += 1 | ||
return valid_splits | ||
|
||
|
||
def main(): | ||
nums = [10, 4, -8, 7] | ||
assert Solution().waysToSplitArray(nums) == 2 | ||
|
||
nums = [2, 3, 1, 0] | ||
assert Solution().waysToSplitArray(nums) == 2 | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters