Skip to content

Commit 60d171f

Browse files
authored
Merge pull request #1160 from 0xff-dev/2780
Add solution and test-cases for problem 2780
2 parents 89b4d10 + 344fe92 commit 60d171f

File tree

3 files changed

+115
-9
lines changed

3 files changed

+115
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# [2780.Minimum Index of a Valid Split][title]
2+
3+
## Description
4+
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`.
5+
6+
You are given a **0-indexed** integer array `nums` of length `n` with one **dominant** element.
7+
8+
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:
9+
10+
- `0 <= i < n - 1`
11+
- `nums[0, ..., i]`, and `nums[i + 1, ..., n - 1]` have the same dominant element.
12+
13+
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.
14+
15+
Return the **minimum** index of a *8valid split**. If no valid split exists, return `-1`.
16+
17+
**Example 1:**
18+
19+
```
20+
Input: nums = [1,2,2,2]
21+
Output: 2
22+
Explanation: We can split the array at index 2 to obtain arrays [1,2,2] and [2].
23+
In array [1,2,2], element 2 is dominant since it occurs twice in the array and 2 * 2 > 3.
24+
In array [2], element 2 is dominant since it occurs once in the array and 1 * 2 > 1.
25+
Both [1,2,2] and [2] have the same dominant element as nums, so this is a valid split.
26+
It can be shown that index 2 is the minimum index of a valid split.
27+
```
28+
29+
**Example 2:**
30+
31+
```
32+
Input: nums = [2,1,3,1,1,1,7,1,2,1]
33+
Output: 4
34+
Explanation: We can split the array at index 4 to obtain arrays [2,1,3,1,1] and [1,7,1,2,1].
35+
In array [2,1,3,1,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5.
36+
In array [1,7,1,2,1], element 1 is dominant since it occurs thrice in the array and 3 * 2 > 5.
37+
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.
38+
It can be shown that index 4 is the minimum index of a valid split.
39+
```
40+
41+
**Example 3:**
42+
43+
```
44+
Input: nums = [3,3,3,3,7,2,2]
45+
Output: -1
46+
Explanation: It can be shown that there is no valid split.
47+
```
48+
49+
## 结语
50+
51+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
52+
53+
[title]: https://leetcode.com/problems/minimum-index-of-a-valid-split
54+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,57 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
l := len(nums)
5+
leftCnt := make(map[int]int)
6+
left := make([]int, l)
7+
m := -1
8+
for i, n := range nums {
9+
left[i] = -1
10+
leftCnt[n]++
11+
if m == -1 {
12+
m = n
13+
left[i] = m
14+
continue
15+
}
16+
selected := m
17+
if leftCnt[n] > leftCnt[m] {
18+
selected = n
19+
}
20+
21+
if leftCnt[selected] > (i+1)/2 {
22+
left[i] = selected
23+
}
24+
m = selected
25+
}
26+
27+
rightCnt := make(map[int]int)
28+
right := make([]int, l)
29+
m = -1
30+
for i := l - 1; i >= 0; i-- {
31+
n := nums[i]
32+
right[i] = -1
33+
rightCnt[n]++
34+
if m == -1 {
35+
m = n
36+
right[i] = m
37+
continue
38+
}
39+
selected := m
40+
if rightCnt[n] > rightCnt[m] {
41+
selected = n
42+
}
43+
if rightCnt[selected] > (l-i)/2 {
44+
right[i] = selected
45+
}
46+
m = selected
47+
}
48+
for i := 0; i < l-1; i++ {
49+
if left[i] == -1 || right[i] == -1 {
50+
continue
51+
}
52+
if left[i] == right[i+1] {
53+
return i
54+
}
55+
}
56+
return -1
557
}

leetcode/2701-2800/2780.Minimum-Index-of-a-Valid-Split/Solution_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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{1, 2, 2, 2}, 2},
17+
{"TestCase2", []int{2, 1, 3, 1, 1, 1, 7, 1, 2, 1}, 4},
18+
{"TestCase3", []int{3, 3, 3, 3, 7, 2, 2}, -1},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)