Skip to content

Add solution and test-cases for problem 2780 #1160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/README.md
Original file line number Diff line number Diff line change
@@ -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
56 changes: 54 additions & 2 deletions leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading