How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focusโor scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
๐ง Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
๐ Select Passive to activate
Difficulty: Medium
Topics: Dynamic Programming, Probability, Sliding Window
Alice is playing a game where she keeps drawing points randomly.
Hereโs how it works:
๐ Your goal: Find the probability that her final score is less than or equal to n
This is not a typical problem โ itโs about probability accumulation.
Think of it like:
๐ At every step, Alice has multiple possible scores
๐ Each score has a probability
๐ We need to track how these probabilities evolve
[k, k + maxPts - 1]
๐ We only care about: Which of these final scores are โค n
We use Dynamic Programming:
dp[i] = probability of getting exactly i pointsdp[0] = 1 (100% probability)
Instead of recalculating probabilities again and again:
๐ Maintain a running sum of last maxPts values
This reduces time complexity significantly.
This problem becomes much easier when visualized:
๐ Using DrawToCode, you can:
class Solution {
public double new21Game(int n, int k, int maxPts) {
if (k == 0 || n >= k + maxPts) return 1.0;
double[] dp = new double[n + 1];
dp[0] = 1.0;
double windowSum = 1.0;
double result = 0.0;
for (int i = 1; i <= n; i++) {
dp[i] = windowSum / maxPts;
if (i < k) {
windowSum += dp[i];
} else {
result += dp[i];
}
if (i - maxPts >= 0) {
windowSum -= dp[i - maxPts];
}
}
return result;
}
}
n = 6, k = 1, maxPts = 10
๐ Probability:
6 / 10 = 0.6
n = 21, k = 17, maxPts = 10
๐ Alice keeps drawing until she reaches at least 17
๐ Final scores range from 17 to 26
We calculate probability of scores โค 21 using DP
๐ Output:
0.73278
k = 0 โ Alice never draws โ probability = 1n >= k + maxPts โ all outcomes are valid โ probability = 1This problem teaches:
๐ Once you visualize probability flow, this problem becomes much easier to understand
โ Written by Saurabh Patil โข B.Tech CSE โข Software Developer๐ฏ Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
๐ Locked steps cannot be moved. Only unlocked steps are draggable.
๐ Enable sound for swap feedback and completion effects.