来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/random-pick-index

题目描述

给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引。 您可以假设给定的数字一定存在于数组中。

注意:
数组大小可能非常大。 使用太多额外空间的解决方案将不会通过测试。

示例:

int[] nums = new int[] {1,2,3,3,3};
Solution solution = new Solution(nums);

// pick(3) 应该返回索引 2,3 或者 4。每个索引的返回概率应该相等。
solution.pick(3);

// pick(1) 应该返回 0。因为只有nums[0]等于1。
solution.pick(1);

解题思路

很明显这个题的场景是数据太大,内存无法全部加载,所以需要想办法压缩数据。

思路有两个:

一个是压缩数据,因为存在重复数据,所以可以合并数据并且记录重复次数,这样初始化时间复杂度为O(n), pick时间复杂度为O(1).

一个是使用数学规律,并不需要加载数据,所以初始化复杂度为O(1),但是在pick的时候记录遇到target的次数cnt,然后取一个[0,cnt)的随机数,如果这个随机数为0 则取这个索引,可以证明每个索引的概率是相同的。

证明:假设有k个target,那么在第cnt次取到索引的概率是

代码展示

哈希表暴力法:

class Solution {
public:
unordered_map<int, vector<int>> m_Nums;
Solution(vector<int>& nums) {
for(int i = 0; i < nums.size(); i++)
{
m_Nums[nums[i]].push_back(i);
}
} int pick(int target) {
int iRand = rand() % m_Nums[target].size();
return m_Nums[target][iRand];
}
}; /**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(nums);
* int param_1 = obj->pick(target);
*/

水池抽样法:

class Solution {
public:
vector<int> m_Nums;
Solution(vector<int>& nums)
:m_Nums(nums)
{
} int pick(int target) {
int iCnt = 0, iIndex;
for(int i = 0; i < m_Nums.size(); i++)
{
if(target == m_Nums[i])
{
iCnt++;
if(rand() % iCnt == 0)
iIndex = i;
}
}
return iIndex;
}
}; /**
* Your Solution object will be instantiated and called as such:
* Solution* obj = new Solution(nums);
* int param_1 = obj->pick(target);
*/

运行结果

LeetCode-398 随机数索引的更多相关文章

  1. Java实现 LeetCode 398 随机数索引

    398. 随机数索引 给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中. 注意: 数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试 ...

  2. C#刷遍Leetcode系列连载 索引

    C#刷遍Leetcode系列文章 索引 索引(陆续发布中,请保持关注) C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - ...

  3. 398 Random Pick Index 随机数索引

    给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] num ...

  4. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  5. [Swift]LeetCode398. 随机数索引 | Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  6. [leetcode] 398. Random Pick Index

    我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...

  7. LeetCode题目答案索引

    LeetCode-Two Sum LeetCode-Median of Two Sorted Arrays LeetCode-Longest Substring Without Repeating C ...

  8. [LeetCode] 398. Random Pick Index ☆☆☆

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  9. 【数据结构与算法】蓄水池抽样算法(Reservoir Sampling)

    问题描述 给定一个数据流,数据流长度 N 很大,且 N 直到处理完所有数据之前都不可知,请问如何在只遍历一遍数据(O(N))的情况下,能够随机选取出 m 个不重复的数据. 比较直接的想法是利用随机数算 ...

  10. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. 输出图形字符的命令 banner

    输出图形字符的命令 banner 有趣的 Linux 命令.来自实验楼操作系统课程 安装 sudo apt install sysvbanner 截图 其他 还有两个类似的命令toilet,figle ...

  2. Vue快速上门(2)-模板语法

    VUE家族系列: Vue快速上门(1)-基础知识 Vue快速上门(2)-模板语法 Vue快速上门(3)-组件与复用 01.模板语法 1.1.template模板 <template>是H5 ...

  3. TCP协议三握四挥、socket模块

    目录 传输层之TCP与UDP协议 应用层 socket模块 socket代码简介 代码优化 半连接池的概念 传输层之TCP与UDP协议 用于应用程序之间的通信 TCP与UDP都是用来规定通信方式的 ​ ...

  4. 在CentOS8中安装gitlab

    安装 docker 及 docker-compose centos8 更新源 cd /etc/yum.repos.d/ sed -i 's/mirrorlist/#mirrorlist/g' /etc ...

  5. javascript计算器

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. [深度学习] Python人脸识别库face_recognition使用教程

    Python人脸识别库face_recognition使用教程 face_recognition号称是世界上最简单的开源人脸识别库,可以通过Python或命令行识别和操作人脸.face_recogni ...

  7. [python]《Python编程快速上手:让繁琐工作自动化》学习笔记7

    1. 用GUI 自动化控制键盘和鼠标第18章 (代码下载) pyautogui模块可以向Windows.OS X 和Linux 发送虚拟按键和鼠标点击.根据使用的操作系统,在安装pyautogui之前 ...

  8. P8340 [AHOI2022] 山河重整

    \(20pts\) 给 \(O(2^n)\) 枚举,\(60pts\) 是 \(O(n^2)\),先看看怎么做.计数题无非容斥和 \(dp\),不妨从 \(dp\) 入手.多项式复杂度的做法意味着无法 ...

  9. vue-element-admin 安装(node方式)

    vue-element-admin node方式安装 需要的环境: git .node.js 1.git clone 项目(2选 1) // github https://github.com/Pan ...

  10. python进阶之路13 二分法 三元表达式 各种生成式 匿名函数

    算法简介及二分法 1.什么是算法 算法就是解决问题的有效方法 不是所有的算法都很高效也有不合格的算法 2.算法应用场景 推荐算法(抖音视频推送 淘宝商品推送) 成像算法(AI相关)...... 几乎涵 ...