Skip to content

Commit e585818

Browse files
authored
Merge pull request #1082 from 0xff-dev/2270
Add solution and test-cases for problem 2270
2 parents 9cbe21d + 6e48907 commit e585818

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/README.md

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [2270.Number of Ways to Split Array][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
74

5+
You are given a **0-indexed** integer array `nums` of length `n`.
6+
7+
`nums` contains a **valid split** at index `i` if the following are true:
8+
9+
- The sum of the first `i + 1` elements is **greater than or equal to** the sum of the last `n - i - 1` elements.
10+
- There is **at least one** element to the right of `i`. That is, `0 <= i < n - 1`.
11+
12+
Return the number of **valid splits** in `nums`.
13+
814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: nums = [10,4,-8,7]
18+
Output: 2
19+
Explanation:
20+
There are three ways of splitting nums into two non-empty parts:
21+
- Split nums at index 0. Then, the first part is [10], and its sum is 10. The second part is [4,-8,7], and its sum is 3. Since 10 >= 3, i = 0 is a valid split.
22+
- Split nums at index 1. Then, the first part is [10,4], and its sum is 14. The second part is [-8,7], and its sum is -1. Since 14 >= -1, i = 1 is a valid split.
23+
- Split nums at index 2. Then, the first part is [10,4,-8], and its sum is 6. The second part is [7], and its sum is 7. Since 6 < 7, i = 2 is not a valid split.
24+
Thus, the number of valid splits in nums is 2.
1325
```
1426

15-
## 题意
16-
> ...
17-
18-
## 题解
27+
**Example 2:**
1928

20-
### 思路1
21-
> ...
22-
Number of Ways to Split Array
23-
```go
2429
```
25-
30+
Input: nums = [2,3,1,0]
31+
Output: 2
32+
Explanation:
33+
There are two valid splits in nums:
34+
- Split nums at index 1. Then, the first part is [2,3], and its sum is 5. The second part is [1,0], and its sum is 1. Since 5 >= 1, i = 1 is a valid split.
35+
- Split nums at index 2. Then, the first part is [2,3,1], and its sum is 6. The second part is [0], and its sum is 0. Since 6 >= 0, i = 2 is a valid split.
36+
```
2637

2738
## 结语
2839

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
ans := 0
5+
l := len(nums)
6+
for i := 1; i < l; i++ {
7+
nums[i] += nums[i-1]
8+
}
9+
for i := 0; i < l-1; i++ {
10+
if nums[i] >= nums[l-1]-nums[i] {
11+
ans++
12+
}
13+
}
14+
return ans
515
}

leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{10, 4, -8, 7}, 2},
17+
{"TestCase2", []int{2, 3, 1, 0}, 2},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)