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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
number-of-ways-to-stay-in-the-same-place-after-some-steps | C++ Bottom-Up DP | c-bottom-up-dp-by-votrubac-s1mf | Intuition\nThis is similar to 935. Knight Dialer.\n#### Catch\nThe array size can be larger than the number of steps. We can ingore array elements greater than | votrubac | NORMAL | 2019-11-24T07:02:11.838413+00:00 | 2019-11-25T01:44:26.649240+00:00 | 6,822 | false | #### Intuition\nThis is similar to [935. Knight Dialer](https://leetcode.com/problems/knight-dialer/discuss/189251/C%2B%2B-5-lines-DP).\n#### Catch\nThe array size can be larger than the number of steps. We can ingore array elements greater than `steps / 2`, as we won\'t able to go back to the first element from there.... | 66 | 0 | [] | 9 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | 【Video】Give me 10 minutes - How we think abou a solution - Python, JavaScript, Java, C++ | video-give-me-10-minutes-how-we-think-ab-otts | Welcome to my post! This post starts with "How we think about a solution". In other words, that is my thought process to solve the question. This post explains | niits | NORMAL | 2023-10-15T03:30:17.116438+00:00 | 2023-10-16T17:22:33.246637+00:00 | 1,435 | false | Welcome to my post! This post starts with "How we think about a solution". In other words, that is my thought process to solve the question. This post explains how I get to my solution instead of just posting solution codes or out of blue algorithms. I hope it is helpful for someone.\n\n# Intuition\nWe only care about ... | 39 | 0 | ['C++', 'Java', 'Python3', 'JavaScript'] | 3 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Step by Step Solution (diagrams) (with how I overcome Memory/Time limit exception) | step-by-step-solution-diagrams-with-how-fjlak | Hi guys, \nThe question is a lot easier to visualize when you think backwards. \nLets set N = steps, M = arrLen\nYou want to find how many possible ways you can | qwerjkl112 | NORMAL | 2019-11-24T04:46:13.762017+00:00 | 2019-11-24T05:14:34.847535+00:00 | 3,542 | false | Hi guys, \nThe question is a lot easier to visualize when you think backwards. \nLets set N = steps, M = arrLen\nYou want to find how many possible ways you can reach the array[0] at Nth step.\nLets ask ourselves, how do we find array[0] at N-1th step.\nSince we are given directions, Left, Stay, Right, we can just add ... | 39 | 0 | [] | 5 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | 7 python approaches with Time and Space analysis | 7-python-approaches-with-time-and-space-aovfr | APP1: DFS. Time: O(3^N) Space: O(1). \n# Result: TLE when 27, 7\n def numWays(self, steps: int, arrLen: int) -> int:\n if steps is None or steps < 0 o | rayfengli | NORMAL | 2020-04-08T16:35:47.001094+00:00 | 2020-05-27T11:38:06.742946+00:00 | 2,255 | false | # APP1: DFS. Time: O(3^N) Space: O(1). \n# Result: TLE when 27, 7\n def numWays(self, steps: int, arrLen: int) -> int:\n if steps is None or steps < 0 or not arrLen:\n return 0\n return self.dfs(steps, arrLen, 0)\n \n def dfs(self, steps, arrLen, pos):\n if pos < 0 or pos >=... | 34 | 0 | ['Dynamic Programming', 'Depth-First Search', 'Memoization', 'Python', 'Python3'] | 5 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | ✅ Not so Hard ✅ Multiple Approaches Brute to Optimal ✅ Recursive + DP ✅ Fastest Beats 100% | not-so-hard-multiple-approaches-brute-to-ra7z | \n\n# YouTube Video Explanation\nhttps://youtu.be/BXVO1cwRbo8\n\n\uD83D\uDD25 Please like, share, and subscribe to support our channel\'s mission of making comp | millenium103 | NORMAL | 2023-10-15T04:08:45.834838+00:00 | 2023-10-15T05:43:55.303809+00:00 | 3,273 | false | \n\n# YouTube Video Explanation\n[https://youtu.be/BXVO1cwRbo8](https://youtu.be/BXVO1cwRbo8)\n\n\uD83D\uDD25 ***Please like, share, and subscribe to support our channel\'s mission of ... | 32 | 1 | ['Dynamic Programming', 'Recursion', 'Memoization', 'Swift', 'Python', 'C++', 'Java', 'Go', 'JavaScript'] | 8 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | [Python] DP | python-dp-by-lee215-1ygi | Explantion\nAdd a dummy value of 0 to the beginning, easier to do sum\n\n\nPython:\npython\n def numWays(self, steps, n, mod=10 ** 9 + 7):\n A = [0, 1 | lee215 | NORMAL | 2019-11-24T17:03:32.260544+00:00 | 2019-12-05T16:33:43.794910+00:00 | 4,651 | false | ## **Explantion**\nAdd a dummy value of `0` to the beginning, easier to do `sum`\n<br>\n\n**Python:**\n```python\n def numWays(self, steps, n, mod=10 ** 9 + 7):\n A = [0, 1]\n for t in xrange(steps):\n A[1:] = [sum(A[i - 1:i + 2]) % mod for i in xrange(1, min(n + 1, t + 3))]\n return ... | 30 | 8 | [] | 8 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Dynamic Programming Staircase || Beginner Friendly || Easy To Understand || Python || Beats 100 % || | dynamic-programming-staircase-beginner-f-wfhr | BEATS 100%\n\n\n# Intuition\n\n\nImagine you are standing at the beginning of a long staircase. You want to reach the top, but you can only take one or two step | vaish_1929 | NORMAL | 2023-10-15T04:27:40.001923+00:00 | 2023-10-15T04:49:08.210728+00:00 | 3,972 | false | # BEATS 100%\n\n\n# Intuition\n\n\nImagine you are standing at the beginning of a long staircase. You want to reach the top, but you can only take one or two steps at a time. How many different ways can you... | 29 | 0 | ['Dynamic Programming', 'PHP', 'Python', 'C++', 'Java', 'Python3', 'Ruby', 'JavaScript'] | 5 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | 🔥Brute Force to Efficient Method🤩 | Java | C++ | Python | JavaScript | C# | PHP | brute-force-to-efficient-method-java-c-p-7cps | 1st Method :- Brute force\n\nHere\'s a step-by-step breakdown of how the code works:\n\n1. Define a modulus value (mod) to handle the modulo operation to avoid | Akhilesh21 | NORMAL | 2023-10-15T01:19:18.973566+00:00 | 2023-10-15T01:19:18.973589+00:00 | 2,722 | false | # 1st Method :- Brute force\n\nHere\'s a step-by-step breakdown of how the code works:\n\n1. Define a modulus value (`mod`) to handle the modulo operation to avoid integer overflow. It\'s set to 1000000007, which is commonly used in competitive programming.\n\n2. Calculate `maxPosition`, which represents the maximum po... | 26 | 3 | ['Dynamic Programming', 'PHP', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#'] | 7 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Python Cache DP | python-cache-dp-by-doudouzi-vqpw | self explanatory solution using dynamic programming\n\n1. dp(pos, step) = sum(dp(pos+1, step-1), dp(pos-1, step-1), dp(pos, step-1))\n2. return 0 when either ou | doudouzi | NORMAL | 2019-11-24T04:09:28.727981+00:00 | 2019-11-24T04:09:28.728022+00:00 | 2,563 | false | self explanatory solution using dynamic programming\n\n1. dp(pos, step) = sum(dp(pos+1, step-1), dp(pos-1, step-1), dp(pos, step-1))\n2. return 0 when either out of range or pos > steps (impossible back to origin)\n3. return 1 when steps == pos\n\n```\nfrom functools import lru_cache\n\nclass Solution:\n def numWays... | 23 | 1 | [] | 9 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | [Java] Iterative DP solution O(n) space | java-iterative-dp-solution-on-space-by-m-olib | ```\nclass Solution {\n \n static int mod = (int)Math.pow(10, 9) + 7;\n \n public int numWays(int steps, int n) {\n \n int[] arr = new | manrajsingh007 | NORMAL | 2019-11-24T04:01:23.926306+00:00 | 2019-11-27T08:47:16.346484+00:00 | 2,569 | false | ```\nclass Solution {\n \n static int mod = (int)Math.pow(10, 9) + 7;\n \n public int numWays(int steps, int n) {\n \n int[] arr = new int[n];\n if(n <= 1) return n;\n arr[0] = 1;\n arr[1] = 1;\n \n for(int j = 1; j < steps; j++){\n \n i... | 14 | 2 | ['Dynamic Programming'] | 5 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | 2ms Beats 99.57%||C++ recursive DP->iterative DP with optimized SC||with Note on Motzkin numbers | 2ms-beats-9957c-recursive-dp-iterative-d-dkem | Intuition\n Describe your first thoughts on how to solve this problem. \n1st approach uses recursion with memo.\n2nd approach uses iterative DP with optimized s | anwendeng | NORMAL | 2023-10-15T02:06:18.127653+00:00 | 2023-10-15T07:01:13.656629+00:00 | 1,550 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1st approach uses recursion with memo.\n2nd approach uses iterative DP with optimized space. The same trick for the space optimization is to use bitwise-and `i&1` to obtain `i%2` like solving the question [Leetcode 2742. Painting the Wall... | 13 | 0 | ['Dynamic Programming', 'C++'] | 2 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | DP = Drone Pilot algorithm = DFS + memo + Drone flying nightmare | dp-drone-pilot-algorithm-dfs-memo-drone-suluq | Okay, I admit that I am too jokey about my tittle. But I want you to get the same lesson that I get from my drone flying. \n\nIntuition:\nHave you every flied a | codedayday | NORMAL | 2020-05-09T07:29:55.524164+00:00 | 2020-05-12T14:09:26.616047+00:00 | 1,445 | false | Okay, I admit that I am too jokey about my tittle. But I want you to get the same lesson that I get from my drone flying. \n\n**Intuition:**\nHave you every flied a drone along a straigt line? This can help you quickly understand the largest index you can visit is no greater than steps/2. Otherwise, you may lose your ... | 12 | 2 | [] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Java Recursive (Memoization) | java-recursive-memoization-by-himsngh-6pco | At every inndex we reach we have 3 possible options either to stay, or to move to the right, or to the left. We travel all the possible position and check whe | himsngh | NORMAL | 2020-08-11T10:46:46.398821+00:00 | 2020-08-22T09:16:51.058677+00:00 | 1,237 | false | At every inndex we reach we have 3 possible options either to stay, or to move to the right, or to the left. We travel all the possible position and check whether we can reach the index 0 or not and save the solution for that particular index in the map. So that when we enncounter it again we have the solution for th... | 11 | 0 | ['Dynamic Programming', 'Recursion', 'Memoization', 'Java'] | 3 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | C++, DP (memoization) with intuition and memory savings, 4ms | c-dp-memoization-with-intuition-and-memo-598t | This task is very similar to the classic question about the number of steps to reach the top:\n70. Climbing Stairs\n\nLet\'s simplify the quesion. How can we re | savvadia | NORMAL | 2019-11-24T09:20:32.386103+00:00 | 2019-11-24T09:23:11.411415+00:00 | 1,011 | false | This task is very similar to the classic question about the number of steps to reach the top:\n[70. Climbing Stairs](https://leetcode.com/problems/climbing-stairs/)\n\nLet\'s simplify the quesion. How can we reach the current position in 1 step?\n - come from left cell\n - come from right cell\n - stay in this cell\n\n... | 11 | 1 | ['Dynamic Programming', 'Memoization'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | ✅ 92.86% DP Optimized | 9286-dp-optimized-by-vanamsen-bvnx | Intuition\nInitially, when we read the problem, it appears to be a classical dynamic programming problem involving movements on a grid or array. At each step, w | vanAmsen | NORMAL | 2023-10-15T11:52:10.875818+00:00 | 2023-10-15T12:50:21.710085+00:00 | 355 | false | # Intuition\nInitially, when we read the problem, it appears to be a classical dynamic programming problem involving movements on a grid or array. At each step, we have three choices: to move left, to move right, or to stay in place. However, the problem comes with constraints: \n1. We can\'t move outside the array.\n2... | 10 | 0 | ['PHP', 'C++', 'Java', 'Go', 'Python3', 'Rust', 'JavaScript', 'C#'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | DP - Line by Line Explanation | dp-line-by-line-explanation-by-tr1nity-7mrk | Please check out LeetCode The Hard Way for more solution explanations and tutorials. If you like it, please give a star and watch my Github Repository. Link in | __wkw__ | NORMAL | 2023-10-15T03:25:14.320791+00:00 | 2023-10-15T16:44:15.536650+00:00 | 570 | false | Please check out LeetCode The Hard Way for more solution explanations and tutorials. If you like it, please give a star and watch my Github Repository. Link in bio.\n\n---\n\nThe first observation is that the computational complexity does not depend on `arrLen`, only the steps and position matter. If we have `n` steps,... | 10 | 0 | ['Dynamic Programming', 'Memoization', 'C', 'Python'] | 2 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Java DP solution (< 20 lines) with detailed explanation. Please comments if it can improve. | java-dp-solution-20-lines-with-detailed-xysci | // \n// dp[arrLen][steps].\n// There are basically three ways to at index j and step i. They are\n// 1 stay at index j. Contribution from dp[j][i-1];\n// 2 move | liyun1988 | NORMAL | 2019-11-24T04:01:57.163176+00:00 | 2019-11-24T17:20:14.709121+00:00 | 1,309 | false | // \n// dp[arrLen][steps].\n// There are basically three ways to at index j and step i. They are\n// 1 stay at index j. Contribution from dp[j][i-1];\n// 2 move right from index j-1. Contribution from dp[j-1][i-1].\n// 3 move left from index j+1. Contribution from dp[j+1][i-1].\n\n// Optimizations:\n// 1 rolling matrix... | 10 | 1 | ['Dynamic Programming', 'Java'] | 3 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Python 3 || 7 lines, recursion, w/comments || T/S: 75% / 75% | python-3-7-lines-recursion-wcomments-ts-zo4wz | \nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n \n @lru_cache(1000)\n def dp(pos = 0, stepsRem = steps)-> int:\n\n | Spaulding_ | NORMAL | 2023-10-15T02:26:03.578527+00:00 | 2024-06-05T15:50:36.241693+00:00 | 131 | false | ```\nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n \n @lru_cache(1000)\n def dp(pos = 0, stepsRem = steps)-> int:\n\n if (pos > stepsRem or # too far to get back with steps remaining\n not 0 <= pos < arrLen): # off the array\... | 9 | 0 | ['Python3'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Beats 100%. Python iterative DP solution with explanation. | beats-100-python-iterative-dp-solution-w-h4sb | \n\nclass Solution:\n def numWays(self, steps: int, length: int) -> int:\n # post-loop-invariant:\n\t\t# dp[i] is the number of ways to reach 0 starti | strongerxi | NORMAL | 2019-11-24T07:01:05.296875+00:00 | 2019-11-24T07:01:05.296907+00:00 | 613 | false | ```\n\nclass Solution:\n def numWays(self, steps: int, length: int) -> int:\n # post-loop-invariant:\n\t\t# dp[i] is the number of ways to reach 0 starting from position \'i\',\n # after taking exactly \'step\' steps, where \'step\' is the loop index below\n # It\'s initialized for \'step\' = 0.... | 9 | 0 | [] | 2 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | 💥💥100% beats runtime and memory [EXPLAINED] | 100-beats-runtime-and-memory-explained-b-xfzl | Intuition\nThe goal is to find how many ways we can stay at index 0 after taking a certain number of steps, where each step allows us to move left, right, or st | r9n | NORMAL | 2024-09-19T19:47:38.431704+00:00 | 2024-09-19T19:47:38.431737+00:00 | 20 | false | # Intuition\nThe goal is to find how many ways we can stay at index 0 after taking a certain number of steps, where each step allows us to move left, right, or stay in place. The key is to realize that after each step, our position depends on where we were in the previous step. The more steps we take, the more position... | 7 | 0 | ['C#'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | 3 choices-dp(take or not take) | 3-choices-dptake-or-not-take-by-priyansh-t83o | Intuition\nFrom the question one can see that there are 3 choices-right,left,stay\nit clearly hints for dp i.e take or not take from the given choices.\n\n\n# C | priyanshulomesh | NORMAL | 2023-10-15T12:27:30.795144+00:00 | 2023-10-15T12:27:30.795162+00:00 | 54 | false | # Intuition\nFrom the question one can see that there are 3 choices-right,left,stay\nit clearly hints for dp i.e take or not take from the given choices.\n\n\n# Complexity\n- Time complexity:\nO(n^2)\n\n- Space complexity:\nO(n^2)\n\n# Code\n```\nclass Solution {\n int mod=1000000007;\n public int solve(int i,int... | 7 | 0 | ['Java'] | 2 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | [Python] The problem description gives away its solution | python-the-problem-description-gives-awa-43r7 | At each step, you can move 1 position to the left, 1 position to the right in the array or stay in the same place (The pointer should not be placed outside the | lamnguyen2187 | NORMAL | 2019-11-24T04:30:55.311622+00:00 | 2019-11-24T04:30:55.311658+00:00 | 366 | false | `At each step, you can move 1 position to the left, 1 position to the right in the array or stay in the same place (The pointer should not be placed outside the array at any time)`\n\nBasically, at an index `idx`, there are potentially 3 cases:\n1. Moving left if `idx-1 >= 1`\n2. Moving right if `idx < arrLen-1`\n3. S... | 7 | 0 | [] | 2 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Video Solution | Java C++ Python | video-solution-java-c-python-by-jeevanku-zunu | \n\n\nclass Solution {\n public int numWays(int steps, int arrLen) {\n int m = (int) 1e9 + 7;\n arrLen = Math.min(arrLen, steps);\n int[ | jeevankumar159 | NORMAL | 2023-10-15T03:40:39.439053+00:00 | 2023-10-15T03:40:39.439076+00:00 | 534 | false | <iframe width="560" height="315" src="https://www.youtube.com/embed/G2jNjuT4Hw8?si=YWjzjgksw_jQTQSJ" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>\n\n```\nclass Solution {\n public int... | 6 | 0 | ['C', 'Python', 'Java'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Java clean dp solution+ recursion (TLE) example for easy understand | java-clean-dp-solution-recursion-tle-exa-ak54 | \nclass Solution {\n public int numWays(int steps, int arrLen) {\n //max steps is 500, you can only move at most to right 500\n int mod=1000000 | CUNY-66brother | NORMAL | 2020-01-17T17:48:53.289682+00:00 | 2020-01-17T20:43:03.838776+00:00 | 589 | false | ```\nclass Solution {\n public int numWays(int steps, int arrLen) {\n //max steps is 500, you can only move at most to right 500\n int mod=1000000007;\n int dp[][]=new int[steps+1][502];\n dp[0][0]=1; \n for(int step=1;step<dp.length;step++){\n for(int pos=0;pos<dp[0].le... | 6 | 0 | [] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | C# | c-by-lightyagami821-uqwg | \n# Code\n\npublic class Solution {\n public int NumWays(int steps, int arrLen) {\n int mod = 1000000007;\n \n int maxPosition = Math.Mi | LightYagami821 | NORMAL | 2023-11-19T04:26:01.187442+00:00 | 2023-11-19T04:26:01.187464+00:00 | 27 | false | \n# Code\n```\npublic class Solution {\n public int NumWays(int steps, int arrLen) {\n int mod = 1000000007;\n \n int maxPosition = Math.Min(steps / 2, arrLen - 1);\n \n int[][] dp = new int[steps + 1][];\n for (int i = 0; i <= steps; i++) {\n dp[i] = new int[maxP... | 5 | 0 | ['C#'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Recursion + Memoization || Easiest explanation || Beginner friendly solution | recursion-memoization-easiest-explanatio-snr3 | Intuition\n Describe your first thoughts on how to solve this problem. \nRecursion.\n\n# Approach\n Describe your approach to solving the problem. \n### BASE CA | BihaniManan | NORMAL | 2023-10-15T08:22:48.872271+00:00 | 2023-10-15T22:29:36.867492+00:00 | 465 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nRecursion.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n### BASE CASES:\n1. If we are on the 0th index, we can\'t make first move as **LEFT**, as it will go out of arrLen, so it\'s our first base case.\n2. Simil... | 5 | 0 | ['Dynamic Programming', 'Recursion', 'Memoization', 'C++'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | [Java] DP Solution with explanation | java-dp-solution-with-explanation-by-xia-ov52 | \nApply dynamic programming to solve the probelm. Use a 2D array dp[steps][index] to count how many possible ways to reach the index i for step s. Notice that | xiaode | NORMAL | 2019-11-24T05:57:40.196257+00:00 | 2019-11-24T17:56:17.031294+00:00 | 430 | false | \nApply dynamic programming to solve the probelm. Use a 2D array `dp[steps][index]` to count how many possible ways to reach the `index i` for `step s`. Notice that the current state only rely on one previous state, so the storage can be optimized to 2 arrays `pre` and `cur`.\nFor recurrence relationship, notice that ... | 5 | 0 | ['Dynamic Programming'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | python dp solution | python-dp-solution-by-hong_tao-cg23 | \n## Boundary analysis:\nwhat is the maximum index you can get?\nfirst of all, you cannot get out the array, thus, N is one of the limit.\nAnd, you can move at | hong_tao | NORMAL | 2019-11-24T04:56:13.486809+00:00 | 2019-11-29T03:53:15.835848+00:00 | 335 | false | \n## Boundary analysis:\nwhat is the maximum index you can get?\nfirst of all, you cannot get out the array, thus, N is one of the limit.\nAnd, you can move at most ```steps``` times, so the maxiumu index you can arrive at is min(steps, N), so all possible index you can arrive at is in the range```[0, min(steps, N)]```... | 5 | 1 | ['Dynamic Programming', 'Python3'] | 2 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | My simple JAVA DP solution | my-simple-java-dp-solution-by-lian9-0i6l | class Solution {\n public int numWays(int steps, int arrLen) {\n //DP solution \n if (arrLen == 1) {\n return 1;\n }\n | lian9 | NORMAL | 2019-11-24T04:55:49.432061+00:00 | 2019-11-24T04:55:49.432096+00:00 | 600 | false | class Solution {\n public int numWays(int steps, int arrLen) {\n //DP solution \n if (arrLen == 1) {\n return 1;\n }\n int mod = 1000_000_007;\n int[] dp = new int[arrLen]; //dp[i] represents for current move, how many possible way I can in i-th position\n dp[0] =... | 5 | 0 | [] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Easy CPP Solution | Memoisation C++ | 2D DP | easy-cpp-solution-memoisation-c-2d-dp-by-tphm | Complexity\n- Time complexity: O(510steps)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(510steps)\n Add your space complexity here, e.g. | saurav_1928 | NORMAL | 2023-10-15T12:14:56.607262+00:00 | 2023-10-15T12:14:56.607285+00:00 | 387 | false | # Complexity\n- Time complexity: O(510*steps)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(510*steps)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public: const int mod = 1e9 + 7;\n long long dp[510][510];\n long long solve(int steps, i... | 4 | 0 | ['Dynamic Programming', 'C++'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Beats 99% | Optimised Solution Using Dynamic Programming | Easy O(steps * maxPosition) Solution | beats-99-optimised-solution-using-dynami-aqi0 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is to count the number of ways to stay at index 0 in an array of length arr | nikkipriya_78 | NORMAL | 2023-10-15T06:23:27.472984+00:00 | 2023-10-15T06:23:27.473005+00:00 | 392 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is to count the number of ways to stay at index 0 in an array of length arrLen after taking steps steps. You can move one step to the left, one step to the right, or stay in the same place at each step. To solve this problem, ... | 4 | 1 | ['Dynamic Programming', 'C', 'Python', 'C++', 'Java', 'Python3', 'Ruby', 'JavaScript'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | 🚩📈Beats 99.70% |🔥Easy and Optimised O(sqrt(n)) Approach | 📈Explained in detail | beats-9970-easy-and-optimised-osqrtn-app-k3rj | Intuition\n Describe your first thoughts on how to solve this problem. \nThe idea behind this solution is to recognize that as the number of steps increases, th | saket_1 | NORMAL | 2023-10-15T06:13:34.454644+00:00 | 2023-10-15T06:13:34.454672+00:00 | 565 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea behind this solution is to recognize that as the number of steps increases, the number of ways to reach the starting position at index 0 forms a bell curve. This bell curve reaches its peak at the midpoint and then starts to decr... | 4 | 1 | ['Array', 'Math', 'Dynamic Programming', 'Python', 'C++', 'Java', 'Python3', 'Ruby', 'JavaScript'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | ✅ C++ Naive solution Little Optimization | c-naive-solution-little-optimization-by-vrev6 | Intuition\n- Let\'s consider an example with arrLen = 3 and steps = 3. We want to calculate the number of ways to reach each position in the array after taking | Tyrex_19 | NORMAL | 2023-10-15T05:29:55.962249+00:00 | 2023-10-15T07:47:58.974036+00:00 | 330 | false | # Intuition\n- Let\'s consider an example with arrLen = 3 and steps = 3. We want to calculate the number of ways to reach each position in the array after taking the given number of steps.\n\n- At the first step, we have two options: we can either stay at position 0 or move one step forward to position 1. So, our possi... | 4 | 0 | ['C++'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Beats 100%🔥Memory & Runtime🦄7 Lines🔮1D DP✅Python 3 | beats-100memory-runtime7-lines1d-dppytho-i8d1 | \n <img alt="image.png" src="https://assets.leetcode.com/users/images/91bea382-d181-4c75-b6a5-ce2965576fe7_1697349959.2977636.png" /> \n\n\n\n# Complexity\n- Ti | atsreecha | NORMAL | 2023-10-15T02:04:45.834207+00:00 | 2023-10-15T06:08:35.507146+00:00 | 223 | false | \n<!--  -->\n | java-solution-faster-than-90-and-steps-t-u401 | Simple recursive approach:\n \n\t recFunc(int stepsLeft, int arrLen, int currentIndex){//i have x steps left and i am currently at position y so in how many | 141200019 | NORMAL | 2021-01-25T18:27:56.634714+00:00 | 2021-02-06T10:25:12.491828+00:00 | 325 | false | * Simple recursive approach:\n ```\n\t recFunc(int stepsLeft, int arrLen, int currentIndex){//i have x steps left and i am currently at position y so in how many ways can i reach position 1\n\t if(stepsLeft==0){ //I have 0 steps left\n\t if(currentIndex==0) return 1; // I am at position 0, so I have met the ter... | 4 | 1 | [] | 2 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | DP with memoization approach - C++ | dp-with-memoization-approach-c-by-eduthe-flkt | Intuition\nTo solve this problem we can use Dynamic Prgramming. We can create a function called f(i, step), where the intuition behind it is to determine the nu | edutheodoro | NORMAL | 2023-10-25T16:56:12.368941+00:00 | 2023-10-25T16:56:12.368973+00:00 | 9 | false | # Intuition\nTo solve this problem we can use Dynamic Prgramming. We can create a function called f(i, step), where the intuition behind it is to determine the number of ways to reach position 0 after a certain number of steps while respecting certain constraints.\n\n- i represents the current position in the array.\n-... | 3 | 0 | ['Dynamic Programming', 'Memoization', 'C++'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | DP || Memoization || Self-Explanatory || Very Easy || Intuitive🔥🔥✅✅ | dp-memoization-self-explanatory-very-eas-iqus | Code\n\nclass Solution {\npublic:\n int mod = 1e9+7;\n map<pair<int,int>,int>dp;\n int solve(int steps,int arrlen,int currsteps,int currind){\n | snehagoyal20 | NORMAL | 2023-10-15T11:50:25.830562+00:00 | 2023-10-15T11:50:25.830587+00:00 | 176 | false | # Code\n```\nclass Solution {\npublic:\n int mod = 1e9+7;\n map<pair<int,int>,int>dp;\n int solve(int steps,int arrlen,int currsteps,int currind){\n if(currsteps == steps){\n return (currind == 0)? 1 : 0;\n }\n if(currind < 0 || currind >= arrlen)return 0;\n\n if(dp.find(... | 3 | 0 | ['Dynamic Programming', 'Memoization', 'C++'] | 2 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Mastering Dynamic Programming: Counting Ways to Reach a Position in an Array | mastering-dynamic-programming-counting-w-pphr | Counting Ways to Reach a Position in an Array\n\n## Approach 1: Recursion\n\n### Explanation\nIn this approach, we\'ll use a recursive function to explore all p | LakshayBrejwal_1_0 | NORMAL | 2023-10-15T08:36:40.044972+00:00 | 2023-10-15T08:36:40.044997+00:00 | 85 | false | # Counting Ways to Reach a Position in an Array\n\n## Approach 1: Recursion\n\n### Explanation\nIn this approach, we\'ll use a recursive function to explore all possible ways to reach a specific position in an array after a given number of steps. The key idea is to consider three options at each step: staying in the cu... | 3 | 0 | ['Array', 'Dynamic Programming', 'Recursion', 'Python', 'C++', 'Java', 'Python3', 'JavaScript', 'C#'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Updated with an O(steps^1.5) algorithm!!! -time and space optimized solution, 0ms beats 100% | updated-with-an-osteps15-algorithm-time-2g5n8 | Original Version #\nSaves half of the space comparing to the typical 2-arrays approach.\nSaves at most half of the time by skipping the unnecessary calculations | yjian012 | NORMAL | 2023-10-15T07:55:21.568297+00:00 | 2023-11-23T02:52:58.747979+00:00 | 67 | false | # Original Version #\nSaves half of the space comparing to the typical 2-arrays approach.\nSaves at most half of the time by skipping the unnecessary calculations.\nAlso optimized for the case `arrLen==2`.\n# Code\n```\n//https://hyper-meta.blogspot.com/\n#pragma GCC target("avx,mmx,sse2,sse3,sse4")\nauto _=[]()noexcep... | 3 | 0 | ['C++'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | [Python] Top Down DP - Clean & Concise | python-top-down-dp-clean-concise-by-hiep-x489 | \nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n @lru_cache(None)\n def dp(index, steps):\n if index < 0 or | hiepit | NORMAL | 2020-10-08T14:07:31.918309+00:00 | 2020-10-08T14:09:44.243793+00:00 | 232 | false | ```\nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n @lru_cache(None)\n def dp(index, steps):\n if index < 0 or index == arrLen: # index outside arraySize\n return 0\n \n if steps == 0:\n return 1 if index == 0 else 0... | 3 | 0 | [] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Java O(S* MIN(S, L)) Short RunTime Bottom-UP DP Beats 98% | java-os-mins-l-short-runtime-bottom-up-d-b8o5 | This is regular DP with some twists. The recurrence equation is easily derived by following logic:\n\nAt any index of ArrayLen one can move left, right or Stay | stack_underflow | NORMAL | 2019-12-14T22:08:19.208804+00:00 | 2019-12-14T22:12:01.334781+00:00 | 176 | false | This is regular DP with some twists. The recurrence equation is easily derived by following logic:\n\nAt any index of ArrayLen one can move left, right or Stay except at corners. Each of these 3 count as move. So `F(S, i) = F(S-1, i) + F(S-1, i-1) + F(S-1, i+1)` is recurrence for same logic which also takes care of cor... | 3 | 0 | [] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Python3 Top-down DFS with Memoization | python3-top-down-dfs-with-memoization-by-7389 | \nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n import functools\n memo = {}\n M = 10 ** 9 + 7\n @functo | sakata_gintoki | NORMAL | 2019-11-25T18:40:41.011168+00:00 | 2019-11-25T18:40:41.011207+00:00 | 317 | false | ```\nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n import functools\n memo = {}\n M = 10 ** 9 + 7\n @functools.lru_cache(maxsize=None)\n def dfs(i,pos):\n if (i,pos) in memo: \n return memo[(i,pos)]\n if i == steps and p... | 3 | 0 | [] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Solution + Explanation ✅ Beats 100% Runtime | solution-explanation-beats-100-runtime-b-ujai | Approach\nImagine standing at position $0$ on a line with a certain number of steps and available positions arrLen. \n> The goal is to find the count of ways to | ProbablyLost | NORMAL | 2023-12-27T10:29:49.974589+00:00 | 2023-12-27T10:29:49.974615+00:00 | 3 | false | # Approach\nImagine standing at position $0$ on a line with a certain number of `steps` and available positions `arrLen`. \n> The goal is to find the count of ways to return to position 0 after taking these steps.\n\nStarting at position 0 with 1 way after 0 steps, we consider movements `(stay, left, right)` at each st... | 2 | 0 | ['Dynamic Programming', 'Swift'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Grid DP🎯 | Top Down➡️Bottom Up | 😱Best Readable C++ Code😱 | 🔥Self Explanatory Comments🔥 | grid-dp-top-downbottom-up-best-readable-7bio5 | \uD83D\uDE0A ~ \uD835\uDE52\uD835\uDE5E\uD835\uDE69\uD835\uDE5D \u2764\uFE0F \uD835\uDE57\uD835\uDE6E \uD835\uDE43\uD835\uDE5E\uD835\uDE67\uD835\uDE5A\uD835\uDE | hirenjoshi | NORMAL | 2023-10-16T09:05:36.105626+00:00 | 2024-08-05T19:47:52.449419+00:00 | 17 | false | \uD83D\uDE0A ~ \uD835\uDE52\uD835\uDE5E\uD835\uDE69\uD835\uDE5D \u2764\uFE0F \uD835\uDE57\uD835\uDE6E \uD835\uDE43\uD835\uDE5E\uD835\uDE67\uD835\uDE5A\uD835\uDE63\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n***Hi There Its Easy! Take A Look At The Code And Comments Within It You... | 2 | 0 | ['Dynamic Programming', 'Matrix', 'C++'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | ✅☑[C++/C/Java/Python/JavaScript] || 3 Approaches || EXPLAINED🔥 | ccjavapythonjavascript-3-approaches-expl-b6sa | PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n#### Approach 1(Top Down DP)\n\n1. Class Definition: The code defines a cla | MarkSPhilip31 | NORMAL | 2023-10-15T19:09:36.810594+00:00 | 2023-10-15T19:09:36.810614+00:00 | 59 | false | # PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### *Approach 1(Top Down DP)*\n\n1. **Class Definition:** The code defines a class called Solution.\n\n1. **Member Variables:**\n\n - **memo:** A 2D vector to store the memoization table for dynamic programming.\n - **MOD:... | 2 | 0 | ['Dynamic Programming', 'C', 'C++', 'Java', 'Python3', 'JavaScript'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Dynamic Programming Solution using Swift | dynamic-programming-solution-using-swift-2r5y | Intuition\nTo solve this problem, we need to consider the possibilities of moving left, right, or staying in the same position at each step. Dynamic programming | tywysocki | NORMAL | 2023-10-15T18:44:35.190514+00:00 | 2023-10-15T18:44:35.190552+00:00 | 27 | false | # Intuition\nTo solve this problem, we need to consider the possibilities of moving left, right, or staying in the same position at each step. Dynamic programming is a suitable approach to keep track of the number of ways to reach each position after each step.\n\n# Approach\nWe create a 2D array dp to store the number... | 2 | 0 | ['Dynamic Programming', 'Swift'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | By-using-Tabulation | by-using-tabulation-by-mg45-ievq | 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 | MG45 | NORMAL | 2023-10-15T07:45:41.975874+00:00 | 2023-10-15T07:50:25.126994+00:00 | 55 | 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:O(steps*(max(arrLen,steps)))\n\n\n- Space complexity:O(steps+arrLen)\n\n\n# Code\n```\nclass Solution {\npublic:\n // int solve... | 2 | 0 | ['C++'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Well explained || Noob solution || DP || Easiest solution | well-explained-noob-solution-dp-easiest-40a8l | Intuition\nEverytime , we have to explore three cases .\n\n# Approach\nInitially , I did it recursively then memoized it .\nEverytime, we will explore the three | _chintu_bhai | NORMAL | 2023-10-15T06:21:30.854285+00:00 | 2023-10-15T06:21:30.854308+00:00 | 96 | false | # Intuition\nEverytime , we have to explore three cases .\n\n# Approach\nInitially , I did it recursively then memoized it .\nEverytime, we will explore the three cases. Everytime our position and steps will get change. \nStarting with starting point and with 0 steps will be exploring all the three cases and will make ... | 2 | 0 | ['C++'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | C# Solution for Number of Ways to Stay in the Same Place After Some Steps Problem | c-solution-for-number-of-ways-to-stay-in-w54g | Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires finding the number of ways to reach index 0 after a certain number | Aman_Raj_Sinha | NORMAL | 2023-10-15T06:20:05.222841+00:00 | 2023-10-15T06:20:05.222858+00:00 | 36 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires finding the number of ways to reach index 0 after a certain number of steps, considering movement to the left, right, or staying in the same place within the given array.\n\n# Approach\n<!-- Describe your approach to ... | 2 | 0 | ['C#'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | C++ || Recursion --> memoization | c-recursion-memoization-by-amol_2004-c4vl | Intuition\n- i cannot exceed to steps/2\n\n# Code\n\nclass Solution {\nprivate:\n long long solve(int steps,int arrLen, int i, vector<vector<long long>> &dp) | amol_2004 | NORMAL | 2023-10-15T05:08:26.311520+00:00 | 2023-10-15T05:08:26.311545+00:00 | 141 | false | # Intuition\n- `i` cannot exceed to `steps/2`\n\n# Code\n```\nclass Solution {\nprivate:\n long long solve(int steps,int arrLen, int i, vector<vector<long long>> &dp){\n if(!i && !steps)\n return 1;\n if(i == arrLen || steps < 0 || i < 0 || i > steps)\n return 0;\n if(dp[st... | 2 | 0 | ['Recursion', 'Memoization', 'C++'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Python3 Solution | python3-solution-by-motaharozzaman1996-16me | \n\n\nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n mod=10**9+7\n n=min(arrLen,steps//2+1)\n ways=[1]+[0]*(n-1) | Motaharozzaman1996 | NORMAL | 2023-10-15T01:30:47.681550+00:00 | 2023-10-15T01:30:47.681571+00:00 | 105 | false | \n\n```\nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n mod=10**9+7\n n=min(arrLen,steps//2+1)\n ways=[1]+[0]*(n-1)\n for step in range(steps):\n ways=[sum(ways[max(0,i-1):i+2])%mod for i in range(min(step+2,steps-step,n))]\n return ways[0] \n`... | 2 | 0 | ['Python', 'Python3'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | ✅✅[C++] RECURSIVE DP SOLUTION || EASY TO UNDERSTAND || MEMOIZATION✅✅ | c-recursive-dp-solution-easy-to-understa-vz03 | \nconst int mod = 1e9+7;\n#define ll long long\nint dp[501][1000];\nclass Solution {\npublic:\n int f(int s,int a,int i=0){\n if(s==0 && i==0) return | Satyam_9766 | NORMAL | 2023-04-01T07:04:27.358535+00:00 | 2023-04-01T07:04:27.358584+00:00 | 487 | false | ```\nconst int mod = 1e9+7;\n#define ll long long\nint dp[501][1000];\nclass Solution {\npublic:\n int f(int s,int a,int i=0){\n if(s==0 && i==0) return 1;\n if(s==0||i<0||i==a) return 0;\n auto &it = dp[s][i];\n if(it!=-1)return it;\n return it = ((f(s-1,a,i)+f(s-1,a,i-1))%mod+f(s... | 2 | 0 | ['Dynamic Programming', 'Recursion', 'Memoization', 'C', 'C++'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | c++ | easy | fast | c-easy-fast-by-venomhighs7-nw7e | \n\n# Code\n\n//from votrubac\nclass Solution {\npublic:\n int numWays(int steps, int arrLen) {\n int sz = min(steps / 2 + 1, arrLen);\n vector<int> v1(s | venomhighs7 | NORMAL | 2022-11-24T04:19:31.966000+00:00 | 2022-11-24T04:19:31.966030+00:00 | 743 | false | \n\n# Code\n```\n//from votrubac\nclass Solution {\npublic:\n int numWays(int steps, int arrLen) {\n int sz = min(steps / 2 + 1, arrLen);\n vector<int> v1(sz + 2), v2(sz + 2);\n v1[1] = 1;\n while (steps-- > 0) {\n for (auto i = 1; i <= sz; ++i)\n v2[i] = ((long)v1[i] + v1[i - 1] + v1[i +... | 2 | 0 | ['C++'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | [C++] Simplest Solution | Recursion -> Memorization | Easy Explanation | c-simplest-solution-recursion-memorizati-kksu | We need to return the no. of possible sequences where we take exaclty steps steps and reach 0 after that.\n\nRECURSION:\n\nCode:\n\nclass Solution {\n int mo | Mythri_Kaulwar | NORMAL | 2022-07-02T02:37:40.852967+00:00 | 2022-07-02T02:37:40.852995+00:00 | 382 | false | We need to return the no. of possible sequences where we take exaclty ```steps``` steps and reach ```0``` after that.\n\n**RECURSION:**\n\n**Code:**\n```\nclass Solution {\n int mod = pow(10, 9) + 7;\n vector<int> ways{0, -1, 1};\npublic:\n int numWays(int n, int len) {\n return solve(n, len);\n }\n ... | 2 | 0 | ['Dynamic Programming', 'Recursion', 'Memoization', 'C', 'C++'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Python 3 DP, 97% Time, 96% Space | python-3-dp-97-time-96-space-by-weichich-dqnb | It\'s the standard DP where dp[i] represents how many ways to reach ith index, with the following optimization:\n1. Using a single list instead of 2D lists, sin | weichichi | NORMAL | 2021-10-27T23:50:17.901319+00:00 | 2021-10-29T19:29:33.309806+00:00 | 194 | false | It\'s the standard DP where `dp[i]` represents how many ways to reach ith index, with the following optimization:\n1. Using a single list instead of 2D lists, since we only have to look back 1 step, we only need to keep 1 value, the overriden value can simply be stored in a tmp variable\n2. No matter how large ```arrLe... | 2 | 0 | ['Dynamic Programming', 'Python3'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | python | 100%/100% - DP - commented with a full explanation | python-100100-dp-commented-with-a-full-e-epos | Ok, so I\'ve just looked at other people\'s solutions, and nothing I\'ve done here is particularly new, I just did a pretty good job at optimizing the method. T | queenTau | NORMAL | 2021-05-16T00:33:53.069818+00:00 | 2021-08-02T04:55:30.111026+00:00 | 343 | false | Ok, so I\'ve just looked at other people\'s solutions, and nothing I\'ve done here is particularly new, I just did a pretty good job at optimizing the method. The basic idea is that if we know the number of ways to reach each position after k steps, then we can compute the number of ways to reach each position after k ... | 2 | 0 | ['Dynamic Programming', 'Python'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | [C++] Optimized DP solution | c-optimized-dp-solution-by-justkj-xhvg | \nint numWays(int steps, int arrLen) {\n int mod = 1e9 + 7;\n vector<int> dp(min(steps/2+1, arrLen), 0);\n dp[0] = 1;\n while (steps | justkj | NORMAL | 2021-04-08T05:44:27.375625+00:00 | 2021-04-08T05:44:42.829987+00:00 | 273 | false | ```\nint numWays(int steps, int arrLen) {\n int mod = 1e9 + 7;\n vector<int> dp(min(steps/2+1, arrLen), 0);\n dp[0] = 1;\n while (steps-- > 0) {\n vector<int> new_dp(dp.size(), 0);\n for (int i = 0; i < dp.size(); ++i) {\n if (i > 0) new_dp[i-1] = (new_dp... | 2 | 0 | [] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Classic DP Bottom Up approach | classic-dp-bottom-up-approach-by-lysyi-u87a | Let\'s begin with recursive reasoning.\nAt each step there are three posibilities for an action\n1. stay\n2. go left if position > 0\n3. go right if position < | lysyi | NORMAL | 2021-01-03T09:08:23.509103+00:00 | 2021-01-03T09:08:23.509136+00:00 | 138 | false | Let\'s begin with recursive reasoning.\nAt each step there are three posibilities for an action\n1. stay\n2. go left if position > 0\n3. go right if position < arrLen - 1\nIf we are on the final step and position = 0 then the sequence of step is correct and should be counted.\nThis idea can be coded directly\n```java\n... | 2 | 0 | [] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | [Rust] 4ms | rust-4ms-by-nickx720-uzjd | \nimpl Solution {\n pub fn num_ways(steps: i32, arr_len: i32) -> i32 {\n let steps = steps as usize;\n let mut dp: Vec<Vec<i32>> = vec![vec![0; ste | nickx720 | NORMAL | 2020-10-30T13:14:42.862424+00:00 | 2020-10-30T13:14:42.862469+00:00 | 71 | false | ```\nimpl Solution {\n pub fn num_ways(steps: i32, arr_len: i32) -> i32 {\n let steps = steps as usize;\n let mut dp: Vec<Vec<i32>> = vec![vec![0; steps + 1]; steps + 1];\n for i in 1..=steps {\n let mut j: usize = 0;\n loop {\n if j <= i && j < arr_len as usize {\n ... | 2 | 0 | [] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Easy to understand Python DFS + Memoization | easy-to-understand-python-dfs-memoizatio-r4rz | Do a dfs search which giving current step and idx, the next state we can move to is:\n\nLeft: dfs(step - 1, idx - 1)\nRight: dfs(step - 1, idx + 1)\nStay: | hao95 | NORMAL | 2020-10-24T22:41:56.353410+00:00 | 2020-10-24T22:43:31.540421+00:00 | 151 | false | Do a dfs search which giving current **step** and **idx**, the next state we can move to is:\n\nLeft: dfs(step - 1, idx - 1)\nRight: dfs(step - 1, idx + 1)\nStay: dfs(step - 1, idx)\nCurrent result wil be the sum of above function calls\' result.\n\nAnd search ending condition is:\n* if idx is out of range\n* ste... | 2 | 0 | [] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Javascript Heap Out Of Memory Even after using DP and BigInt HELP? | javascript-heap-out-of-memory-even-after-t4nh | Here is what it does.\n\n\n```\n/*\n * @param {number} steps\n * @param {number} arrLen\n * @return {number}\n /\nvar numWays = function(steps, arrLen) {\n c | lgdelacruz | NORMAL | 2020-09-05T16:48:22.159459+00:00 | 2020-09-05T16:48:22.159505+00:00 | 1,796 | false | Here is what it does.\n\n\n```\n/**\n * @param {number} steps\n * @param {number} arrLen\n * @return {number}\n */\nvar numWays = function(steps, arrLen) {\n const memo = [];\n for (let i = 0; i < arrLen +... | 2 | 0 | [] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Java solution from TLE to Accepted | java-solution-from-tle-to-accepted-by-ml-z9mh | This problem is very straightforward, most of you must have already figured out what could be the dynamic programming equation which is DP[i][s] - number of way | mlogn | NORMAL | 2020-08-30T18:42:35.318416+00:00 | 2020-08-30T18:49:39.085585+00:00 | 151 | false | This problem is very straightforward, most of you must have already figured out what could be the dynamic programming equation which is `DP[i][s] - number of ways to reach at index i after taking exactly s steps` so DP equation will be `DP[i][s] = DP[i-1][s-1] + DP[i][s-1] + DP[i+1][s-1]`.\nSince `dp equation` only dep... | 2 | 0 | [] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Java Recursion + Memo | java-recursion-memo-by-jyotiprakashrout4-sto3 | \nclass Solution {\n \n public int numWays(int steps, int arrLen) {\n int[][] memo = new int[steps + 1][steps + 1];\n return helper(steps , | jyotiprakashrout434 | NORMAL | 2020-07-31T22:45:30.567838+00:00 | 2020-07-31T22:45:30.567873+00:00 | 294 | false | ```\nclass Solution {\n \n public int numWays(int steps, int arrLen) {\n int[][] memo = new int[steps + 1][steps + 1];\n return helper(steps , arrLen , 0 , memo );\n }\n \n private int helper(int moves , int N , int i , int[][]memo){\n \n if(i > moves){\n return 0 ... | 2 | 0 | ['Recursion', 'Memoization', 'Java'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Python 3, DFS with memoization | python-3-dfs-with-memoization-by-timzeng-6fp5 | \nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n if steps == 0 or arrLen == 1:\n return 1\n \n vi | timzeng | NORMAL | 2020-05-26T05:08:01.977342+00:00 | 2020-05-26T05:08:01.977392+00:00 | 240 | false | ```\nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n if steps == 0 or arrLen == 1:\n return 1\n \n visited = {}\n return self.helper(0, steps, arrLen, visited) % (10 ** 9 + 7)\n \n def helper(self, pos, steps, n, visited):\n if (pos, step... | 2 | 0 | ['Python3'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Python3 DP O(n) space | python3-dp-on-space-by-mictw-44y4 | \npython\nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n N, MOD = min(steps, arrLen), 10**9+7\n moves = [1] + [0]*N\n | mictw | NORMAL | 2020-04-04T22:33:50.585020+00:00 | 2020-04-04T22:33:50.585067+00:00 | 183 | false | \n```python\nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n N, MOD = min(steps, arrLen), 10**9+7\n moves = [1] + [0]*N\n for s in range(steps):\n pre = 0\n for i in range(N):\n pre, moves[i] = (moves[i], (pre + moves[i] + moves[i+1]) %... | 2 | 0 | ['Dynamic Programming', 'Python3'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | TLE--> Memoization -- > bottomup DP[with easy to understand comments] | tle-memoization-bottomup-dpwith-easy-to-14mag | TLE: \n\nclass Solution {\n public int numWays(int steps, int arrLen) {\n return dfs(steps,arrLen,0);\n }\n \n int dfs(int steps,int len,int | sr2311 | NORMAL | 2020-03-26T16:01:23.158647+00:00 | 2020-03-26T16:01:23.158699+00:00 | 124 | false | TLE: \n```\nclass Solution {\n public int numWays(int steps, int arrLen) {\n return dfs(steps,arrLen,0);\n }\n \n int dfs(int steps,int len,int index){\n \n int left,right,stay;\n left=right=stay=0;\n \n if(index< 0 || index ==len) // base case when we go out of arr... | 2 | 0 | [] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Accepted C# DP Solution | accepted-c-dp-solution-by-maxpushkarev-009n | \n public class Solution\n {\n private const int MODULO = 1_000_000_007;\n\n public int NumWays(int steps, int arrLen)\n {\n | maxpushkarev | NORMAL | 2020-03-23T12:33:44.237886+00:00 | 2020-03-23T12:33:44.237923+00:00 | 100 | false | ```\n public class Solution\n {\n private const int MODULO = 1_000_000_007;\n\n public int NumWays(int steps, int arrLen)\n {\n checked\n {\n int maxIdx = Math.Min(steps, arrLen - 1);\n int[,] dp = new int[steps + 1, maxIdx + 1];\n\n ... | 2 | 0 | ['Dynamic Programming'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Key point of fast DP. | key-point-of-fast-dp-by-specialforceatp-hp9l | Let DP[i] denote the number of ways to get to position i after n steps, for the n+1 th step, we have the recurrence relation DP[i] = DP[i] + DP[i-1]+DP[i+1]. Th | specialforceatp | NORMAL | 2020-02-06T04:29:25.339808+00:00 | 2020-02-06T04:29:25.339861+00:00 | 127 | false | Let DP[i] denote the number of ways to get to position i after n steps, for the n+1 th step, we have the recurrence relation DP[i] = DP[i] + DP[i-1]+DP[i+1]. The DP solution solves the problem in O(arrLen*steps) and gets a TLE in some test cases.\n\nThe key point to improve it is, when arrlen is larger than steps, for ... | 2 | 0 | [] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Share the dp | share-the-dp-by-sparkbean-wyou | java\n 0 1 2 3 \n -----------------\n 0| 0 | 0 | 0 | 0 |\n -----------------\n 1| 1 | 1 | 2 | 4 |\n -----------------\n 2| 0 | 1 | 2 | 4 | \n ----- | sparkbean | NORMAL | 2019-11-24T20:24:43.738592+00:00 | 2019-11-24T20:24:43.738637+00:00 | 96 | false | ```java\n 0 1 2 3 \n -----------------\n 0| 0 | 0 | 0 | 0 |\n -----------------\n 1| 1 | 1 | 2 | 4 |\n -----------------\n 2| 0 | 1 | 2 | 4 | \n -----------------\n```\nThis table is the dp table of the given example. The line index means the position in the array, the column index means the current step, ... | 2 | 0 | [] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Java 6ms 1D-DP | java-6ms-1d-dp-by-tmazur-bzya | We count the number of ways we can get to each position in array at each step.\n\nIntuition: \n1. For step 1, we can always reach, idx=0, and idx=1 in one way.\ | tmazur | NORMAL | 2019-11-24T05:01:11.099725+00:00 | 2019-11-24T05:01:11.099760+00:00 | 185 | false | We count the number of ways we can get to each position in array at each step.\n\nIntuition: \n1. For step 1, we can always reach, idx=0, and idx=1 in one way.\n2. For each following step, we can get to each position by summing the neiboring positions from the previous step.\n\n```\nclass Solution {\n \n private ... | 2 | 0 | [] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | C++ Intuitive Solution Recursion With Memoization | c-intuitive-solution-recursion-with-memo-00ih | The key factor was that array Length was of the order of 1000000. But we can clearly observe that the arrayLength won\'t matter if it exceeds the number of step | gagandeepahuja09 | NORMAL | 2019-11-24T04:04:35.452582+00:00 | 2019-11-24T04:18:51.279343+00:00 | 271 | false | The key factor was that array Length was of the order of 1000000. But we can clearly observe that the arrayLength won\'t matter if it exceeds the number of steps. Hence we can write\n\n```\n if(len > steps)\n len = steps;\n```\n\n**Code:**\n\n\n\n```\n#define ll long long int\n\nclass Solution {\npublic:\n ... | 2 | 1 | [] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Python top-down DP solution | python-top-down-dp-solution-by-otoc-fnko | \n def numWays(self, steps: int, arrLen: int) -> int:\n def recursive(x, n):\n mod = 10 ** 9 + 7\n if n == x:\n r | otoc | NORMAL | 2019-11-24T04:03:31.098570+00:00 | 2019-12-04T01:23:58.446656+00:00 | 205 | false | ```\n def numWays(self, steps: int, arrLen: int) -> int:\n def recursive(x, n):\n mod = 10 ** 9 + 7\n if n == x:\n return 1\n if n == 0 or x > n:\n return 0\n if (x, n) in dp:\n return dp[(x, n)]\n dp[(x, n)] =... | 2 | 1 | [] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | [C++] DP | c-dp-by-orangezeit-0c8p | \nclass Solution {\npublic:\n int numWays(int steps, int arrLen) {\n\t\t// the length of DP should be min of arrLen and steps / 2 + 1, otherwise we may have | orangezeit | NORMAL | 2019-11-24T04:03:24.693073+00:00 | 2019-11-24T20:30:34.197325+00:00 | 277 | false | ```\nclass Solution {\npublic:\n int numWays(int steps, int arrLen) {\n\t\t// the length of DP should be min of arrLen and steps / 2 + 1, otherwise we may have TLE\n\t\t// if you reach beyond steps / 2 + 1, you will never come back to 0\n\t\tconst int len(min(steps / 2 + 1, arrLen)), mod(1e9 + 7);\n vector<lo... | 2 | 0 | ['Dynamic Programming'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Python - Simple memoization | python-simple-memoization-by-cool_shark-v4j9 | py\nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n def recurse(idx, cnt):\n if cnt == steps:\n if id | cool_shark | NORMAL | 2019-11-24T04:01:56.617708+00:00 | 2019-11-24T04:01:56.617742+00:00 | 194 | false | ```py\nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n def recurse(idx, cnt):\n if cnt == steps:\n if idx == 0:\n return 1\n return 0\n if (idx, cnt) in memo:\n return memo[(idx,cnt)]\n ways... | 2 | 0 | [] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Simple python solution using recursion + Top-down approach + simple memorization with cache | simple-python-solution-using-recursion-t-3jcd | IntuitionThe problem is about counting the number of ways to return to position 0 after a specific number of steps. Since we can move left, right, or stay, it f | Adi125 | NORMAL | 2025-01-20T09:08:17.877743+00:00 | 2025-01-20T09:08:17.877743+00:00 | 21 | false | # Intuition
The problem is about counting the number of ways to return to position `0` after a specific number of steps. Since we can move left, right, or stay, it feels like a problem that can be solved by exploring all possible moves recursively.
To avoid recomputing the same states, we can use memoization to speed... | 1 | 0 | ['Recursion', 'Memoization', 'Python3'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Faster than 100% - Easy to understand memoization solution | faster-than-100-easy-to-understand-memoi-ti65 | Complexity\n- Time complexity: O(s * min(s, len)) \n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(s * min(s, len)) \n Add your space comp | trar | NORMAL | 2024-11-01T06:43:16.900453+00:00 | 2024-11-01T06:43:16.900481+00:00 | 3 | false | # Complexity\n- Time complexity: $$ O(s * min(s, len)) $$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(s * min(s, len)) $$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```javascript []\n/**\n * @param {number} steps\n * @param {number} arrLen\n * @return {numbe... | 1 | 0 | ['JavaScript'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Easy C++ Solution || Dynamic Programming | easy-c-solution-dynamic-programming-by-n-cyva | \n\n# Code\ncpp []\nclass Solution {\npublic:\n const int mod=1e9+7;\n vector<vector<int>>dp;\n int numWays(int steps, int arrLen) {\n dp.resize | NehaGupta_09 | NORMAL | 2024-09-01T14:01:39.676008+00:00 | 2024-09-01T14:01:39.676045+00:00 | 4 | false | \n\n# Code\n```cpp []\nclass Solution {\npublic:\n const int mod=1e9+7;\n vector<vector<int>>dp;\n int numWays(int steps, int arrLen) {\n dp.resize(steps+1,vector<int>(steps+1,-1));\n return fun(steps,0,arrLen)%mod;\n }\n int fun(int steps,int pos,int &arrLen)\n {\n if(steps==0 an... | 1 | 0 | ['Dynamic Programming', 'C++'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Easy Java Solution || Dynamic Programming | easy-java-solution-dynamic-programming-b-8fx8 | 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 | ravikumar50 | NORMAL | 2024-06-21T11:50:54.342473+00:00 | 2024-06-21T11:50:54.342503+00:00 | 11 | 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 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Dynamic Programming with hashmap,Unique Solution | dynamic-programming-with-hashmapunique-s-y2d6 | Basically here we are using HashMap as a dp table\n\nrest of the logic remians the same\n\nTO PICK, OR NOT TO PICK\n\n\n# Code\n\n/**\n * @param {number} steps\ | AbhayDutt | NORMAL | 2023-10-21T00:18:57.190509+00:00 | 2023-10-21T00:18:57.190529+00:00 | 1 | false | Basically here we are using HashMap as a dp table\n\nrest of the logic remians the same\n\nTO PICK, OR NOT TO PICK\n\n\n# Code\n```\n/**\n * @param {number} steps\n * @param {number} arrLen\n * @return {number}\n */\n// var numWays = function(steps, arrLen) {\n \n// };\n\n//logic\nvar numWays = function (steps, arrL... | 1 | 0 | ['Hash Table', 'Dynamic Programming', 'JavaScript'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | C++,Beats 85%time complexity,recursion+memoization,easy to understand | cbeats-85time-complexityrecursionmemoiza-titu | Intuition\nSo,here the questions says, where we can roll back to 0th idnex after moving one step forward,backward, or stay . since the maximum value of steps gi | robink2404 | NORMAL | 2023-10-20T01:59:52.380352+00:00 | 2023-10-20T01:59:52.380393+00:00 | 10 | false | # Intuition\nSo,here the questions says, where we can roll back to 0th idnex after moving one step forward,backward, or stay . since the maximum value of steps given in limits is 500 then the arrlen above 501 will be irrelevant as we if go there we would nevr=er had enough steps to return; so reduces size of arrlen to ... | 1 | 0 | ['Dynamic Programming', 'Recursion', 'Memoization', 'C++'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | [C++] Iterative DP Solution, 100% Time (0ms), ~92% Space (6.61MB) | c-iterative-dp-solution-100-time-0ms-92-ye6i1 | This problem can be solved thinking that each time, each position in the array we are going to consider can be reached in up to three ways:\n moving from the pr | Ajna2 | NORMAL | 2023-10-16T23:20:33.762393+00:00 | 2023-10-16T23:20:33.762424+00:00 | 3 | false | This problem can be solved thinking that each time, each position in the array we are going to consider can be reached in up to three ways:\n* moving from the previous position going `right`;\n* staying the previous position with `stay`;\n* moving from the follwing position going `left`.\n\nFor example, if at some poin... | 1 | 0 | ['Array', 'Dynamic Programming', 'C++'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | 🔥C++ Easy & Efficient approach || Dynamic Programming || Time & Space Complexity are linear 🔥 | c-easy-efficient-approach-dynamic-progra-b4es | \n Describe your first thoughts on how to solve this problem. \n\n# Approach\nThe following algorithm is used to calculate the number of ways to reach the top o | vishukumar | NORMAL | 2023-10-15T20:54:09.616038+00:00 | 2023-10-15T20:54:09.616064+00:00 | 8 | false | \n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe following algorithm is used to calculate the number of ways to reach the top of the staircase:\n1. Define a modulo constant to prevent integer overflow.\n2. Calculate the maximum possible position on the staircase that can be reach... | 1 | 0 | ['C++'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | ✅ [Solution][Swift] Dynamic Programming | solutionswift-dynamic-programming-by-ada-k69x | TC: O(arrlen * steps)\nSC: O(arrlen * steps)\n\nclass Solution {\n func numWays(_ steps: Int, _ arrLen: Int) -> Int {\n let modulo = 1_000_000_000 + 7 | adanilyak | NORMAL | 2023-10-15T18:03:23.160176+00:00 | 2023-10-15T18:03:23.160199+00:00 | 4 | false | **TC:** $$O(arrlen * steps)$$\n**SC:** $$O(arrlen * steps)$$\n```\nclass Solution {\n func numWays(_ steps: Int, _ arrLen: Int) -> Int {\n let modulo = 1_000_000_000 + 7\n var memo = [String: Int]()\n func rec(_ i: Int, _ remain: Int) -> Int {\n guard remain > 0 else { return i == 0 ?... | 1 | 0 | ['Dynamic Programming', 'Swift'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | 🔥 Easy solution | Python 3 🔥| | easy-solution-python-3-by-arbazkhanpatha-4ta1 | Intuition\nNumber of Ways to Stay in the Same Place After Some Steps\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Initialize | arbazkhanpathan0348 | NORMAL | 2023-10-15T17:54:43.726626+00:00 | 2023-10-15T17:54:43.726643+00:00 | 7 | false | # Intuition\nNumber of Ways to Stay in the Same Place After Some Steps\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize constants and variables:\n\n 1. MOD is set to 10^9 + 7, which is used for taking the modulus of the result to avoid integer overflow.\n 1. Calcul... | 1 | 0 | ['Python3'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Fast Java Solutions: Optimized Bottom-up DP and Top-Down DP solution provided, Beats 96% | fast-java-solutions-optimized-bottom-up-dts9m | Provide Both Optimized Bottom-up DP and Top-Down DP solutions\n\n\n\n# Code\n\nclass Solution {\n int modulo = (int)Math.pow(10, 9) + 7;\n public int numW | wenyuoy0306 | NORMAL | 2023-10-15T17:42:03.325778+00:00 | 2023-10-15T17:42:03.325808+00:00 | 20 | false | Provide Both Optimized Bottom-up DP and Top-Down DP solutions\n\n\n\n# Code\n```\nclass Solution {\n int modulo = (int)Math.pow(10, 9) + 7;\n public int numWays(int steps, int arrLen) {\n arrLen = Math.min(steps / 2 + 1, arrLen);\n\n //Bottom-up DP\n int[] memo = new int[arrLen];\n mem... | 1 | 0 | ['Dynamic Programming', 'Backtracking', 'Memoization', 'Java'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Ruby || DP | ruby-dp-by-alecn2002-ehck | \n# Code\nruby\nMOD = 10**9 + 7\n\nclass Numeric\n def min(v) = (self > v ? v : self)\nend\n\ndef num_ways(steps, arr_len)\n max_pos = (steps / 2).min(arr | alecn2002 | NORMAL | 2023-10-15T15:03:19.184579+00:00 | 2023-10-15T15:03:19.184604+00:00 | 5 | false | \n# Code\n```ruby\nMOD = 10**9 + 7\n\nclass Numeric\n def min(v) = (self > v ? v : self)\nend\n\ndef num_ways(steps, arr_len)\n max_pos = (steps / 2).min(arr_len - 1)\n (dp = Array.new(max_pos + 3, 0))[1] = 1\n steps.times.inject(dp) {|dp, i|\n [0] + dp.each_cons(3).collect {|c| c.sum % MOD } + [0]\n... | 1 | 0 | ['Ruby'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | ✅Accepted Java Code || Beats 80% | accepted-java-code-beats-80-by-thilaknv-ted5 | Complexity\n- Time complexity : O(m * n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity : O(m * n)\n Add your space complexity here, e.g. O(n | thilaknv | NORMAL | 2023-10-15T14:58:43.720120+00:00 | 2023-10-15T14:58:43.720137+00:00 | 7 | false | # Complexity\n- Time complexity : $$O(m * n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity : $$O(m * n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```JAVA []\nclass Solution {\npublic int numWays(int steps, int arr) {\n if(steps == 1 || arr == 1)\n re... | 1 | 0 | ['Array', 'Dynamic Programming', 'Java'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | C++ DP solution memoization... | c-dp-solution-memoization-by-kunalagra19-vd0y | \n\n# Code\n\nclass Solution {\npublic:\n const int mod=1e9+7;\n int dp[501][501];\n int helper(int steps,int len,int arrlen)\n {\n if(len==0 | kunalagra197 | NORMAL | 2023-10-15T14:29:11.155042+00:00 | 2023-10-15T14:29:11.155071+00:00 | 55 | false | \n\n# Code\n```\nclass Solution {\npublic:\n const int mod=1e9+7;\n int dp[501][501];\n int helper(int steps,int len,int arrlen)\n {\n if(len==0 && steps==0)return 1;\n if(len<0 || len==arrlen || steps<0)return 0;\n if(len>steps)return 0;\n if(dp[len][steps]!=-1)return dp[len][st... | 1 | 0 | ['Dynamic Programming', 'Memoization', 'C++'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | My java easy recursive 5ms solution 99% faster | my-java-easy-recursive-5ms-solution-99-f-z8dd | 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 | raghavrathore7415 | NORMAL | 2023-10-15T13:47:38.558296+00:00 | 2023-10-15T14:24:39.979766+00:00 | 28 | 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 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | How to overcome Memory Limit Exceed (MLE)?, here is the solution to fix this! (C++) | how-to-overcome-memory-limit-exceed-mle-j4gij | Intuition\n We will use recursion to find out the number of ways.\n Describe your first thoughts on how to solve this problem. \n\n# Why MLE\n1. If you are m | geeteshyadav | NORMAL | 2023-10-15T12:57:04.889497+00:00 | 2023-10-15T12:57:04.889528+00:00 | 26 | false | # Intuition\n We will use recursion to find out the number of ways.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Why MLE\n1. If you are making dp array like **dp[arrLen][steps + 1]**, then its definitely going to give MLE **( 10^6 * 501)** and it is something about 10^9. \n2. But we can ... | 1 | 0 | ['Divide and Conquer', 'Dynamic Programming', 'Recursion', 'Memoization', 'C++'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | C# Solution ; | c-solution-by-moderneinstein-0erz | Intuition\n Describe your first thoughts on how to solve this problem. \n- At each point during the algorithm , there are multiple paths of\n- computation to c | ModernEinstein_ | NORMAL | 2023-10-15T12:41:55.536589+00:00 | 2023-10-15T12:41:55.536609+00:00 | 10 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. --> \n- At each point during the algorithm , there are multiple paths of\n- computation to consider, \n-Dynamic programmming may be used for this situation ; \n-At each phase of the algorithm , consider the number of \n-of ways to stay the ... | 1 | 0 | ['C#'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | 1269. Number of Ways to Stay in the Same Place After Some Steps || Java | 1269-number-of-ways-to-stay-in-the-same-ifhb0 | \nclass Solution {\n int mod = (int)(1e9+7); // Define a constant for modulo operation\n HashMap<String, Integer> hp = new HashMap<String, Integer>(); / | ArpAry | NORMAL | 2023-10-15T12:40:07.607950+00:00 | 2023-10-15T12:40:07.607973+00:00 | 1 | false | ```\nclass Solution {\n int mod = (int)(1e9+7); // Define a constant for modulo operation\n HashMap<String, Integer> hp = new HashMap<String, Integer>(); // Create a HashMap for memoization\n\n // Recursive function to calculate the number of ways to reach a position\n public int solve(int steps, int arrL... | 1 | 0 | ['Memoization'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Catching the 'catch' || Java Solution | catching-the-catch-java-solution-by-rish-l6d9 | This problem is not very hard as the problem statement and the difficulty tag demostrates so. \nAnyone with a touch of dynamic programming practice would easily | Rishabh2804 | NORMAL | 2023-10-15T11:49:08.090363+00:00 | 2023-10-15T11:49:08.090383+00:00 | 8 | false | This problem is not very hard as the problem statement and the difficulty tag demostrates so. <br>\nAnyone with a touch of dynamic programming practice would easily think a straightforward solution, \nonly to find that the contrants are too high for a 2D dp. \n\n   `1 <= n <= 10^6`\n   `1 <= steps <= 500`\n \... | 1 | 0 | ['Dynamic Programming', 'Memoization', 'Java'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | plain 2d dp | plain-2d-dp-by-mr_stark-v9tg | \nclass Solution {\npublic:\n \n vector<vector<int>> dp;\n int mod = 1e9+7;\n int solve(int i,int s, int n)\n {\n if(i>=n || i<0)\n | mr_stark | NORMAL | 2023-10-15T11:34:39.208027+00:00 | 2023-10-15T11:34:39.208049+00:00 | 10 | false | ```\nclass Solution {\npublic:\n \n vector<vector<int>> dp;\n int mod = 1e9+7;\n int solve(int i,int s, int n)\n {\n if(i>=n || i<0)\n return 0;\n if(s == 0){\n return i == 0;\n }\n \n if(dp[i][s]!=-1)\n return dp[i][s];\n long lo... | 1 | 0 | ['C'] | 1 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | python | python-by-ski-p3r-fm53 | \nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n modulo = 1000000007\n max_len = min(arrLen, 1 + steps // 2)\n w | ski-p3r | NORMAL | 2023-10-15T11:33:01.710304+00:00 | 2023-10-15T11:33:01.710327+00:00 | 8 | false | ```\nclass Solution:\n def numWays(self, steps: int, arrLen: int) -> int:\n modulo = 1000000007\n max_len = min(arrLen, 1 + steps // 2)\n ways = [0] * (max_len + 1)\n ways[0] = 1\n for i in range(steps):\n left = 0\n for j in range(min(max_len, i + 2, steps - ... | 1 | 0 | ['Python', 'Python3'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | very easy solution with dp | very-easy-solution-with-dp-by-teenuburi-idrc | \n\n# Complexity\n- Time complexity:\nO(nn)\n\n- Space complexity:\nO(nn)\n\n# Code\n\nclass Solution {\n func numWays(_ steps: Int, _ arrLen: Int) -> Int {\ | teenuburi | NORMAL | 2023-10-15T11:19:52.264021+00:00 | 2023-10-15T11:19:52.264037+00:00 | 7 | false | \n\n# Complexity\n- Time complexity:\nO(n*n)\n\n- Space complexity:\nO(n*n)\n\n# Code\n```\nclass Solution {\n func numWays(_ steps: Int, _ arrLen: Int) -> Int {\n var arrLen = min(steps, arrLen)\n\n var dp = [[Int]](repeating: [Int](repeating: -1, count: arrLen+2), count: steps+1)\n\n return nu... | 1 | 0 | ['Swift'] | 0 |
number-of-ways-to-stay-in-the-same-place-after-some-steps | Best Java Solution || Beats 95% | best-java-solution-beats-95-by-ravikumar-75o6 | 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 | ravikumar50 | NORMAL | 2023-10-15T10:48:07.905441+00:00 | 2023-10-15T10:48:07.905459+00:00 | 30 | 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 |
get-maximum-in-generated-array | [Python] Simulate process, explained | python-simulate-process-explained-by-dba-dsh8 | In this problem you just need to do what is asked: if we have even and odd indexes, generate data in correct way. There is a couple of small tricks to make your | dbabichev | NORMAL | 2021-01-15T08:35:07.618844+00:00 | 2021-01-15T08:35:07.618872+00:00 | 5,951 | false | In this problem you just need to do what is asked: if we have even and odd indexes, generate data in correct way. There is a couple of small tricks to make your code cleaner:\n1. Create `nums = [0] * (n + 2)` to handle case `n = 0`.\n2. Use ` nums[i] = nums[i//2] + nums[(i//2)+1] * (i%2)` to handle both cases of even a... | 90 | 2 | [] | 10 |
get-maximum-in-generated-array | C++ Simple and Short Solution 0 ms faster than 100% | c-simple-and-short-solution-0-ms-faster-t2fd4 | \nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if (n == 0 || n == 1) return n;\n \n vector<int> arr(n+1);\n arr | yehudisk | NORMAL | 2021-01-15T09:38:08.941986+00:00 | 2021-01-15T09:38:08.942012+00:00 | 7,478 | false | ```\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if (n == 0 || n == 1) return n;\n \n vector<int> arr(n+1);\n arr[0] = 0;\n arr[1] = 1;\n int maxi = 1;\n \n for (int i = 2; i <= n; i++) {\n arr[i] = i % 2 == 0 ? arr[i/2] : arr[i / ... | 55 | 2 | ['C'] | 6 |
get-maximum-in-generated-array | C++ Precompute + O(1) | c-precompute-o1-by-votrubac-28mn | We compute the function for 100 elements once. Then, we scan the array and record the maximum element so far.\n\nThis way, all further queries will requrie O(1) | votrubac | NORMAL | 2020-11-08T04:03:23.496618+00:00 | 2020-11-08T04:09:02.683040+00:00 | 3,253 | false | We compute the function for 100 elements once. Then, we scan the array and record the maximum element so far.\n\nThis way, all further queries will requrie O(1) lookup.\n\n```cpp\nint f[101] = { 0, 1, 0};\nclass Solution {\npublic:\n int getMaximumGenerated(int n) {\n if (f[2] == 0) {\n for (int i ... | 27 | 25 | [] | 8 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.