[Leetcode] Random Pick Index

No Comments

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);

[Problem Link]

  • Create a Map for every target -> range[]. Now, given a target we can query its range in O(1) time. Note, that length of this range is simply range[1]-range[0]+1.
  • Given a range (for any target), generate a random_number between 0 to length_of_range (Java’s Random class allows us to generate a random number between 0 (inclusive) to n (exclusive)). We then simple return i + random_number.
class Solution {
 
    Map<Integer, int[]> rangeMap;
    Random r;
 
    public Solution(int[] nums) {
        r = new Random();
        rangeMap = new HashMap<Integer, int[]>();
        int i = 0;
        while ( i < nums.length ) {
            int num = nums[i];
            int j = i;
            while ( j < nums.length && nums[j] == num )
                j++;
            int range[] = { i, j-1 };
            rangeMap.put(num, range);
            i = j;
        }        
    }
 
    public int pick(int target) {
        int range[] = rangeMap.get(target);
        return range[0] + r.nextInt(range[1]-range[0]+1);
    }
}
 
/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(nums);
 * int param_1 = obj.pick(target);
 */

[Leetcode] Moving Average from Data Stream

No Comments

Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window.

Example:

MovingAverage m = new MovingAverage(3);
m.next(1) = 1
m.next(10) = (1 + 10) / 2
m.next(3) = (1 + 10 + 3) / 3
m.next(5) = (10 + 3 + 5) / 3

[Problem Link]

Use (and keep updating) the running total of the List to help calculate the moving average in O(1) time.

class MovingAverage {
 
    List<Integer> nums;
    double sum;
    int size;
 
    /** Initialize your data structure here. */
    public MovingAverage(int size) {
        this.nums = new ArrayList<Integer>();
        this.sum = 0;
        this.size = size;
    }
 
    public double next(int val) {
        nums.add(val);
        if ( nums.size() <= this.size ) {
            this.sum += val;
            return this.sum/nums.size();
        }        
        int num = nums.remove(0);
        this.sum = this.sum - num + val;
        return this.sum/nums.size();
    }
}
 
/**
 * Your MovingAverage object will be instantiated and called as such:
 * MovingAverage obj = new MovingAverage(size);
 * double param_1 = obj.next(val);
 */