LeetCode-398 随机数索引
来源:力扣(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 随机数索引的更多相关文章
- Java实现 LeetCode 398 随机数索引
398. 随机数索引 给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中. 注意: 数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试 ...
- C#刷遍Leetcode系列连载 索引
C#刷遍Leetcode系列文章 索引 索引(陆续发布中,请保持关注) C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - ...
- 398 Random Pick Index 随机数索引
给定一个可能含有重复元素的整数数组,要求随机输出给定的数字的索引. 您可以假设给定的数字一定存在于数组中.注意:数组大小可能非常大. 使用太多额外空间的解决方案将不会通过测试.示例:int[] num ...
- LeetCode 解题报告索引
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中...... ...
- [Swift]LeetCode398. 随机数索引 | Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- [leetcode] 398. Random Pick Index
我是链接 看到这道题,想到做的几道什么洗牌的题,感觉自己不是很熟,但也就是rand()函数的调用,刚开始用map<int, vector<int >>来做,tle,后来就想着直 ...
- LeetCode题目答案索引
LeetCode-Two Sum LeetCode-Median of Two Sorted Arrays LeetCode-Longest Substring Without Repeating C ...
- [LeetCode] 398. Random Pick Index ☆☆☆
Given an array of integers with possible duplicates, randomly output the index of a given target num ...
- 【数据结构与算法】蓄水池抽样算法(Reservoir Sampling)
问题描述 给定一个数据流,数据流长度 N 很大,且 N 直到处理完所有数据之前都不可知,请问如何在只遍历一遍数据(O(N))的情况下,能够随机选取出 m 个不重复的数据. 比较直接的想法是利用随机数算 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 用openpyxl创建工作簿和工作表
import osimport openpyxl #设置默认路径os.chdir(r'D:/openpyxl/') #创建工作簿变量 wb = openpyxl.Workbook() #创建工作表变量 ...
- LoadRunner11录制脚本
1.打开LoadRunner11后界面如下: 2.点击"创建/编辑脚本",会打开一个新窗口,如下: 3.这里新建一个web/html格式的测试.点击"文件"-& ...
- django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: rest_framework_swagger
在启动服务时报django.core.exceptions.ImproperlyConfigured: Application labels aren't unique, duplicates: re ...
- python 之 random.sample() 报ValueError: Sample larger than population or is negative
def device_id(): device = ''.join(random.sample(string.digits, 19)) return device print(device_id()) ...
- 分享.net framework4.0无法安装的几种处理方案.
[关于.net framework4.0安装失败]-------------)方案1:http://www.win7xtzj.com/win10jiaocheng/39834.html 关键词: -- ...
- [编程基础] Python对象的浅拷贝与深拷贝笔记
Python中的赋值语句不创建对象的副本,它们只将名称绑定到对象.对于不可变的对象,这通常没有什么区别.但是对于处理可变对象或可变对象的集合,您可能需要寻找一种方法来创建这些对象的"真实副本 ...
- python之路26 面向对象魔法方法、元类、元类定制类、对象的产生行为 __new__方法
面向对象的魔法方法 魔法方法:类中定义的双下方法都称为魔法方法 不需要人为调用 在特定的条件下会自动触发运行 eg:__init__创建空对象之后自动触发给对象添加独有的数据 1.__init__ 对 ...
- solidity 内存(memory) 可变数组的增删改查 操作
// SPDX-License-Identifier: MIT pragma solidity ^0.8.9; library Array { function push(uint256[] memo ...
- 双层拖拽事件,用鼠标画矩形,拖动右下角可以再次改变矩形大小,方案一 有BUG
<template> <div class="mycanvas-container"> <vue-drag-resize :isActive = 't ...
- 包子类&包子铺类-吃货类&测试类
包子类&包子铺类 资源类:包子类设置包子的属性皮陷包子的状态:有true,没有false package Demo01.WaitAndNotify; /** * 资源类:包子类设置包子的属性 ...