Code Interview Note
  • 0. Introduction
  • 1. Basic
    • Python Basic
    • Java Basic
    • Primitive Type
    • Basic Question
    • Number
  • 2. Array and Numbers
    • General
    • TwoSum
    • Buy and Sell Stock
    • SubArray
      • SubArray + HashMap
    • Sliding Window
      • Sliding Window At Most Problem
    • Word Break
    • Passes Problem
    • Majority Element
    • Partition Array
    • Sort Colors
    • Anagram
    • Ugly Number
    • TwoPointer
    • Swipe Line
    • Calculator
    • Sudoku
  • 2.1 String
    • String
    • Palindrome
    • Parentheses
    • Decode String
    • Calculator
    • Abbreviation
  • 3. Linkedlist
    • Dummy Node
    • Double Pointers
  • 4. Stack and Queue
    • General
    • Increase/Decrease Stack
  • 5. Binary Search
    • General
    • BS on result
    • Save the half which has result
    • Rotated Sorted Array
    • Split Array Largest Sum
  • 6. Binary Tree
    • General
    • Path Sum
    • Lowest Common Ancestor
    • BST
    • Convert
    • Traverse
    • Valid Ordered Tree
    • Construct Binary Tree
    • Tree depth and width
    • Vertical Order Traverse
  • 7. Heap
    • Geneal
    • TopK
  • 8. Simulation
    • General
    • Read4
    • Encode Decode
    • LRU/LFU
    • Robot
    • GetRandom O(1)
    • Probability
  • 9. DFS
    • Backtrack
    • General
    • Subset
    • Permutation
    • Combination
  • 10. HashTable
    • General
  • 11. Sort
    • General
  • 12. Recursion
    • General
  • 13. Dynamic Programming
    • Graph
    • General
    • Coordinate
    • Double Sequence
    • Longest Common Subsequence
    • Rolling Array
    • House Robber
    • Backpack
    • Memorization
    • Diagonal
  • 14. BFS
    • General
    • Number of Islands
    • The Maze
  • 15. Graph
    • Shortest Path
    • Undirected Graph
    • Topology Sort
    • Word Ladder
    • Tarjan's Algo
  • 16. Divide & Conquer
    • General
  • 17. UnionFind
    • General
    • Grouping
  • 18. Trie
    • General
    • Word Square
  • 19. Company Summary
    • Oracle
    • Amazon
      • DP
    • Google
    • Hackerrank
    • LinkedIn
  • 20. Design
  • 21. Math
  • Behavior Question
  • Internet
  • OS
Powered by GitBook
On this page
  • Shuffle Card
  • Select a random number from stream with equal probability

Was this helpful?

  1. 8. Simulation

Probability

PreviousGetRandom O(1)Next9. DFS

Last updated 5 years ago

Was this helpful?

Shuffle Card

Write a method to shuffle a deck of cards. It must be a perfect shuffle - in other words, each 52! permutations of the deck has to be equally likely. Assume that you are given a random number generator which is perfect

抽出的52!組合是同機率 1/52!

  • 抽的數字從array後面選,選完之後swap,之後每次選都是沒抽過的數字

void shuffleCard(int[] cards, int n){
    for(int i=0; i<cards.length; i++){
        int j=rand.nextInt(i, n);
        swap(cards[i], cards[j]);
    }
}

Select a random number from stream with equal probability

Given a stream of numbers, generate a random number from the stream. You are allowed to use only O(1) space and the probability of each number is 1/n, n is the number seen so far

  • init a count as 0, means the number has been seen so far

  • for each number x, generate ran number 0 to count-1. If ran is count-1, set ans to ran

  • 對於每一個數字而言, 當下被選中的機率是1/n

  • 之後每次iteration, 若n2沒被選中 (n-1)/n, 保留的數字可能是n-1中的一個 1/(n-1) => (n-1)/n * 1/(n-1) = 1/n => 機率仍然是 1/n

int count=0;
int ans=0;

int selectRandom(int x){
    count++;
    if(count==1) return x;

        Random ran = new Random();
        // generate a number for 0 to count-1
        int n = nextInt(count);
        if(n == count-1) ans = x
        return x;        
}
link