Leetcode 381. Insert Delete GetRandom O(1) - Duplicates allowed 可重复常数增删改系统 解题报告

本文介绍LeetCode 381题目的解题思路及实现细节,该题目要求设计一个支持插入、删除和随机获取数据的操作,并且允许元素重复的数据结构,所有操作需在平均O(1)时间内完成。

1 解题思想

这道题是380的进化版,其实也就是在插入的时候允许了重复的值,变的更加复杂了一些。
这道题是基于这题来的,所以请先看下这题,我只说下如何从380过渡到381
Leetcode 380. Insert Delete GetRandom O(1) 常数增删改系统 解题报告

因为一个值要多个位置,所以原来的那个HashMap肯定要从<值-位置>,改成<值-位置的集合>

那么查询还是直接操作ArrayList没有难度
而插入的时候,是插入到位置集合也没有难度

问题在于删除。

删除的时候,idea和之前380一样,同样是将ArrayList的最后一个交换到前面,但是现在一个值有多个位置,所以我们要交换对位置,不能和原来一样直接替换。

所以根据值的集合选的不同,有所不同:
如你可以使用:
ArrayList来存储集合,那么你替换的时候需要遍历一下位置替换
我选择了Stack,那么就需要保证出栈的顺序有保障

或者其他更高级的快速的结构,随你,只要方便就好。。思想就是380的拓展,懂了380这个自然能变

2 原题

Design a data structure that supports all following operations in average O(1) time.
Note: Duplicate elements are allowed. 

insert(val): Inserts an item val to the collection.
remove(val): Removes an item val from the collection if present.
getRandom: Returns a random element from current collection of elements. The probability of each element being returned is linearly related to the number of same value the collection contains.

Example: 
// Init an empty collection.
RandomizedCollection collection = new RandomizedCollection();

// Inserts 1 to the collection. Returns true as the collection did not contain 1.
collection.insert(1);

// Inserts another 1 to the collection. Returns false as the collection contained 1. Collection now contains [1,1].
collection.insert(1);

// Inserts 2 to the collection, returns true. Collection now contains [1,1,2].
collection.insert(2);

// getRandom should return 1 with the probability 2/3, and returns 2 with the probability 1/3.
collection.getRandom();

// Removes 1 from the collection, returns true. Collection now contains [1,2].
collection.remove(1);

// getRandom should return 1 and 2 both equally likely.
collection.getRandom();

3 AC解

public class RandomizedCollection {
    //用来索引下在ArrayList中的关系,做到快速定位
    private HashMap<Integer,Stack<Integer>> locations;
    //存储原始数据
    private ArrayList<Integer> list;
    private Random random;
    //为了排序,控制栈的
    ArrayList<Integer> tmpForStack;

    /** Initialize your data structure here. */
    public RandomizedCollection() {
        this.locations = new HashMap<Integer,Stack<Integer>>();
        this.list = new ArrayList<Integer>();
        this.random = new Random();
        this.tmpForStack = new ArrayList<Integer>();
    }


    /**
     * 直接插入
     * */
    public boolean insert(int val) {
        boolean flag = false;
        if( locations.containsKey(val) == false ){
            locations.put(val,new Stack<Integer>());
        }
        if(locations.get(val).isEmpty())
            flag = true;
        locations.get(val).push(list.size());
        list.add(val);
        return flag;
    }

    /** Removes a value from the set. Returns true if the set contained the specified element. */
    /**
     * 核心思想在于:把删除的元素交换到ArrayList最后一位上,这样就可以O1完成了,
     * 
     * 唯一的一点不同是,要保证在每一个元素对应的位置的栈,要保证其顺序是从小到大,每次出栈的一定是最大的,所以我这里交换List后,更新那个元素的栈时需要先把比他大的弹出来,再放回去
     * */
    public boolean remove(int val) {
        if( locations.containsKey(val) == false || locations.get(val).isEmpty() ) return false;
        int tmpLocation = locations.get(val).pop();
        if(tmpLocation != list.size() - 1){
            int lastVal = list.get(list.size() -1 );
            list.set(tmpLocation,lastVal);
            locations.get(lastVal).pop();
            tmpForStack.clear();
            while(locations.get(lastVal).isEmpty() == false && locations.get(lastVal).peek() > tmpLocation){
                tmpForStack.add(locations.get(lastVal).pop());
            }
            locations.get(lastVal).push(tmpLocation);
            for(int tval:tmpForStack)
                locations.get(lastVal).push(tval);
        }
        list.remove(list.size() - 1);
        return true;
    }

    /** Get a random element from the set. */
    public int getRandom() {
        return list.get(random.nextInt(list.size()));
    }
}

/**
 * Your RandomizedCollection object will be instantiated and called as such:
 * RandomizedCollection obj = new RandomizedCollection();
 * boolean param_1 = obj.insert(val);
 * boolean param_2 = obj.remove(val);
 * int param_3 = obj.getRandom();
 */
YOLOv11人物目标检测数据集 目标类别:['Persona'] 中文类别:['人物'] 训练集:1683 张 验证集:182 张 测试集:0 张 总计:1865 张 该数据集提供了data.yaml文件,内容如下: train: ../train/images val: ../valid/images test: ../test/images nc: 1 names: ['Persona'] YOLOv11城市街头与室内场景人物目标检测数据集 该数据集涵盖了城市街头、商业街区、办公场所及室内生活等多种场景中的人物目标检测,具有广泛的应用价值。通过多样的场景设置和丰富的拍摄角度,能够有效提升模型在复杂环境下的目标识别能力,为智慧城市、安防监控及人机交互等领域提供高质量的训练数据支持。 从数据分布来看,该数据集包含1683张训练集图像、182张验证集图像以及0张测试集图像,整体比例分配合理。训练集与验证集的比例约为9.25:1,能够充分满足模型训练与性能评估的需求,确保模型在不同场景下的泛化能力得到充分验证。 该数据集的标注工作严谨规范,所有图像均经过人工精确标注,每个目标都配有清晰的边界框和统一的标签“Persona”。标注过程遵循严格的标准,确保了数据的高质量和一致性,为后续的模型训练和性能优化提供了可靠的基础。 基于其多样化的场景覆盖和高质量的标注数据,该数据集可广泛应用于智慧城市管理、公共场所安防监控、智能零售分析以及家庭安防等多个领域。特别是在需要精准识别复杂环境中人物的目标检测任务中,该数据集能够显著提升模型的准确性和鲁棒性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值