|
| 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 |
0 commit comments