Git Product home page Git Product logo

random-pick-index's Introduction

Random pick index

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.

Note:

The array size can be very large. Solution that uses too much extra space will not pass the judge.

Example:

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

// pick(3) should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(3);

// pick(1) should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(1);

Implementation 1 : Naive

class Solution {
    Map<Integer,List<Integer>> map;
    Random random; 
    
    public Solution(int[] nums) {
        map = new HashMap<Integer, List<Integer>>();
        random = new Random();
        for(int i = 0; i < nums.length; i++) {
            map.putIfAbsent(nums[i], new ArrayList<Integer>());
            map.get(nums[i]).add(i);
        }
    }
    
    public int pick(int target) {
        int occurances = map.get(target).size();  
        int randomIndex = random.nextInt(occurances);
        return map.get(target).get(randomIndex);
    }
}

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

Implementation 2 : No Extra Space

class Solution {
    int[] nums;
    Random random;
    
    public Solution(int[] nums) {
       this.nums =  nums;
       random = new Random(); 
    }
    
    public int pick(int target) {
        int count = 0;
        int index = 0;
        
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] == target) {
                count++;
                // This if changes the probability of an index i to be selected
                if(random.nextInt(count) == 0) { 
                    index = i;
                }
            }
        }
        return index;
    }
}

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

Tip : To better understand the second implementation, run through some examples. e.g. int[] nums = [1, 2, 3] , target = 3 e.g. int[] nums = [1, 2, 3, 3] , target = 3 e.g. int[] nums = [1, 2, 3, 3, 3] , target = 3

References :

https://www.youtube.com/watch?v=Tt1j0mVzHdg

random-pick-index's People

Contributors

emahtab avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.