Skip to content

Commit 68ce033

Browse files
author
Aarzoo
committed
add solutions, explanation and update README of the day 2270
1 parent 727b065 commit 68ce033

File tree

8 files changed

+203
-0
lines changed

8 files changed

+203
-0
lines changed

Algorithms/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
| 2064 | Minimized Maximum of Products Distributed to Any Store | [C++](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.cpp), [Java](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.java), [JavaScript](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.js), [Python](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.py), [Go](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.go) | [Explanation](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Explanation/explanation.md) | Medium |
5959
| 2070 | Most Beautiful Item for Each Query | [C++](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.cpp), [Java](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.java), [JavaScript](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.js), [Python](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.py), [Go](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.go) | [Explanation](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Explanation/explanation.md) | Medium |
6060
| 2134 | Minimum Swaps to Group All 1's Together II | [C++](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.cpp), [Java](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.java), [JavaScript](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.js), [Python](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.py), [Go](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.go) | [Explanation](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Explanation/explanation.md) | Medium |
61+
| 2270 | 2270. Number of Ways to Split Array | [C++](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.cpp), [Java](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.java), [JavaScript](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.js), [Python](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.py), [Go](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.go) | [Explanation](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Explanation/explanation.md) | Medium |
6162
| 2275 | Largest Combination With Bitwise AND Greater Than Zero | [C++](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.cpp), [Java](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.java), [JavaScript](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.js), [Python](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.py), [Go](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.go) | [Explanation](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Explanation/explanation.md) | Medium |
6263
| 2406 | Divide Intervals Into Minimum Number of Groups | [C++](./Algorithms/src/2406.%20Divide%20Intervals%20Into%20Minimum%20Number%20of%20Groups/Code/solution.cpp), [Java](./Algorithms/src/2406.%20Divide%20Intervals%20Into%20Minimum%20Number%20of%20Groups/Code/solution.java), [JavaScript](./Algorithms/src/2406.%20Divide%20Intervals%20Into%20Minimum%20Number%20of%20Groups/Code/solution.js), [Python](./Algorithms/src/2406.%20Divide%20Intervals%20Into%20Minimum%20Number%20of%20Groups/Code/solution.py), [Go](./Algorithms/src/2406.%20Divide%20Intervals%20Into%20Minimum%20Number%20of%20Groups/Code/solution.go) | [Explanation](./Algorithms/src/2406.%20Divide%20Intervals%20Into%20Minimum%20Number%20of%20Groups/Explanation/explanation.md) | Medium |
6364
| 2416 | Sum of Prefix Scores of Strings | [C++](./Algorithms/src/2416.%20Sum%20of%20Prefix%20Scores%20of%20Strings/Code/solution.cpp), [Java](./Algorithms/src/2416.%20Sum%20of%20Prefix%20Scores%20of%20Strings/Code/solution.java), [JavaScript](./Algorithms/src/2416.%20Sum%20of%20Prefix%20Scores%20of%20Strings/Code/solution.js), [Python](./Algorithms/src/2416.%20Sum%20of%20Prefix%20Scores%20of%20Strings/Code/solution.py), [Go](./Algorithms/src/2416.%20Sum%20of%20Prefix%20Scores%20of%20Strings/Code/solution.go) | [Explanation](./Algorithms/src/2416.%20Sum%20of%20Prefix%20Scores%20of%20Strings/Explanation/explanation.md) | Hard |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution
2+
{
3+
public:
4+
int waysToSplitArray(vector<int> &nums)
5+
{
6+
long long total_sum = 0; // Total sum of the array
7+
for (int num : nums)
8+
{
9+
total_sum += num;
10+
}
11+
12+
long long prefix_sum = 0; // Prefix sum
13+
int count = 0; // Count of valid splits
14+
15+
for (int i = 0; i < nums.size() - 1; i++)
16+
{
17+
prefix_sum += nums[i];
18+
long long right_sum = total_sum - prefix_sum;
19+
if (prefix_sum >= right_sum)
20+
{
21+
count++;
22+
}
23+
}
24+
25+
return count;
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func waysToSplitArray(nums []int) int {
2+
totalSum := 0
3+
for _, num := range nums {
4+
totalSum += num
5+
}
6+
7+
prefixSum := 0
8+
count := 0
9+
10+
for i := 0; i < len(nums)-1; i++ {
11+
prefixSum += nums[i]
12+
rightSum := totalSum - prefixSum
13+
if prefixSum >= rightSum {
14+
count++
15+
}
16+
}
17+
18+
return count
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public int waysToSplitArray(int[] nums) {
3+
long totalSum = 0;
4+
for (int num : nums) {
5+
totalSum += num;
6+
}
7+
8+
long prefixSum = 0;
9+
int count = 0;
10+
11+
for (int i = 0; i < nums.length - 1; i++) {
12+
prefixSum += nums[i];
13+
long rightSum = totalSum - prefixSum;
14+
if (prefixSum >= rightSum) {
15+
count++;
16+
}
17+
}
18+
19+
return count;
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var waysToSplitArray = function (nums) {
6+
let totalSum = nums.reduce((a, b) => a + b, 0); // Total sum of the array
7+
let prefixSum = 0; // Prefix sum
8+
let count = 0; // Count of valid splits
9+
10+
for (let i = 0; i < nums.length - 1; i++) {
11+
prefixSum += nums[i];
12+
let rightSum = totalSum - prefixSum;
13+
if (prefixSum >= rightSum) {
14+
count++;
15+
}
16+
}
17+
18+
return count;
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def waysToSplitArray(self, nums: List[int]) -> int:
3+
total_sum = sum(nums) # Total sum of the array
4+
prefix_sum = 0 # Prefix sum
5+
count = 0 # Count of valid splits
6+
7+
for i in range(len(nums) - 1):
8+
prefix_sum += nums[i]
9+
right_sum = total_sum - prefix_sum
10+
if prefix_sum >= right_sum:
11+
count += 1
12+
13+
return count
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Number of Ways to Split Array - Step-by-Step Explanation
2+
3+
This README provides a detailed explanation of the solution for the problem **"Number of Ways to Split Array"**. Below, we go through the thought process and steps for each implementation in **C++**, **Java**, **JavaScript**, **Python**, and **Go**.
4+
5+
---
6+
7+
## 🚀 Problem Description
8+
9+
Given an array `nums`, count the number of ways to split the array into two non-empty parts such that the sum of the left part is **greater than or equal to** the sum of the right part.
10+
11+
---
12+
13+
## 🧠 Intuition
14+
15+
The idea is to calculate the sum of the left and right parts of the array for each split point and check if the left sum is greater than or equal to the right sum. By using **prefix sums**, we can avoid recalculating sums repeatedly and ensure the solution is efficient.
16+
17+
---
18+
19+
## 🛠️ Approach
20+
21+
1. Calculate the **total sum** of the entire array.
22+
2. Use a **prefix sum** to keep track of the cumulative sum of elements from the start up to the current index.
23+
3. At each split point (from index `0` to `n-2`), calculate the right sum as the difference between the total sum and the prefix sum.
24+
4. Check if the left sum (prefix sum) is greater than or equal to the right sum. If yes, increase the count of valid splits.
25+
5. Return the final count of valid splits.
26+
27+
---
28+
29+
## 📚 Step-by-Step Explanation
30+
31+
### **C++ Implementation**
32+
33+
1. **Calculate the total sum**: Start by iterating through the array to compute the total sum of all elements.
34+
2. **Initialize variables**: Use a `prefix_sum` to store the cumulative sum of the left part and a `count` to keep track of valid splits.
35+
3. **Iterate through the array**: For each split point, update the prefix sum with the current element.
36+
4. **Compute the right sum**: Subtract the prefix sum from the total sum to get the right part's sum.
37+
5. **Check the condition**: Compare the prefix sum with the right sum. If the prefix sum is greater than or equal to the right sum, increment the count.
38+
6. **Return the result**: Once the loop finishes, return the count of valid splits.
39+
40+
---
41+
42+
### **Java Implementation**
43+
44+
1. **Calculate the total sum**: Use a loop to compute the total sum of all elements in the array.
45+
2. **Initialize variables**: Create variables for `prefixSum` (to track the cumulative left sum) and `count` (to count valid splits).
46+
3. **Iterate through the array**: Loop from the start of the array to the second-to-last element.
47+
4. **Update the prefix sum**: Add the current element to the `prefixSum` during each iteration.
48+
5. **Compute the right sum**: Subtract the `prefixSum` from the `totalSum` to get the right part's sum dynamically.
49+
6. **Check the condition**: If the `prefixSum` is greater than or equal to the right sum, increment the `count`.
50+
7. **Return the count**: After the loop, return the total number of valid splits.
51+
52+
---
53+
54+
### **JavaScript Implementation**
55+
56+
1. **Calculate the total sum**: Use the `reduce()` function to compute the sum of all elements in the array.
57+
2. **Initialize variables**: Create variables `prefixSum` for the cumulative left sum and `count` to track valid splits.
58+
3. **Iterate through the array**: Use a `for` loop to traverse the array, stopping at the second-to-last element.
59+
4. **Update the prefix sum**: Add the current element to `prefixSum` in each iteration.
60+
5. **Compute the right sum**: Subtract `prefixSum` from `totalSum` to dynamically calculate the sum of the right part.
61+
6. **Check the condition**: Compare `prefixSum` with the right sum. If `prefixSum` is greater than or equal to the right sum, increment `count`.
62+
7. **Return the count**: At the end of the loop, return the total count of valid splits.
63+
64+
---
65+
66+
### **Python Implementation**
67+
68+
1. **Calculate the total sum**: Use Python's `sum()` function to find the sum of all elements in the array.
69+
2. **Initialize variables**: Create `prefix_sum` for the cumulative left sum and `count` to store the number of valid splits.
70+
3. **Iterate through the array**: Loop through the array, stopping before the last element.
71+
4. **Update the prefix sum**: Add the current element to `prefix_sum` during each iteration.
72+
5. **Compute the right sum**: Subtract the `prefix_sum` from the `total_sum` to get the right part's sum.
73+
6. **Check the condition**: If the `prefix_sum` is greater than or equal to the right sum, increment the `count`.
74+
7. **Return the count**: After the loop finishes, return the total count of valid splits.
75+
76+
---
77+
78+
### **Go Implementation**
79+
80+
1. **Calculate the total sum**: Use a `for` loop to compute the sum of all elements in the array.
81+
2. **Initialize variables**: Create `prefixSum` to store the left cumulative sum and `count` to track valid splits.
82+
3. **Iterate through the array**: Loop through the array, stopping before the last element.
83+
4. **Update the prefix sum**: Add the current element to `prefixSum` in each iteration.
84+
5. **Compute the right sum**: Subtract `prefixSum` from `totalSum` to dynamically compute the right sum.
85+
6. **Check the condition**: Compare `prefixSum` and the right sum. If `prefixSum` is greater than or equal to the right sum, increment `count`.
86+
7. **Return the count**: At the end of the loop, return the total number of valid splits.
87+
88+
---
89+
90+
## ⚙️ Complexity Analysis
91+
92+
- **Time Complexity**:
93+
The algorithm runs in \(O(n)\) time, where \(n\) is the size of the array. This is because we iterate through the array once to calculate the total sum and once more to evaluate valid splits.
94+
95+
- **Space Complexity**:
96+
The solution uses \(O(1)\) extra space since we only use a few variables (`total_sum`, `prefix_sum`, `count`).
97+
98+
---
99+
100+
## ✨ Conclusion
101+
102+
This approach ensures optimal performance with minimal space usage, making it well-suited for large inputs. The use of prefix sums significantly reduces redundant computations, achieving \(O(n)\) efficiency across all implementations.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Welcome to the LeetCode Solutions repository! Here, you'll find daily solutions
6060
| 2064 | Minimized Maximum of Products Distributed to Any Store | [C++](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.cpp), [Java](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.java), [JavaScript](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.js), [Python](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.py), [Go](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Code/solution.go) | [Explanation](./Algorithms/src/2064.%20Minimized%20Maximum%20of%20Products%20Distributed%20to%20Any%20Store/Explanation/explanation.md) | Medium |
6161
| 2070 | Most Beautiful Item for Each Query | [C++](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.cpp), [Java](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.java), [JavaScript](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.js), [Python](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.py), [Go](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Code/solution.go) | [Explanation](./Algorithms/src/2070.%20Most%20Beautiful%20Item%20for%20Each%20Query/Explanation/explanation.md) | Medium |
6262
| 2134 | Minimum Swaps to Group All 1's Together II | [C++](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.cpp), [Java](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.java), [JavaScript](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.js), [Python](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.py), [Go](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Code/solution.go) | [Explanation](./Algorithms/src/2134.%20Minimum%20Swaps%20to%20Group%20All%201's%20Together%20II/Explanation/explanation.md) | Medium |
63+
| 2270 | 2270. Number of Ways to Split Array | [C++](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.cpp), [Java](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.java), [JavaScript](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.js), [Python](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.py), [Go](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Code/solution.go) | [Explanation](./Algorithms/src/2270.%20Number%20of%20Ways%20to%20Split%20Array/Explanation/explanation.md) | Medium |
6364
| 2275 | Largest Combination With Bitwise AND Greater Than Zero | [C++](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.cpp), [Java](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.java), [JavaScript](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.js), [Python](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.py), [Go](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Code/solution.go) | [Explanation](./Algorithms/src/2275.%20Largest%20Combination%20With%20Bitwise%20AND%20Greater%20Than%20Zero/Explanation/explanation.md) | Medium |
6465
| 2406 | Divide Intervals Into Minimum Number of Groups | [C++](./Algorithms/src/2406.%20Divide%20Intervals%20Into%20Minimum%20Number%20of%20Groups/Code/solution.cpp), [Java](./Algorithms/src/2406.%20Divide%20Intervals%20Into%20Minimum%20Number%20of%20Groups/Code/solution.java), [JavaScript](./Algorithms/src/2406.%20Divide%20Intervals%20Into%20Minimum%20Number%20of%20Groups/Code/solution.js), [Python](./Algorithms/src/2406.%20Divide%20Intervals%20Into%20Minimum%20Number%20of%20Groups/Code/solution.py), [Go](./Algorithms/src/2406.%20Divide%20Intervals%20Into%20Minimum%20Number%20of%20Groups/Code/solution.go) | [Explanation](./Algorithms/src/2406.%20Divide%20Intervals%20Into%20Minimum%20Number%20of%20Groups/Explanation/explanation.md) | Medium |
6566
| 2416 | Sum of Prefix Scores of Strings | [C++](./Algorithms/src/2416.%20Sum%20of%20Prefix%20Scores%20of%20Strings/Code/solution.cpp), [Java](./Algorithms/src/2416.%20Sum%20of%20Prefix%20Scores%20of%20Strings/Code/solution.java), [JavaScript](./Algorithms/src/2416.%20Sum%20of%20Prefix%20Scores%20of%20Strings/Code/solution.js), [Python](./Algorithms/src/2416.%20Sum%20of%20Prefix%20Scores%20of%20Strings/Code/solution.py), [Go](./Algorithms/src/2416.%20Sum%20of%20Prefix%20Scores%20of%20Strings/Code/solution.go) | [Explanation](./Algorithms/src/2416.%20Sum%20of%20Prefix%20Scores%20of%20Strings/Explanation/explanation.md) | Hard |

0 commit comments

Comments
 (0)