question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
time-needed-to-buy-tickets | Swift solution | swift-solution-by-azm819-xb7r | Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n) \n\n# Co | azm819 | NORMAL | 2024-04-09T06:49:59.195554+00:00 | 2024-04-09T06:49:59.195609+00:00 | 62 | false | # Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n func timeRequiredToBuy(_ tickets: [Int], _ k: Int) -> Int {\n var result = 0\n for ... | 2 | 0 | ['Array', 'Queue', 'Swift', 'Simulation'] | 1 |
time-needed-to-buy-tickets | c++ || optimal approach || O(n) | c-optimal-approach-on-by-ankitsingh07s-z6sa | image.png\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Comp | ankitsingh07s | NORMAL | 2024-04-09T06:48:21.530165+00:00 | 2024-04-09T06:48:21.530199+00:00 | 1 | false | image.png\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- ... | 2 | 0 | ['C++'] | 0 |
time-needed-to-buy-tickets | ✅100% || Easy Solution🔥With explanation🔥||Beginner Friendly | 100-easy-solutionwith-explanationbeginne-9sle | Intuition\nTo solve this problem, we need to calculate the time required to buy tickets given a specific index \'k\'. The idea is to iterate through the tickets | ranjan_rr | NORMAL | 2024-04-09T06:41:26.670920+00:00 | 2024-04-09T06:41:26.670943+00:00 | 25 | false | # Intuition\n**To solve this problem, we need to calculate the time required to buy tickets given a specific index \'**k**\'. The idea is to iterate through the tickets array and sum up the values, then calculate the number of elements bigger than the **k-th** element that occur after it, as these elements will not dec... | 2 | 0 | ['Array', 'Greedy', 'Counting', 'C++'] | 0 |
new-21-game | One Pass DP O(N) | one-pass-dp-on-by-lee215-jtiq | Intuition:\nThe same problems as "Climbing Stairs" or "Fibo Sequence".\n\n## Explanation:\ndp[i] is the probability that we get points i at some moment.\nIn ano | lee215 | NORMAL | 2018-05-20T03:25:37.797325+00:00 | 2019-10-22T17:06:57.725045+00:00 | 47,263 | false | ## **Intuition**:\nThe same problems as "Climbing Stairs" or "Fibo Sequence".\n\n## **Explanation**:\n`dp[i]` is the probability that we get points `i` at some moment.\nIn another word:\n`1 - dp[i]`is the probability that we skip the points `i`.\n\nThe do equation is that:\n`dp[i] = sum(last W dp values) / W`\n\nTo get... | 428 | 27 | [] | 55 |
new-21-game | Logical Thinking | logical-thinking-by-gracemeng-1shr | Game rule is as follows\n\npoint 0 initially\nwhile (point < K) {\n draw w from [1, W] randomly \n point += w\n}\nprobability(point <= N) ?\n\n> Let\'s o | gracemeng | NORMAL | 2019-01-29T17:26:54.484807+00:00 | 2019-01-29T17:26:54.484867+00:00 | 15,217 | false | >**Game rule** is as follows\n```\npoint 0 initially\nwhile (point < K) {\n draw w from [1, W] randomly \n point += w\n}\nprobability(point <= N) ?\n```\n> **Let\'s observe that**\n```\npoint total range [0, K + W - 1]\n```\n> we define `probability[i]` as probability to get i points\nsince all start with point ... | 229 | 9 | [] | 19 |
new-21-game | My take on how to reach at Solution | my-take-on-how-to-reach-at-solution-by-c-erld | Firstly, observe that when Alice is done playing, her points can be in range [K, K+W-1]. She can\'t get past K+W because she stops playing when she is at K or m | cooldown | NORMAL | 2018-05-20T17:18:23.380778+00:00 | 2018-10-22T22:43:01.039861+00:00 | 11,495 | false | Firstly, observe that when Alice is done playing, her points can be in range [K, K+W-1]. She can\'t get past K+W because she stops playing when she is at K or more.\n\nNow, we will see how to find the probabilities to stay in the positions in the above range and store them in p[] array.\n\nWe need to know the probabili... | 164 | 3 | [] | 15 |
new-21-game | Java O(K + W) DP solution with explanation | java-ok-w-dp-solution-with-explanation-b-ptgc | Firstly I came up with a basic DP solution which cost O((K + W) * W) time and it runs TLE:\n\n\nclass Solution {\n public double new21Game(int N, int K, int | wangzi6147 | NORMAL | 2018-05-20T03:49:36.088634+00:00 | 2018-10-17T05:12:27.747424+00:00 | 12,681 | false | Firstly I came up with a basic DP solution which cost `O((K + W) * W)` time and it runs TLE:\n\n```\nclass Solution {\n public double new21Game(int N, int K, int W) {\n if (K == 0) {\n return 1;\n }\n int max = K + W - 1;\n double[] dp = new double[max + 1];\n dp[0] = 1;... | 114 | 1 | [] | 22 |
new-21-game | Python 3 - Memorize DFS from O(KW+W) to O(K + W) | python-3-memorize-dfs-from-okww-to-ok-w-mutin | Intuiatively, we can generate the following equation:\n#### F(x) = (F(x+1) + F(x+2) + ... + F(x+W)) / W\nWhere F(x) is the prob that we will eventually have <= | vividlau | NORMAL | 2019-01-18T05:03:38.493188+00:00 | 2021-01-14T03:01:24.975728+00:00 | 4,184 | false | Intuiatively, we can generate the following equation:\n#### F(x) = (F(x+1) + F(x+2) + ... + F(x+W)) / W\nWhere F(x) is the prob that we will eventually have <= N points when we have x points.\nWe will stop picking when we have >= K points, so the exit of the recursion is `cur >= K`.\nThere are K+W states in total and f... | 68 | 3 | [] | 9 |
new-21-game | Image Explanation🏆- [Complete Intuition - Maths, Probability, DP, Sliding Window] - C++/Java/Python | image-explanation-complete-intuition-mat-ts4n | 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 | aryan_0077 | NORMAL | 2023-05-25T04:12:39.306700+00:00 | 2023-05-25T04:24:27.428828+00:00 | 16,632 | false | # Video Solution (`Aryan Mittal`) - Link in LeetCode Profile\n`New 21 Game` by `Aryan Mittal`\n\n\n\n# Approach & Intution\n means the probability getting point "n" at any count of draws. \ne.g. when random number is 1~3, we have:\ndp(n)=dp(n-3)\(1/3)+dp(n-2)\(1/3)+dp(n-1)\*(1/3 | yuanzhi247012 | NORMAL | 2019-11-26T05:28:31.755083+00:00 | 2019-11-26T05:31:36.932245+00:00 | 2,488 | false | dp(n) means the probability getting point "n" at any count of draws. \ne.g. when random number is 1~3, we have:\ndp(n)=dp(n-3)\\*(1/3)+dp(n-2)\\*(1/3)+dp(n-1)\\*(1/3)\nAnd this kind of sum can be optimized using sliding window sum.\n\nWe cannot iterate DP on draw count, because it is hard to decide when to stop iterati... | 48 | 0 | [] | 3 |
new-21-game | Intuition on why counting is incorrect | intuition-on-why-counting-is-incorrect-b-d3vq | When we are counting the number of ways of reaching score >= K and dividing it by the number of ways when score >=K and score <N , we are assuming that the prob | hindustani_bhau | NORMAL | 2020-09-14T18:22:20.710140+00:00 | 2020-09-14T18:22:20.710205+00:00 | 1,682 | false | When we are counting the number of ways of reaching ```score >= K ``` and dividing it by the number of ways when ```score >=K and score <N``` , we are assuming that the probability of reaching each of those scores by different paths is the same.\n\nLet\'s say ```W = 10```\nFor example, to reach score 3, we can have mul... | 44 | 0 | [] | 4 |
new-21-game | Can someone explain why counting number of ways to get to X approach doesn't give correct answer?? | can-someone-explain-why-counting-number-m3yst | I understand it\'s not efficient and sufferes from extreme complexity. BUT I was wondering if someone can tell me why I don\'t get the correct answer for Exampl | edaengineer | NORMAL | 2018-05-20T05:38:51.527002+00:00 | 2018-05-20T05:38:51.527002+00:00 | 2,380 | false | I understand it\'s not efficient and sufferes from extreme complexity. **BUT I was wondering if someone can tell me why I don\'t get the correct answer for Example-3 using this approach???**\n\nYour answer 0.55218, Expected Output: 0.73278\n\nways[X] shows the number of ways to get to X points\ntotal: total number o... | 26 | 0 | [] | 8 |
new-21-game | A setp by setp explanation | a-setp-by-setp-explanation-by-zhjy23212-alyo | This question takes me a while to understand what this means.\n\nAfter struggled for a while, I would like to post how I understand if you are confused about wh | zhjy23212 | NORMAL | 2019-09-10T05:15:11.996883+00:00 | 2019-09-10T05:15:44.795110+00:00 | 2,943 | false | This question takes me a while to understand what this means.\n\nAfter struggled for a while, I would like to post how I understand if you are confused about what happened even after reading some of the solutions posted here.\n\nTake one example first: \n\nN = 12, W = 10, K = 10\n\nThe stop range for the game is [10, 1... | 20 | 0 | ['Dynamic Programming', 'Python3'] | 4 |
new-21-game | Python🔥Java🔥C++🔥Simple Solution🔥Easy to Understand | pythonjavacsimple-solutioneasy-to-unders-njrd | !! BIG ANNOUNCEMENT !!\nI am currently Giving away my premium content well-structured assignments and study materials to clear interviews at top companies relat | techwired8 | NORMAL | 2023-05-25T01:23:25.122407+00:00 | 2023-05-25T01:26:48.894278+00:00 | 9,182 | false | **!! BIG ANNOUNCEMENT !!**\nI am currently Giving away my premium content well-structured assignments and study materials to clear interviews at top companies related to computer science and data science to my current Subscribers. This is only for first 10,000 Subscribers. **DON\'T FORGET** to Subscribe\n\n# Search \u... | 17 | 2 | ['Dynamic Programming', 'C++', 'Java', 'Python3'] | 2 |
new-21-game | 🏆C++ || DP + Sliding Window | c-dp-sliding-window-by-chiikuu-i98f | Code\n\nclass Solution {\npublic:\n double new21Game(int n, int k, int mp) {\n if(k==0 || n>k+mp)return 1;\n vector<double>dp(n+1,0.0);\n | CHIIKUU | NORMAL | 2023-05-25T08:04:52.420513+00:00 | 2023-05-25T08:04:52.420544+00:00 | 1,526 | false | # Code\n```\nclass Solution {\npublic:\n double new21Game(int n, int k, int mp) {\n if(k==0 || n>k+mp)return 1;\n vector<double>dp(n+1,0.0);\n dp[0]=1;\n double ans=0.0;\n double ps=1;\n for(int i=1;i<=n;i++){\n dp[i] = ps/mp;\n if(i<k)ps += dp[i];\n ... | 13 | 1 | ['C++'] | 0 |
new-21-game | C++ || Bottom Up DP || O(N) || Beats 100% | c-bottom-up-dp-on-beats-100-by-mukuldev0-4v7q | If points are curr then probability at this stage depends on curr+1,curr+2,....., curr+maxPts.\nProbability for curr will then be the sum of all these elements | MukulDev007 | NORMAL | 2023-05-25T07:58:20.414790+00:00 | 2023-05-25T07:58:20.414839+00:00 | 904 | false | If points are curr then probability at this stage depends on curr+1,curr+2,....., curr+maxPts.\nProbability for curr will then be the sum of all these elements divided my maxPts.\nThe base case will be when points are greater or equal to k, where if points are less than or equal to n then probability at this stage woul... | 13 | 0 | ['Math', 'Dynamic Programming', 'Memoization', 'Sliding Window', 'C++'] | 2 |
new-21-game | ✅ 0ms | ✨ O(N) O(maxPts) | 🏆 Most Efficient Solution | 👍 Simplest Code with Circular Buffer | 0ms-on-omaxpts-most-efficient-solution-s-pgtx | Intuition\n Describe your first thoughts on how to solve this problem. \n## Probability Analysis\nWhen Alice has x points and she tries next round, the probabil | hero080 | NORMAL | 2023-05-25T05:07:06.684784+00:00 | 2023-05-25T05:21:55.264092+00:00 | 3,260 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n## Probability Analysis\nWhen Alice has x points and she tries next round, the probability of getting y points that round is:\n\n$$P(x \\rightarrow y | x) = 1 / L$$ if $x < k$ and $y \\in [x + 1, x + L]$\nwhere $L$ is `maxPts` and $x \\ri... | 13 | 0 | ['Math', 'Dynamic Programming', 'Sliding Window', 'Probability and Statistics', 'C++'] | 5 |
new-21-game | Easy to understand Java solution with probability calculation examples | easy-to-understand-java-solution-with-pr-hehp | \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[ | silvercenturion | NORMAL | 2021-06-05T16:22:17.633395+00:00 | 2021-06-05T16:34:25.488519+00:00 | 1,617 | false | ```\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 = probabili... | 13 | 1 | [] | 4 |
new-21-game | [C++] One solution and two optimization with explanation | c-one-solution-and-two-optimization-with-gezo | The idea:\nAs always, try to think recursively first, let P(i) return the probability of reaching i. Through the description of the question, we can deduce that | haoran_xu | NORMAL | 2021-06-13T11:08:58.684368+00:00 | 2021-06-13T11:11:40.183460+00:00 | 926 | false | The idea:\nAs always, try to think recursively first, let `P(i)` return the probability of reaching `i`. Through the description of the question, we can deduce that `P(i) = P(i - 1) / maxpts + P(i - 2) / maxpts + ... + P(i - maxpts) / maxpts` where `0 <= i - whatever < k`. For the final answer all we need to do is just... | 10 | 0 | [] | 0 |
new-21-game | [Python/C++] Posterior probability + climbing stairs | pythonc-posterior-probability-climbing-s-l6oa | The question ask us to play a game:\n\nlet X = 0\nwhile X <= K:\n\tX += draw a sample from W, where W = [1, 10]\n\nand figure out that, at the end of the game, | ftliao | NORMAL | 2020-07-01T11:45:36.148484+00:00 | 2020-07-01T12:24:30.127428+00:00 | 975 | false | The question ask us to play a game:\n```\nlet X = 0\nwhile X <= K:\n\tX += draw a sample from W, where W = [1, 10]\n```\nand figure out that, at the end of the game, what\'s the probability \n```\nP( X \u2264 N | X \u2265 K) \uFF1F\n```\n\nFor each probablity X = i, it is the relation:\n```\nP( X = i ) = P( X = i - 1) ... | 10 | 0 | [] | 2 |
new-21-game | C++ 12ms O(K+W) solution with explanation | c-12ms-okw-solution-with-explanation-by-spap5 | First, the destination conditional probability is P(<=N | >= K) which equals to P(<= N && >= K) / P(>=K).\n\nWithout limitations, \nP(x) = 1 / w * (P(x - 1) + P | zhoubowei | NORMAL | 2018-05-20T15:17:41.517980+00:00 | 2018-09-18T11:36:37.453659+00:00 | 2,567 | false | First, the destination conditional probability is **P(<=N | >= K)** which equals to **P(<= N && >= K) / P(>=K)**.\n\nWithout limitations, \nP(x) = 1 / w * (P(x - 1) + P(x - 2) +... + P(x-w)) \n= 1 / w * sumP(x - w, x - 1)\n= **1 / w * sumP(lb, ub)**.\nHere, P(x) means the probability of becoming x points after some dra... | 10 | 0 | [] | 3 |
new-21-game | Diagram & Image Explaination🥇 C++ Full Optimized🔥 DP | Well Explained | diagram-image-explaination-c-full-optimi-dxs7 | Diagram\n Describe your first thoughts on how to solve this problem. \n\n\n# Approach\n Describe your approach to solving the problem. \nCheck if Alice can alwa | 7mm | NORMAL | 2023-05-25T00:01:36.385657+00:00 | 2023-05-25T00:09:01.595553+00:00 | 3,386 | false | # Diagram\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nCheck if Alice can always get k or mor... | 9 | 1 | ['Dynamic Programming', 'Sliding Window', 'C++', 'Java', 'Python3'] | 4 |
new-21-game | C++ solution using dp | c-solution-using-dp-by-maitreya47-poh8 | We store the probability of every state, i.e. probability of getting 2, 3, etc at any point- in our dp vector. Realise that play will stop at k, so atmax the pl | maitreya47 | NORMAL | 2021-06-21T03:30:02.765884+00:00 | 2021-06-21T03:30:02.765929+00:00 | 1,491 | false | We store the probability of every state, i.e. probability of getting 2, 3, etc at any point- in our dp vector. Realise that play will stop at k, so atmax the player will draw is (k-1) + maxPts, so we are guaranteed to get anything <= k-1-maxPts. We start with zero points so probability of ending up with 0 points also =... | 8 | 0 | ['Dynamic Programming', 'C', 'C++'] | 3 |
new-21-game | Java Solution for New 21 Game Problem | java-solution-for-new-21-game-problem-by-1sch | Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to calculate the probability that Alice has n or fewer points. To achieve t | Aman_Raj_Sinha | NORMAL | 2023-05-25T03:53:42.314754+00:00 | 2023-05-25T03:53:42.314836+00:00 | 1,820 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe goal is to calculate the probability that Alice has n or fewer points. To achieve this, we can consider the probability at each point and use dynamic programming to build up the probabilities iteratively.\n\n# Approach\n<!-- Describe ... | 7 | 0 | ['Java'] | 0 |
new-21-game | Swift: New 21 Game (+ Test Cases) | swift-new-21-game-test-cases-by-asahioce-8ap4 | swift\nclass Solution {\n func new21Game(_ n: Int, _ k: Int, _ maxPts: Int) -> Double {\n guard k != 0 && n < (k + maxPts) else { return 1.0 }\n | AsahiOcean | NORMAL | 2021-06-02T23:55:27.057712+00:00 | 2021-06-02T23:55:27.057742+00:00 | 687 | false | ```swift\nclass Solution {\n func new21Game(_ n: Int, _ k: Int, _ maxPts: Int) -> Double {\n guard k != 0 && n < (k + maxPts) else { return 1.0 }\n var dp = [Double](repeating: 0, count: n+1)\n dp[0] = 1.0\n var result = 0.0, val = 1.0\n for i in 1...n {\n dp[i] = val / ... | 7 | 1 | ['Swift'] | 0 |
new-21-game | 👨💻 Easy Approach | C++ | O(N) | easy-approach-c-on-by-anuj_10-evyq | Intuition\n Describe your first thoughts on how to solve this problem. \nTo solve the problem, we can use dynamic programming to calculate the probability that | anuj_10 | NORMAL | 2023-05-25T05:29:06.722160+00:00 | 2023-05-25T05:29:06.722185+00:00 | 922 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo solve the problem, we can use dynamic programming to calculate the probability that Alice has n or fewer points. We can define a dynamic programming array dp of size n + 1, where dp[i] represents the probability of having i or fewer po... | 6 | 0 | ['Dynamic Programming', 'C++'] | 0 |
new-21-game | Taking recursion solution to linear solution | taking-recursion-solution-to-linear-solu-d783 | step 1 wirte recursion solution\n\n\ndouble solver(int n, int k, int mp,int sr, vector<double>&dp)\n {\n if(sr>=k)\n return sr<=n?1:0;\n | vikassingh2810 | NORMAL | 2022-06-10T17:31:04.236856+00:00 | 2022-06-10T17:32:24.247131+00:00 | 599 | false | step 1 wirte recursion solution\n\n```\ndouble solver(int n, int k, int mp,int sr, vector<double>&dp)\n {\n if(sr>=k)\n return sr<=n?1:0;\n \n if(dp[sr]!=-1)\n return dp[sr];\n \n double ans=0;\n for(int i=1;i<=mp;i++)\n {\n ans+=solve... | 6 | 0 | ['Dynamic Programming'] | 1 |
new-21-game | [Python3] top-down & bottom-up dp | python3-top-down-bottom-up-dp-by-ye15-lnkj | Algo \nDefine fn(n) as the probability of points between K and N, i.e. [K, N] given existing point n. Then, \n\n1) if n < K, fn(n) = 1/W*(fn(n+1) + fn(n+2) + .. | ye15 | NORMAL | 2020-11-16T20:08:10.628759+00:00 | 2021-05-13T17:30:37.239301+00:00 | 1,075 | false | Algo \nDefine `fn(n)` as the probability of points between `K` and `N`, i.e. `[K, N]` given existing point `n`. Then, \n\n1) if `n < K`, `fn(n) = 1/W*(fn(n+1) + fn(n+2) + ... + fn(n+W))`;\n2) if `K <= n <= N`, `fn(n) = 1`;\n3) if `N < n`, `fn(n) = 0`. \n\nTrick: if `n+1 < K`, then from \n`fn(n) = 1/W*(fn(n+1) + fn(n+2)... | 6 | 0 | ['Python3'] | 2 |
new-21-game | Python 12-line code||rolling sum | python-12-line-coderolling-sum-by-gulugu-kq43 | \nclass Solution:\n def new21Game(self, n: int, k: int, maxPts: int) -> float:\n if n >= k - 1 + maxPts: return 1 #the last possible stop-point is k-1 | gulugulugulugulu | NORMAL | 2022-04-10T21:24:02.888959+00:00 | 2022-04-10T21:24:02.888994+00:00 | 884 | false | ```\nclass Solution:\n def new21Game(self, n: int, k: int, maxPts: int) -> float:\n if n >= k - 1 + maxPts: return 1 #the last possible stop-point is k-1, if we roll a maxPts and it will end within n, that means anyway it will end within n with prob 1, there is no need to continue\n dp = [0] * (n + 1) ... | 5 | 0 | ['Python', 'Python3'] | 0 |
new-21-game | O(N) java solution with explanation | on-java-solution-with-explanation-by-utt-ldrq | \tlet prob[i] denote the probability of reaching the point i\n w is the maximum coin which we can draw\n\n the probability of drawing each coin is equal, | uttu_dce | NORMAL | 2021-11-27T20:08:43.728058+00:00 | 2021-11-27T20:08:55.008605+00:00 | 687 | false | \tlet prob[i] denote the probability of reaching the point i\n w is the maximum coin which we can draw\n\n the probability of drawing each coin is equal, so the probability of drawing each coin in 1/w\n \n if we want to know the probability of drawing 14, then we can do it as follows\n prob[13] * probabi... | 5 | 0 | [] | 0 |
new-21-game | Monte Carlo Simulations | monte-carlo-simulations-by-sebastienp-zy7q | Disclaimer: this solution does not pass the test as the required precision is 10^-5, but for real applications, it gives quite a interesting insight. Also this | sebastienp | NORMAL | 2020-07-08T15:09:45.291693+00:00 | 2020-07-13T05:41:27.553027+00:00 | 1,231 | false | Disclaimer: this solution does not pass the test as the required precision is 10^-5, but for real applications, it gives quite a interesting insight. Also this is more a solution towards statistics than Software Engineer. Nonetheless, I think it\'s interesting to know different approaches:\n\n```\nimport random\nclass ... | 5 | 0 | [] | 2 |
new-21-game | C++ DP O(K) time and space | c-dp-ok-time-and-space-by-alreadydone-s8ho | Build an array dp such that dp[i] == new21Game(N-K+i+1, i+1, W), so that dp[K-1] == new21Game(N,K,W).\ndp[i] can be computed from dp[i-1], ..., dp[i-W] because | alreadydone | NORMAL | 2019-05-28T05:26:45.594748+00:00 | 2019-05-28T05:26:45.594792+00:00 | 665 | false | Build an array dp such that `dp[i] == new21Game(N-K+i+1, i+1, W)`, so that `dp[K-1] == new21Game(N,K,W)`.\n`dp[i]` can be computed from `dp[i-1], ..., dp[i-W]` because of the following formula:\n\nThis is because:\nFrom the initial state 0, we ... | 5 | 0 | [] | 0 |
new-21-game | C++ top-bottom solution | c-top-bottom-solution-by-zackyne-ffyz | according to the solution:\n\nf(x) = 1/W [f(x + 1) + f(x + 2) + ... + f(x + W)] (1)\nf(x - 1) = 1/W [f(x) + f(x + 1) + ... + f(x + W - 1)] (2)\n | zackyne | NORMAL | 2019-04-20T15:27:24.437453+00:00 | 2019-04-20T15:27:24.437496+00:00 | 407 | false | according to the solution:\n\nf(x) = 1/W *[f(x + 1) + f(x + 2) + ... + f(x + W)] (1)\nf(x - 1) = 1/W *[f(x) + f(x + 1) + ... + f(x + W - 1)] (2)\n\n(2) - (1), we can get :\nf(x - 1) = f(x) - 1/W *[f(x + W) - f(x) (3)\n\nbut in (3) equation, the x must less than K - 1, this is... | 5 | 1 | [] | 1 |
new-21-game | O(N) dp solution from uwi, with explanation | on-dp-solution-from-uwi-with-explanation-djbf | A intuitive idea is simply use dp[i] as the probability to reach i, dp[i]=probability[i].\n\nThen dp[i]=sum(dp[i-j]) for j in [1, W], but this causes O(KW) comp | luckypants | NORMAL | 2018-05-20T03:18:53.456090+00:00 | 2018-05-20T03:18:53.456090+00:00 | 1,594 | false | A intuitive idea is simply use `dp[i]` as the probability to reach `i`, `dp[i]=probability[i]`.\n\nThen `dp[i]=sum(dp[i-j]) for j in [1, W]`, but this causes O(KW) complexity.\n\nIn uwi\'s code, \n\n`dp[i]` is the prefix for numbers larger than or equal to i, so `probability[i]=sum(dp[:i+1])`\n\nLet\'s use `p[i]` for `... | 5 | 0 | [] | 0 |
new-21-game | Java Solution | Memoization->Bottom Up->Optimization | 3 step optimization for acceptance | 3 ms | java-solution-memoization-bottom-up-opti-perp | Approach\nFor complete acceptance of the solution 3 step process is given below where first we make a recusion along with memoization(gives TLE) then optimising | adarsh-mishra27 | NORMAL | 2023-05-25T15:39:20.729436+00:00 | 2023-05-25T15:42:31.587955+00:00 | 715 | false | # Approach\nFor complete acceptance of the solution 3 step process is given below where first we make a recusion along with memoization(gives TLE) then optimising it further using bottom-up iterative approach(gives TLE) then finally applying sliding window in iterative code for most optimized solution!\n\n# Code \nStep... | 4 | 0 | ['Dynamic Programming', 'Memoization', 'Sliding Window', 'Probability and Statistics', 'Java'] | 2 |
new-21-game | Math solution: O(k) time, O(maxPts) space, explained | math-solution-ok-time-omaxpts-space-expl-yrgw | Explanation\nHere we\'ll call maxPts a dice, since it is essentially one. (Yep, 1d10000 would make a big die, not for a casual DnD game).\n\nImagine Alice getti | VladimirTheLeet | NORMAL | 2023-05-25T03:44:41.122538+00:00 | 2023-05-27T19:36:41.440616+00:00 | 533 | false | # Explanation\n*Here we\'ll call `maxPts` a dice, since it is essentially one. (Yep, 1d10000 would make a big die, not for a casual DnD game).*\n\nImagine Alice getting points as traverse through the integers left to right, from 0 to the values of k and n.\n\n\u2026\u2007$k-2$\u2007\u2007\u2007$k-1$\u2007\u2007\u2007**... | 4 | 0 | ['Math', 'Dynamic Programming', 'Swift', 'Sliding Window', 'Probability and Statistics'] | 0 |
new-21-game | Easy DP solution ✅✅ | easy-dp-solution-by-zenitsu02-mcl9 | \n# Approach\nCheck if Alice can always get k or more points. If so, return 1.0.\n\nCreate a vector dp of size n + 1 to store the probabilities of getting each | Zenitsu02 | NORMAL | 2023-05-25T00:55:30.908562+00:00 | 2023-05-25T00:55:30.908588+00:00 | 1,398 | false | \n# Approach\nCheck if Alice can always get k or more points. If so, return 1.0.\n\nCreate a vector dp of size n + 1 to store the probabilities of getting each point from 0 to n. Initialize dp[0] = 1.0.\n\nInitialize two variables,sum and res, to keep track of the sum of probabilities and the final result, respectively... | 4 | 0 | ['C++'] | 1 |
new-21-game | Recursion with memoization TLE, O(n^2), then optimization to iterative sliding window | recursion-with-memoization-tle-on2-then-otcka | \nclass Solution:\n """\n Recursive solution: Begin with cur_points.\n If cur_points<k, play next round:\n For each draw from 1 to maxPts:\n | zhangvict | NORMAL | 2022-01-10T23:30:26.528184+00:00 | 2022-01-11T00:14:04.350456+00:00 | 481 | false | ```\nclass Solution:\n """\n Recursive solution: Begin with cur_points.\n If cur_points<k, play next round:\n For each draw from 1 to maxPts:\n play the next round with draw points. Find the probability of winning from that many points. Divide by maxPts.\n \n Add the weighted probabilites toget... | 4 | 0 | [] | 1 |
new-21-game | My attempt at making sense | my-attempt-at-making-sense-by-sumondutta-8tu0 | \n\t i) [1...,w] is the range the value that can be drawn in a single draw\n\t ii) the game stops as soon as sum of all values drawn becomes k or more.\n\t | sumondutta | NORMAL | 2021-11-14T00:04:49.609446+00:00 | 2021-11-14T01:55:40.496232+00:00 | 317 | false | 1. \n\t i) [1...,w] is the range the value that can be drawn in a single draw\n\t ii) the game stops as soon as **sum** of all values drawn becomes k or more.\n\t iii) the goal is to find the probability that the **sum** is n or less at the end of the game. Let\'s call it sol(n).\n2. The game does not stop unti... | 4 | 0 | [] | 0 |
new-21-game | C++ bottom-up DP solution | c-bottom-up-dp-solution-by-ianchew-lgis | \nclass Solution {\npublic:\n double new21Game(int N, int K, int W) {\n // Max score is K + W - 1 points.\n const int last_score = min(N, K + W | ianchew | NORMAL | 2020-05-02T22:32:39.402074+00:00 | 2020-05-02T22:32:39.402123+00:00 | 603 | false | ```\nclass Solution {\npublic:\n double new21Game(int N, int K, int W) {\n // Max score is K + W - 1 points.\n const int last_score = min(N, K + W - 1);\n // dp[n] is the probability that score n will be reached at some point.\n std::vector<double> dp(last_score + 2);\n // Since th... | 4 | 0 | ['Dynamic Programming', 'C'] | 0 |
new-21-game | Simple C++ solution using DP (tabulation) method | simple-c-solution-using-dp-tabulation-me-kkkp | Intuition\nThe intuition behind this solution is to use dynamic programming to calculate the probabilities of achieving different scores in the New 21 Game. The | priyam05soni | NORMAL | 2023-05-25T06:41:50.228381+00:00 | 2023-05-25T06:41:50.228403+00:00 | 658 | false | # Intuition\nThe intuition behind this solution is to use dynamic programming to calculate the probabilities of achieving different scores in the New 21 Game. The algorithm iteratively calculates the probabilities and keeps track of the cumulative sum.\n\n# Approach\n- The game has three parameters: \'n\' represents th... | 3 | 0 | ['Dynamic Programming', 'C++'] | 0 |
new-21-game | [ C++ ] [ Dynamic Programming ] [ Commented Solution ] | c-dynamic-programming-commented-solution-lso5 | Code\n\nclass Solution {\n public:\n double new21Game(int n, int k, int maxPts) {\n // When the game ends, the point is in [k..k - 1 + maxPts]\n // P = | Sosuke23 | NORMAL | 2023-05-25T02:05:10.516572+00:00 | 2023-05-25T02:05:10.516601+00:00 | 1,236 | false | # Code\n```\nclass Solution {\n public:\n double new21Game(int n, int k, int maxPts) {\n // When the game ends, the point is in [k..k - 1 + maxPts]\n // P = 1, if n >= k - 1 + maxPts\n // P = 0, if n < k (note the constraints already have k <= n)\n if (k == 0 || n >= k - 1 + maxPts)\n return 1.0;\... | 3 | 0 | ['C++'] | 0 |
new-21-game | Can anyone help me explain my TLE code with memorization? | can-anyone-help-me-explain-my-tle-code-w-u1ba | I just use a map to store each K and corresponding probability.\nEach time I try a num in range[1,W]\nI think in this way, no duplicate dfs will be calculated a | codoge1 | NORMAL | 2018-05-20T04:12:49.713971+00:00 | 2018-10-03T13:41:17.171538+00:00 | 606 | false | I just use a map to store each K and corresponding probability.\nEach time I try a num in range[1,W]\nI think in this way, no duplicate dfs will be calculated and it should be similar to dp solution.\nCan anyone tell how this can be slower than dp...? It cannot pass all the test cases.\n\nclass Solution {\n Map<Inte... | 3 | 0 | [] | 2 |
new-21-game | Naive DFS to Sliding Window step by step intuitive conversion giving O(k+m) TC | naive-dfs-to-sliding-window-step-by-step-6umk | This problem can be naively solved using a DFS DP solution:\n\nclass Solution:\n def new21Game(self, n: int, k: int, m: int) -> float:\n @cache\n | boriswilliams | NORMAL | 2024-08-18T16:11:37.048062+00:00 | 2024-08-19T10:50:40.646641+00:00 | 40 | false | This problem can be naively solved using a DFS DP solution:\n```\nclass Solution:\n def new21Game(self, n: int, k: int, m: int) -> float:\n @cache\n def f(p):\n if p >= k:\n return 1 if p <= n else 0\n return sum(f(p+i) for i in range(1, m+1))/m\n return f(0)... | 2 | 0 | ['Python3'] | 0 |
new-21-game | [Simple] Beat 100% + explanation | simple-beat-100-explanation-by-aleclc-knh2 | \n\nclass Solution:\n # In each draw, prob is based on last maxPts draws\n # So we can use a sliding window starting at 1..=maxPts\n def new21Game(self | AlecLC | NORMAL | 2023-07-28T20:40:15.571831+00:00 | 2023-07-28T21:06:20.115837+00:00 | 49 | false | \n```\nclass Solution:\n # In each draw, prob is based on last maxPts draws\n # So we can use a sliding window starting at 1..=maxPts\n def new21Game(self, n: int, k: i... | 2 | 0 | ['Dynamic Programming', 'Sliding Window', 'Python3'] | 0 |
new-21-game | New 21 Game || C++ || DP || DSA | new-21-game-c-dp-dsa-by-rajivraj841311-m0w2 | Intuition\n Describe your first thoughts on how to solve this problem. \n Concept of Dynamic programming is used to solve this problem.\n\n# Approach\n Describe | rajivraj841311 | NORMAL | 2023-05-25T17:53:24.973208+00:00 | 2023-05-25T17:53:24.973249+00:00 | 294 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n* Concept of Dynamic programming is used to solve this problem.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n* O(N)\n\n-... | 2 | 0 | ['C++'] | 0 |
new-21-game | Give it 2 hours . TRY it yourself for logic building from scratch ! ✔🐱🏍🙌 | give-it-2-hours-try-it-yourself-for-logi-pcnv | Intuition\n##### Build-up for the game :-\n\nIn this game, Alice starts with 0 points and draws numbers randomly. She wants to know the chance of having n or fe | Ayush_Singh_610 | NORMAL | 2023-05-25T17:24:04.012304+00:00 | 2023-05-25T17:24:04.012344+00:00 | 280 | false | # Intuition\n##### Build-up for the game :-\n\nIn this game, Alice starts with 0 points and draws numbers randomly. She wants to know the chance of having `n` or fewer points at the end.\n\nTo figure this out, we can use a trick called "counting." We start counting from 0 and keep track of the chance of having each num... | 2 | 1 | ['Math', 'Dynamic Programming', 'Sliding Window', 'Probability and Statistics', 'Python3'] | 0 |
new-21-game | Mathematical Intuition (Very Easy) || O(N) Time Complexity || O(N) Space Complexity | mathematical-intuition-very-easy-on-time-8vuw | Mathematical Intuition\n Describe your first thoughts on how to solve this problem. \nSince we can draw a number $a_i \leq maxPts$ any number of time before get | psycho0_0 | NORMAL | 2023-05-25T14:10:59.779359+00:00 | 2023-05-25T14:27:37.209378+00:00 | 446 | false | # Mathematical Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSince we can draw a number $a_i \\leq maxPts$ any number of time before getting $k \\leq \\sum_{i=1}^{N} a_i \\leq n $. We can reframe the problem in the following way: \nGiven list of numbers $a_i$ where $ 1 \\leq a_i \\leq ... | 2 | 0 | ['Math', 'Dynamic Programming', 'Sliding Window', 'Java'] | 0 |
new-21-game | C++||DP||Sliding window || Beats 100%||Most Optimized | cdpsliding-window-beats-100most-optimize-aam7 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | coder_shailesh411 | NORMAL | 2023-05-25T08:35:27.085074+00:00 | 2023-05-25T08:35:27.085105+00:00 | 575 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 2 | 0 | ['Dynamic Programming', 'Sliding Window', 'C++'] | 0 |
new-21-game | DP of O(n) | dp-of-on-by-xatcher-rclc | Intuition\nUsing probability and sliding window with DP\n\n# Approach\nWithin code\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# C | xatcher | NORMAL | 2023-05-25T05:42:41.603769+00:00 | 2023-05-25T05:42:41.603812+00:00 | 247 | false | # Intuition\nUsing probability and sliding window with DP\n\n# Approach\nWithin code\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\nclass Solution {\npublic:\n double new21Game(int N, int K, int maxPts) {\n // Corner cases\n if (K == 0) return 1.0;\n ... | 2 | 0 | ['Dynamic Programming', 'Sliding Window', 'Probability and Statistics', 'C++'] | 0 |
new-21-game | Easy and simple solution in JavaScript. Almost like editorial but in JS :) | easy-and-simple-solution-in-javascript-a-rczz | Please upvote\n# Code\n\n/**\n * @param {number} n\n * @param {number} k\n * @param {number} maxPts\n * @return {number}\n */\n function new21Game(n, k, maxP | AzamatAbduvohidov | NORMAL | 2023-05-25T04:16:13.414747+00:00 | 2023-05-25T04:16:13.414778+00:00 | 1,166 | false | # Please upvote\n# Code\n```\n/**\n * @param {number} n\n * @param {number} k\n * @param {number} maxPts\n * @return {number}\n */\n function new21Game(n, k, maxPts) {\n let dp = new Array(n+1);\n dp[0] = 1;\n s = k > 0 ? 1 : 0;\n for (let i = 1; i <= n; i++) {\n dp[i] = s / ma... | 2 | 0 | ['JavaScript'] | 0 |
new-21-game | Easy & More Optimized JavaScript Solution Using Dynamic Programming | easy-more-optimized-javascript-solution-igx5e | Time complexity:\n Add your time complexity here, e.g. O(n) \n O(n)\n\n# Space complexity:\n Add your space complexity here, e.g. O(n) \n O(n)\n\n# Code\n | RockstarPabitra | NORMAL | 2023-05-25T02:50:23.712441+00:00 | 2023-05-25T02:50:23.712492+00:00 | 307 | false | # Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(n)\n\n# Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(n)\n\n# Code\n```\n/**\n * @param {number} n\n * @param {number} k\n * @param {number} maxPts\n * @return {number}\n */\nvar new21Game = function(n, k... | 2 | 0 | ['Math', 'Dynamic Programming', 'Sliding Window', 'Probability and Statistics', 'JavaScript'] | 0 |
new-21-game | [Ruby] Reverse solution, with image examplanation step by step | ruby-reverse-solution-with-image-exampla-45jp | Intuition\n\nSupposing $n = 5, k = 4, maxPts = 5$.\n\nwe use an array result[] to represent:\n\n\n result[x] := The probability that we finally have less or eq | hegwin | NORMAL | 2023-01-15T17:18:44.541510+00:00 | 2023-01-15T17:19:44.366034+00:00 | 142 | false | # Intuition\n\nSupposing $n = 5, k = 4, maxPts = 5$.\n\nwe use an array `result[]` to represent:\n\n```\n result[x] := The probability that we finally have less or equal than n points \n when we currently have x points in hand\n and we draw cards till we have k points\n```\n\nBy that definition:\n 1. When we ... | 2 | 0 | ['Math', 'Dynamic Programming', 'Sliding Window', 'Ruby'] | 1 |
new-21-game | DP and Probability || Explaination || ✔ | dp-and-probability-explaination-by-athar-kkhx | -> Solution is inspired from https://leetcode.com/problems/new-21-game/discuss/132334/One-Pass-DP-O(N) and https://leetcode.com/problems/new-21-game/discuss/228 | athar_mohammad | NORMAL | 2022-08-05T13:48:20.421624+00:00 | 2022-08-05T14:09:18.235643+00:00 | 543 | false | -> Solution is inspired from https://leetcode.com/problems/new-21-game/discuss/132334/One-Pass-DP-O(N) and https://leetcode.com/problems/new-21-game/discuss/228406/Logical-Thinkingsolution \n```\nclass Solution {\n \npublic:\n double new21Game(int n, int k, int maxPts) {\n if(n >= k+maxPts || k == 0)return ... | 2 | 0 | ['Dynamic Programming'] | 0 |
new-21-game | DP solution with Go (and explanation) | dp-solution-with-go-and-explanation-by-m-4qru | Let\'s take n = 21, k = 17, maxPts = 10 as an example\n\nThe finish state would be from 17 to 26 (as 16 will continue, and maximum draw will be 10)\n\nIf you\'r | marc000 | NORMAL | 2022-03-04T07:55:52.390650+00:00 | 2022-03-27T17:33:25.610358+00:00 | 211 | false | Let\'s take n = 21, k = 17, maxPts = 10 as an example\n\nThe finish state would be from 17 to 26 (as 16 will continue, and maximum draw will be 10)\n\nIf you\'re currently in 16, what\'s the probability your final result less than n (21)? \nIt should be 0.5, as you can draw 1,2,3,4,5. So, 5 / 10 = 0.5 as P(16)\n\nIf y... | 2 | 0 | [] | 1 |
new-21-game | C++ Brute and Optimal Approaches | c-brute-and-optimal-approaches-by-trinit-lxpf | Approach 1: TLE but is useful to understand the intuition\n\n// basically we ll have to find the probability of alice getting\n// a number between [k...n]. beca | trinity_06 | NORMAL | 2021-10-17T18:46:12.910811+00:00 | 2021-10-17T18:46:12.910860+00:00 | 384 | false | Approach 1: TLE but is useful to understand the intuition\n```\n// basically we ll have to find the probability of alice getting\n// a number between [k...n]. because we dont stop drawing until we reach k\n\n// current_prob[i]->denotes the probability of reaching the number i\n\n//note that 2 events are occuring togeth... | 2 | 0 | [] | 0 |
new-21-game | c++ back to front, sliding window | c-back-to-front-sliding-window-by-liqt20-i1es | \ndouble new21Game(int n, int k, int maxPts) {\n int maxScore = k - 1 + maxPts;\n if (maxScore <= n) {\n return 1.0;\n }\n | liqt20 | NORMAL | 2021-09-22T23:48:06.206858+00:00 | 2021-09-22T23:48:06.206906+00:00 | 351 | false | ```\ndouble new21Game(int n, int k, int maxPts) {\n int maxScore = k - 1 + maxPts;\n if (maxScore <= n) {\n return 1.0;\n }\n vector<double> dp(k + maxPts, 0);\n for(int i = k; i <= n; i++) {\n dp[i] = 1.0;\n }\n double sum = n - k + 1;\n for... | 2 | 0 | [] | 0 |
new-21-game | Javascript solution O(N) with explanation (and math proof) | javascript-solution-on-with-explanation-182i1 | Based on Lee\'s post: https://leetcode.com/problems/new-21-game/discuss/132334/One-Pass-DP-O(N), I figured out a way to understand the solution. Hopefully you f | 6117392 | NORMAL | 2021-05-29T00:05:28.517127+00:00 | 2021-05-29T00:11:16.222690+00:00 | 827 | false | Based on Lee\'s post: https://leetcode.com/problems/new-21-game/discuss/132334/One-Pass-DP-O(N), I figured out a way to understand the solution. Hopefully you find the thought process to be intuitive.\n\nAssume we have:\n```\nmax = 10, k = 12, n = 15\n```\nSince max is 10, probability of drawing any card is `0.1`.\n\nT... | 2 | 0 | ['Math', 'Dynamic Programming', 'Sliding Window', 'JavaScript'] | 1 |
new-21-game | [Java] Clean O(K + W)-Time O(W)-Space Solution || with comments | java-clean-ok-w-time-ow-space-solution-w-jr1e | The most basic solution, though TLE, explains everything clearer: \n\n\nclass Solution {\n public double new21Game(int N, int K, int W) {\n if (K == 0 | xieyun95 | NORMAL | 2021-04-30T02:59:19.648844+00:00 | 2021-04-30T02:59:19.648884+00:00 | 650 | false | The most basic solution, though TLE, explains everything clearer: \n\n```\nclass Solution {\n public double new21Game(int N, int K, int W) {\n if (K == 0) return 1.0;\n \n // dp[i] : prob of getting no more than N points when starting with i points\n double[] dp = new double[K];\n ... | 2 | 1 | ['Dynamic Programming', 'Queue', 'Java'] | 0 |
new-21-game | Python DP linear time clear deduction based on equation | python-dp-linear-time-clear-deduction-ba-dz1t | Update: now the description has been updated, the same approach still applies. It is actually using sliding window implicitly. Suppose dp[i] represents the prob | workcool | NORMAL | 2021-03-17T04:35:23.122544+00:00 | 2023-05-25T05:16:01.987289+00:00 | 633 | false | *Update: now the description has been updated, the same approach still applies. It is actually using sliding window implicitly. Suppose dp[i] represents the prob with start with i points.*\nThen \n\n dp[i] = 1/maxPts * (dp[i+1] + dp[i+2] + ... + dp[i+maxPts])\ndp[i+1] = 1/maxPts * ( dp[i+2] + ... + dp[i+maxPt... | 2 | 0 | ['Dynamic Programming', 'Sliding Window', 'Python3'] | 1 |
new-21-game | C++ Solution, use queue | c-solution-use-queue-by-woshiyuanlei-qyuu | \nclass Solution {\npublic:\n double new21Game(int N, int K, int W) {\n if(K == 0 || W == 1) return 1.0;\n double res = 0;\n double sum | woshiyuanlei | NORMAL | 2021-03-02T07:04:56.079723+00:00 | 2021-03-02T07:04:56.079766+00:00 | 264 | false | ```\nclass Solution {\npublic:\n double new21Game(int N, int K, int W) {\n if(K == 0 || W == 1) return 1.0;\n double res = 0;\n double sum = 1.0;\n queue<double> q;\n for(int i = 1; i < W; i++) q.push(0);\n q.push(1.0);\n for(int i = 1; i < K; i++) {\n q.pu... | 2 | 0 | [] | 0 |
new-21-game | Fast 8-line JAVA O(N) solution | fast-8-line-java-on-solution-by-legendar-0ju2 | dp[i] stores possibility of getting i.\n\n\n public double new21Game(int N, int K, int W) {\n if (K == 0) return 1.0;\n double[] dp = new doubl | legendaryengineer | NORMAL | 2020-04-24T22:50:01.325158+00:00 | 2020-05-01T00:02:19.410753+00:00 | 331 | false | dp[i] stores possibility of getting i.\n\n```\n public double new21Game(int N, int K, int W) {\n if (K == 0) return 1.0;\n double[] dp = new double[K + W];\n dp[0] = 1.0;\n for (int i = 1; i <= Math.min(N, K + W - 1); i++) {\n double pos = dp[Math.min(i - 1, K - 1)] - (i - W - ... | 2 | 2 | [] | 0 |
new-21-game | Javascript - DP | javascript-dp-by-denzel777-3d2p | \nconst new21Game = (N, K, W) => {\n if (K === 0 || N >= K + W) return 1;\n\n let sum = 1;\n let ans = 0;\n const dp = [];\n dp[0] = 1;\n\n for (let i = 1 | denzel777 | NORMAL | 2019-10-16T21:36:46.405895+00:00 | 2019-10-16T21:36:46.405942+00:00 | 211 | false | ```\nconst new21Game = (N, K, W) => {\n if (K === 0 || N >= K + W) return 1;\n\n let sum = 1;\n let ans = 0;\n const dp = [];\n dp[0] = 1;\n\n for (let i = 1; i <= N; i++) {\n dp[i] = sum / W;\n\n if (i < K) {\n sum = sum + dp[i];\n } else {\n ans = ans + dp[i];\n }\n\n if (i - W >= 0) {\... | 2 | 2 | [] | 0 |
new-21-game | Java - DP - 3 ms, faster than 98.51% - 35.5 MB, less than 24.32% | java-dp-3-ms-faster-than-9851-355-mb-les-bz01 | \nclass Solution {\n public double new21Game(int N, int K, int W) {\n if (K == 0 || K + W <= N) {\n return 1;\n }\n if (N < K | woshilxd912 | NORMAL | 2019-06-20T03:36:37.999971+00:00 | 2019-06-20T03:36:38.000013+00:00 | 583 | false | ```\nclass Solution {\n public double new21Game(int N, int K, int W) {\n if (K == 0 || K + W <= N) {\n return 1;\n }\n if (N < K) {\n return 0;\n }\n double base = 1.0 / W;\n double[] dp = new double[N + 1];\n double sum = 0;\n double res ... | 2 | 0 | [] | 2 |
new-21-game | Sliding window + DP | sliding-window-dp-by-yuxiong-u42z | """\nSliding window + DP\nRunning Time: O(N)\n\nDefine dp[i] = probability of getting i points\nOptimal Substructure:\n\t\t\n dp[x] = 1/W * dp[x-1] + 1/W * d | yuxiong | NORMAL | 2018-11-11T18:11:16.978840+00:00 | 2018-11-11T18:11:16.978878+00:00 | 982 | false | """\nSliding window + DP\nRunning Time: O(N)\n\nDefine dp[i] = probability of getting i points\nOptimal Substructure:\n\t\t\n dp[x] = 1/W * dp[x-1] + 1/W * dp[x-2] + 1/W * dp[x-3] + ... + 1/W * dp[x-W]\n Or: dp[x] = 1/W * (dp[x-1] + dp[x-2] + dp[x-3] + ... + dp[x-W])\n\nSo with dp[0]=1, our base case, we can keep... | 2 | 0 | [] | 1 |
new-21-game | C++ Beats 100% DP with Simple Explanation | c-beats-100-dp-with-simple-explanation-b-8jhn | Approach\nIt\'s fairly clear this question will have to be DP, since it\'s similar to \'count number of ways to reach i\', but instead we\'re counting the proba | tangd6 | NORMAL | 2023-12-12T03:01:15.776757+00:00 | 2023-12-12T03:01:15.776790+00:00 | 1,001 | false | # Approach\nIt\'s fairly clear this question will have to be DP, since it\'s similar to \'count number of ways to reach i\', but instead we\'re counting the probabilities to reach i. So our dp will be\n\ndp[i] = probability of reaching i. (not quite, but it\'s a good way to think about it)\n\nTherefore, our solution wi... | 1 | 0 | ['C++'] | 0 |
new-21-game | Python | DP | Explained | python-dp-explained-by-dinar-r9m1 | The solution:\n\nclass Solution:\n def new21Game(self, n: int, k: int, maxPts: int) -> float:\n if k == 0:\n return 1\n dp = [1] + [ | dinar | NORMAL | 2023-05-30T14:44:10.765006+00:00 | 2023-05-30T14:44:10.765033+00:00 | 43 | false | **The solution**:\n```\nclass Solution:\n def new21Game(self, n: int, k: int, maxPts: int) -> float:\n if k == 0:\n return 1\n dp = [1] + [0] * n\n s = 1\n for i in range(1, n + 1):\n if i - maxPts >= k:\n break\n dp[i] = s / maxPts\n ... | 1 | 0 | ['Dynamic Programming', 'Python'] | 0 |
new-21-game | Java very easy solution | java-very-easy-solution-by-abhineshchand-94oe | Intuition\n Describe your first thoughts on how to solve this problem. \n- First of all what is this :P. First find the person who created this question and giv | abhineshchandra1234 | NORMAL | 2023-05-25T20:25:25.662645+00:00 | 2023-05-25T20:25:25.662704+00:00 | 117 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- First of all what is this :P. First find the person who created this question and give my blessings also.\n- We will solve this problem using dp\n- The questions says alice has to start from 0 points and she can pick any points from 1 t... | 1 | 0 | ['Math', 'Dynamic Programming', 'Sliding Window', 'Probability and Statistics', 'Java'] | 1 |
new-21-game | Java Easy Solution | java-easy-solution-by-prerna__02-skuq | \n\n# Code\n\nclass Solution {\n public double new21Game(int n, int k, int maxPts) {\n if(k==0) return 1.00;\n if(k==1 && maxPts<=n) return 1.0 | prerna__02 | NORMAL | 2023-05-25T19:12:24.070580+00:00 | 2023-05-25T19:12:24.070624+00:00 | 257 | false | \n\n# Code\n```\nclass Solution {\n public double new21Game(int n, int k, int maxPts) {\n if(k==0) return 1.00;\n if(k==1 && maxPts<=n) return 1.00;\n double dp[] = new double[n+1];\n dp[0] = 1.00;\n double prev=0.00;\n for(int i=1; i<=n; i++){\n if((i-maxPts-1)>=... | 1 | 0 | ['Java'] | 0 |
new-21-game | Python Easy Solution | python-easy-solution-by-celestial2897-vkpj | Code\n\nclass Solution:\n def new21Game(self, n: int, k: int, maxPts: int) -> float:\n dp = [1.0] + [0] * n\n Wsum = 1.0\n if k == 0 or | celestial2897 | NORMAL | 2023-05-25T18:58:37.246406+00:00 | 2023-05-25T18:58:37.246444+00:00 | 371 | false | # Code\n```\nclass Solution:\n def new21Game(self, n: int, k: int, maxPts: int) -> float:\n dp = [1.0] + [0] * n\n Wsum = 1.0\n if k == 0 or n >= k + maxPts:\n return 1\n for i in range(1, n+1):\n dp[i] = Wsum/maxPts\n if i < k:\n Wsum += dp... | 1 | 0 | ['Python', 'Python3'] | 0 |
new-21-game | 837. New 21 Game || Java | 837-new-21-game-java-by-arpary-cmrt | \nclass Solution {\n public double new21Game(int n, int k, int maxPts) {\n if(k==0||n>=k+maxPts)\n return 1;\n double dp[]=new doubl | ArpAry | NORMAL | 2023-05-25T17:32:18.703661+00:00 | 2023-05-25T17:32:18.703705+00:00 | 61 | false | ```\nclass Solution {\n public double new21Game(int n, int k, int maxPts) {\n if(k==0||n>=k+maxPts)\n return 1;\n double dp[]=new double[n+1];\n Arrays.fill(dp,0.0);\n dp[0]=1;\n double cur_sum=dp[0];\n for(int i=1;i<=n;i++)\n {\n dp[i]=cur_sum/(... | 1 | 0 | [] | 0 |
new-21-game | ✅ Intuitive solution – even if you're bad at Dynamic Programming | intuitive-solution-even-if-youre-bad-at-dlj33 | Intuition\nI thought it was much more intuitive to take this problem backwards.\n\nLet\'s say that maxPts = 6, k = 5, and n = 7.\n\nThat means that Alice rolls | elliotfiske | NORMAL | 2023-05-25T17:26:36.372592+00:00 | 2023-05-25T17:38:01.057187+00:00 | 121 | false | # Intuition\nI thought it was much more intuitive to take this problem *backwards*.\n\nLet\'s say that `maxPts = 6`, `k = 5`, and `n = 7`.\n\nThat means that Alice rolls a dice each turn, and if her total ends up between 5 and 7 she wins. Any higher than 7 and she loses.\n\nThis smells like dynamic programming, so let\... | 1 | 0 | ['Swift'] | 1 |
new-21-game | Rust dp solution | rust-dp-solution-by-arbuztw-hp9w | \nimpl Solution {\n pub fn new21_game(n: i32, k: i32, max_pts: i32) -> f64 {\n let (n, k, max_pts) = (n as usize, k as usize, max_pts as usize);\n | arbuztw | NORMAL | 2023-05-25T17:21:05.518273+00:00 | 2023-05-25T17:21:05.518306+00:00 | 184 | false | ```\nimpl Solution {\n pub fn new21_game(n: i32, k: i32, max_pts: i32) -> f64 {\n let (n, k, max_pts) = (n as usize, k as usize, max_pts as usize);\n let total_max_pts = k + max_pts;\n let mut dp: Vec<f64> = Vec::with_capacity(total_max_pts);\n let mut sum: f64 = 0.0;\n dp.push(1.0... | 1 | 0 | ['Dynamic Programming', 'Rust'] | 0 |
new-21-game | C++ Dynamic Programming with Sliding window O(n) | c-dynamic-programming-with-sliding-windo-1s5f | \n# Code\n\nclass Solution {\npublic:\n double new21Game(int n, int k, int maxPts) {\n if(k==0 || n>= k+maxPts)\n return 1.0;\n vector<double> d | vmalik25 | NORMAL | 2023-05-25T15:43:17.090633+00:00 | 2023-05-25T15:43:17.090673+00:00 | 50 | false | \n# Code\n```\nclass Solution {\npublic:\n double new21Game(int n, int k, int maxPts) {\n if(k==0 || n>= k+maxPts)\n return 1.0;\n vector<double> dp(n + 1, 0.0);\n double winsum=0;\n \n for(int i=k;i<k+maxPts;++i){\n if (i<=n){\n winsum+=1;\n dp[i]=1.0;\n }\n... | 1 | 0 | ['Dynamic Programming', 'Sliding Window', 'C++'] | 0 |
new-21-game | Simple C++ Solution | simple-c-solution-by-divyanshu_singh_cs-3id9 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Divyanshu_singh_cs | NORMAL | 2023-05-25T13:25:35.673859+00:00 | 2023-05-25T13:25:35.673903+00:00 | 104 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['C++'] | 0 |
new-21-game | C++ EASY SOLUTION || DYNAMIC PROGRAMMING. | c-easy-solution-dynamic-programming-by-a-cdvi | \n\n# Code\n\nclass Solution {\npublic:\n double new21Game(int n, int k, int maxPts) {\n\n if (k == 0 || n >= k + maxPts) {\n return 1.0 | ayush_0204 | NORMAL | 2023-05-25T08:09:33.596329+00:00 | 2023-05-25T08:09:33.596354+00:00 | 25 | false | \n\n# Code\n```\nclass Solution {\npublic:\n double new21Game(int n, int k, int maxPts) {\n\n if (k == 0 || n >= k + maxPts) {\n return 1.0;\n }\n vector<double> dp(n + 1);\n double currSum = 1.0;\n double ans = 0.0;\n\n dp[0] = 1.0;\n\n for (int i = 1;... | 1 | 0 | ['C++'] | 0 |
new-21-game | New 21 Game 100% Beats | new-21-game-100-beats-by-prayas_saxena-hxr8 | 100% Beats\n# Code\n\npublic class Solution {\n public double New21Game(int n, int k, int maxPts) {\n if(k==0||n>=k+maxPts){\n return 1.0;\ | prayas_saxena | NORMAL | 2023-05-25T06:36:57.115860+00:00 | 2023-05-25T06:36:57.115900+00:00 | 568 | false | **100% Beats**\n# Code\n```\npublic class Solution {\n public double New21Game(int n, int k, int maxPts) {\n if(k==0||n>=k+maxPts){\n return 1.0;\n }\n double[] arr=new double[n+1];\n double Sum=1.0, result=0.0;\n arr[0]=1;\n for(int i =1;i<arr.Length;i++){\n ... | 1 | 0 | ['Math', 'Probability and Statistics', 'C#'] | 0 |
new-21-game | simple python code using hashmap | simple-python-code-using-hashmap-by-nand-tlvo | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Nandagopal26 | NORMAL | 2023-05-25T06:17:35.297537+00:00 | 2023-05-25T06:17:35.297572+00:00 | 1,235 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['Python3'] | 1 |
new-21-game | Solution in C++ | solution-in-c-by-ashish_madhup-vr0s | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | ashish_madhup | NORMAL | 2023-05-25T05:13:58.325652+00:00 | 2023-05-25T05:13:58.325695+00:00 | 489 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['C++'] | 0 |
new-21-game | 837. New 21 Game, Daily LeetCoding Challenge | 837-new-21-game-daily-leetcoding-challen-istx | \nclass Solution {\npublic:\n double new21Game(int n, int k, int maxPts) {\n if (k==0 || n>=k+maxPts) return 1.0;\n vector<double>dp(n+1,0);\n | pspraneetsehra08 | NORMAL | 2023-05-25T04:01:39.476053+00:00 | 2023-05-25T04:01:39.476123+00:00 | 22 | false | ```\nclass Solution {\npublic:\n double new21Game(int n, int k, int maxPts) {\n if (k==0 || n>=k+maxPts) return 1.0;\n vector<double>dp(n+1,0);\n dp[0]=1.0;\n double result=0;\n double runningSum=dp[0];\n for (int i=1;i<=n;i++)\n {\n dp[i]=runningSum/(double)maxPts;\n ... | 1 | 0 | [] | 0 |
new-21-game | C++ | c-by-david500-tu5c | While Alice has less than k, she will keep drawing. On the last turn, k-1, she could draw the maximum number, maxPts (w), so the maximum she can ever draw is k- | david500 | NORMAL | 2023-05-25T04:00:54.370696+00:00 | 2023-05-25T04:00:54.370729+00:00 | 242 | false | While Alice has less than k, she will keep drawing. On the last turn, k-1, she could draw the maximum number, maxPts (w), so the maximum she can ever draw is k-1+w.\n\nIf n is >= k-1+w, then it will always be true.\n\nWe can have a dp of 0 -> k+w-1, where dp[i] gives the probability that we can i or less.\n\ndp[0] is t... | 1 | 0 | ['C++'] | 0 |
new-21-game | Python3 Solution | python3-solution-by-motaharozzaman1996-1thz | \n\nclass Solution:\n def new21Game(self, n: int, k: int, maxPts: int) -> float:\n if k==0 or n>=k+maxPts:\n return 1\n\n dp=[1.0]+[ | Motaharozzaman1996 | NORMAL | 2023-05-25T03:11:43.491005+00:00 | 2023-05-25T03:11:43.491031+00:00 | 1,248 | false | \n```\nclass Solution:\n def new21Game(self, n: int, k: int, maxPts: int) -> float:\n if k==0 or n>=k+maxPts:\n return 1\n\n dp=[1.0]+[0.0]*n\n maxPts_sum=1.0\n for i in range(1,n+1):\n dp[i]=maxPts_sum/maxPts\n if i<k:\n maxPts_sum+=dp[i]\n... | 1 | 0 | ['Python', 'Python3'] | 0 |
new-21-game | C# using DP + sliding window | c-using-dp-sliding-window-by-dmitriy-mak-9q6w | Approach\n- To calculate the probability of winning, we can use DP (dynamic programming) to keep track of the probabilities for each point.\n- Use sliding windo | dmitriy-maksimov | NORMAL | 2023-05-25T03:03:47.730294+00:00 | 2023-05-25T03:03:47.730334+00:00 | 210 | false | # Approach\n- To calculate the probability of winning, we can use DP (dynamic programming) to keep track of the probabilities for each point.\n- Use sliding window `windowSum` to efficiently calculate probabilities.\n- Calculate the probabilities. `dp[0]` is `1.0`. For each point `i` ($$1..n$$), the probability of reac... | 1 | 0 | ['C#'] | 0 |
new-21-game | ✅ O(n) Easy Solution Explained | on-easy-solution-explained-by-eleev-ojst | Approach\nThis solution applies dynamic programming to calculate the probability of Alice reaching a specific score. Initializing a DP array with the certainty | eleev | NORMAL | 2023-05-25T00:22:27.905071+00:00 | 2023-05-25T00:52:36.800121+00:00 | 115 | false | # Approach\nThis solution applies dynamic programming to calculate the probability of Alice reaching a specific score. Initializing a DP array with the certainty of 0 points (probability of 1), it iteratively builds probabilities for scores from 1 to `n` by averaging the last `maxPtsd` scores.\n\nA `sum` variable track... | 1 | 0 | ['Dynamic Programming', 'Swift', 'Probability and Statistics'] | 0 |
new-21-game | 🗓️ Daily LeetCoding Challenge May, Day 25 | daily-leetcoding-challenge-may-day-25-by-mlmy | This problem is the Daily LeetCoding Challenge for May, Day 25. Feel free to share anything related to this problem here! You can ask questions, discuss what yo | leetcode | OFFICIAL | 2023-05-25T00:00:17.688939+00:00 | 2023-05-25T00:00:17.689017+00:00 | 3,376 | false | This problem is the Daily LeetCoding Challenge for May, Day 25.
Feel free to share anything related to this problem here!
You can ask questions, discuss what you've learned from this problem, or show off how many days of streak you've made!
---
If you'd like to share a detailed solution to the problem, please creat... | 1 | 0 | [] | 7 |
new-21-game | Solution | solution-by-deleted_user-q6yu | C++ []\nclass Solution {\npublic:\n double new21Game(int n, int k, int mx) {\n if (k == 0 || n >= k + mx) return 1.0;\n vector<double> dp(n+1); | deleted_user | NORMAL | 2023-05-05T13:13:21.069771+00:00 | 2023-05-05T13:57:46.069176+00:00 | 963 | false | ```C++ []\nclass Solution {\npublic:\n double new21Game(int n, int k, int mx) {\n if (k == 0 || n >= k + mx) return 1.0;\n vector<double> dp(n+1);\n dp[0] = 1.0;\n double pref = 1.0;\n double res = 0.0; \n for (int i = 1; i <= n; i++){\n dp[i] = pref / mx;\n ... | 1 | 0 | ['C++', 'Java', 'Python3'] | 0 |
new-21-game | Java solution using dp | java-solution-using-dp-by-rishabhk02-mknd | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | rishabhk02 | NORMAL | 2023-01-19T08:35:11.153227+00:00 | 2023-01-19T08:35:11.153259+00:00 | 553 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['Java'] | 0 |
new-21-game | Fast C++ O(n) solution | fast-c-on-solution-by-obose-wt06 | Intuition\n Describe your first thoughts on how to solve this problem. \nMy first thought was to use dynamic programming to approach this problem. \n# Approach\ | Obose | NORMAL | 2023-01-17T19:55:11.755221+00:00 | 2023-01-17T19:55:11.755268+00:00 | 658 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nMy first thought was to use dynamic programming to approach this problem. \n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can use an array of doubles, dp, to store the probability that we reach each score. We can... | 1 | 0 | ['C++'] | 0 |
new-21-game | Python DP solution explained | python-dp-solution-explained-by-evilgeni-ygxm | python\n"""\nThis code gives TLE but is easier to explaine, see the second code below for a no-TLE version\n"""\nclass Solution:\n def new21Game(self, n: int | evilgenius | NORMAL | 2022-02-07T23:14:52.961231+00:00 | 2022-02-07T23:15:15.963535+00:00 | 526 | false | ```python\n"""\nThis code gives TLE but is easier to explaine, see the second code below for a no-TLE version\n"""\nclass Solution:\n def new21Game(self, n: int, k: int, maxPts: int) -> float:\n\t\t# each index in dp stores the probability that, starting from this index, the final points <= n\n dp=[0]*(k+1)\n... | 1 | 0 | ['Math', 'Dynamic Programming', 'Python'] | 0 |
new-21-game | C++ Easy to Understand Dynamic programming (Recursive memoized TLE solution also included) | c-easy-to-understand-dynamic-programming-h5cc | DP\n\nclass Solution {\npublic:\n double new21Game(int N, int K, int W) {\n vector<double> dp(N+1,0);dp[0]=1;\n double sum=0;\n for(int | pjdope | NORMAL | 2020-09-14T22:08:41.014185+00:00 | 2020-09-14T22:08:41.014214+00:00 | 533 | false | DP\n```\nclass Solution {\npublic:\n double new21Game(int N, int K, int W) {\n vector<double> dp(N+1,0);dp[0]=1;\n double sum=0;\n for(int i=1;i<=N;i++)\n {\n if(i-1<K)\n sum+=dp[i-1];\n if(i-W-1>=0)\n sum-=dp[i-W-1];\n dp[i]=... | 1 | 0 | [] | 1 |
new-21-game | c# dp | c-dp-by-lchunlosaltos-wuxr | ```\nlearn from this java solution\nhttps://leetcode.com/problems/new-21-game/discuss/723643/Java-DP-%2B-Sliding-Window\npublic double New21Game(int N, int K, i | lchunlosaltos | NORMAL | 2020-07-08T22:17:47.922285+00:00 | 2020-07-08T22:21:12.171973+00:00 | 137 | false | ```\nlearn from this java solution\nhttps://leetcode.com/problems/new-21-game/discuss/723643/Java-DP-%2B-Sliding-Window\npublic double New21Game(int N, int K, int W) {\n //N = target let\'s say 21;\n //K = is stop point; 17;\n //W = num range 1..W; 1..10;\n if (N>=K-1+W) return 1.0;\n ... | 1 | 0 | [] | 0 |
new-21-game | Python DP | python-dp-by-xuzs9298-si2l | \nclass Solution:\n def new21Game(self, N: int, K: int, W: int) -> float:\n dp = [0 for i in range(N+2)]\n dp[0] = 1\n dp[1] = -1\n | xuzs9298 | NORMAL | 2020-03-19T13:11:05.645645+00:00 | 2020-03-19T13:11:05.645678+00:00 | 378 | false | ```\nclass Solution:\n def new21Game(self, N: int, K: int, W: int) -> float:\n dp = [0 for i in range(N+2)]\n dp[0] = 1\n dp[1] = -1\n ans = 1\n curprob = 0\n for i in range(0,K):\n curprob = curprob + dp[i]\n if i+W > N:\n ans -= curprob... | 1 | 0 | [] | 0 |
new-21-game | Java solution with explanation | java-solution-with-explanation-by-vchoud-wa25 | \nclass Solution {\n\n public double new21Game(int N, int K, int W) {\n //After K points Alice is going to stop the game\n \n /**\n | vchoudhari | NORMAL | 2020-01-16T13:49:00.604904+00:00 | 2020-01-16T13:49:00.604940+00:00 | 567 | false | ```\nclass Solution {\n\n public double new21Game(int N, int K, int W) {\n //After K points Alice is going to stop the game\n \n /**\n if N >= K - 1 + W, probability is 1\n Reason: let\'s say Alice lands on K - 1 Points and then draw a next card, \n let as... | 1 | 0 | [] | 2 |
new-21-game | Simple C# DP Solution | simple-c-dp-solution-by-maxpushkarev-ixbc | \n public class Solution\n {\n public double New21Game(int n, int k, int w)\n {\n int maxPoints = k - 1 + w;\n double[ | maxpushkarev | NORMAL | 2019-11-10T07:19:09.447142+00:00 | 2019-11-10T07:19:09.447177+00:00 | 242 | false | ```\n public class Solution\n {\n public double New21Game(int n, int k, int w)\n {\n int maxPoints = k - 1 + w;\n double[] dp = new double[maxPoints + 1];\n\n for (int lastPoints = k; lastPoints <= maxPoints; lastPoints++)\n {\n dp[lastPoint... | 1 | 0 | [] | 0 |
new-21-game | Java Sliding Window + DP Explanation | java-sliding-window-dp-explanation-by-ma-48s9 | Was able to reach the basic DP solution on my own, but found the offical solutions explanation for the sliding window optimization pretty much incomprehensible | mark49 | NORMAL | 2018-12-04T01:24:09.670364+00:00 | 2018-12-04T01:24:09.670407+00:00 | 477 | false | Was able to reach the basic DP solution on my own, but found the offical solutions explanation for the sliding window optimization pretty much incomprehensible so thought I\'d give it a shot. \n\nBasically, as you should have already figured out if you are reading this, the **Prob[i]** represents that probability that ... | 1 | 1 | [] | 0 |
new-21-game | C++ straight forward solution | c-straight-forward-solution-by-jfc43-62oi | class Solution { public: double new21Game(int N, int K, int W) { if (K==0) return 1; vector<double> dp(K+W,0); dp[0]=1.0 | jfc43 | NORMAL | 2018-10-19T09:22:04.816016+00:00 | 2018-10-19T09:22:04.816085+00:00 | 390 | false | ```
class Solution {
public:
double new21Game(int N, int K, int W) {
if (K==0)
return 1;
vector<double> dp(K+W,0);
dp[0]=1.0;
double S = 0;
for (int i=1;i<K;++i) {
if (i>=W+1)
S=S-dp[i-W-1]+dp[i-1];
else
S=S+... | 1 | 0 | [] | 0 |
new-21-game | C++ 4ms,12lines solution with ONLY ONE line key code | c-4ms12lines-solution-with-only-one-line-fbyp | Here is some explanation of my code.\n\nvector<double> p[i] saves result possibility when the number starts at i, and p[0] is the final result.\n(p[l, r] is th | guoxiongfeng | NORMAL | 2018-08-07T11:25:24.850757+00:00 | 2018-08-07T11:25:24.850757+00:00 | 299 | false | **Here is some explanation of my code.**\n\n```vector<double> p[i]``` saves result possibility when the number starts at ```i```, and ```p[0]``` is the final result.\n(```p[l, r]``` is the slice between ```p[l]``` and ```p[r]```)\n\nWe all know that when variable ```i``` near the target ```K``` or ```N```, ```p[i]``` ... | 1 | 0 | [] | 0 |
new-21-game | A python O(KW) solution and improved O(K) solution with explanation | a-python-okw-solution-and-improved-ok-so-2dc1 | The idea is from back to front.\nLet\'s consider the base case, if current sum is K-i, the probability of next sum to be inside [K,N] is min(N+1-K, i+W+1-K).\n\ | byshen | NORMAL | 2018-05-28T06:07:35.841785+00:00 | 2018-05-28T06:07:35.841785+00:00 | 672 | false | The idea is from back to front.\nLet\'s consider the base case, if current sum is `K-i`, the probability of next sum to be inside `[K,N]` is `min(N+1-K, i+W+1-K)`.\n\nIn the `O(KW)` solution, `s[i]` is the probility of starting from this point, the probability to be inside `[K,N]`. So we divide into two cases, \n\nthe ... | 1 | 0 | [] | 2 |
new-21-game | Java O(K) DP Solution, 21 ms | java-ok-dp-solution-21-ms-by-podgen4-w7t2 | \nclass Solution {\n public double new21Game(int N, int K, int W) {\n if(K == 0)\n return 1.0;\n double[] preSum = new double[K+1];\ | podgen4 | NORMAL | 2018-05-25T22:52:29.724074+00:00 | 2018-05-25T22:52:29.724074+00:00 | 410 | false | ```\nclass Solution {\n public double new21Game(int N, int K, int W) {\n if(K == 0)\n return 1.0;\n double[] preSum = new double[K+1];\n double currProb = 0.0;\n for(int i = K-1; i >= 0; --i) {\n int tmp = W - K +1 +i;\n double subtractVal = 0.0;\n ... | 1 | 0 | [] | 1 |
new-21-game | Why would this brute-force solution give wrong answer on small inputs | why-would-this-brute-force-solution-give-djkh | I was trying to build my solution starting from brute-force and then optimizing it. Hence, I wrote the brute force code below. However, it doesn\'t work on the | faangboy | NORMAL | 2018-05-21T01:34:20.829444+00:00 | 2018-05-21T01:34:20.829444+00:00 | 327 | false | I was trying to build my solution starting from brute-force and then optimizing it. Hence, I wrote the brute force code below. However, it doesn\'t work on the third test case mentioned in the question description that is ,\nN = 21, K =17 and W = 10.\nCan anyone please help me analyze it.\nP.S : I know its a bad soluti... | 1 | 0 | [] | 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.