Image Explanation🏆- [Complete Intuition - Maths, Probability, DP, Sliding Window] - C++/Java/Python
Video Solution (Aryan Mittal) - Link in LeetCode Profile\nNew 21 Game by Aryan Mittal\n\n\n\n# Approach & Intution\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n# Code
# Video Solution (`Aryan Mittal`) - Link in LeetCode Profile\n`New 21 Game` by `Aryan Mittal`\n![lc.png](https://assets.leetcode.com/users/images/ce5147fe-97f0-487f-8037-acee7774c4cc_1684988558.8669872.png)\n\n\n# Approach & Intution\n![image.png](https://assets.leetcode.com/users/images/86ee1163-deca-4b59-9703-2e164430f40b_1684987829.4958892.png)\n![image.png](https://assets.leetcode.com/users/images/b57d72a3-d6b0-4fd2-98d7-c0098982f1a7_1684987836.61083.png)\n![image.png](https://assets.leetcode.com/users/images/1f42f5ee-38aa-475f-930f-392a5baecfa0_1684987843.2183628.png)\n![image.png](https://assets.leetcode.com/users/images/2bd7905b-927a-40fc-8941-c1dea17ed6b6_1684987849.5927286.png)\n![image.png](https://assets.leetcode.com/users/images/cda91bd7-1e20-495b-b80e-6b5bf71cf599_1684987855.1874256.png)\n![image.png](https://assets.leetcode.com/users/images/a4fca16b-385d-4ea4-a5a3-8b4246b7bf65_1684987861.9208536.png)\n![image.png](https://assets.leetcode.com/users/images/39328460-c540-4259-8414-e6e9364b3828_1684987869.604397.png)\n![image.png](https://assets.leetcode.com/users/images/ba3ed311-c37f-4517-bf54-d572f176f762_1684987880.2754056.png)\n![image.png](https://assets.leetcode.com/users/images/73db129d-2917-4013-a677-8d8dc64414ff_1684987890.0187504.png)\n![image.png](https://assets.leetcode.com/users/images/5e6b82a8-e01b-4fff-b4f1-3a7ce0e8ae6f_1684987903.0567613.png)\n![image.png](https://assets.leetcode.com/users/images/e90a04fa-f2d0-47d2-96fa-2eae27cd3920_1684987910.9672809.png)\n![image.png](https://assets.leetcode.com/users/images/97c46d26-17eb-4af3-991b-16f8c176e915_1684987917.743623.png)\n![image.png](https://assets.leetcode.com/users/images/b6c41849-03dc-462a-b5ba-cf19b0f0604f_1684987926.7459476.png)\n![image.png](https://assets.leetcode.com/users/images/c625a53e-db17-4004-98c3-9ea1daba6354_1684987934.1687083.png)\n![image.png](https://assets.leetcode.com/users/images/ee42af9e-6dde-4f3d-8d09-68f97d56e154_1684987942.1917584.png)\n![image.png](https://assets.leetcode.com/users/images/21e99043-b3b2-435b-83c4-0b11eb5dad8e_1684987948.3058927.png)\n![image.png](https://assets.leetcode.com/users/images/6a3935ad-5f47-4c74-93ff-0388d5fb816d_1684987953.9130611.png)\n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    double new21Game(int N, int K, int maxPts) {\n        // Corner cases\n        if (K == 0) return 1.0;\n        if (N >= K-1 + maxPts) return 1.0;\n\n        // dp[i] is the probability of getting point i.\n        vector<double> dp(N + 1, 0.0);\n\n        double probability = 0.0; // dp of N or less points.\n        double windowSum = 1.0; // Sliding required window sum\n        dp[0] = 1.0;\n        for (int i = 1; i <= N; i++) {\n            dp[i] = windowSum / maxPts;\n\n            if(i < K) windowSum += dp[i];\n            else probability += dp[i];\n            \n            if(i >= maxPts) windowSum -= dp[i - maxPts];\n        }\n\n        return probability;\n    }\n};\n```\n```Java []\nclass Solution {\n    public double new21Game(int N, int K, int maxPts) {\n        // Corner cases\n        if (K == 0) return 1.0;\n        if (N >= K - 1 + maxPts) return 1.0;\n\n        // dp[i] is the probability of getting point i.\n        double[] dp = new double[N + 1];\n        Arrays.fill(dp, 0.0);\n\n        double probability = 0.0; // dp of N or less points.\n        double windowSum = 1.0; // Sliding required window sum\n        dp[0] = 1.0;\n        for (int i = 1; i <= N; i++) {\n            dp[i] = windowSum / maxPts;\n\n            if (i < K) windowSum += dp[i];\n            else probability += dp[i];\n\n            if (i >= maxPts) windowSum -= dp[i - maxPts];\n        }\n\n        return probability;\n    }\n}\n```\n```Python []\nclass Solution:\n    def new21Game(self, N, K, maxPts):\n        # Corner cases\n        if K == 0:\n            return 1.0\n        if N >= K - 1 + maxPts:\n            return 1.0\n\n        # dp[i] is the probability of getting point i.\n        dp = [0.0] * (N + 1)\n\n        probability = 0.0  # dp of N or less points.\n        windowSum = 1.0  # Sliding required window sum\n        dp[0] = 1.0\n        for i in range(1, N + 1):\n            dp[i] = windowSum / maxPts\n\n            if i < K:\n                windowSum += dp[i]\n            else:\n                probability += dp[i]\n\n            if i >= maxPts:\n                windowSum -= dp[i - maxPts]\n\n        return probability\n```
['Math', 'Dynamic Programming', 'C++', 'Java', 'Python3']