[LeetCode] 2780. Minimum Index of a Valid Split

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 valid 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.

Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
nums has exactly one dominant element.

合法分割的最小下标。

如果在长度为 m 的整数数组 arr 中 超过一半 的元素值为 x,那么我们称 x 是 支配元素 。

给你一个下标从 0 开始长度为 n 的整数数组 nums ,数据保证它含有一个 支配 元素。

你需要在下标 i 处将 nums 分割成两个数组 nums[0, ..., i] 和 nums[i + 1, ..., n - 1] ,如果一个分割满足以下条件,我们称它是 合法 的:

  • 0 <= i < n - 1
  • nums[0, ..., i] 和 nums[i + 1, ..., n - 1] 的支配元素相同。
    这里, nums[i, ..., j] 表示 nums 的一个子数组,它开始于下标 i ,结束于下标 j ,两个端点都包含在子数组内。特别地,如果 j < i ,那么 nums[i, ..., j] 表示一个空数组。

请你返回一个 合法分割 的 最小 下标。如果合法分割不存在,返回 -1 。

思路

遍历两遍数组。第一遍扫描的时候用投票法找到 input 数组里的众数,记为 candidate。第二遍扫描的时候,根据当前的下标 i 分别计算 i 左侧和右侧的众数是不是依然是candidate,如果是,那么这个下标 i 就是一个合法的分割点。找到第一个分割点即可返回了。

复杂度

时间O(n)
空间O(1)

代码

Java实现

class Solution {
    public int minimumIndex(List<Integer> nums) {
        int candidate = nums.get(0);
        int count = 1;
        int n = nums.size();

        // 使用 Boyer-Moore 投票法找到可能的众数
        for (int i = 1; i < n; i++) {
            if (nums.get(i) == candidate) {
                count++;
            } else {
                count--;
            }
            if (count == 0) {
                candidate = nums.get(i);
                count = 1;
            }
        }

        // 计算 candidate 在整个 nums 数组中出现的真实次数
        int totalCount = 0;
        for (int num : nums) {
            if (num == candidate) {
                totalCount++;
            }
        }

        // 再次遍历数组,寻找符合条件的最小索引
        int left = 0;
        for (int i = 0; i < n; i++) {
            if (nums.get(i) == candidate) {
                left++;
            }
            int right = totalCount - left; // 右侧 candidate 的个数
            if ((left * 2 > i + 1) && (right * 2 > n - i - 1)) {
                return i;
            }
        }
        return -1;
    }
}

相关题目

169. Majority Element
229. Majority Element II
2780. Minimum Index of a Valid Split
posted @ 2025-03-28 01:28  CNoodle  阅读(19)  评论(0)    收藏  举报