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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
minimum-moves-to-spread-stones-over-grid | Backtracking solution | backtracking-solution-by-jkoppula-l133 | \n# Code\n\nclass Solution {\n public int minimumMoves(int[][] grid) {\n List<Cell> emptyCells = new ArrayList<>();\n List<Cell> excessCells = | jkoppula | NORMAL | 2024-01-23T18:03:38.428194+00:00 | 2024-01-23T18:03:38.428228+00:00 | 225 | false | \n# Code\n```\nclass Solution {\n public int minimumMoves(int[][] grid) {\n List<Cell> emptyCells = new ArrayList<>();\n List<Cell> excessCells = new ArrayList<>();\n \n for(int row=0;row<3;row++){\n for(int col=0;col<3;col++){\n if(grid[row][col] == 0){\n ... | 4 | 0 | ['Backtracking', 'Java'] | 1 |
minimum-moves-to-spread-stones-over-grid | Video Solution | Explanation With Drawings | In Depth | C++ | Java | video-solution-explanation-with-drawings-e34l | Intuition and approach discussed in detail in video solution\nhttps://youtu.be/0Yk5KFD9gm0\n\n# Code\n\nclass Solution {\npublic:\n int minMovs = -1;\n in | Fly_ing__Rhi_no | NORMAL | 2023-09-10T16:44:26.145118+00:00 | 2023-09-10T16:44:26.145140+00:00 | 1,588 | false | # Intuition and approach discussed in detail in video solution\nhttps://youtu.be/0Yk5KFD9gm0\n\n# Code\n```\nclass Solution {\npublic:\n int minMovs = -1;\n int minimumMoves(vector<vector<int>>& grid) {\n minMovs = INT_MAX;\n int rows = grid.size(), cols = grid[0].size();\n vector<pair<int, i... | 4 | 0 | ['C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Python3, Permutations, Beats 100% | python3-permutations-beats-100-by-silvia-iwge | Intuition\nWe build two arrays: zeros to store indices of zeros in the grid, and d to store the indices we can use. If a cell can be used multiple times, we add | silvia42 | NORMAL | 2023-09-10T16:34:23.192738+00:00 | 2023-09-10T21:02:18.603761+00:00 | 709 | false | # Intuition\nWe build two arrays: `zeros` to store indices of zeros in the grid, and `d` to store the indices we can use. If a cell can be used multiple times, we add its indices to array `d` the corresponding number of times.\n\nWe evaluate all permutations, calculate the distances, and select the one with the smalles... | 4 | 0 | ['Python3'] | 1 |
minimum-moves-to-spread-stones-over-grid | python3 backtracking - in neetcode.io codestyle | python3-backtracking-in-neetcodeio-codes-jhmn | Followed the same style of coding as done in neetcode.io series. Some might find this easy to follow.\n--\n# Code\n\nclass Solution:\n def minimumMoves(self, | t00rb0y | NORMAL | 2024-03-09T13:29:54.256667+00:00 | 2024-03-09T13:29:54.256689+00:00 | 138 | false | Followed the same style of coding as done in neetcode.io series. Some might find this easy to follow.\n--\n# Code\n```\nclass Solution:\n def minimumMoves(self, grid: List[List[int]]) -> int:\n \n ROWS = len(grid)\n COLS = len(grid[0])\n\n donors = {}\n zeros = set()\n for i... | 3 | 0 | ['Backtracking', 'Python3'] | 1 |
minimum-moves-to-spread-stones-over-grid | Simple and clear python3 solution | 61 ms - faster than 92.33% solutions | simple-and-clear-python3-solution-61-ms-8bfbp | Complexity\n- Time complexity: O(m^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\nwh | tigprog | NORMAL | 2023-10-27T20:04:15.483213+00:00 | 2023-10-27T20:04:15.483237+00:00 | 161 | false | # Complexity\n- Time complexity: $$O(m^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\nwhere (see code) `n = grid.length where ceil == 0`, `m = grid.length where ceil != (0 or 1)`\nNB, `max(n + m) = 9`, so the greates... | 3 | 0 | ['Dynamic Programming', 'Python3'] | 0 |
minimum-moves-to-spread-stones-over-grid | Using Bit Mask DP easy solution | using-bit-mask-dp-easy-solution-by-maybk-5u75 | Intuition\nEasy Constraints\n\n# Approach\nBit Masks DP\n\n# Complexity\n- Time complexity:\nO(8x8x2^8)\n\n- Space complexity:\nO(8x8x2^8)\n\n# Code\n\nclass So | maybk | NORMAL | 2023-09-14T18:14:05.981324+00:00 | 2023-09-14T18:14:05.981353+00:00 | 455 | false | # Intuition\nEasy Constraints\n\n# Approach\nBit Masks DP\n\n# Complexity\n- Time complexity:\nO(8x8x2^8)\n\n- Space complexity:\nO(8x8x2^8)\n\n# Code\n```\nclass Solution {\npublic:\n int minimumMoves(vector<vector<int>>& grid) {\n vector<pair<int,int>>can,need;\n int n=grid.size(),m=grid[0].size();\n... | 3 | 0 | ['C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | 🔥Clean & Best C++ Code⭐ || 💯Recursion with BackTracking✅ | clean-best-c-code-recursion-with-backtra-uqqp | Code\n## Please Upvote if u found it useful\uD83E\uDD17\n\nclass Solution {\npublic:\n int minimumMoves(vector<vector<int>>& grid) {\n int mini = INT_ | aDish_21 | NORMAL | 2023-09-11T16:17:24.960200+00:00 | 2023-09-11T16:17:24.960225+00:00 | 99 | false | # Code\n## Please Upvote if u found it useful\uD83E\uDD17\n```\nclass Solution {\npublic:\n int minimumMoves(vector<vector<int>>& grid) {\n int mini = INT_MAX;\n int cnt_0 = 0;\n for(int i = 0 ; i < 3 ; i++){\n for(int j = 0 ; j < 3 ; j++)\n if(grid[i][j] == 0)\n ... | 3 | 0 | ['Backtracking', 'Recursion', 'Matrix', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Python dfs solution | python-dfs-solution-by-lm105013-ae9c | Intuition\n Describe your first thoughts on how to solve this problem. \nExcess stones must go to a empty cell, find all blank and excess cells, and use dfs to | lm105013 | NORMAL | 2023-09-11T13:27:54.910962+00:00 | 2023-09-11T13:27:54.910983+00:00 | 195 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nExcess stones must go to a empty cell, find all blank and excess cells, and use dfs to find the best way to move each stone\n\n# Code\n```\nclass Solution:\n def minimumMoves(self, grid: List[List[int]]) -> int:\n excess, blank ... | 3 | 0 | ['Python3'] | 0 |
minimum-moves-to-spread-stones-over-grid | [C++] Backtracking from the zero cells | c-backtracking-from-the-zero-cells-by-aw-rtb1 | Intuition\n Describe your first thoughts on how to solve this problem. \nTry all possible solutions to move stones to the cells without stone. \n\n# Approach\n | pepe-the-frog | NORMAL | 2023-09-11T11:43:18.827040+00:00 | 2023-09-11T11:43:18.827057+00:00 | 234 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTry all possible solutions to move stones to the cells without stone. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Collect the cells without stone (`grid[r][c] == 0`)\n2. Try to grab a stone from the cell wi... | 3 | 0 | ['C++'] | 1 |
minimum-moves-to-spread-stones-over-grid | JavaScript - Backtracking - 55ms - O((#zero cells + 1)!) | javascript-backtracking-55ms-ozero-cells-tit0 | Intuition\nSolved it first using greedy approach. Failed.\n\n# Approach\nI had the list of cells with zeros and extras left over from the greedy approach, so th | justacoder3 | NORMAL | 2023-09-10T23:52:32.531856+00:00 | 2023-09-12T03:42:54.329507+00:00 | 245 | false | # Intuition\nSolved it first using greedy approach. Failed.\n\n# Approach\nI had the list of cells with zeros and extras left over from the greedy approach, so this led to a faster recursive backtracking approach than most other solutions.\n# Complexity\n- Time complexity:\nFor every zero cell we try getting one from a... | 3 | 0 | ['JavaScript'] | 1 |
minimum-moves-to-spread-stones-over-grid | C++ Bit Masking DP Solution | c-bit-masking-dp-solution-by-dhavalkumar-ykok | Complexity\n- Time complexity: O(n.m.2 ^ {2m})\n\n- Space complexity: O(n.2 ^ m)\n\n\n# Code\n\nclass Solution {\npublic:\n int minimumMoves(vector<vector<in | dhavalkumar | NORMAL | 2023-09-10T07:06:48.825111+00:00 | 2023-09-11T18:24:35.786355+00:00 | 166 | false | # Complexity\n- Time complexity: $$O(n.m.2 ^ {2m})$$\n\n- Space complexity: $$O(n.2 ^ m)$$\n\n\n# Code\n```\nclass Solution {\npublic:\n int minimumMoves(vector<vector<int>>& grid) {\n vector<vector<int>> has;\n vector<pair<int, int>> need;\n\n for (int i = 0; i < 3; i++) {\n for (int... | 3 | 0 | ['Dynamic Programming', 'Bit Manipulation', 'C++'] | 1 |
minimum-moves-to-spread-stones-over-grid | Try all Possible permutation(i.e Atmost 3!)[O(1)] | try-all-possible-permutationie-atmost-3o-x0h9 | Intuition\nMy idea is that we can try all the possible permuation of number greater than 1 and we will get the moves required for each step by\n|x1-x2| + |y1-y2 | __Abcd__ | NORMAL | 2023-09-10T04:45:08.688311+00:00 | 2023-09-10T04:53:43.407865+00:00 | 367 | false | # Intuition\nMy idea is that we can try all the possible permuation of number greater than 1 and we will get the moves required for each step by\n|x1-x2| + |y1-y2|\nwill repeat it for all the permuations possible \nand then will get the answer... \n\n\n# Space complexity : \nSince i know array that i have created will ... | 3 | 0 | ['C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | C++ || Easy Understanding || BackTracking | c-easy-understanding-backtracking-by-n_i-1ow8 | Code\n\nclass Solution {\nprivate:\n bool check(vector<vector<int>> &grid){\n for(int i = 0 ; i < 3 ; i++){\n for(int j = 0 ; j < 3 ; j++){ | __SAI__NIVAS__ | NORMAL | 2023-09-10T04:35:39.618365+00:00 | 2023-09-10T04:35:39.618384+00:00 | 355 | false | # Code\n```\nclass Solution {\nprivate:\n bool check(vector<vector<int>> &grid){\n for(int i = 0 ; i < 3 ; i++){\n for(int j = 0 ; j < 3 ; j++){\n if(grid[i][j] != 1) return false;\n }\n }\n return true;\n }\n \n int helper(vector<vector<int>> &grid)... | 3 | 0 | ['C++'] | 1 |
minimum-moves-to-spread-stones-over-grid | Simple Backtraking solution | simple-backtraking-solution-by-bhattpara-aw20 | \n# Code\n\nclass Solution {\npublic:\n int minimumMoves(vector<vector<int>>& grid) {\n // Base Case\n int t = 0;\n for (int i = 0; i < | bhattparaj1 | NORMAL | 2023-09-10T04:25:13.794162+00:00 | 2023-09-10T04:25:13.794181+00:00 | 115 | false | \n# Code\n```\nclass Solution {\npublic:\n int minimumMoves(vector<vector<int>>& grid) {\n // Base Case\n int t = 0;\n for (int i = 0; i < 3; ++i)\n for (int j = 0; j < 3; ++j)\n if (grid[i][j] == 0)\n t++;\n if (t == 0)\n return 0;\... | 3 | 1 | ['C++'] | 1 |
minimum-moves-to-spread-stones-over-grid | Simple Solution || Easy to Understand | simple-solution-easy-to-understand-by-qb-7d4r | Intuition\nSince, problem size is small, we can check for each permutation of stones.\n\n# Approach\nArrange the grid with no stones in all possible permutation | calm_porcupine | NORMAL | 2023-09-10T04:20:10.216310+00:00 | 2023-09-10T04:20:10.216327+00:00 | 146 | false | # Intuition\nSince, problem size is small, we can check for each permutation of stones.\n\n# Approach\nArrange the grid with no stones in all possible permutations and satisfy the request with the grids with more than 1 stone.\n\n\n# Code\n```\nclass Solution {\npublic:\n int solve(map<pair<int, int>, int>more, vect... | 3 | 0 | ['Backtracking', 'C++'] | 1 |
minimum-moves-to-spread-stones-over-grid | easy backtracking | easy-backtracking-by-shashank__11-kiih | \n# Intuition\nLooking at the constraints be can see that be can apply backtraking to it.\n# Approach\nWhen Ever be see a cell with 0 value we now try to find a | shashank__11 | NORMAL | 2023-09-10T04:05:07.753333+00:00 | 2023-09-10T05:57:09.442719+00:00 | 726 | false | \n# Intuition\nLooking at the constraints be can see that be can apply backtraking to it.\n# Approach\nWhen Ever be see a cell with 0 value we now try to find a cell in which there are more than one value so that be can take 1 from it and then continue with that cell\n\n# Code\n```\nclass Solution {\npublic:\n int m... | 3 | 0 | ['Backtracking', 'C++'] | 2 |
minimum-moves-to-spread-stones-over-grid | EASY Brute-Force: Manhattan-distance | easy-brute-force-manhattan-distance-by-z-fwkw | 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 | zedzogrind | NORMAL | 2023-09-10T04:04:26.325231+00:00 | 2023-09-10T04:04:26.325262+00:00 | 393 | 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)$$ --... | 3 | 0 | ['Python3'] | 0 |
minimum-moves-to-spread-stones-over-grid | easy short efficient solution | easy-short-efficient-solution-by-maveric-k3qf | cpp\nclass Solution {\npublic:\n int minimumMoves(vector<vector<int>>&v) {\n int ans = INT_MAX;\n for(int i=0; i<3; ++i){\n for(int | maverick09 | NORMAL | 2023-09-10T04:01:34.710505+00:00 | 2023-09-10T04:47:33.016427+00:00 | 576 | false | ```cpp\nclass Solution {\npublic:\n int minimumMoves(vector<vector<int>>&v) {\n int ans = INT_MAX;\n for(int i=0; i<3; ++i){\n for(int j=0; j<3; ++j){\n if(v[i][j]){\n continue;\n }\n for(int a=0; a<3; ++a){\n ... | 3 | 0 | ['Recursion', 'C'] | 1 |
minimum-moves-to-spread-stones-over-grid | Python3 | Beats 90% | No external dependencies | python3-beats-90-no-external-dependencie-rc20 | IntuitionSee comments in the codeCode | simosound94 | NORMAL | 2025-03-15T12:42:19.728918+00:00 | 2025-03-15T12:42:19.728918+00:00 | 61 | false | # Intuition
See comments in the code
# Code
```python3 []
class Solution:
def minimumMoves(self, grid: List[List[int]]) -> int:
"""
:type grid: List[List[int]]
:rtype: int
[[1,3,0],
[1,0,0],
[1,0,3]]
[(0,2), (1,1), (1,2), (2,1)]
[(0,1,0), (2,2,1)]
... | 2 | 0 | ['Python3'] | 0 |
minimum-moves-to-spread-stones-over-grid | Java Simple Backtracking that passed all test cases | java-simple-backtracking-that-passed-all-4r6y | \n\n# Code\n\npublic int minimumMoves(int[][] grid) {\n List<int[]> suppliers=new ArrayList<>();\n List<int[]> consumers=new ArrayList<>();\n | acthyy | NORMAL | 2024-03-16T06:48:41.912534+00:00 | 2024-03-16T06:48:41.912558+00:00 | 222 | false | \n\n# Code\n```\npublic int minimumMoves(int[][] grid) {\n List<int[]> suppliers=new ArrayList<>();\n List<int[]> consumers=new ArrayList<>();\n for(int i=0; i<grid.length; i++){\n for(int j=0; j<grid.length; j++){\n if(grid[i][j]==0){\n consumers.add(ne... | 2 | 0 | ['Java'] | 3 |
minimum-moves-to-spread-stones-over-grid | [Python3] Python Backtracking w/ Hashmap | python3-python-backtracking-w-hashmap-by-dkvj | Problem Breakdown:\n- There\'s a 3x3 board where each cell has a number that representes how many stones it has \n- Some cells have 0 stones, others have non-ze | abhinavp246 | NORMAL | 2023-11-06T01:45:34.909053+00:00 | 2023-11-06T01:45:34.909069+00:00 | 68 | false | Problem Breakdown:\n- There\'s a 3x3 board where each cell has a number that representes how many stones it has \n- Some cells have 0 stones, others have non-zero stones \n- We need to find a way to move the stones from the non-zero cells to the zero cells that minimizes the total distance the stones are moved\n\nI was... | 2 | 0 | ['Backtracking', 'Python'] | 0 |
minimum-moves-to-spread-stones-over-grid | Java | Backtracking with Bitmask Memorization | java-backtracking-with-bitmask-memorizat-umdf | 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 | zenggq | NORMAL | 2023-10-05T14:31:49.428016+00:00 | 2023-10-05T14:44:56.479521+00:00 | 42 | 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$$O(k^2*2^k)$$. $$k$$ is the number of empty cells.\n\n- Space complexity:\n... | 2 | 0 | ['Backtracking', 'Memoization', 'Bitmask', 'Java'] | 0 |
minimum-moves-to-spread-stones-over-grid | Python easy to understand | permutation & pruning | python-easy-to-understand-permutation-pr-10uj | Intuition\n Describe your first thoughts on how to solve this problem. \nStore coordinates of empty slots to a list, and coordinates of extra stones to a list. | icode999 | NORMAL | 2023-10-04T15:38:44.060699+00:00 | 2023-10-04T15:38:44.060723+00:00 | 206 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nStore coordinates of empty slots to a list, and coordinates of extra stones to a list. All we need is to try different element-wise matching of the two lists.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Iterat... | 2 | 0 | ['Python3'] | 1 |
minimum-moves-to-spread-stones-over-grid | Min cost flow O(F*m*n) - Successive shortest path - 4ms - Beats 100% | min-cost-flow-ofmn-successive-shortest-p-sm6p | Intuition\nThe problem can be thought of assigning the excess nodes to nodes with nothing. That is, squares on the grid[i][j] will haves excess of grid[i][j]-1 | mhayter1 | NORMAL | 2023-09-11T07:22:56.640451+00:00 | 2023-09-11T07:25:28.073431+00:00 | 48 | false | # Intuition\nThe problem can be thought of assigning the excess nodes to nodes with nothing. That is, squares on the grid[i][j] will haves excess of grid[i][j]-1 if grid[i][j]>=2.\n# Approach\nCreate a graph with edges from grid[i][j]>=2 to grid[i][j]==0 with capacity 1 and cost being the manhattan distance (abs(y1-y2... | 2 | 0 | ['Shortest Path', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Easy✅ || Beginner friendly || DFS || Recursion || Backtracking || C++ | easy-beginner-friendly-dfs-recursion-bac-9l29 | Explanation\nBFS wont work, since we are required to find out all possible ways and pick up the minimum moves possible\n\n- Now for every zero th cell we need t | freakieee | NORMAL | 2023-09-10T11:58:27.844337+00:00 | 2023-09-10T11:58:27.844364+00:00 | 141 | false | # Explanation\nBFS wont work, since we are required to find out all possible ways and pick up the minimum moves possible\n\n- Now for every zero th cell we need to try out every extra cell(the cell whose value is > 1) \n- First we will traverse the grid and store the positions of zero th and extra cell in 2 separate ve... | 2 | 0 | ['Backtracking', 'Depth-First Search', 'Recursion', 'C++'] | 1 |
minimum-moves-to-spread-stones-over-grid | C++/Python, backtracking/bitmask dp solution with explanation | cpython-backtrackingbitmask-dp-solution-ejghv | backtracking (permutation)\nif there are m cells whose number of stone is 0,\nthere are also m stones on different cells whose number of stone > 1.\nSo, use bac | shun6096tw | NORMAL | 2023-09-10T10:42:46.725289+00:00 | 2023-09-10T11:35:44.999873+00:00 | 158 | false | ### backtracking (permutation)\nif there are m cells whose number of stone is 0,\nthere are also m stones on different cells whose number of stone > 1.\nSo, use backtracking to list permutation of stone to find the minimum cost.\ne.g., there are some stone at [(0,0), (0,1)], there are some cells need a stone [(2,2), (1... | 2 | 0 | ['Dynamic Programming', 'Backtracking', 'Depth-First Search', 'C', 'Python'] | 0 |
minimum-moves-to-spread-stones-over-grid | C++ | Beats 100% | Easy to Understand | Recursive Solution | c-beats-100-easy-to-understand-recursive-zxk7 | 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 | charucjoshi | NORMAL | 2023-09-10T09:42:19.371490+00:00 | 2023-09-10T09:42:19.371508+00:00 | 50 | 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 | ['C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Backtracking C++ solution || Well commented and explained | backtracking-c-solution-well-commented-a-5e2e | Intuition\nConstraints are very less so we can just try every possible combination.\n Describe your first thoughts on how to solve this problem. \n\n# Approach\ | anonymouss_01 | NORMAL | 2023-09-10T08:44:16.622499+00:00 | 2023-09-10T08:44:16.622518+00:00 | 163 | false | # Intuition\nConstraints are very less so we can just try every possible combination.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nStore the pos with 0 in v1 and for greater than 1 in v2. Then try all possible combinations of pair formed and return the minimum distance.\n<!-- Desc... | 2 | 0 | ['Math', 'Backtracking', 'Recursion', 'Matrix', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Fairly Simple Backtracking Solution✅|| with Intuition and Comments📚 | fairly-simple-backtracking-solution-with-5aj3 | Intuition\nEach of the surplus stones can go to any vacant spot in the grid. So do just that! Transfer one of the extra stones (say John) to one of the vacant s | neil_paul | NORMAL | 2023-09-10T07:07:08.557937+00:00 | 2023-09-10T07:12:51.225837+00:00 | 298 | false | # Intuition\nEach of the surplus stones can go to any vacant spot in the grid. So do just that! Transfer one of the extra stones (say John) to one of the vacant spots (say McClane) and make a recursive call for the reamining stones. After all cells have one stone each, come back and fix John stone in any other vacant s... | 2 | 0 | ['Backtracking', 'Recursion', 'Matrix', 'Python3'] | 1 |
minimum-moves-to-spread-stones-over-grid | BFS + Permutations Detailed Explanation with well-commented Code | bfs-permutations-detailed-explanation-wi-pbqn | UPVOTE IF YOU LIKE \n# Intuition\n- The Intuition for this problem is bfs + trying all possible permutations.\n\n# Approach\n- We have to first find out which c | shaileshsingh23423 | NORMAL | 2023-09-10T04:55:58.319360+00:00 | 2023-09-10T04:58:10.059713+00:00 | 710 | false | # UPVOTE IF YOU LIKE \n# Intuition\n- The Intuition for this problem is bfs + trying all possible permutations.\n\n# Approach\n- We have to first find out which cell have more than 1 stone.\n- Now, suppose we have `3 cells` having more than 1 stone. Then, we have 6 choices from which cell we start and to which cell we ... | 2 | 0 | ['Backtracking', 'Breadth-First Search', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | BFS || Fast | bfs-fast-by-lil_toeturtle-2bri | \nclass Solution {\n int[] dir={-1, 0, 1, 0, -1};\n public int minimumMoves(int[][] grid) {\n HashSet<String> vis=new HashSet<>();\n int l=0 | Lil_ToeTurtle | NORMAL | 2023-09-10T04:43:09.190526+00:00 | 2023-09-10T04:43:09.190545+00:00 | 826 | false | ```\nclass Solution {\n int[] dir={-1, 0, 1, 0, -1};\n public int minimumMoves(int[][] grid) {\n HashSet<String> vis=new HashSet<>();\n int l=0;\n Queue<int[][]> q = new LinkedList<>();\n q.add(grid);\n q.add(null);\n while(!q.isEmpty()) {\n int[][] g=q.poll();... | 2 | 1 | ['Breadth-First Search', 'Java'] | 1 |
minimum-moves-to-spread-stones-over-grid | [C++] l Brute Force l Recursion | c-l-brute-force-l-recursion-by-vineet-0-1ots | Intution\nBrute Force\n\n\n\n\nTry all possiblities\n\n# Approach\njust using Brute Force\n\n\n\n\n\n\n# Code\n\nclass Solution {\n vector<vector<int>> check | Vineet-0 | NORMAL | 2023-09-10T04:36:42.248111+00:00 | 2023-09-10T04:36:42.248143+00:00 | 110 | false | # Intution\nBrute Force\n\n\n\n\nTry all possiblities\n\n# Approach\njust using Brute Force\n\n\n\n\n\n\n# Code\n```\nclass Solution {\n vector<vector<int>> check = {{1,1,1},{1,1,1},{1,1,1}};\npublic:\n int minimumMoves(vector<vector<int>>& grid) {\n if(grid==check)\n return 0;\n int ans ... | 2 | 0 | ['Recursion', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Brute Force Solution with desi jugaad (i-var) | brute-force-solution-with-desi-jugaad-i-lwa8i | # Intuition \n\n\n# Approach\n\nFor every cell containing ZERO find the cell with value more than ONE with minimum manhatan distance, as this is a greedy way | i-var | NORMAL | 2023-09-10T04:31:02.616608+00:00 | 2023-09-10T04:31:02.616638+00:00 | 46 | false | <!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFor every cell containing ZERO find the cell with value more than ONE with minimum manhatan distance, as this is a greedy way of selecting the contributor cel... | 2 | 0 | ['C++'] | 1 |
minimum-moves-to-spread-stones-over-grid | Backtracking | backtracking-by-nitleet12-xudx | Intuition\n Describe your first thoughts on how to solve this problem. \nTransfer one stone from the cell having more than one stone to any empty cell. \nTry ea | nitleet12 | NORMAL | 2023-09-10T04:16:49.600588+00:00 | 2023-09-10T04:16:49.600611+00:00 | 201 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTransfer one stone from the cell having more than one stone to any empty cell. \nTry each way to transfer.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nBacktracking\n\n# Complexity\n- Time complexity:\n<!-- Add ... | 2 | 1 | ['Backtracking', 'Recursion', 'Java'] | 0 |
minimum-moves-to-spread-stones-over-grid | C++| Short and Concise Solution | c-short-and-concise-solution-by-coder348-e1vb | Code\n\nclass Solution {\npublic:\n int func(vector<vector<int>>& v){\n int ans = INT_MAX;\n for(int i=0; i<3; ++i){\n for(int j=0; | coder3484 | NORMAL | 2023-09-10T04:04:20.484684+00:00 | 2023-09-10T04:04:20.484708+00:00 | 1,294 | false | # Code\n```\nclass Solution {\npublic:\n int func(vector<vector<int>>& v){\n int ans = INT_MAX;\n for(int i=0; i<3; ++i){\n for(int j=0; j<3; ++j){\n if(v[i][j]) continue;\n for(int a=0; a<3; ++a){\n for(int b=0; b<3; ++b){\n ... | 2 | 0 | ['C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | 🚀 Easy C++ || Recursive || simple || | easy-c-recursive-simple-by-satenderrkuma-5tds | \n\n# Code\n\nclass Solution {\npublic:\n int solve(vector<pair<int,int>>& arr1 , vector<pair<int,int>>& arr2 , int ind ,vector<vector<int>>& grid ){\n | satenderrkumar11 | NORMAL | 2023-09-10T04:01:32.402217+00:00 | 2023-09-10T04:03:16.734994+00:00 | 126 | false | \n\n# Code\n```\nclass Solution {\npublic:\n int solve(vector<pair<int,int>>& arr1 , vector<pair<int,int>>& arr2 , int ind ,vector<vector<int>>& grid ){\n if(ind == arr2.size())return 0;\n \n int res = INT_MAX;\n for(auto [i,j]:arr1){\n if(grid[i][j] > 1){\n int ... | 2 | 0 | ['C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Wow! Just go through all paths | wow-just-go-through-all-paths-by-war02_a-khqb | IntuitionInitially thought its simple BFS, then got some test cases which make me think broader,
We have to go throgh all possible paths and pick minimum one.Th | war02_abhishek | NORMAL | 2025-02-09T10:03:45.827608+00:00 | 2025-02-09T10:05:24.259985+00:00 | 76 | false | # Intuition
Initially thought its simple BFS, then got some test cases which make me think broader,
We have to go throgh all possible paths and pick minimum one.Though DP will work but unable to think DP states
So decided to build recursion solution first.
# Approach
First note all extra's(>1), their location in vecto... | 1 | 0 | ['Recursion', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Javascript Backtracking with memoization | javascript-backtracking-with-memoization-fwrq | \n# Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n) \nO(2^N)\n\n- Space complexity:\n Add your space complexity here, e.g. O(n) \nO(N) | vijay369 | NORMAL | 2024-02-26T07:35:52.203022+00:00 | 2024-02-26T07:35:52.203054+00:00 | 44 | false | \n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(2^N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N)\n\n# Code\n```\n/**\n * @param {number[][]} grid\n * @return {number}\n */\nvar minimumMoves = function(grid) {\n // Memo to retreive ... | 1 | 0 | ['Backtracking', 'Recursion', 'Memoization', 'JavaScript'] | 0 |
minimum-moves-to-spread-stones-over-grid | Easy C++ Solution || Beats 98% (Using Backtracking) ✅✅ | easy-c-solution-beats-98-using-backtrack-lq62 | Code\n\nclass Solution {\npublic:\n int helper(vector<vector<int>> &usez,vector<vector<int>> &usem,int i,int n){\n if(i>=n){\n return 0;\n | Abhi242 | NORMAL | 2024-02-24T09:56:04.403288+00:00 | 2024-02-24T09:56:04.403313+00:00 | 605 | false | # Code\n```\nclass Solution {\npublic:\n int helper(vector<vector<int>> &usez,vector<vector<int>> &usem,int i,int n){\n if(i>=n){\n return 0;\n }\n int ans=INT_MAX;\n for(int k=0;k<usem.size();k++){\n if(usem[k][2]>1){\n usem[k][2]--;\n ... | 1 | 0 | ['Backtracking', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | BFS Approach | bfs-approach-by-alireza_ghods-thl1 | Intuition\n\nBFS is particularly well-suited for finding the shortest path or the minimum number of moves to reach a goal because it explores all possible moves | alireza_ghods | NORMAL | 2024-02-12T07:10:51.796053+00:00 | 2024-02-12T07:10:51.796083+00:00 | 50 | false | # Intuition\n\nBFS is particularly well-suited for finding the shortest path or the minimum number of moves to reach a goal because it explores all possible moves at a given depth before moving on to the next depth. This ensures that the first time it reaches the goal state (one stone per cell), it has found the path w... | 1 | 0 | ['Python3'] | 0 |
minimum-moves-to-spread-stones-over-grid | Permutation approach (50ms) | permutation-approach-50ms-by-deepanshus-8pw8 | Intuition\nEvery zero needs to borrow from some position. No need to traverse grid to match these two positions.\n# Approach\nCreate a list zeroes with of all c | deepanshus | NORMAL | 2024-01-17T16:38:15.544492+00:00 | 2024-01-17T16:38:15.544521+00:00 | 5 | false | # Intuition\nEvery zero needs to borrow from some position. No need to traverse grid to match these two positions.\n# Approach\nCreate a list `zeroes` with of all coordinates of all cells with value 0. Make another list `extras` for cells with value greater than 1. But create duplicate entries for each extra stone. \nF... | 1 | 0 | ['Combinatorics', 'C++'] | 1 |
minimum-moves-to-spread-stones-over-grid | BEATS 93% !!! | beats-93-by-infinite_memory-dprq | Intuition\nUnderestimated the problem but was difficult to me than most mediums I guess.\nMy idea was to have 2 lists one with indexes of zeros and other with i | InFiNiTe_MeMoRy | NORMAL | 2024-01-13T19:30:55.500592+00:00 | 2024-01-13T19:30:55.500620+00:00 | 26 | false | # Intuition\nUnderestimated the problem but was difficult to me than most mediums I guess.\nMy idea was to have 2 lists one with indexes of zeros and other with indexes of 2s or 3s and then try each 2\'s or 3\'s for all zeros and taking minimum sum Manhattan distance.\n\n# Approach\nI created a class called Triad in wh... | 1 | 0 | ['Array', 'Backtracking', 'Recursion', 'Java'] | 0 |
minimum-moves-to-spread-stones-over-grid | Detailed explanation with Approach + Comments | detailed-explanation-with-approach-comme-ev97 | Intuition\nEvery time we move a cell, we want to check the state of the grid, ie, if all cells have 1 stone each. Backtracking/Recursion can help us evaluate th | vishwajeet1 | NORMAL | 2024-01-08T17:17:54.126309+00:00 | 2024-01-08T17:17:54.126346+00:00 | 39 | false | # Intuition\nEvery time we move a cell, we want to check the state of the grid, ie, if all cells have 1 stone each. Backtracking/Recursion can help us evaluate that. This approach checks for all possible scenarios of filling up the grid while also checking the minimum number of moves required to do so in that iteration... | 1 | 0 | ['Backtracking', 'Recursion', 'Java'] | 0 |
minimum-moves-to-spread-stones-over-grid | Easy Recursive (Beats 100% of submissions) | easy-recursive-beats-100-of-submissions-t28de | Intuition\n Describe your first thoughts on how to solve this problem. \nUse Recursion for only few elements which are zero and more than 1\n# Approach\n Descri | robin1302 | NORMAL | 2023-12-15T15:47:09.276605+00:00 | 2023-12-15T15:50:00.327825+00:00 | 118 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse Recursion for only few elements which are zero and more than 1\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Create two list one containing Zero indexes and another for bulky indexes (> 1)\n- Recursively call... | 1 | 0 | ['C#'] | 1 |
minimum-moves-to-spread-stones-over-grid | Backtracking with intuition explained. Faster than 95% | backtracking-with-intuition-explained-fa-lb81 | Intuition\nThe idea is to have all the zeros in a list and all the values > 1 in another list. Then bruteforce the solution using recursion.\n\n\n# Code\n\npubl | theaion | NORMAL | 2023-09-28T20:05:12.040009+00:00 | 2023-09-28T20:05:12.040032+00:00 | 109 | false | # Intuition\nThe idea is to have all the zeros in a list and all the values > 1 in another list. Then bruteforce the solution using recursion.\n\n\n# Code\n```\npublic class Solution {\n public int MinimumMoves(int[][] grid) {\n List<(int, int)> zeros = new();\n List<(int, int, int)> moreThanOne = new(... | 1 | 0 | ['Backtracking', 'Recursion', 'C#'] | 1 |
minimum-moves-to-spread-stones-over-grid | 0 ms || fastest || backtracking approach made easy. | 0-ms-fastest-backtracking-approach-made-zful9 | Intuition\nTo figure out backtracking , i wasn\'t able to get it in the first go , so i tried (GREEDY) filling zeros from the positions close to them having mor | dr_vegapunnk | NORMAL | 2023-09-18T12:29:00.865078+00:00 | 2023-09-18T12:29:00.865102+00:00 | 137 | false | # Intuition\nTo figure out backtracking , i wasn\'t able to get it in the first go , so i tried (GREEDY) filling zeros from the positions close to them having more stones and going on to filling the position with zero stones ,but was failing 91 test cases. \n\nthen it picked me that the ordering in which i am picking t... | 1 | 0 | ['Backtracking', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Go | Recursion+Backtracking | go-recursionbacktracking-by-b3k3r-dcrm | Intuition\n Describe your first thoughts on how to solve this problem. \nOnly factor in what it takes to move stones from a cell for the grid with > 1 stones to | b3k3r | NORMAL | 2023-09-15T02:25:05.342784+00:00 | 2023-09-15T02:25:05.342835+00:00 | 34 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nOnly factor in what it takes to move stones from a cell for the grid with > 1 stones to one with 0 stones. Dont have to bother with any other cells. Distance can be calculated using equation for Manhattan distance from one cell to another... | 1 | 0 | ['Backtracking', 'Recursion', 'Go'] | 0 |
minimum-moves-to-spread-stones-over-grid | C++ || easy solution | c-easy-solution-by-shradhaydham24-09th | 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 | shradhaydham24 | NORMAL | 2023-09-11T14:39:26.518481+00:00 | 2023-09-11T14:39:26.518521+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 | ['C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Python - short brute force | python-short-brute-force-by-ante-415k | Code\n\nclass Solution:\n def neigh(self, i):\n r, c = i//3, i%3\n for rr, cc in [(r+1, c), (r-1, c), (r, c+1), (r, c-1)]: \n if 0 < | Ante_ | NORMAL | 2023-09-10T11:15:02.298100+00:00 | 2023-09-10T11:15:02.298130+00:00 | 90 | false | # Code\n```\nclass Solution:\n def neigh(self, i):\n r, c = i//3, i%3\n for rr, cc in [(r+1, c), (r-1, c), (r, c+1), (r, c-1)]: \n if 0 <= rr < 3 and 0 <= cc < 3: yield rr*3+cc\n\n def minimumMoves(self, g):\n processing, end_state, seen, moves = [[g[i//3][i%3] for i in range(9)]]... | 1 | 0 | ['Hash Table', 'Backtracking', 'Queue', 'Python', 'Python3'] | 0 |
minimum-moves-to-spread-stones-over-grid | 100% fast ||Simple and Short Recurrsion || C++ | 100-fast-simple-and-short-recurrsion-c-b-k88h | 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 | ankush920 | NORMAL | 2023-09-10T07:29:16.512127+00:00 | 2023-09-10T07:29:16.512152+00:00 | 467 | 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++'] | 1 |
minimum-moves-to-spread-stones-over-grid | ✅ C++ ✅ | Easy | Backtracking | ✅Accepted✅ | c-easy-backtracking-accepted-by-mohit_ka-lmnf | \n\n# Intuition\nConstraints are very small, So apply brute force \n\n# Approach\n1. First check if any zero is present or not.\n2. Simply traverse the matrix.\ | mohit_kaushal | NORMAL | 2023-09-10T06:59:01.070877+00:00 | 2023-09-10T09:23:27.118875+00:00 | 192 | false | \n\n# Intuition\nConstraints are very small, So apply brute force \n\n# Approach\n1. First check if any zero is present or not.\n2. Simply traverse the matrix.\n3. If any cell is empty means equal to zero, again traverse the matrix to find non-zero cell having stones more than 1.\n4. For every target position of empty ... | 1 | 0 | ['Backtracking', 'Recursion', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | ✅ Easy || Backtracking || C++ || recursion|| Brute force | easy-backtracking-c-recursion-brute-forc-bvun | Intuition\nA cell with value zero can be be fill by any cell having value greater than 1\nTry all case and for minimum\n\n# Approach\nRecursion for all case \n\ | pradeep068 | NORMAL | 2023-09-10T06:29:39.545811+00:00 | 2023-09-10T06:29:39.545830+00:00 | 89 | false | # Intuition\nA cell with value zero can be be fill by any cell having value greater than 1\nTry all case and for minimum\n\n# Approach\nRecursion for all case \n\n# Code\n```\nclass Solution {\npublic:\n int minimumMoves(vector<vector<int>> grid){\n vector<vector<int>>non;\n int x,y;\n for(int i... | 1 | 0 | ['C++'] | 1 |
minimum-moves-to-spread-stones-over-grid | 🔥✅ Easy || Backtracking || C++ || 100% || Fully Commented🔥✅ | easy-backtracking-c-100-fully-commented-3jzfc | Intuition\n Describe your first thoughts on how to solve this problem. \nThe code is designed to find the minimum number of moves required to clear a grid where | Coder_Aayush | NORMAL | 2023-09-10T05:57:44.524110+00:00 | 2023-09-10T05:57:44.524138+00:00 | 10 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**The code is designed to find the minimum number of moves required to clear a grid where each cell contains a certain number of stones. The grid is represented as a 3x3 matrix, and the goal is to move stones from one cell to another in t... | 1 | 0 | ['Backtracking', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Dijkstra | dijkstra-by-celonymire-qdev | Intuition\n Describe your first thoughts on how to solve this problem. \nI somehow couldn\'t come up with the right way to bruteforce, so I did an "optimized" b | CelonyMire | NORMAL | 2023-09-10T05:29:13.949454+00:00 | 2023-09-10T05:29:13.949481+00:00 | 30 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI somehow couldn\'t come up with the right way to bruteforce, so I did an "optimized" bruteforce via Dijkstra. Dijkstra can compute the minimum amount of operations to get to each possible state of the grid.\n\n# Approach\n<!-- Describe y... | 1 | 0 | ['Hash Table', 'Heap (Priority Queue)', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Best C++ sol | best-c-sol-by-raveeshgoyal-frpw | Code\n\nclass Solution {\npublic:\n void helper(int i,int j,int curr,int &ans,vector<vector<int>>& grid){\n if(i==3){\n ans=min(ans,curr); | raveeshgoyal | NORMAL | 2023-09-10T04:57:14.588263+00:00 | 2023-09-10T04:57:28.359160+00:00 | 177 | false | # Code\n```\nclass Solution {\npublic:\n void helper(int i,int j,int curr,int &ans,vector<vector<int>>& grid){\n if(i==3){\n ans=min(ans,curr); \n return;\n }\n if(j==3){\n helper(i+1,0,curr,ans,grid);\n return;\n }\n if(grid[i][j]!=... | 1 | 0 | ['C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | EASY SOLUTION || C++ || EXPLANATION | easy-solution-c-explanation-by-amol_2004-n6eg | Intuition\n Describe your first thoughts on how to solve this problem. \nSince the constraints are very less we can apply recursion here.\n\n\n# Approach\n Desc | amol_2004 | NORMAL | 2023-09-10T04:41:58.020813+00:00 | 2023-09-10T04:41:58.020832+00:00 | 15 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n***Since the constraints are very less we can apply recursion here.***\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n`For every cell find all the possible cells that can give 1 stone and then take its minimum`\... | 1 | 0 | ['C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | ✅Recursion✅ || Beats 100% 🏆 || Brute Force || c++ | recursion-beats-100-brute-force-c-by-nit-sndc | \n\n\n# Code\n\nclass Solution {\npublic:\n int solve(vector<vector<int>>& vpG, vector<vector<int>>& vpS, int i) {\n if (i == vpS.size()) return 0;\n\ | trinosauraus629 | NORMAL | 2023-09-10T04:34:58.234327+00:00 | 2023-09-10T05:19:02.935325+00:00 | 18 | false | \n\n\n# Code\n```\nclass Solution {\npublic:\n int solve(vector<vector<int>>& vpG, vector<vector<int>>& vpS, int i) {\n if (i == vpS.size()) return 0;\n\n int ans = INT_MAX;\n // Take... | 1 | 0 | ['Backtracking', 'Greedy', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Code giving TLE on one test case | code-giving-tle-on-one-test-case-by-saks-6whr | \nclass Solution {\npublic:\n \n bool isValid(int x,int y)\n {\n if(x<0 || y<0 || x>=3 || y>=3)\n return false;\n \n re | saksham_singhal_2001 | NORMAL | 2023-09-10T04:28:37.572019+00:00 | 2023-09-10T04:44:17.290175+00:00 | 236 | false | ```\nclass Solution {\npublic:\n \n bool isValid(int x,int y)\n {\n if(x<0 || y<0 || x>=3 || y>=3)\n return false;\n \n return true;\n }\n \n vector<int> mnMoves(vector<vector<int>> &grid,int x,int y)\n {\n // cout<<x<<" "<<y<<" ";\n queue<vector<int>> ... | 1 | 0 | ['Breadth-First Search', 'Recursion', 'C'] | 1 |
minimum-moves-to-spread-stones-over-grid | Bitmasking | bitmasking-by-khem_404-92ml | 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 | khem_404 | NORMAL | 2023-09-10T04:25:14.379149+00:00 | 2023-09-10T04:25:14.379165+00:00 | 37 | 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 |
minimum-moves-to-spread-stones-over-grid | [Dart] Simple BFS Solution | dart-simple-bfs-solution-by-hyungzin-56jn | Intuition\ndart simple BFS solution\n\n# Approach\nvisit for back track\nfocus on the stones > 1\nadd the list (deep copy with original) at every new case\n\n# | hyungzin | NORMAL | 2023-09-10T04:09:33.170434+00:00 | 2023-09-10T04:09:33.170454+00:00 | 162 | false | # Intuition\ndart simple BFS solution\n\n# Approach\nvisit for back track\nfocus on the stones > 1\nadd the list (deep copy with original) at every new case\n\n# Complexity\n- Time complexity:\n\n\n- Space complexity:\n\n# Code\n```\nimport \'dart:collection\';\nclass Solution {\n final visit = <String>{};\n int mi... | 1 | 0 | ['Breadth-First Search', 'Dart'] | 0 |
minimum-moves-to-spread-stones-over-grid | Easy c++ Recursion ✅✅✅✅ | easy-c-recursion-by-rohan_cs-n9z7 | 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 | rohan_cs | NORMAL | 2023-09-10T04:08:03.652041+00:00 | 2023-09-10T04:08:03.652063+00:00 | 180 | 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 |
minimum-moves-to-spread-stones-over-grid | Java || Easy Solution || recusrion | java-easy-solution-recusrion-by-akash280-s69e | \nclass Solution {\n public int minimumMoves(int[][] grid) {\n int count=0;\n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n | akash2802 | NORMAL | 2023-09-10T04:07:05.066820+00:00 | 2023-09-10T04:07:05.066844+00:00 | 222 | false | ```\nclass Solution {\n public int minimumMoves(int[][] grid) {\n int count=0;\n for(int i=0;i<3;i++){\n for(int j=0;j<3;j++){\n if(grid[i][j]==0)\n count++;\n }\n }\n return findMoves(grid,0,count);\n }\n public int findMoves(... | 1 | 0 | ['Java'] | 1 |
minimum-moves-to-spread-stones-over-grid | Python brute force | python-brute-force-by-zonda_yang-x9bu | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Code\n\nclass Solution:\n def minimumMoves(self, grid: List[List[int]]) -> int:\ | zonda_yang | NORMAL | 2023-09-10T04:05:12.385161+00:00 | 2023-09-10T04:05:12.385181+00:00 | 1,025 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Code\n```\nclass Solution:\n def minimumMoves(self, grid: List[List[int]]) -> int:\n zero_list = []\n more_list = []\n for y in range(3):\n for x in range(3):\n if grid[y][x] == 0:\n ... | 1 | 0 | ['Python3'] | 0 |
minimum-moves-to-spread-stones-over-grid | Easyy to understand | easyy-to-understand-by-priyanshu1880-x6s8 | 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 | priyanshu1880 | NORMAL | 2023-09-10T04:02:52.668201+00:00 | 2023-09-10T04:02:52.668227+00:00 | 67 | 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 |
minimum-moves-to-spread-stones-over-grid | BFS on all possible states | bfs-on-all-possible-states-by-theabbie-qi5q | \nfrom collections import deque\n\nclass Solution:\n def minimumMoves(self, grid: List[List[int]]) -> int:\n def key(g):\n res = 0\n | theabbie | NORMAL | 2023-09-10T04:02:28.002796+00:00 | 2023-09-10T04:02:28.002818+00:00 | 279 | false | ```\nfrom collections import deque\n\nclass Solution:\n def minimumMoves(self, grid: List[List[int]]) -> int:\n def key(g):\n res = 0\n for i in range(3):\n for j in range(3):\n res = 10 * res + g[i][j]\n return res\n target = [[1] * 3 ... | 1 | 0 | [] | 0 |
minimum-moves-to-spread-stones-over-grid | Precompute all from target | precompute-all-from-target-by-theabbie-0x35 | null | theabbie | NORMAL | 2025-04-10T07:21:38.779092+00:00 | 2025-04-10T07:21:38.779092+00:00 | 1 | false | ```python3 []
from collections import deque
dp = {}
initial = (1,) * 9
queue = deque([initial])
dp[initial] = 0
neighbors = [[] for _ in range(9)]
for i in range(9):
r, c = i // 3, i % 3
for dr, dc in ((-1, 0), (1, 0), (0, -1), (0, 1)):
nr, nc = r + dr, c + dc
if 0 <= nr < 3 and 0 <= nc < 3:
... | 0 | 0 | ['Python3'] | 0 |
minimum-moves-to-spread-stones-over-grid | C++ Easy Backtracking Algorithm with comments | c-easy-backtracking-algorithm-with-comme-gx0r | Code | ronakj527 | NORMAL | 2025-03-22T21:29:04.179704+00:00 | 2025-03-22T21:29:04.179704+00:00 | 6 | false |
# Code
```cpp []
class Solution {
public:
int helper(vector<pair<int, int>> &toFill, vector<vector<int>>& av, int i) {
/* i == toFill.size() means we have iterated over all toFill elements. All elements have been filled, return 0 */
if (i == toFill.size()) return 0;
int answer = IN... | 0 | 0 | ['Backtracking', 'Greedy', 'Recursion', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Recursion | TC: 2ms | Java | recursion-tc-2ms-java-by-mp78xiambv-dveo | Intuition
For every cell with zero, we have options to take it from cells with > 1
ApproachComplexity
Time complexity: 9 * (number of cells with > 1) ^(number o | mP78XiaMbv | NORMAL | 2025-02-18T08:55:00.023493+00:00 | 2025-02-18T08:55:00.023493+00:00 | 7 | false | # Intuition
- For every cell with zero, we have options to take it from cells with > 1
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: 9 * (number of cells with > 1) ^(number of cells == 0)
<!-- Add... | 0 | 0 | ['Java'] | 0 |
minimum-moves-to-spread-stones-over-grid | python backtrack | python-backtrack-by-tianwen0815-1ujc | IntuitionThe problem is akin to finding an optimal assignment of items (stones) to positions. A brute-force approach can be used, employing backtracking to try | tianwen0815 | NORMAL | 2025-01-06T00:20:08.733922+00:00 | 2025-01-06T00:20:08.733922+00:00 | 11 | false | ### Intuition
The problem is akin to finding an optimal assignment of items (stones) to positions. A brute-force approach can be used, employing backtracking to try all possible assignments of stones to empty grid spots and then calculating the minimum distance for each assignment.
### Approach
- **Representation of g... | 0 | 0 | ['Python3'] | 0 |
minimum-moves-to-spread-stones-over-grid | Same As Sliding Puzzle using BFS and Backtrack | same-as-sliding-puzzle-using-bfs-and-bac-4qew | IntuitionApproachComplexity
Time complexity:
Space complexity:
Same Pattern Problemhttps://leetcode.com/problems/sliding-puzzle/description/Code | shido_AKV | NORMAL | 2025-01-02T19:05:35.887335+00:00 | 2025-01-02T19:05:35.887335+00:00 | 22 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Same Pa... | 0 | 0 | ['C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | C++ | c-by-tinachien-9lip | \nclass Solution {\n int ret = INT_MAX;\npublic:\n int minimumMoves(vector<vector<int>>& grid) {\n dfs(0, 0, grid);\n return ret;\n }\n | TinaChien | NORMAL | 2024-12-15T16:23:52.773791+00:00 | 2024-12-15T16:23:52.773816+00:00 | 6 | false | ```\nclass Solution {\n int ret = INT_MAX;\npublic:\n int minimumMoves(vector<vector<int>>& grid) {\n dfs(0, 0, grid);\n return ret;\n }\n \n void dfs(int cur, int moves, vector<vector<int>>& grid){\n if( moves >= ret )\n return;\n if(cur == 9){\n ret = m... | 0 | 0 | [] | 0 |
minimum-moves-to-spread-stones-over-grid | BFS | bfs-by-linda2024-jsyp | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | linda2024 | NORMAL | 2024-12-20T20:29:44.162220+00:00 | 2024-12-20T20:29:44.162220+00:00 | 10 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['C#'] | 0 |
minimum-moves-to-spread-stones-over-grid | Bruteforced DFS recursion. Beats 100% | bruteforced-dfs-recursion-beats-100-by-n-7ha3 | Intuition\nI can\'t think of a way to solve this without bruteforce.\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nTo omit redund | novodimaporo | NORMAL | 2024-11-12T08:32:01.499372+00:00 | 2024-11-12T08:44:23.553919+00:00 | 14 | false | # Intuition\nI can\'t think of a way to solve this without bruteforce.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nTo omit redundant computations, filter-out the cells that we are interested. The empty cells, and the cells with more than 1 stones. Bruteforced with DFS recursion.\... | 0 | 0 | ['Depth-First Search', 'Kotlin'] | 0 |
minimum-moves-to-spread-stones-over-grid | C++, BEAT 100%, PERMUTATION, FAST AND EASY TO UNDERSTAND | c-beat-100-permutation-fast-and-easy-to-dzjv2 | Intuition\n Describe your first thoughts on how to solve this problem. \n\nThe only cells we should think about is the cells less than 1 and cells more than 1.\ | wcf29 | NORMAL | 2024-11-12T00:21:21.373204+00:00 | 2024-11-12T00:21:58.033092+00:00 | 9 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThe only cells we should think about is the cells less than 1 and cells more than 1.\n\nSo, first iterate though entire grid to find **less-than-1 cells** and **more-than-1 cells**, store them to vector less and more.\n\nThan, the probl... | 0 | 0 | ['C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | python3 permutations | python3-permutations-by-0icy-hj8b | \n\n# Code\npython3 []\nclass Solution:\n def minimumMoves(self, grid: List[List[int]]) -> int:\n req = []\n extra = []\n for r in range | 0icy | NORMAL | 2024-11-03T15:16:56.822555+00:00 | 2024-11-03T15:16:56.822591+00:00 | 7 | false | \n\n# Code\n```python3 []\nclass Solution:\n def minimumMoves(self, grid: List[List[int]]) -> int:\n req = []\n extra = []\n for r in range(3):\n for c in range(3):\n if grid[r][c] == 0:\n req.append((r,c))\n elif grid[r][c] > 1:\n ... | 0 | 0 | ['Python3'] | 0 |
minimum-moves-to-spread-stones-over-grid | Clean code with concise explanation | C++ | clean-code-with-concise-explanation-c-by-adt6 | Approach\n- There would always be a one to one mapping i.e. if there are x(>1) 1\'s in a cell then there would be for sure x-1 cells with 0 who\'re in need of t | Chaitanya_89 | NORMAL | 2024-10-09T15:34:35.041095+00:00 | 2024-10-09T15:39:26.576300+00:00 | 37 | false | # Approach\n- There would always be a one to one mapping i.e. if there are x(>1) 1\'s in a cell then there would be for sure x-1 cells with 0 who\'re in need of these extra ones.\n- Collect the cells who are in deficit and also have extra 1s with\'em\n- Permute all the possible combinations for extra and deficit blanks... | 0 | 0 | ['Greedy', 'Matrix', 'C++'] | 0 |
minimum-moves-to-spread-stones-over-grid | Backtracking (Python) | backtracking-python-by-treerecursion-6kor | Intuition\n Describe your first thoughts on how to solve this problem. \nTransfer stones from givers to receivers. Minimize the number of moves by backtracking | treerecursion | NORMAL | 2024-10-07T15:41:14.008265+00:00 | 2024-10-07T15:41:14.008309+00:00 | 7 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTransfer stones from givers to receivers. Minimize the number of moves by backtracking.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Index grid cells from top-left to bottom-right. \n2. Scan the grid. If a c... | 0 | 0 | ['Python3'] | 0 |
minimum-moves-to-spread-stones-over-grid | C++ Solution || Brute-Force || Aryan Mittal | c-solution-brute-force-aryan-mittal-by-v-5ksk | \n# Code\ncpp []\nclass Solution {\n\n vector<vector<int>> xtra, zero;\n int ret;\n void solve(int i, int count) {\n // base case\n if(i | Vaibhav_Arya007 | NORMAL | 2024-10-06T20:33:07.172858+00:00 | 2024-10-06T20:33:07.172899+00:00 | 3 | false | \n# Code\n```cpp []\nclass Solution {\n\n vector<vector<int>> xtra, zero;\n int ret;\n void solve(int i, int count) {\n // base case\n if(i >= zero.size()) {\n ret = min(ret, count);\n return;\n }\n for(int k = 0; k < xtra.size(); k++) {\n if(xtra[k]... | 0 | 0 | ['C++'] | 0 |
reverse-linked-list | [C++] Iterative vs. Recursive Solutions Compared and Explained, ~99% Time, ~85% Space | c-iterative-vs-recursive-solutions-compa-h5tr | Not sure how this problem is expecting me to use less memory than this, but here is the deal:\n we are going to use 3 variables: prevNode, head and nextNode, th | ajna | NORMAL | 2020-08-21T13:11:44.534562+00:00 | 2020-11-03T22:36:07.845278+00:00 | 64,304 | false | Not sure how this problem is expecting me to use less memory than this, but here is the deal:\n* we are going to use 3 variables: `prevNode`, `head` and `nextNode`, that you can easily guess what are meant to represent as we go;\n* we will initialise `prevNode` to `NULL`, while `nextNode` can stay empty;\n* we are then... | 1,033 | 4 | ['Linked List', 'C', 'C++'] | 34 |
reverse-linked-list | In-place iterative and recursive Java solution | in-place-iterative-and-recursive-java-so-o2vm | public ListNode reverseList(ListNode head) {\n /* iterative solution */\n ListNode newHead = null;\n while (head != null) {\n Li | braydencn | NORMAL | 2015-05-04T22:18:53+00:00 | 2018-10-23T07:42:21.754521+00:00 | 227,386 | false | public ListNode reverseList(ListNode head) {\n /* iterative solution */\n ListNode newHead = null;\n while (head != null) {\n ListNode next = head.next;\n head.next = newHead;\n newHead = head;\n head = next;\n }\n return newHead;\n }... | 1,030 | 7 | ['Java'] | 70 |
reverse-linked-list | JAVA 0ms 100% Easy Understanding | java-0ms-100-easy-understanding-by-bhanu-92pg | \n\n\nclass Solution {\n public ListNode reverseList(ListNode head) {\n ListNode prev = null; \n ListNode current = head;\n \n \n | Bhanu0909 | NORMAL | 2022-10-09T15:26:12.386004+00:00 | 2022-10-13T06:34:37.526655+00:00 | 109,518 | false | \n\n```\nclass Solution {\n public ListNode reverseList(ListNode head) {\n ListNode prev = null; \n ListNode current = head;\n \n \n while(current != null) { \n Lis... | 868 | 0 | ['Linked List', 'Java'] | 38 |
reverse-linked-list | Using 2 Methods || Iterative & Recursive || Beats 97.91% | using-2-methods-iterative-recursive-beat-89wo | Intuition\nThe idea is to use three pointers curr, prev, and forward to keep track of nodes to update reverse links.\n Describe your first thoughts on how to so | keshavsharma519 | NORMAL | 2023-02-20T20:45:36.500328+00:00 | 2023-02-20T20:45:36.500367+00:00 | 104,783 | false | # Intuition\nThe idea is to use three pointers curr, prev, and forward to keep track of nodes to update reverse links.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach 1 - using Iterative Method \n\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n... | 704 | 0 | ['C++'] | 16 |
reverse-linked-list | Step By Step Explained with Images. Easiest to understand. Java, C++, Python, JavaScript, Go Codes | step-by-step-explained-with-images-easie-ripe | Intuition\nThis is a basic linked list reversal algorithm. Can be used in various other problems so just understand this one and memorize it.\n\n# Approach\n\n\ | Kunal_Tajne | NORMAL | 2024-08-09T14:29:31.616237+00:00 | 2024-08-22T00:10:09.628517+00:00 | 52,173 | false | # Intuition\nThis is a basic linked list reversal algorithm. Can be used in various other problems so just understand this one and memorize it.\n\n# Approach\n\n![Screensh... | 575 | 1 | ['Linked List', 'Recursion', 'C++', 'Java', 'Go', 'Python3', 'JavaScript'] | 26 |
reverse-linked-list | 【Video】Solution with visualization | video-solution-with-visualization-by-nii-h754 | IntuitionI think the easiest way to understand linked list should be drawing.Solution Video⭐️⭐️ Don't forget to subscribe to my channel! ⭐️⭐️■ Subscribe URL
htt | niits | NORMAL | 2024-11-22T17:20:36.297394+00:00 | 2024-12-27T18:20:50.482325+00:00 | 29,845 | false | # Intuition
I think the easiest way to understand linked list should be drawing.
---
# Solution Video
https://youtu.be/9TsQmdRAxT8
### ⭐️⭐️ Don't forget to subscribe to my channel! ⭐️⭐️
**■ Subscribe URL**
http://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1
Subscribers: 11,092
Thank you for... | 499 | 2 | ['C++', 'Java', 'Python3', 'JavaScript'] | 4 |
reverse-linked-list | Python Iterative and Recursive Solution | python-iterative-and-recursive-solution-aoc9g | class Solution:\n # @param {ListNode} head\n # @return {ListNode}\n def reverseList(self, head):\n prev = None\n while head:\n | tusizi | NORMAL | 2015-05-16T11:51:19+00:00 | 2018-10-26T20:48:55.044010+00:00 | 116,596 | false | class Solution:\n # @param {ListNode} head\n # @return {ListNode}\n def reverseList(self, head):\n prev = None\n while head:\n curr = head\n head = head.next\n curr.next = prev\n prev = curr\n return prev\n\n\nRecursion\n\n class Solution:... | 433 | 1 | [] | 42 |
reverse-linked-list | Easy || 0 ms || 100% || Fully Explained || Java, C++, Python, JS, C, Python3 (Recursive & Iterative) | easy-0-ms-100-fully-explained-java-c-pyt-dunr | Java Solution (Recursive Approach):\nRuntime: 0 ms, faster than 100.00% of Java online submissions for Reverse Linked List.\n\nclass Solution {\n public List | PratikSen07 | NORMAL | 2022-08-21T11:00:27.581173+00:00 | 2022-08-31T12:14:18.009397+00:00 | 96,035 | false | # **Java Solution (Recursive Approach):**\nRuntime: 0 ms, faster than 100.00% of Java online submissions for Reverse Linked List.\n```\nclass Solution {\n public ListNode reverseList(ListNode head) {\n // Special case...\n if (head == null || head.next == null) return head;\n // Create a new nod... | 406 | 2 | ['Linked List', 'Recursion', 'C', 'Iterator', 'Python', 'Java', 'Python3', 'JavaScript'] | 21 |
reverse-linked-list | C++ Iterative and Recursive | c-iterative-and-recursive-by-jianchao-li-1t8q | Well, since the head pointer may also be modified, we create a pre that points to it to facilitate the reverse process.\n\nFor the example list 1 -> 2 -> 3 -> 4 | jianchao-li | NORMAL | 2015-07-06T07:27:59+00:00 | 2018-10-25T09:58:09.969754+00:00 | 92,581 | false | Well, since the `head` pointer may also be modified, we create a `pre` that points to it to facilitate the reverse process.\n\nFor the example list `1 -> 2 -> 3 -> 4 -> 5` in the problem statement, it will become `0 -> 1 -> 2 -> 3 -> 4 -> 5` (we init `pre -> val` to be `0`). We also set a pointer `cur` to `head`. Then ... | 356 | 7 | ['Linked List', 'Recursion', 'Iterator', 'C++'] | 47 |
reverse-linked-list | My Java recursive solution | my-java-recursive-solution-by-caodanbobo-qr9t | public class Solution {\n public ListNode reverseList(ListNode head) {\n if(head==null || head.next==null)\n return head;\n | caodanbobo | NORMAL | 2015-06-18T01:46:43+00:00 | 2018-10-24T10:47:28.662420+00:00 | 46,972 | false | public class Solution {\n public ListNode reverseList(ListNode head) {\n if(head==null || head.next==null)\n return head;\n ListNode nextNode=head.next;\n ListNode newHead=reverseList(nextNode);\n nextNode.next=head;\n head.next=null;\n ... | 147 | 4 | ['Java'] | 17 |
reverse-linked-list | Javascript ES6 less code solution | javascript-es6-less-code-solution-by-sal-emzg | \nvar reverseList = function(head) {\n let [prev, current] = [null, head]\n while(current) {\n [current.next, prev, current] = [prev, current, curr | saltant | NORMAL | 2019-06-17T09:13:30.528452+00:00 | 2019-06-17T09:16:28.253051+00:00 | 14,318 | false | ```\nvar reverseList = function(head) {\n let [prev, current] = [null, head]\n while(current) {\n [current.next, prev, current] = [prev, current, current.next]\n }\n return prev\n}\n``` | 127 | 2 | ['JavaScript'] | 16 |
reverse-linked-list | Python solution - Simple Iterative | python-solution-simple-iterative-by-giri-hpqe | I'm not sure if it's already posted here, but this is simple iterative approach. The idea is to change next with prev, prev with current, and current with next. | girikuncoro | NORMAL | 2015-09-11T23:19:19+00:00 | 2015-09-11T23:19:19+00:00 | 30,909 | false | I'm not sure if it's already posted here, but this is simple iterative approach. The idea is to change next with prev, prev with current, and current with next.\n\n def reverseList(self, head):\n prev = None\n curr = head\n\n while curr:\n next = curr.next\n curr.next = pre... | 120 | 2 | ['Iterator', 'Python'] | 11 |
reverse-linked-list | 0ms 100% ||✅Step By Step Visualization. Easiest to understand. Java, C++, Python, | java-0ms-100-step-by-step-explained-with-kgvz | ✅IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE AT THE END😊 ✅Intuition💡:Iterative Approach:
Use two pointers: prev (starts as null) and curr (starts at head).
At each | Varma5247 | NORMAL | 2025-03-18T06:38:08.286741+00:00 | 2025-04-08T05:31:42.381068+00:00 | 7,767 | false | ✅**IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE AT THE END**😊 ✅
# Intuition💡:
### **Iterative Approach:**
- Use two pointers: ```prev``` (starts as ```null```) and ```curr``` (starts at ```head```).
- At each step:
1 . Store ```curr.next``` in ```temp```.
2 . Reverse ```curr.next``` to point to ```prev```.
3 ... | 106 | 3 | ['Linked List', 'Two Pointers', 'Iterator', 'Python', 'C++', 'Java', 'Python3'] | 7 |
reverse-linked-list | 🔥✅✅ Beat 100% | 0 ms ✅✅🔥 | beat-100-0-ms-by-devogabek-fnpq | UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE \n# UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE\n# UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE\n# UPVOTE UPVOTE UPVOTE UPV | DevOgabek | NORMAL | 2024-03-21T00:09:40.977626+00:00 | 2024-03-21T12:28:40.879101+00:00 | 26,696 | false | # UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE \n# UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE\n# UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE\n# UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE\n# UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE\n# UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE\n# UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE UPVOTE\n# UP... | 81 | 9 | ['Linked List', 'Recursion', 'Python', 'C++', 'Python3', 'JavaScript'] | 18 |
reverse-linked-list | Detailed explanation of recursive solution + alternate simpler recursive solution | detailed-explanation-of-recursive-soluti-163y | For anyone still trying to understand the recursive solution, here\'s the step by step explanation, considering the example given in the question:\n\n1 -> 2 -> | anirudhgoel | NORMAL | 2022-01-23T03:56:52.716590+00:00 | 2022-01-23T03:56:52.716618+00:00 | 8,425 | false | For anyone still trying to understand the recursive solution, here\'s the step by step explanation, considering the example given in the question:\n\n`1 -> 2 -> 3 -> 4 -> 5`\n\nFor the first 4 calls of the `reverseList` function, we just go into the call stack, as the base case is not hit. When we call the function for... | 74 | 0 | ['Recursion', 'Python'] | 6 |
reverse-linked-list | Python Iterative & Recursive | python-iterative-recursive-by-wangqiuc-swx1 | To reverse a linked list, we actually reverse the direction of every next pointer. \nFor example, we reverse a linked list 1->2->3->4->5 by changing every -> to | wangqiuc | NORMAL | 2019-03-02T00:11:10.653830+00:00 | 2019-08-14T04:43:54.039912+00:00 | 7,321 | false | To reverse a linked list, we actually reverse the direction of every next pointer. \nFor example, we reverse a linked list 1->2->3->4->5 by changing every -> to <- and get the result as 1<-2<-3<-4<-5. And we need to return the pointer to 5 instead of to 1.\nTo solve it efficiently, we can do it in one loop iteration or... | 69 | 3 | [] | 5 |
reverse-linked-list | ✅Best Method 🔥 100% || C++ || JAVA || PYTHON || Beginner Friendly🔥🔥🔥 | best-method-100-c-java-python-beginner-f-d5s2 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe given code snippet implements the iterative approach to reverse a linked list. The | rahulvarma5297 | NORMAL | 2023-06-10T05:08:04.918036+00:00 | 2023-06-10T05:12:20.599697+00:00 | 4,873 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe given code snippet implements the iterative approach to reverse a linked list. The intuition behind it is to use three pointers: `prev`, `head`, and `nxt`. By reversing the pointers between the nodes, we can reverse the linked list.\n... | 63 | 0 | ['Linked List', 'C++', 'Java', 'Python3'] | 6 |
reverse-linked-list | Easy to understand recursive solution ( beats 98% , takes 2 arguments ) | easy-to-understand-recursive-solution-be-ws30 | Each call goes something like this : \n1->2->3 to 3->2->1\n\n(1, null ) :\nnext =2 ;\n1->next = null;\nreturn (2,1)\n\n(2, 1)\nnext = 3;\n2->next = 1;\nreturn ( | spooja__ | NORMAL | 2019-11-30T14:40:34.287811+00:00 | 2020-08-14T16:09:59.189673+00:00 | 8,398 | false | Each call goes something like this : \n1->2->3 to 3->2->1\n\n(1, null ) :\nnext =2 ;\n1->next = null;\nreturn (2,1)\n\n(2, 1)\nnext = 3;\n2->next = 1;\nreturn (3,2)\n\n(3,2)\nnext = null;\n3->next = 2;\nreturn (null, 3)\n\n(null, 3)\nreturn 3; **( the new head node )**\n\n```\n ListNode* helper(ListNode* head, ListNod... | 55 | 1 | ['Recursion', 'C', 'C++'] | 3 |
reverse-linked-list | Javascript Iterative and Recursive solution | javascript-iterative-and-recursive-solut-8gw5 | My Javascript solution based on Back To Back SWE\n\n## Iterative\n- Time: O(n)\n- Space: O(1)\n\njsx\nvar reverseList = function(head) {\n let prev = null\n | roy6234leetcode | NORMAL | 2020-09-29T06:07:20.207387+00:00 | 2020-09-29T06:10:29.872742+00:00 | 7,616 | false | My Javascript solution based on [Back To Back SWE](https://www.youtube.com/watch?v=O0By4Zq0OFc&feature=youtu.be)\n\n## Iterative\n- Time: O(n)\n- Space: O(1)\n\n```jsx\nvar reverseList = function(head) {\n let prev = null\n let curr = head\n let next = null\n \n while(curr!== null){\n // save next... | 51 | 0 | ['Recursion', 'Iterator', 'JavaScript'] | 10 |
reverse-linked-list | ✔️Full Explanation ✔️Python ✔️C++ ✔️Java ✔️C# ✔️Go | full-explanation-python-c-java-c-go-by-o-csz5 | Explanation\n- We initialize three pointers: prev, current, and next.\n- prev will initially be nullptr because it will be the new tail of the reversed list.\n- | otabek_kholmirzaev | NORMAL | 2024-03-21T00:19:09.118222+00:00 | 2024-03-21T17:07:57.957060+00:00 | 3,010 | false | # Explanation\n- We initialize three pointers: **prev**, **current**, and **next**.\n- **prev** will initially be **nullptr** because it will be the new tail of the reversed list.\n- **current** starts from the head of the original list.\n- Inside the loop, we:\n - Store the next node of **current** in the variable ... | 49 | 0 | ['Python', 'C++', 'Java', 'Go', 'C#'] | 3 |
reverse-linked-list | Accepted C Solutions both iteratively and recursively | accepted-c-solutions-both-iteratively-an-nqgg | struct ListNode* reverseList(struct ListNode* head) {\n \tif(NULL==head) return head;\n \n \tstruct ListNode *p=head;\n \tp=head->next;\n \thead- | redace85 | NORMAL | 2015-05-05T11:03:57+00:00 | 2015-05-05T11:03:57+00:00 | 12,152 | false | struct ListNode* reverseList(struct ListNode* head) {\n \tif(NULL==head) return head;\n \n \tstruct ListNode *p=head;\n \tp=head->next;\n \thead->next=NULL;\n \twhile(NULL!=p){\n \t\tstruct ListNode *ptmp=p->next;\n \t\tp->next=head;\n \t\thead=p;\n \t\tp=ptmp;\n \t}\n \treturn h... | 49 | 0 | [] | 2 |
reverse-linked-list | Simple Iterative & Recursive Solution With Diagram (beats 100%) | simple-iterative-recursive-solution-with-lxud | Iterative\n\n\n\nfunc reverseList(head *ListNode) *ListNode {\n if head == nil {\n return nil\n }\n \n var revHead *ListNode\n\n for head | mayankagarwal2402 | NORMAL | 2022-11-13T05:58:21.150014+00:00 | 2023-09-18T11:13:51.201734+00:00 | 3,826 | false | # **Iterative**\n\n\n```\nfunc reverseList(head *ListNode) *ListNode {\n if head == nil {\n return nil\n }\n \n var revHead *ListNode\n\n for head != nil {\n tmp := head.Next\n ... | 43 | 0 | ['Go'] | 4 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.