zoukankan      html  css  js  c++  java
  • 381. Insert Delete GetRandom O(1)

    package LeetCode_381
    
    /**
     * 381. Insert Delete GetRandom O(1) - Duplicates allowed
     * https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/description/
     *
     * 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.
     * */
    class RandomizedCollection() {
        /*
        *solution: HashMap+Array;
        * key of HashMap: number, value of HashMap is the list of index of the element in array,
        * Array<Pair<Int,Int>>, first: element, second: the index of element in the value(index list) of HashMap,
        * insert O(1): HashMap,
        * delete O(1), HashMap,
        * getRandom O(1), Array, because it save item in memory continuously
        * */
    
        /** Initialize your data structure here. */
        val map = HashMap<Int, ArrayList<Int>>()
        val list = ArrayList<Pair<Int, Int>>()
        val random = java.util.Random()
    
        /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
        fun insert(`val`: Int): Boolean {
            val indexList = map.getOrDefault(`val`, ArrayList())
            //add the index of element in array
            indexList.add(list.size)
            map.put(`val`, indexList)
            list.add(Pair(`val`, indexList.lastIndex))
            return indexList.size == 1
        }
    
        /** Removes a value from the collection. Returns true if the collection contained the specified element. */
        fun remove(`val`: Int): Boolean {
            if (!map.containsKey(`val`)) {
                return false
            }
            val indexList = map.get(`val`)
            val indexNeedEvict = indexList!!.get(indexList.lastIndex)
            val lastPair = list.get(list.lastIndex)
    
            //update index, replace the position between the element need to delete and the last element
            map.get(lastPair.first)!!.set(lastPair.second, indexNeedEvict)
            //swap
            list.set(indexNeedEvict, lastPair)
    
            //clean it
            list.removeAt(list.lastIndex)
            indexList.removeAt(indexList.lastIndex)
            if (indexList.size == 0) {
                map.remove(`val`)
            }
            return true
        }
    
        /** Get a random element from the collection. */
        fun getRandom(): Int {
            val randomIndex = random.nextInt(list.size)
            return list.get(randomIndex).first
        }
    }
    
    /**
     * Your RandomizedCollection object will be instantiated and called as such:
     * var obj = RandomizedCollection()
     * var param_1 = obj.insert(`val`)
     * var param_2 = obj.remove(`val`)
     * var param_3 = obj.getRandom()
     */

    related:

    380. Insert Delete GetRandom O(1)

  • 相关阅读:
    作为一名程序员应该具备哪些素质
    从100万个整数里找出100个最大的数
    数据库 SQL语句优化
    服务器上文件打包下载
    ThreadPoolExcutor
    几种序列化与get、set方法的关系
    idea没有错误出现红色波浪线怎么去掉?
    两个对象的属性赋值
    RandomStringUtils的使用
    IDEA中类似eclipse的workSpace的实现
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13621295.html
Copyright © 2011-2022 走看看