Simplified Blackjack
Last updated
Last updated
Consider a simplified game of blackjack / 21, with an infinite deck of cards labeled 1-10. That means there is always a 10% chance to draw any card value regardless of what's been drawn before. The dealer always plays deterministically, according to the following rules:
If the dealer's total is less than 17, the dealer draws another card.
If the dealer's total is at least 17, but not more than 21, the dealer stands.
If the dealer's total is over 21 the dealer has busted (lost).
Write a function to determine the probability the dealer will bust (given a particular hand).
The Idea: Like with many problems, often the best way to begin is to first go some examples just to make sure you understand the problem contextually. This is especially more important with mathematical problems such as these.
We know immediately that if the hand greater than 21, then the probability of busting is 100%. Similarly, if the hand is between 17 and 21, the dealer stands, so the probability of busting is 0%.
Now lets deal with the case when the hand is 16 or lower. This is where need to account for having to potentially draw another card. For example, if our hand is 15
, then 4/10
cards will favor the bust, and there is a 1/10
chance we will draw a 16
. In the case that we do, it will follow that we then have a 5/10
chance of then leading into a bust. Since both events have to happen and each is independent, the chances of it happening is 1/10*5/10
. In total we then have 4/10 + 5/100
in favor of a bust.
Notice how the entire tree sums to 1: P(bust0) + P(not bust) + P(bust1) = 4/10 + 5/10 + 10/100 = 1
Complexity: If your hand between 22 and N, then the complexity will be O(16-N!)
Complexity: O(1) time after preprocessing but technically since were only preprocessing numbers 1 - 16 (a constant amount), preprocessing is also finished in constant time. Constant space too.
As another example, consider if the hand is 14.Now there are 3/10 cards that can immediately result in a bust. However, now there is a 2/10 chance of having to redraw, both of which have thier own probabilities associated with them.
Notice that ALOT of computations are reused in the tree. So an incredibly faster approach to this problem would be to literally compute all the answers, and then just statically _return them when a function call asks for it. In the image below for example, a hand of _13 can reuse just take 1/10 of tree(14).