LeetCode 398 随机数索引[水塘抽样] HERODING的LeetCode之路

在这里插入图片描述

解题思路

第一个方法是哈希+水塘抽样,即用哈希表存储每个num的下标数组,然后pick阶段在每个下标数组中进行水塘抽样,水塘抽样可以将空间复杂度降为O(1),原理如下:
图片.png

代码

class Solution {
private:
    unordered_map<int, vector<int>> mp;
public:
    Solution(vector<int>& nums) {
        for(int i = 0; i < nums.size(); i ++) {
            mp[nums[i]].emplace_back(i);
        }
    }
    
    int pick(int target) {
        int n = mp[target].size();
        int index = mp[target][0];
        for(int i = 0; i < n; i ++) {
            if(rand() % (i + 1) == 0) {
                index = mp[target][i];
            }
        }
        return index;
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(nums);
 * int param_1 = obj->pick(target);
 */

但是思考一下可以发现,这样做简直就是画蛇添足,因为空间已经被占用了,那么水塘抽样更大可不必,空间复杂度没优化不说还增大了时间复杂度,所以修改一下如下:

class Solution {
private:
    unordered_map<int, vector<int>> mp;
public:
    Solution(vector<int>& nums) {
        for(int i = 0; i < nums.size(); i ++) {
            mp[nums[i]].emplace_back(i);
        }
    }
    
    int pick(int target) {
        int n = mp[target].size();
        int index = mp[target][0];
        for(int i = 0; i < n; i ++) {
            if(rand() % (i + 1) == 0) {
                index = mp[target][i];
            }
        }
        return index;
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(nums);
 * int param_1 = obj->pick(target);
 */

那么如何利用水塘抽样的优势呢,可以定义一个vector的引用,直接绑定数组本身,再使用水塘抽样,那么不会耗费额外的空间复杂度了,代码如下:

class Solution {
private:
    vector<int>& nums;
public:
    Solution(vector<int>& nums) : nums(nums){
        //nums = nums;
    } 
    int pick(int target) {
        int n = nums.size();
        int index = 1;
        int ans = 0;
        for(int i = 0; i < n; i ++) {
            if(nums[i] == target) {
                if(rand() % index == 0) {
                    ans = i;
                }
                index ++;
            }
        }
        return ans;
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(nums);
 * int param_1 = obj->pick(target);
 */
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HERODING77

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值