1157. Online Majority Element In Subarray

本文介绍了一个名为MajorityChecker的类的实现细节,该类提供了查询数组子区间中多数元素的功能。通过两种不同的算法策略,一种是基于Boyer-Moore投票算法的直接搜索,另一种是结合哈希映射与随机抽样的高效查询,文章深入探讨了如何在给定阈值下找到数组子区间内的多数元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Implementing the class MajorityChecker, which has the following API:

MajorityChecker(int[] arr) constructs an instance of MajorityChecker with the given array arr;
int query(int left, int right, int threshold) has arguments such that:
0 <= left <= right < arr.length representing a subarray of arr;
2 * threshold > right - left + 1, ie. the threshold is always a strict majority of the length of the subarray
Each query(…) returns the element in arr[left], arr[left+1], …, arr[right] that occurs at least threshold times, or -1 if no such element exists.

Example:

MajorityChecker majorityChecker = new MajorityChecker([1,1,2,2,1,1]);
majorityChecker.query(0,5,4); // returns 1
majorityChecker.query(0,3,3); // returns -1
majorityChecker.query(2,3,2); // returns 2

Constraints:

1 <= arr.length <= 20000
1 <= arr[i] <= 20000
For each query, 0 <= left <= right < len(arr)
For each query, 2 * threshold > right - left + 1
The number of queries is at most 10000

三国杀理论:
class MajorityChecker {

    int[] nums;
    
    public MajorityChecker(int[] arr) {
        nums = arr;
    }
    
    public int query(int left, int right, int threshold) {
        int count = 0;
        int num = 0;
        for (int i = left; i <= right; i++) {
            if (nums[i] == num) count++;
            else {
                if (count == 0) num = nums[i];
                else count--;
            }
        }
        count = 0;
        for (int i = left; i <= right; i++) {
            if (nums[i] == num) count++;
        }
        return count >= threshold ? num : -1;
    }
}

/**
 * Your MajorityChecker object will be instantiated and called as such:
 * MajorityChecker obj = new MajorityChecker(arr);
 * int param_1 = obj.query(left,right,threshold);
 */
概率角度:1/2的概率抽不中,那么抽20次还抽不中的概率是1/1000000,因此我们认为接近于0.
class MajorityChecker {
    HashMap<Integer, List<Integer>> map = new HashMap<>();
    Random random;
    int[] arr;
    
    public MajorityChecker(int[] arr) {
        this.arr = arr;
        for (int i = 0; i < arr.length; i++) {
            if (!map.containsKey(arr[i])) map.put(arr[i], new ArrayList<>());
            map.get(arr[i]).add(i);
        }
        random = new Random();
    }
    
    public int query(int left, int right, int threshold) {
        int range = right - left + 1;
        for (int i = 0; i < 20; i++) {
            int r = random.nextInt(range);
            int num = arr[left + r];
            List<Integer> list = map.get(num);
            int index1 = Collections.binarySearch(list, left);
            int index2 = Collections.binarySearch(list, right);
            if (index1 < 0) index1 = -index1-1;
            if (index2 < 0) index2 = -index2-2;
            if (index2 - index1 + 1 >= threshold) return num;
        }
        return -1;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值