Easy to understand Java solution with probability calculation examples
\nclass Solution {\n    public double new21Game(int N, int K, int W) {\n        if (N >= K + W - 1)\n            return 1.0;\n\n        double[] p = new double[
```\nclass Solution {\n    public double new21Game(int N, int K, int W) {\n        if (N >= K + W - 1)\n            return 1.0;\n\n        double[] p = new double[N + 1];\n\n        // p[i] = probability of being able to sum up to i\n        // eg:\n        // W = 5 i.e cards = 1, 2, 3, 4, 5\n        // 1/W = probability of picking any one of the above to make sum = i\n        // p[3] = (p[2] + p[1] + p[0]) * 1/W\n        // if we look closely here we will be either picking 1, 2 or 3 to make our\n        // sum 3 for p[2] will pick 1, for p[1] pick 2 and p[0] pick 3. Cannot go\n        // for the whole range till 5 because p[3 - 3] is the last number in the\n        // range that has a probability\n        // similaryly\n        // p[6] = (p[5] + p[4] + p[3] + p[2] + p[1]) * 1/W\n        // lets say we have K = 7 and N = 10 in that case\n        // for i <= K\n        // p[i] = (p[i - 1] + p[i - 2] + p[i - 3] + ... p[i - W]) * 1 / W\n        // If we look closely here we are kind of moving in a sliding window\n        // for eg. for p[6] the sum of previous numbers will be \n        // p[5] + p[4] + p[3] + p[2] + p[1]\n        // and for p[7] previous sum will be\n        // p[6] + p[5] + p[4] + p[3] + p[2]\n        // we only need the sum of numbers within the range W before i\n        // therefore we added p[7 - 1] = p[6] and removed p[7 - 1 - W] = p[6 - 5] = [1]\n        p[0] = 1.0;\n\n        double prev = 0.0;\n        // for all i <= k\n        for (int i = 1; i <= K; i++) {\n            // add previously calculated p and remove last element\n            // in prev which will be out of the window if it exits\n            // eg for i = 6, add p[5] to running sum and remove\n            // p[5 - W] since i = 6, p[6 - 1 - W]\n            prev += p[i - 1] - ((i - W - 1) >= 0 ? p[i - 1 - W]: 0);\n            p[i] = prev * 1 / (W * 1.0);\n        }\n\n        double res = p[K];\n        // for all i > K\n        // Also notice here\n        // consider the same eg W = 5, K = 7, N = 10\n        // sum for p[8] = p[7] + p[6] + p[5] + p[4] + p[3]\n        // sum for p[9] = p[7] + p[6] + p[5] + p[4]\n        // sum for p[10] = p[7] + p[6] + p[5]\n        // we do not add up the values of p[9] and p[8]\n        // Which means we stop at p[9] or p[8] if we ever pick\n        // 2 after having sum = 6 or 3 after sum = 6\n        for (int i = K + 1; i < N + 1; i++) {\n            // we just remove the last element and not add the i - 1st\n            // element because we have already passed K and we do not \n            // need previously calculated values, we just remove the ones\n            // that go out of our current i\'s range\n            prev -= (i - 1 - W >= 0 ? p[i - 1 - W] : 0);\n            p[i] = prev * 1 / (W * 1.0);\n            res += p[i];\n        }\n\n\t\treturn res;\n    }\n\n}\n```
[]