From 6e48907aef8c60b1d5744df0db106eee1d650175 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Fri, 3 Jan 2025 08:54:54 +0800 Subject: [PATCH] Add solution and test-cases for problem 2270 --- .../README.md | 39 ++++++++++++------- .../Solution.go | 14 ++++++- .../Solution_test.go | 13 +++---- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/README.md b/leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/README.md index c85001572..83467e73a 100755 --- a/leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/README.md +++ b/leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/README.md @@ -1,28 +1,39 @@ # [2270.Number of Ways to Split Array][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given a **0-indexed** integer array `nums` of length `n`. + +`nums` contains a **valid split** at index `i` if the following are true: + +- The sum of the first `i + 1` elements is **greater than or equal to** the sum of the last `n - i - 1` elements. +- There is **at least one** element to the right of `i`. That is, `0 <= i < n - 1`. + +Return the number of **valid splits** in `nums`. + **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [10,4,-8,7] +Output: 2 +Explanation: +There are three ways of splitting nums into two non-empty parts: +- 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. +- 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. +- 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. +Thus, the number of valid splits in nums is 2. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Number of Ways to Split Array -```go ``` - +Input: nums = [2,3,1,0] +Output: 2 +Explanation: +There are two valid splits in nums: +- 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. +- 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. +``` ## 结语 diff --git a/leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/Solution.go b/leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/Solution.go index d115ccf5e..6484e0276 100644 --- a/leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/Solution.go +++ b/leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/Solution.go @@ -1,5 +1,15 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + ans := 0 + l := len(nums) + for i := 1; i < l; i++ { + nums[i] += nums[i-1] + } + for i := 0; i < l-1; i++ { + if nums[i] >= nums[l-1]-nums[i] { + ans++ + } + } + return ans } diff --git a/leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/Solution_test.go b/leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/Solution_test.go index 14ff50eb4..4fca660a3 100644 --- a/leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/Solution_test.go +++ b/leetcode/2201-2300/2270.Number-of-Ways-to-Split-Array/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{10, 4, -8, 7}, 2}, + {"TestCase2", []int{2, 3, 1, 0}, 2}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }