Number of Ways to Split Array - Leetcode 2270 - Python
Key Takeaways
The video demonstrates a solution to the Leetcode 2270 problem, 'Number of Ways to Split Array', using a prefix sum approach in Python, with a focus on efficient linear time and constant space complexity.
Full Transcript
hey everyone welcome back and let's write some more neat code today so today let's solve a problem number of ways to split array and I'm not going to lie for a beginner like this problem there are some like learnings that you can take away from it I thought it was a little bit on the easy side just in terms of like the acceptance rate I saw the acceptance rate was like 50% and I solved a different prefix sum problem that I thought was much more difficult than this one which had like an 80% completion rate so my takeway is that these completion rates are kind of random so I'm not really going to pay attention to them anymore but to get into this problem we're given an array of integers there actually could be negative integers and that's not really going to change the problem very much at all imagine we're given this as an input array there is something called a split in this problem the definition of it is basically splitting the array into two parts where the left and right parts are both nonm you can't like SP split here this would be empty you can't split here either this would be empty now why didn't they Define the problem like that I don't know but it's not that hard I think to take the way they worded this problem and arrive at this kind of meaning so non-empty splits now if you do perform a split specifically we are interested in the sum of each portion I'll call it the left and right portion so the sum here is 10 the sum here is I believe this will be -1 + 4 I think that's going to be three what we need to determine is this current split valid a valid split is defined as where the left side is a sum of greater than or equal to the right side and this does pass that definition so they gave us that like over here so this is a valid split what we want to do is count the total number of valid splits so we'll do the same exact thing uh rather than here splitting it we're going to split here and we're going to split here so there's only three possible splits that we could do now obviously for an arbitrary split like this one if we were to take this total sum and then this total sum that's an O of n operation for any given split it is like that but once we do a single split can you see how we can then eliminate some of the repeated work if I had this split already and I already had the left and right sum what could I do to then arrive at this split and get me this left and right sum in constant time can I reuse some of this to get this and the answer is yes because obviously the main difference between this line and this line is just this little portion over here if I already have the left sum I can get this left sum just by taking this value and adding it there how do I get the right sum well whatever it already was I can remove this value giving me the new right sum over here and that's how you can solve this problem in linear time and constant space I'll quickly do the dry run the easiest thing to do is just to get the initial sum of all the elements and let's call that our right sum I believe it's going to be 12 that's going to be four I think it's roughly 11 hopefully I'm correct and then now we're going to start iterating our left sum we can initially say is equal to zero and now I'm going to update it by taking the 10 and adding it to the left sum I'm going to use a different color and this will be 10 and I'm going to remove 10 from here that'll be 1 okay I was I was wrong I think the initial sum was 13 I really don't know how my math skills have gotten so poor I thought this was 12 but anyways removing 10 we will get a positive three over here and so right now this is valid this split is valid so we found a valid split okay now let's try the next split let's take four and add it over here we get 14 and remove it over here we get -1 this is also a valid split so we found two valid splits let's try uh the next spot over here let's take 8 and add it over here I think that will give us a postive 6 and let's take 8 and remove it from here that will actually add 8 to this making this a positive 7 and so now the condition doesn't hold this is not greater than or equal to that it makes sense this is 7even by itself and all of this should be postive 6 so we only counted two valid splits at this point we can stop because we don't need to actually be over here because we don't want to do the split so when we iterate over here we will technically only over the first nus1 elements now let's go ahead and code it up so like I said I'm going to initialize my right as just being the sum of all the elements technically this is an O of n operation but we only needed to do that once and now we can iterate over the input like this for I in range length minus one because we don't want to include the last element we can also initialize our left with zero and our result I guess also Z that's the one that we're going to end up returning and so now in here it's just going to be as simple as those steps I talked about we can add to the left the current uh element and from the right we can subtract the current element and then we just need an if statement to determine if we're going to be incrementing the result or not so if the left is greater than or equal to the right we increment the result and that's the solution if you want to shorten this a tiny bit you can use use a Turner operator and you can say something like this add one if left greater than right else add zero and then you can get rid of those two lines even though we have a loop here and this is also an oent operation this is still clearly a linear time solution it's just two passes I guess and there's no extra data structures being used except of course I forgot the equal sign when I was rewriting it into a Turner operator uh but you can see that this is working code it's pretty efficient This falls into the prefix some pattern if you want to learn more about that you can check out NE code. I think I cover it in my Advanced algorithms course thanks for watching and I'll see you soon
Original Description
🚀 https://neetcode.io/ - A better way to prepare for Coding Interviews
🧑💼 LinkedIn: https://www.linkedin.com/in/navdeep-singh-3aaa14161/
🐦 Twitter: https://twitter.com/neetcode1
⭐ BLIND-75 PLAYLIST: https://www.youtube.com/watch?v=KLlXCFG5TnA&list=PLot-Xpze53ldVwtstag2TL4HQhAnC8ATf
Problem Link: https://leetcode.com/problems/number-of-ways-to-split-array/description/
0:00 - Read the problem
0:30 - Drawing Explanation
5:04 - Coding Explanation
leetcode 2270
#neetcode #leetcode #python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeetCodeIO · NeetCodeIO · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Leetcode 149 - Maximum Points on a Line - Python
NeetCodeIO
Design Linked List - Leetcode 707 - Python
NeetCodeIO
Minimum Time to Collect All Apples in a Tree - Leetcode 1443 - Python
NeetCodeIO
Design Browser History - Leetcode 1472 - Python
NeetCodeIO
Number of Good Paths - Leetcode 2421 - Python
NeetCodeIO
Flip String to Monotone Increasing - Leetcode 926 - Python
NeetCodeIO
Maximum Sum Circular Subarray - Leetcode 918 - Python
NeetCodeIO
Find Closest Node to Given Two Nodes - Leetcode 2359 - Python
NeetCodeIO
Concatenated Words - Leetcode 472 - Python
NeetCodeIO
Data Stream as Disjoint Intervals - Leetcode 352 - Python
NeetCodeIO
LFU Cache - Leetcode 460 - Python
NeetCodeIO
N-th Tribonacci Number - Leetcode 1137
NeetCodeIO
Best Team with no Conflicts - Leetcode 1626 - Python
NeetCodeIO
Greatest Common Divisor of Strings - Leetcode 1071 - Python
NeetCodeIO
Shortest Path in a Binary Matrix - Leetcode 1091 - Python
NeetCodeIO
Insert into a Binary Search Tree - Leetcode 701 - Python
NeetCodeIO
Delete Node in a BST - Leetcode 450 - Python
NeetCodeIO
Shuffle the Array (Constant Space) - Leetcode 1470 - Python
NeetCodeIO
Fruits into Basket - Leetcode 904 - Python
NeetCodeIO
Number of Subarrays of size K and Average Greater than or Equal to Threshold - Leetcode 1343 Python
NeetCodeIO
Naming a Company - Leetcode 2306 - Python
NeetCodeIO
As Far from Land as Possible - Leetcode 1162 - Python
NeetCodeIO
Shortest Path with Alternating Colors - Leetcode 1129 - Python
NeetCodeIO
Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python
NeetCodeIO
Count Odd Numbers in an Interval Range - Leetcode 1523 - Python
NeetCodeIO
Contains Duplicate II - Leetcode 219 - Python
NeetCodeIO
Path with Maximum Probability - Leetcode 1514 - Python
NeetCodeIO
Add to Array-Form of Integer - Leetcode 989 - Python
NeetCodeIO
Unique Paths II - Leetcode 63 - Python
NeetCodeIO
Minimum Distance between BST Nodes - Leetcode 783 - Python
NeetCodeIO
Design Hashmap - Leetcode 706 - Python
NeetCodeIO
Range Sum Query Immutable - Leetcode 303 - Python
NeetCodeIO
Binary Tree Zigzag Level Order Traversal - Leetcode 103 - Python
NeetCodeIO
Middle of the Linked List - Leetcode 876 - Python
NeetCodeIO
Course Schedule IV - Leetcode 1462 - Python
NeetCodeIO
Single Element in a Sorted Array - Leetcode 540 - Python
NeetCodeIO
Capacity to Ship Packages - Leetcode 1011 - Python
NeetCodeIO
IPO - Leetcode 502 - Python
NeetCodeIO
Minimize Deviation in Array - Leetcode 1675 - Python
NeetCodeIO
Longest Turbulent Array - Leetcode 978 - Python
NeetCodeIO
Last Stone Weight II - Leetcode 1049 - Python
NeetCodeIO
Construct Quad Tree - Leetcode 427 - Python
NeetCodeIO
Find Duplicate Subtrees - Leetcode 652 - Python
NeetCodeIO
Sort an Array - Leetcode 912 - Python
NeetCodeIO
Ones and Zeroes - Leetcode 474 - Python
NeetCodeIO
Remove Duplicates from Sorted Array II - Leetcode 80 - Python
NeetCodeIO
Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
NeetCodeIO
Concatenation of Array - Leetcode 1929 - Python
NeetCodeIO
Symmetric Tree - Leetcode 101 - Python
NeetCodeIO
Check Completeness of a Binary Tree - Leetcode 958 - Python
NeetCodeIO
Construct Binary Tree from Inorder and Postorder Traversal - Leetcode 106 - Python
NeetCodeIO
Find Peak Element - Leetcode 162 - Python
NeetCodeIO
Accounts Merge - Leetcode 721 - Python
NeetCodeIO
Binary Tree Preorder Traversal (Iterative) - Leetcode 144 - Python
NeetCodeIO
Binary Tree Postorder Traversal (Iterative) - Leetcode 145 - Python
NeetCodeIO
Number of Zero-Filled Subarrays - Leetcode 2348 - Python
NeetCodeIO
Minimum Score of a Path Between Two Cities - Leetcode 2492 - Python
NeetCodeIO
Sqrt(x) - Leetcode 69 - Python
NeetCodeIO
Successful Pairs of Spells and Potions - Leetcode 2300 - Python
NeetCodeIO
Optimal Partition of String - Leetcode 2405 - Python
NeetCodeIO
More on: Algorithm Basics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
Bloom Filters, Explained Properly
Dev.to · Daksh Gargas
Prefix Sums: The Preprocessing Trick That Makes Range Queries Instant
Medium · Programming
I Thought I Was Ready for the Interview — Then One Simple Math Question Destroyed Me
Medium · Programming
Week 2(Day 10): LeetCode Two Pointers(slow & fast): Remove Duplicates from Sorted Array (Brute…
Medium · Python
Chapters (3)
Read the problem
0:30
Drawing Explanation
5:04
Coding Explanation
🎓
Tutor Explanation
DeepCamp AI