From 344fe92294ba0e02a647c1de2a24f3ea44be173b Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 27 Mar 2025 09:42:29 +0800 Subject: [PATCH] Add solution and test-cases for problem 2780 --- .../README.md | 54 ++++++++++++++++++ .../Solution.go | 56 ++++++++++++++++++- .../Solution_test.go | 14 ++--- 3 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/README.md diff --git a/leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/README.md b/leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/README.md new file mode 100644 index 000000000..5c4d57e55 --- /dev/null +++ b/leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/README.md @@ -0,0 +1,54 @@ +# [2780.Minimum Index of a Valid Split][title] + +## Description +An element `x` of an integer array `arr` of length `m` is **dominant** if **more than half** the elements of `arr` have a value of `x`. + +You are given a **0-indexed** integer array `nums` of length `n` with one **dominant** element. + +You can split `nums` at an index `i` into two arrays `nums[0, ..., i]` and `nums[i + 1, ..., n - 1]`, but the split is only **valid** if: + +- `0 <= i < n - 1` +- `nums[0, ..., i]`, and `nums[i + 1, ..., n - 1]` have the same dominant element. + +Here, `nums[i, ..., j]` denotes the subarray of `nums` starting at index `i` and ending at index `j`, both ends being inclusive. Particularly, if `j < i` then `nums[i, ..., j]` denotes an empty subarray. + +Return the **minimum** index of a *8valid split**. If no valid split exists, return `-1`. + +**Example 1:** + +``` +Input: nums = [1,2,2,2] +Output: 2 +Explanation: We can split the array at index 2 to obtain arrays [1,2,2] and [2]. +In array [1,2,2], element 2 is dominant since it occurs twice in the array and 2 * 2 > 3. +In array [2], element 2 is dominant since it occurs once in the array and 1 * 2 > 1. +Both [1,2,2] and [2] have the same dominant element as nums, so this is a valid split. +It can be shown that index 2 is the minimum index of a valid split. +``` + +**Example 2:** + +``` +Input: nums = [2,1,3,1,1,1,7,1,2,1] +Output: 4 +Explanation: We can split the array at index 4 to obtain arrays [2,1,3,1,1] and [1,7,1,2,1]. +In array [2,1,3,1,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5. +In array [1,7,1,2,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5. +Both [2,1,3,1,1] and [1,7,1,2,1] have the same dominant element as nums, so this is a valid split. +It can be shown that index 4 is the minimum index of a valid split. +``` + +**Example 3:** + +``` +Input: nums = [3,3,3,3,7,2,2] +Output: -1 +Explanation: It can be shown that there is no valid split. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/minimum-index-of-a-valid-split +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/Solution.go b/leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/Solution.go index d115ccf5e..6d6b4c4aa 100755 --- a/leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/Solution.go +++ b/leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/Solution.go @@ -1,5 +1,57 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) int { + l := len(nums) + leftCnt := make(map[int]int) + left := make([]int, l) + m := -1 + for i, n := range nums { + left[i] = -1 + leftCnt[n]++ + if m == -1 { + m = n + left[i] = m + continue + } + selected := m + if leftCnt[n] > leftCnt[m] { + selected = n + } + + if leftCnt[selected] > (i+1)/2 { + left[i] = selected + } + m = selected + } + + rightCnt := make(map[int]int) + right := make([]int, l) + m = -1 + for i := l - 1; i >= 0; i-- { + n := nums[i] + right[i] = -1 + rightCnt[n]++ + if m == -1 { + m = n + right[i] = m + continue + } + selected := m + if rightCnt[n] > rightCnt[m] { + selected = n + } + if rightCnt[selected] > (l-i)/2 { + right[i] = selected + } + m = selected + } + for i := 0; i < l-1; i++ { + if left[i] == -1 || right[i] == -1 { + continue + } + if left[i] == right[i+1] { + return i + } + } + return -1 } diff --git a/leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/Solution_test.go b/leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/Solution_test.go index 14ff50eb4..e05eb0faa 100755 --- a/leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/Solution_test.go +++ b/leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/Solution_test.go @@ -10,12 +10,12 @@ 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{1, 2, 2, 2}, 2}, + {"TestCase2", []int{2, 1, 3, 1, 1, 1, 7, 1, 2, 1}, 4}, + {"TestCase3", []int{3, 3, 3, 3, 7, 2, 2}, -1}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }