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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
word-search | Java DFS with Explanations | java-dfs-with-explanations-by-gracemeng-mb8v | For each cell in board, if it matches the first character of word, we implement DFS starting at it to see if we could find word. \nThe bottleneck of it is to ma | gracemeng | NORMAL | 2018-09-09T05:59:30.904545+00:00 | 2018-09-15T15:33:38.272333+00:00 | 2,640 | false | For each cell in board, if it matches the first character of word, we implement DFS starting at it to see if we could find word. \nThe bottleneck of it is to mark cell visited for we cannot visit a cell multiple times. The fastest way is to modify the original cell rather than to use external storage.\n****\n```\n p... | 25 | 1 | [] | 4 |
word-search | C++ & Python Solution || DFS Traversal || Backtracking (Easiest Solution)🔥✅ | c-python-solution-dfs-traversal-backtrac-vaxo | C++ Solution\n\n// If it helps plz upvote :)\n\nclass Solution {\nprivate:\n bool dfs(vector<vector<char>> &board,vector<vector<int>> &visited,string &word,i | chamoli2k2 | NORMAL | 2022-08-29T12:47:43.250699+00:00 | 2024-07-08T06:13:40.578613+00:00 | 1,793 | false | **C++ Solution**\n```\n// If it helps plz upvote :)\n\nclass Solution {\nprivate:\n bool dfs(vector<vector<char>> &board,vector<vector<int>> &visited,string &word,int i,int j,int idx){\n // base case\n if(idx == word.size()) return true;\n\n if(i < 0 || i >= board.size() || j < 0 || j >= board[0... | 22 | 0 | ['Backtracking', 'Depth-First Search', 'C', 'C++'] | 5 |
word-search | Python sol by DFS [w/ Comment ] | python-sol-by-dfs-w-comment-by-brianchia-bfqh | Python sol by DFS with 4-connected path\n\n---\n\nImplementation by DFS:\n\n\nclass Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n | brianchiang_tw | NORMAL | 2020-07-21T09:37:10.763030+00:00 | 2020-07-21T09:37:10.763073+00:00 | 4,657 | false | Python sol by DFS with 4-connected path\n\n---\n\n**Implementation** by DFS:\n\n```\nclass Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n \n if not board: \n \n # Quick response for empty board\n return False\n \n h, w = len(boar... | 20 | 0 | ['Depth-First Search', 'Recursion', 'Python', 'Python3'] | 3 |
word-search | Java || 0ms || Fast || Explanation of optimizations | java-0ms-fast-explanation-of-optimizatio-sijb | The Java code below runs at 0ms in April 2024. \n\n---\nFast solutions previously on the runtime graph with coding errors\nUpdate: As of November 2022, the ru | dudeandcat | NORMAL | 2021-10-08T04:46:16.814460+00:00 | 2024-04-03T05:12:24.814604+00:00 | 1,323 | false | The Java code below runs at 0ms in April 2024. \n\n---\n**Fast solutions** previously **on the runtime graph with coding errors**\nUpdate: As of November 2022, the runtime graph has been reset. The code from the runtime graph that is discussed in this section, no longer appears on the runtime graph but may still app... | 19 | 0 | ['Java'] | 2 |
word-search | Swift Solution -- Clean & Easy Understand | swift-solution-clean-easy-understand-by-bi4oc | \nclass Solution {\n func exist(_ board: [[Character]], _ word: String) -> Bool {\n guard board.count != 0, board[0].count != 0, word.count != 0 else | qilio | NORMAL | 2019-07-30T03:06:33.195019+00:00 | 2019-08-18T01:58:46.987788+00:00 | 1,303 | false | ```\nclass Solution {\n func exist(_ board: [[Character]], _ word: String) -> Bool {\n guard board.count != 0, board[0].count != 0, word.count != 0 else { return false }\n \n let word = Array(word)\n let rows = board.count, cols = board[0].count\n var isVisited = Array(repeating: A... | 19 | 0 | ['Backtracking', 'Swift'] | 2 |
word-search | AC in 84ms, by using DFS. | ac-in-84ms-by-using-dfs-by-hongzhi-tloa | I used DFS, and got AC in 84ms, any improvement?\n\n class Solution {\n private:\n vector > board;\n string word;\n bool used;\n | hongzhi | NORMAL | 2014-06-25T02:39:18+00:00 | 2014-06-25T02:39:18+00:00 | 11,042 | false | I used DFS, and got AC in 84ms, any improvement?\n\n class Solution {\n private:\n vector<vector<char> > *board;\n string *word;\n bool **used;\n private:\n bool isInboard(int i, int j)\n {\n if(i < 0)return false;\n if(i >= board->size())return false;\... | 19 | 3 | [] | 9 |
word-search | Using DFS and Backtracking to Search for Word in Grid | using-dfs-and-backtracking-to-search-for-axmw | Intuition\nTo solve this problem, we can utilize a depth-first search (DFS) approach to explore all possible paths in the grid starting from each cell. We will | CS_MONKS | NORMAL | 2024-04-03T00:28:48.786486+00:00 | 2024-04-03T09:10:12.921055+00:00 | 4,218 | false | # Intuition\nTo solve this problem, we can utilize a depth-first search (DFS) approach to explore all possible paths in the grid starting from each cell. We will recursively search for the word by traversing horizontally and vertically neighboring cells while ensuring that each letter is used only once.\n\n# Approach\n... | 18 | 0 | ['Backtracking', 'Depth-First Search', 'Python', 'C++', 'Java', 'Python3'] | 7 |
word-search | ✅C++ solution using backtracking with explanations! | c-solution-using-backtracking-with-expla-ekbg | If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistake please let me know. Thank you!\u27 | dhruba-datta | NORMAL | 2022-01-15T12:30:52.663573+00:00 | 2022-01-22T17:40:37.313538+00:00 | 2,595 | false | > **If you\u2019re interested in coding you can join my Discord Server, link in the comment section. Also if you find any mistake please let me know. Thank you!\u2764\uFE0F**\n> \n\n---\n\n## Explanation:\n\n### Solution 01\n\n- We are given a matrix of (m x n) and we are required to check whether the given word exists... | 18 | 0 | ['Backtracking', 'Depth-First Search', 'C++'] | 5 |
word-search | Java | TC: O(RC*(3^L)) | SC: O(L) | Optimal DFS solution without visited matrix | java-tc-orc3l-sc-ol-optimal-dfs-solution-h0qg | java\n/**\n * For each char, perform Depth-First Search in all four directions.\n *\n * Time Complexity:\n * 1. If L > R*C ==> TC = O(1)\n * 2. If L <= R*C ==> | NarutoBaryonMode | NORMAL | 2021-10-14T08:36:59.150880+00:00 | 2021-10-14T09:26:32.782731+00:00 | 1,790 | false | ```java\n/**\n * For each char, perform Depth-First Search in all four directions.\n *\n * Time Complexity:\n * 1. If L > R*C ==> TC = O(1)\n * 2. If L <= R*C ==> TC = O(R*C * 4*(3^L))\n * 3^L ==> For the dfsHelper function, first time we have at most 4 directions\n * to explore, but the choices are r... | 17 | 0 | ['Array', 'Backtracking', 'Depth-First Search', 'Recursion', 'Matrix', 'Java'] | 1 |
word-search | JAVA | Clean and Simple ✅ | java-clean-and-simple-by-sourin_bruh-va4m | Please Upvote :D\n##### 1. Using a visited boolean array:\nWe use a visited boolean array to mark if we have already visited a certain position or not.\nWhile l | sourin_bruh | NORMAL | 2022-11-05T14:45:36.212423+00:00 | 2022-11-24T10:24:54.392319+00:00 | 1,932 | false | ### **Please Upvote** :D\n##### 1. Using a visited boolean array:\nWe use a visited boolean array to mark if we have already visited a certain position or not.\nWhile looking for the next letter, we set the current position to `true`, then we call our DFS then set the position back to `false` because if we don\'t find ... | 16 | 0 | ['Backtracking', 'Depth-First Search', 'Recursion', 'Java'] | 1 |
word-search | C++ backtracking solution without extra data structure | c-backtracking-solution-without-extra-da-1ip1 | Use board itself to mark whether we have visited it before.\n\n class Solution {\n public:\n bool exist(vector>& board, string word) {\n | froxie | NORMAL | 2015-10-24T23:59:50+00:00 | 2015-10-24T23:59:50+00:00 | 2,806 | false | Use board itself to mark whether we have visited it before.\n\n class Solution {\n public:\n bool exist(vector<vector<char>>& board, string word) {\n if (board.size() == 0) return false;\n for (int i=0; i<board.size(); ++i) {\n for (int j=0; j<board[i].size(); ++j) {\n ... | 16 | 0 | ['C++'] | 3 |
word-search | DFS+ Backtracking|Trie| Frequency Count||27ms Beats 98.55% | dfs-backtrackingtrie-frequency-count27ms-g9la | Intuition\n Describe your first thoughts on how to solve this problem. \nDFS+ Backtracking\n\nLater try other better approach.\n2nd approach is Trie+DFS+Backtra | anwendeng | NORMAL | 2024-04-03T01:03:09.314148+00:00 | 2024-04-03T05:54:53.434151+00:00 | 2,159 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDFS+ Backtracking\n\nLater try other better approach.\n2nd approach is Trie+DFS+Backtracking; with some improvement, there is no need extra handeling for edge cases. Good for pratice for implementing Trie! Almost the same speed like 1st o... | 15 | 0 | ['String', 'Backtracking', 'Depth-First Search', 'Trie', 'C++'] | 5 |
word-search | TLE->8000ms->3500ms | Finally Python with NO "TLE"!! | Pro optimization | Hushh | tle-8000ms-3500ms-finally-python-with-no-zkpk | Optimization number 1\n Using if -elif instead of for loop helped me resolve TLE:\nAs if will return as soon as it gets TRUE, for will keep checking next condit | dhanrajbhosale7797 | NORMAL | 2022-10-31T19:20:42.138662+00:00 | 2022-10-31T19:28:11.741804+00:00 | 774 | false | **Optimization number 1**\n* Using if -elif instead of for loop helped me resolve TLE:\nAs if will return as soon as it gets TRUE, for will keep checking next conditions.\n(Even multiple "or" did\'nt work for me)\n\n**"PLEASE UPVOTE FOR MY AN HOUR SPENT"**\n\n```\nclass Solution:\n def exist(self, board: List[List[s... | 15 | 1 | ['Depth-First Search', 'Python'] | 3 |
word-search | C++ Simple and Clean DFS Solution | c-simple-and-clean-dfs-solution-by-yehud-qtl4 | \nclass Solution {\npublic:\n bool startsHere(vector<vector<char>>& board, int x, int y, string& word, int idx) {\n if (idx == word.size()) return tru | yehudisk | NORMAL | 2021-08-07T23:19:20.346782+00:00 | 2021-09-24T14:09:58.996884+00:00 | 1,693 | false | ```\nclass Solution {\npublic:\n bool startsHere(vector<vector<char>>& board, int x, int y, string& word, int idx) {\n if (idx == word.size()) return true;\n if (x < 0 || x >= board.size() || y < 0 || y >= board[0].size() || \n board[x][y] == \'.\' || board[x][y] != word[idx]) return false;\... | 15 | 0 | ['C'] | 4 |
word-search | CPP Solution; 20 ms runtime; better than 100% memory usage; | cpp-solution-20-ms-runtime-better-than-1-zumy | Implements a simple DFS; Code is commented where I thought necessary, hope this helps. Comment if you have any queries!\n\n\tclass Solution {\n\tpulic:\n boo | sidthakur1 | NORMAL | 2019-08-18T21:51:04.071259+00:00 | 2019-08-18T21:51:04.071294+00:00 | 2,023 | false | Implements a simple DFS; Code is commented where I thought necessary, hope this helps. Comment if you have any queries!\n\n\tclass Solution {\n\tpulic:\n bool exist(vector<vector<char>>& board, string word) \n {\n\t\t\tif(board.empty())\n\t\t\t\treturn false;\n \n\t\t\tfor(int i=0; i<board.size(); i++)\n\t... | 15 | 1 | ['Depth-First Search', 'C++'] | 2 |
word-search | Python 3 || dfs with a little bit of pruning || T/S: 99% / 99% | python-3-dfs-with-a-little-bit-of-prunin-84er | \nclass Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n\n row, col = len(board), len(board[0])\n R, C, seen = range(r | Spaulding_ | NORMAL | 2022-11-24T02:56:29.233651+00:00 | 2024-05-26T19:00:59.011023+00:00 | 1,866 | false | ```\nclass Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n\n row, col = len(board), len(board[0])\n R, C, seen = range(row), range(col), set()\n\n def dfs(coord, i=0):\n \n if len(word) == i: return True\n \n r,c = coord\n\n ... | 14 | 0 | ['Python', 'Python3'] | 2 |
word-search | 0ms TLE_explained 100%faster | 0ms-tle_explained-100faster-by-gkeshav_1-1wzr | Do upvote\n# Sol #1. DFS (800ms)\n\nclass Solution {\npublic:\n bool dfs(vector<vector<char>> &arr, string word, int k, int r, int c)//pass arr by reference\ | gkeshav_1 | NORMAL | 2022-05-31T09:49:10.229268+00:00 | 2022-06-01T07:09:39.360030+00:00 | 782 | false | Do upvote\n# Sol #1. DFS (800ms)\n```\nclass Solution {\npublic:\n bool dfs(vector<vector<char>> &arr, string word, int k, int r, int c)//pass arr by reference\n {\n \n if(r>=arr.size()||r<0||c>=arr[0].size()||c<0||arr[r][c] != word[k])\n return false;\n\n k++;\n if(... | 14 | 0 | ['C', 'C++'] | 2 |
word-search | C++ | DFS with explanation | c-dfs-with-explanation-by-ashwinfury1-0xs2 | Approach\n1. Search the board for the first letter in the word.\n2. When we encouter that letter set index to 0 perform DFS. (index - 0 = first letter of word)\ | ashwinfury1 | NORMAL | 2020-07-21T07:46:53.315401+00:00 | 2020-07-21T07:46:53.315445+00:00 | 1,467 | false | ### Approach\n1. Search the board for the first letter in the word.\n2. When we encouter that letter set index to 0 perform DFS. (index - 0 = first letter of word)\n\t1. Use a visited array to mark the letters we visited to avoid duplicates\n\t2. increment the index to look for the next letter in the word.\n\t3. If we ... | 14 | 2 | [] | 3 |
word-search | JavaScript Clean DFS Solution | javascript-clean-dfs-solution-by-control-9110 | javascript\nvar exist = function(board, word) {\n const ROW_NUM = board.length, COL_NUM = board[0].length;\n \n function callDFS(r, c, idx) {\n | control_the_narrative | NORMAL | 2020-06-17T15:24:13.013166+00:00 | 2020-06-30T19:24:38.098487+00:00 | 2,014 | false | ```javascript\nvar exist = function(board, word) {\n const ROW_NUM = board.length, COL_NUM = board[0].length;\n \n function callDFS(r, c, idx) {\n if(word.length === idx) return true;\n if(r >= ROW_NUM || r < 0 || board[r][c] !== word[idx]) return false; \n \n board[r][c] = \'#\'; /... | 14 | 0 | ['Depth-First Search', 'JavaScript'] | 5 |
word-search | 99.77% Python Solution with Precheck | 9977-python-solution-with-precheck-by-bb-aw1i | class Solution(object):\n def exist(self, board, word):\n """\n :type board: List[List[str]]\n :type word: str\n :rtype: bool\n | bby880201 | NORMAL | 2016-03-14T18:59:02+00:00 | 2018-09-02T01:12:02.412855+00:00 | 4,521 | false | class Solution(object):\n def exist(self, board, word):\n """\n :type board: List[List[str]]\n :type word: str\n :rtype: bool\n """\n def preCheck():\n preDict = {}\n\n for i in word:\n if i in preDict: preDict[i]+=1\n ... | 14 | 1 | ['Python'] | 6 |
word-search | Java solutions || easy to solve | java-solutions-easy-to-solve-by-infox_92-77ta | \npublic class Solution {\n static boolean[][] visited;\n public boolean exist(char[][] board, String word) {\n visited = new boolean[board.length] | Infox_92 | NORMAL | 2022-11-04T14:26:45.502976+00:00 | 2022-11-04T14:26:45.503017+00:00 | 3,196 | false | ```\npublic class Solution {\n static boolean[][] visited;\n public boolean exist(char[][] board, String word) {\n visited = new boolean[board.length][board[0].length];\n \n for(int i = 0; i < board.length; i++){\n for(int j = 0; j < board[i].length; j++){\n if((word... | 13 | 0 | ['Bit Manipulation', 'Java', 'JavaScript'] | 1 |
word-search | Without using Visited Array | 6ms Simple Easy to Understand | without-using-visited-array-6ms-simple-e-a3fj | \nclass Solution {\n public boolean exist(char[][] board, String word) {\n for(int i = 0; i < board.length; i++)\n for(int j = 0; j < board | fordbellman1 | NORMAL | 2020-07-21T13:01:50.550753+00:00 | 2020-07-21T13:01:50.550886+00:00 | 682 | false | ```\nclass Solution {\n public boolean exist(char[][] board, String word) {\n for(int i = 0; i < board.length; i++)\n for(int j = 0; j < board[0].length; j++)\n if(board[i][j] == word.charAt(0) && isFound(board, i, j, word, 0))\n return true;\n \n ret... | 13 | 1 | [] | 1 |
word-search | Java clean DFS (Backtracking) solution, with explanation | java-clean-dfs-backtracking-solution-wit-o4qf | \nclass Solution {\n boolean[][] visited;\n public boolean exist(char[][] board, String word) {\n visited = new boolean[board.length][board[0].leng | allenchan09 | NORMAL | 2019-09-10T01:07:02.776300+00:00 | 2019-09-10T01:13:44.275187+00:00 | 2,758 | false | ```\nclass Solution {\n boolean[][] visited;\n public boolean exist(char[][] board, String word) {\n visited = new boolean[board.length][board[0].length];\n /* ensure all the nodes will be searched as the beginning point */\n for (int i = 0; i < board.length; i++) {\n for (int j = ... | 13 | 0 | ['Backtracking', 'Depth-First Search', 'Java'] | 8 |
word-search | C++ Solution using Backtracking | c-solution-using-backtracking-by-chaseth-mud5 | class Solution {\n public:\n bool exist(vector<vector<char>>& board, string word) {\n if(board.size()==0 || board[0].size()==0 )\n | chasethebug | NORMAL | 2016-01-11T03:23:06+00:00 | 2018-09-10T14:40:05.638156+00:00 | 2,651 | false | class Solution {\n public:\n bool exist(vector<vector<char>>& board, string word) {\n if(board.size()==0 || board[0].size()==0 )\n return true;\n \n for(int i=0; i<board.size(); i++){\n for(int j=0; j<board[0].size(); j++){\n ... | 13 | 0 | [] | 2 |
word-search | Easy Python Solution : 90.29 beats% || With Comments | easy-python-solution-9029-beats-with-com-p8sa | Code\n\nclass Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n ROWS, COLS = len(board), len(board[0])\n visited = set( | ypranav | NORMAL | 2023-08-10T14:39:15.162557+00:00 | 2023-08-10T19:18:37.042507+00:00 | 1,768 | false | # Code\n```\nclass Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n ROWS, COLS = len(board), len(board[0])\n visited = set()\n\n def dfs(r,c,idx):\n # if idx == len(word), then word has been found\n if idx == len(word):\n return True\n\n ... | 12 | 0 | ['Backtracking', 'Depth-First Search', 'Recursion', 'Python3'] | 1 |
word-search | JAVA solution || DFS || Backtracking || full explanation | java-solution-dfs-backtracking-full-expl-fxg3 | 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 | kadamyogesh7218 | NORMAL | 2022-11-24T07:47:23.738317+00:00 | 2022-11-24T07:47:23.738359+00:00 | 2,382 | 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)$$ --... | 12 | 0 | ['Array', 'Backtracking', 'Matrix', 'Java'] | 2 |
word-search | Some Thoughts about the Follow Up: Pruning | some-thoughts-about-the-follow-up-prunin-aapw | Since there are already many excellent posts discussing the solution, I\'ll ignore the discussion of the solution and go directly with the follow up. My solutio | hywrynn | NORMAL | 2021-12-28T19:39:07.930258+00:00 | 2022-01-16T20:45:14.983964+00:00 | 1,501 | false | Since there are already many excellent posts discussing the solution, I\'ll ignore the discussion of the solution and go directly with the follow up. My solution used backtracking.\n\nA very common way for pruning is memoization, in which, we keep record of the "state" and the corresponding "answer". And once we encoun... | 12 | 1 | ['Memoization'] | 3 |
word-search | simple c++ solution | Backtracking | DFS | simple-c-solution-backtracking-dfs-by-xo-t75n | \nclass Solution {\npublic:\n bool issafe(vector<vector<char>>& board,int i, int j,string word, int k){\n if(board[i][j]==word[k]){\n retur | xor09 | NORMAL | 2021-02-02T03:32:01.757643+00:00 | 2021-02-02T03:32:01.757679+00:00 | 1,540 | false | ```\nclass Solution {\npublic:\n bool issafe(vector<vector<char>>& board,int i, int j,string word, int k){\n if(board[i][j]==word[k]){\n return true;\n }\n return false;\n }\n \n bool solve(vector<vector<char>>& board,int i, int j,int m,int n,string word, int k){\n if(... | 12 | 0 | ['Backtracking', 'Depth-First Search', 'C'] | 4 |
word-search | EASY CODE WITH COMMENTS || Simple backtracking || dfs based approach | easy-code-with-comments-simple-backtrack-qw7p | Intuition\nThe idea is to use backtracking and dfs keeping track of the visited cells.\n\n# Approach\n\n- Traverse through each cell \n- check if the current le | vebzb | NORMAL | 2024-04-03T07:15:55.844710+00:00 | 2024-04-03T07:15:55.844767+00:00 | 1,034 | false | # Intuition\nThe idea is to use backtracking and dfs keeping track of the visited cells.\n\n# Approach\n\n- Traverse through each cell \n- check if the current letter matches or not\n- If the current cell matches with the current letter:\n 1. mark the cell visited(\'#\')\n 2. Recursively explore each of the adjacent ... | 11 | 0 | ['Backtracking', 'Depth-First Search', 'C++'] | 0 |
word-search | ✔️ 100% Fastest Swift Solution | 100-fastest-swift-solution-by-sergeylesc-q6mn | \nclass Solution {\n func exist(_ board: [[Character]], _ word: String) -> Bool {\n let chs = reverseIfNeeded(Array(word))\n if isValid(board, | sergeyleschev | NORMAL | 2022-04-06T05:49:17.241452+00:00 | 2022-04-06T05:49:17.241496+00:00 | 1,060 | false | ```\nclass Solution {\n func exist(_ board: [[Character]], _ word: String) -> Bool {\n let chs = reverseIfNeeded(Array(word))\n if isValid(board, chs) == false { return false }\n var res = false\n \n func backtrack(_ path: [[Int]], _ position: [Int], _ target: Int) {\n i... | 11 | 0 | ['Swift'] | 1 |
word-search | JS | DFS + Backtracking | Beats 88% | Clean Code | js-dfs-backtracking-beats-88-clean-code-q3r7j | DFS + Backtracking will take you far\n\n\nconst exist = function(board, word) {\n const n = board.length, m = board[0].length;\n if (word.length < 1) retu | adt | NORMAL | 2021-10-07T18:20:36.131722+00:00 | 2021-10-11T17:39:07.261400+00:00 | 1,412 | false | DFS + Backtracking will take you far\n\n```\nconst exist = function(board, word) {\n const n = board.length, m = board[0].length;\n if (word.length < 1) return false;\n\n const dfs = (i, j, pos) => {\n if (i === n || i < 0 || j === m || j < 0 || board[i][j] !== word[pos]) return false;\n if (pos ... | 11 | 0 | ['Backtracking', 'Depth-First Search', 'Recursion', 'JavaScript'] | 1 |
word-search | In Detail Explanation 💯 || [Java/C++/JavaScript/Python3/C#/Go] | in-detail-explanation-javacjavascriptpyt-vzs3 | Approach\n\n#### Main Method (exist):\n1. Iterate through each cell of the board using nested loops.\n2. For each cell, if the character matches the first chara | Shivansu_7 | NORMAL | 2024-04-03T07:56:00.548231+00:00 | 2024-04-03T07:56:00.548265+00:00 | 861 | false | # Approach\n\n#### Main Method (`exist`):\n1. Iterate through each cell of the board using nested loops.\n2. For each cell, if the character matches the first character of the word:\n - Initiate a DFS from that cell to search for the entire word.\n - If the DFS returns true (indicating the word is found), return tr... | 10 | 0 | ['Backtracking', 'Python', 'C++', 'Java', 'Go', 'Python3', 'C#'] | 3 |
word-search | C++ || DFS || Backtracking || easy Solution | c-dfs-backtracking-easy-solution-by-indr-6msk | \nclass Solution {\npublic:\n bool search(int index,int i,int j,vector<vector<char>> &board, string word){\n if(index == word.size()){\n re | indresh149 | NORMAL | 2022-11-24T07:24:45.726800+00:00 | 2022-11-24T07:24:45.726832+00:00 | 1,090 | false | ```\nclass Solution {\npublic:\n bool search(int index,int i,int j,vector<vector<char>> &board, string word){\n if(index == word.size()){\n return true;\n }\n if(i<0 || j<0 || i >= board.size() || j >= board[0].size()){\n return false;\n }\n bool ans = false;\... | 10 | 0 | ['Depth-First Search', 'Recursion', 'C'] | 1 |
word-search | C++ solution||easy understanding||recursion||dfs | c-solutioneasy-understandingrecursiondfs-1xjm | \nclass Solution {\npublic:\n bool search(int i,int j,int n,int m,vector<vector<char>>& board, string word,int k){\n if(k==word.size()) return true;\n | Shankhadeep2000 | NORMAL | 2022-10-12T12:07:16.820463+00:00 | 2022-10-12T12:07:16.820502+00:00 | 1,932 | false | ```\nclass Solution {\npublic:\n bool search(int i,int j,int n,int m,vector<vector<char>>& board, string word,int k){\n if(k==word.size()) return true;\n if(i<0||j<0||i==n||j==m||board[i][j]!=word[k]) return false;\n char ch = board[i][j];\n board[i][j]=\'#\';\n bool opt1= search(i... | 10 | 0 | ['Depth-First Search', 'Recursion', 'C'] | 3 |
word-search | Rust | Backtracking | Nice and Concise with Functional Style | rust-backtracking-nice-and-concise-with-o53lp | I like the fact that a significant part of this already short (in comparison) solution is just assigning some variables for convenience and legibility (first 4 | wallicent | NORMAL | 2022-06-12T17:25:09.981262+00:00 | 2022-06-12T17:33:54.214723+00:00 | 365 | false | I like the fact that a significant part of this already short (in comparison) solution is just assigning some variables for convenience and legibility (first 4 lines of `exist(...)`. Using iterators, `any()` and the `windows(2)` trick shortens down the solution quite a bit compared to a more imperative. Note that `dfs`... | 10 | 0 | ['Rust'] | 0 |
word-search | ✔️ C# - Simple to understand using DFS, With Comments | c-simple-to-understand-using-dfs-with-co-tdub | Thumbs up if you find this helpful \uD83D\uDC4D\n\n\npublic class Solution {\n public bool Exist(char[][] board, string word) {\n \n // Create | keperkjr | NORMAL | 2021-10-07T03:52:22.444082+00:00 | 2021-10-07T03:54:34.313556+00:00 | 401 | false | **Thumbs up if you find this helpful** \uD83D\uDC4D\n\n```\npublic class Solution {\n public bool Exist(char[][] board, string word) {\n \n // Create a \'visitied\' node matrix to keep track of the\n // items we\'ve already seen\n var rowsVisited = new bool[board.Length][];\n for (... | 10 | 0 | ['Depth-First Search'] | 0 |
word-search | C++ || Concise DFS | c-concise-dfs-by-suniti0804-4ej3 | \nbool dfs(vector<vector<char>>& board, int i, int j, string& word)\n {\n if(word.size() == 0) \n return true;\n if(i < 0 || i >= bo | suniti0804 | NORMAL | 2021-07-11T04:29:59.930535+00:00 | 2021-07-11T04:29:59.930563+00:00 | 1,272 | false | ```\nbool dfs(vector<vector<char>>& board, int i, int j, string& word)\n {\n if(word.size() == 0) \n return true;\n if(i < 0 || i >= board.size() || j < 0 || j >= board[0].size() || board[i][j] != word[0])\n return false;\n \n char ch = board[i][j];\n board[i]... | 10 | 0 | ['Depth-First Search', 'C', 'C++'] | 2 |
word-search | python 3 || DFS || Backtracking | python-3-dfs-backtracking-by-adarshbirad-5o8q | ```\n\nclass Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n def dfs(board,i,j,count,word):\n if(count == len(wor | adarshbiradar | NORMAL | 2020-07-22T06:38:21.944237+00:00 | 2020-07-22T06:38:21.944287+00:00 | 1,845 | false | ```\n\nclass Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n def dfs(board,i,j,count,word):\n if(count == len(word)):\n return True\n if i<0 or j<0 or i>=len(board) or j>=len(board[0]) or word[count]!=board[i][j]:\n return False\n ... | 10 | 0 | ['Backtracking', 'Depth-First Search', 'Python3'] | 2 |
word-search | JAVA DFS Simple solution | java-dfs-simple-solution-by-anjali100btc-zkc4 | If you found the solution helpful, kindly upvote or like. :)\n\n\nclass Solution {\n boolean visited[][];\n public boolean exist(char[][] board, String wo | anjali100btcse17 | NORMAL | 2020-07-21T07:54:59.729883+00:00 | 2020-07-21T07:54:59.729916+00:00 | 893 | false | If you found the solution helpful, kindly upvote or like. :)\n\n```\nclass Solution {\n boolean visited[][];\n public boolean exist(char[][] board, String word) {\n \tvisited= new boolean[board.length][board[0].length];\n\t\tfor(int i=0; i<board.length; i++)\n\t\t\tfor(int j=0; j<board[0].length; j++)\n\t\... | 10 | 1 | [] | 1 |
word-search | Word search - Python3 Solution with a Detailed Explanation | word-search-python3-solution-with-a-deta-zzp3 | This solution comes from here. The idea is that you loop over every element of a matrix one by one (lines #1 and #2), and check whether you can find the word. T | peyman_np | NORMAL | 2020-07-20T05:10:01.481015+00:00 | 2020-08-25T22:11:59.856726+00:00 | 2,161 | false | This solution comes from [here](https://leetcode.com/problems/word-search/discuss/27665/Python-simple-dfs-solution). The idea is that you loop over every element of a matrix one by one (lines `#1` and `#2`), and check whether you can find the word. The main part is the `dfs` function that for each value (`board[i][j]`)... | 10 | 0 | ['Python', 'Python3'] | 2 |
word-search | DFS + Backtracking Solution without extra space (visited map) | dfs-backtracking-solution-without-extra-f81al | \nfunc exist(board [][]byte, word string) bool {\n\tfor i := 0; i < len(board); i++ {\n\t\tfor j := 0; j < len(board[0]); j++ {\n\t\t\tif board[i][j] == word[0] | humoyun | NORMAL | 2020-07-02T04:21:19.307175+00:00 | 2020-07-02T04:24:17.031334+00:00 | 894 | false | ```\nfunc exist(board [][]byte, word string) bool {\n\tfor i := 0; i < len(board); i++ {\n\t\tfor j := 0; j < len(board[0]); j++ {\n\t\t\tif board[i][j] == word[0] {\n\t\t\t\trs := dfs(board, i, j, 0, word)\n\n\t\t\t\tif rs {\n\t\t\t\t\treturn true\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\treturn false\n}\n\nfunc dfs(board ... | 10 | 0 | ['Backtracking', 'Depth-First Search', 'Go'] | 2 |
word-search | Java | Brute force to Search Pruning | Fully Explained | java-brute-force-to-search-pruning-fully-sk49 | Intuition\nWe need to construct the given word from the characters in the board. This is a trial-and-error algorithm which we will solve using backtracking.\n\n | nadaralp | NORMAL | 2022-11-24T07:26:15.348718+00:00 | 2022-11-24T07:33:06.161410+00:00 | 3,532 | false | # Intuition\nWe need to construct the given word from the characters in the board. This is a trial-and-error algorithm which we will solve using `backtracking`.\n\nNote: While meeting this question for the first time a few years ago, I thought of doing it with DP. The reason why DP doesn\'t work here is that you cannot... | 9 | 0 | ['Java'] | 1 |
word-search | JS | Backtrack(DFS) | js-backtrackdfs-by-dynamicrecursion-sw4k | \n//approach: backtracking(dfs)\n//basic template of backtracking would be to loop, choose, explore and unchoose\n//loop: you want to iterate over all the numbe | dynamicRecursion | NORMAL | 2021-08-25T19:43:14.401920+00:00 | 2021-08-25T19:43:14.401964+00:00 | 921 | false | ```\n//approach: backtracking(dfs)\n//basic template of backtracking would be to loop, choose, explore and unchoose\n//loop: you want to iterate over all the numbers so that you can find it\'s possible values\n//choose: you start with the index value(the 0th index number), so that you can find the next combined possibl... | 9 | 0 | ['Backtracking', 'Recursion', 'JavaScript'] | 0 |
word-search | Java Solution 4 ms, faster than 99.10% using backtracking | java-solution-4-ms-faster-than-9910-usin-5bgq | PLEASE UPVOTE THIS SOLUTION IF YOU LIKE THIS \u270C\uFE0F\n\nIntiuition: \n- To search a word in grid, we can start from a index (x, y) and then try every dire | satyaDcoder | NORMAL | 2021-02-16T08:40:16.380412+00:00 | 2021-03-05T08:03:21.979434+00:00 | 1,045 | false | **PLEASE UPVOTE THIS SOLUTION IF YOU LIKE THIS** \u270C\uFE0F\n\nIntiuition: \n- To search a word in grid, we can start from a index (x, y) and then try every direction using dfs.\n- If word char is match with the cell than Increment the wordIndex and try that cell neighbour.\n- if word doesn\'t match then replace the... | 9 | 0 | ['Backtracking', 'Java'] | 3 |
word-search | ✅Easy✨||C++|| Beats 100% || With Explanation || | easyc-beats-100-with-explanation-by-olak-id6l | \n# Approach\n Describe your approach to solving the problem. \n1. Search function:\n\n. The recursive search function explores the board to find a match for th | olakade33 | NORMAL | 2024-04-03T07:31:13.090001+00:00 | 2024-04-03T07:31:13.090036+00:00 | 929 | false | \n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Search function:\n\n. The recursive search function explores the board to find a match for the given word starting from the current position (i, j).\n. It returns true if the entire word is found, and false otherwise.\nThe function explores neigh... | 8 | 0 | ['C++'] | 0 |
word-search | Backtracking || Simple Explanation with comments | backtracking-simple-explanation-with-com-oiv0 | Intuition\nThis problem seems to be a classic application of Depth First Search (DFS) in addition with Backtracking. We traverse the board starting from each ce | tourism | NORMAL | 2024-04-03T05:27:29.422558+00:00 | 2024-04-03T07:14:06.578769+00:00 | 737 | false | # Intuition\nThis problem seems to be a classic application of Depth First Search (DFS) in addition with Backtracking. We traverse the board starting from each cell, and at each step, we check if the current cell matches the current character of the word. If it does, we explore its neighbors recursively to find the nex... | 8 | 0 | ['Array', 'String', 'Backtracking', 'Depth-First Search', 'Matrix', 'C++'] | 3 |
word-search | DFS / Backtracking in C++, Js & Python | dfs-backtracking-in-c-js-python-by-nuoxo-uudv | C++\ncpp\nclass Solution\n{\n public:\n\n bool exist(vector<vector<char>>& board, string word)\n {\n int rows, cols;\n\n | nuoxoxo | NORMAL | 2022-11-24T00:02:54.426183+00:00 | 2022-11-24T00:05:31.558832+00:00 | 2,127 | false | # C++\n```cpp\nclass Solution\n{\n public:\n\n bool exist(vector<vector<char>>& board, string word)\n {\n int rows, cols;\n\n if (!board.size())\n\t\t return (false);\n rows = (int) board.size();\n cols = (int) board[0].size();\n for (int i = 0... | 8 | 1 | ['C', 'Python', 'C++', 'JavaScript'] | 2 |
word-search | ✅ Python Simple and Clean || Backtracking Solution | python-simple-and-clean-backtracking-sol-4egq | If you like Pls Upvote :-)\n\n\nclass Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n rows=len(board)\n cols=len(boar | gyanesh09 | NORMAL | 2022-08-01T03:42:08.974698+00:00 | 2022-08-01T03:42:41.287216+00:00 | 884 | false | **If you like Pls Upvote :-)**\n\n```\nclass Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n rows=len(board)\n cols=len(board[0])\n visited=set()\n def dfs(i,j,curr):\n if curr==len(word):\n return True\n \n if i<0 ... | 8 | 0 | ['Backtracking', 'Python'] | 1 |
word-search | 0ms TLE_EXPLAINED EASY || 100%FAST | 0ms-tle_explained-easy-100fast-by-gkesha-f4d1 | \t Do upvote\u2764\uFE0F\n# Sol #1. DFS (800ms)\n\nclass Solution {\npublic:\n bool dfs(vector<vector<char>> &arr, string word, int k, int r, int c)//pass ar | gkeshav_1 | NORMAL | 2022-06-07T20:04:48.411309+00:00 | 2022-11-24T12:58:13.861608+00:00 | 659 | false | \t Do upvote\u2764\uFE0F\n# Sol #1. DFS (800ms)\n```\nclass Solution {\npublic:\n bool dfs(vector<vector<char>> &arr, string word, int k, int r, int c)//pass arr by reference\n {\n \n if(r>=arr.size()||r<0||c>=arr[0].size()||c<0||arr[r][c] != word[k])\n return false;\n\n k++;\n... | 8 | 0 | ['Backtracking', 'Depth-First Search', 'C', 'C++'] | 3 |
word-search | JS | Backtracking | Read this if you are having problems using a Set | js-backtracking-read-this-if-you-are-hav-q6l4 | I spent some time confused as to why my solution, which was seamingly very similar to other\'s in Java and Python was not working. There are two main ways to so | dadavivid | NORMAL | 2022-04-11T13:20:21.935282+00:00 | 2022-04-11T13:22:18.927491+00:00 | 548 | false | I spent some time confused as to why my solution, which was seamingly very similar to other\'s in Java and Python was not working. There are two main ways to solve this problem with backtracking: changing the value of the board when visiting a position, or keeping a record of your visited positions and check against it... | 8 | 0 | ['Backtracking', 'Ordered Set', 'JavaScript'] | 2 |
word-search | Easy C++ dfs solution | easy-c-dfs-solution-by-ab1533489-epdq | \nclass Solution {\nprivate:\n bool dfs(vector<vector<char>>& board,int i, int j , int count, string word){\n if(count == word.length())\n | ab1533489 | NORMAL | 2022-03-11T13:54:28.396455+00:00 | 2022-03-11T13:54:28.396478+00:00 | 301 | false | ```\nclass Solution {\nprivate:\n bool dfs(vector<vector<char>>& board,int i, int j , int count, string word){\n if(count == word.length())\n return true;\n if(i<0 || i>=board.size()||j<0 || j>=board[i].size() || board[i][j]!=word[count])\n return false;\n char temp = boar... | 8 | 0 | ['Depth-First Search', 'Recursion', 'C'] | 0 |
word-search | Python solution using backtracking with explanation | python-solution-using-backtracking-with-a5wxd | \n######################################################\n\n# Runtime: 1412ms - 94.74%\n# Memory: 14.2MB - 71.98%\n\n################################ | saisasank25 | NORMAL | 2021-10-24T13:14:03.306650+00:00 | 2021-10-24T13:14:03.306675+00:00 | 755 | false | ```\n######################################################\n\n# Runtime: 1412ms - 94.74%\n# Memory: 14.2MB - 71.98%\n\n######################################################\n\nclass Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n \n # Below code will help in reduc... | 8 | 0 | ['Backtracking', 'Depth-First Search', 'Python'] | 3 |
word-search | [Python] Simple Solution | DFS | python-simple-solution-dfs-by-ianliuy-bn7j | python\nclass Solution(object):\n def exist(self, board, word):\n for i in xrange(len(board)):\n for j in xrange(len(board[0])):\n | ianliuy | NORMAL | 2021-01-06T07:21:18.709636+00:00 | 2021-01-09T05:43:02.396582+00:00 | 1,991 | false | ```python\nclass Solution(object):\n def exist(self, board, word):\n for i in xrange(len(board)):\n for j in xrange(len(board[0])):\n if self.dfs(board, word, i, j): return True\n return False\n \n def dfs(self, board, word, i, j):\n if i <= -1 or i >=... | 8 | 0 | ['Depth-First Search', 'Python', 'Python3'] | 0 |
word-search | C++ [Simplest Solution possible] (Beats 85% Solution) | c-simplest-solution-possible-beats-85-so-vdd3 | \n// Using DFS + Backtracking\nclass Solution {\npublic:\n\t// Check the boundary cases of the board.\n bool isSafe(int N, int M, int i, int j){\n ret | not_a_cp_coder | NORMAL | 2020-07-21T08:39:30.147149+00:00 | 2020-07-22T05:34:56.913815+00:00 | 1,515 | false | ```\n// Using DFS + Backtracking\nclass Solution {\npublic:\n\t// Check the boundary cases of the board.\n bool isSafe(int N, int M, int i, int j){\n return (i<N) && (i>=0) && (j>=0) && (j<M);\n }\n bool backtrack(vector<vector<char>>& board, string& word, int pos, int i, int j){\n if(word[pos]!=... | 8 | 1 | ['Backtracking', 'Depth-First Search', 'Recursion', 'C++'] | 4 |
word-search | [Java] Trie based Approach | Google asked | java-trie-based-approach-google-asked-by-ishk | \nclass Solution {\n TrieNode root;\n int row, col;\n public boolean exist(char[][] board, String word) {\n //Approach - Trie Approach\n | akshaybahadur21 | NORMAL | 2020-03-29T22:27:39.734440+00:00 | 2020-03-29T22:27:39.734497+00:00 | 1,511 | false | ```\nclass Solution {\n TrieNode root;\n int row, col;\n public boolean exist(char[][] board, String word) {\n //Approach - Trie Approach\n root = new TrieNode();\n insertInTrie(word);\n TrieNode curr = root;\n row = board.length;\n col = board[0].length;\n for ... | 8 | 0 | ['Trie', 'Java'] | 3 |
word-search | Python Easy to understand- 95% Faster | python-easy-to-understand-95-faster-by-l-dyf3 | Please let me know if explanation is needed.\n\nclass Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n m = len(board)\n | logan_kd | NORMAL | 2020-01-18T21:10:21.263540+00:00 | 2020-06-16T14:13:15.745875+00:00 | 2,142 | false | Please let me know if explanation is needed.\n```\nclass Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n m = len(board)\n n = len(board[0])\n for i in range(m):\n for j in range(n):\n if board[i][j] == word[0]:\n res = self.b... | 8 | 1 | ['Python', 'Python3'] | 4 |
word-search | Beats100%|| simple backtracking approach ||easy solution||explained code | beats100-simple-backtracking-approach-ea-9e05 | Intuition\njust like rat in maze problem.\n\n# Approach\njust using backtracking.\n\n# Complexity\n- Time complexity:\nO(m n word.length() back)\n\n- Space comp | twaritagrawal | NORMAL | 2024-04-03T07:40:47.509565+00:00 | 2024-04-03T07:40:47.509597+00:00 | 560 | false | # Intuition\njust like rat in maze problem.\n\n# Approach\njust using backtracking.\n\n# Complexity\n- Time complexity:\nO(m* n* word.length() back)\n\n- Space complexity:\nO(m * n)\n\n\n# Code\n```\nclass Solution {\npublic:\n bool backtrack(vector<vector<char>>& board,string &word,int i,int j,int &m,int &n,int &inde... | 7 | 0 | ['Array', 'String', 'Backtracking', 'Recursion', 'Matrix', 'C++'] | 4 |
word-search | [C++, Java, Kotlin] Simple Explained Solution ✅ | c-java-kotlin-simple-explained-solution-evu5k | Approach\nSo, the idea is to go through all the positions, and if a position in the matrix contains the first character of the word, I start a depth-first searc | Z3ROsum | NORMAL | 2024-04-03T01:15:11.869457+00:00 | 2024-04-03T01:15:11.869496+00:00 | 318 | false | # Approach\nSo, the idea is to go through all the positions, and if a position in the matrix contains the first character of the word, I start a depth-first search starting from that position.\nAt each step, I check if I can go left, right, up, or down (if those positions\u2019 characters match the next character in th... | 7 | 0 | ['Kotlin'] | 1 |
word-search | Word Search Java Solution || Easy Recursive Approach || T.C :- O(m * n) | word-search-java-solution-easy-recursive-lqk3 | \nclass Solution {\n public boolean exist(char[][] board, String word) {\n\t\n\t//finding first character of given word in given matrix \n for(int i=0 | palpradeep | NORMAL | 2022-11-07T02:17:21.928069+00:00 | 2022-11-07T02:17:21.928116+00:00 | 1,485 | false | ```\nclass Solution {\n public boolean exist(char[][] board, String word) {\n\t\n\t//finding first character of given word in given matrix \n for(int i=0; i<board.length; i++)\n {\n for(int j=0; j<board[0].length; j++)\n {\n\t\t\t//if first character found then we call recursion f... | 7 | 0 | ['Java'] | 0 |
word-search | C++ | DFS | 2 Methods 0(n) & O(1) | Backtracking | c-dfs-2-methods-0n-o1-backtracking-by-va-47z3 | \n\nMethod - 1:\nO(n * word size)\nn = rowscol\n\nclass Solution {\npublic:\n \n bool search(vector<vector<char>>& board, int i, int j, string& word, int | vaibhav4859 | NORMAL | 2021-10-09T16:14:41.176161+00:00 | 2021-10-09T16:14:41.176194+00:00 | 1,141 | false | \n\n***Method - 1:***\nO(n * word size)\nn = rows*col\n```\nclass Solution {\npublic:\n \n bool search(vector<vector<char>>& board, int i, int j, string& word, int pos, vector<vector<bool>>& visited){\n ... | 7 | 0 | ['Backtracking', 'Depth-First Search', 'Recursion', 'C', 'C++'] | 1 |
word-search | C++ solution || Easy Understand | c-solution-easy-understand-by-kannu_priy-mc7t | \nclass Solution {\npublic:\n bool backtrack(vector<vector<char>>& board, string& word, int pos, int i, int j, int r, int c){\n if(i >= r || j >= c || | kannu_priya | NORMAL | 2021-04-20T12:46:33.237922+00:00 | 2021-04-20T12:47:00.471946+00:00 | 832 | false | ```\nclass Solution {\npublic:\n bool backtrack(vector<vector<char>>& board, string& word, int pos, int i, int j, int r, int c){\n if(i >= r || j >= c || i < 0 || j < 0 || word[pos] != board[i][j])\n return false;\n \n if(pos == word.length()-1)\n return true;\n \n ... | 7 | 1 | ['Backtracking', 'C', 'C++'] | 2 |
count-of-sub-multisets-with-bounded-sum | [C++/Python] Knapsack DP | cpython-knapsack-dp-by-lee215-gl6q | Intuition\nKnapsack DP problem, where we can have multiple same item.\n\n\n# Explanation\nIn this Knapsack DP problem,\ndp[i] is the ways to sum up i.\ndp[0] = | lee215 | NORMAL | 2023-10-14T16:04:31.171670+00:00 | 2023-10-15T03:30:14.030040+00:00 | 4,351 | false | # **Intuition**\nKnapsack DP problem, where we can have multiple same item.\n<br>\n\n# **Explanation**\nIn this Knapsack DP problem,\n`dp[i]` is the ways to sum up `i`.\n`dp[0] = 1` for empty set,\nand we want to find `sum(dp[l] + ... + dp[r])`.\n\nIterate all items,\nassume we have `c` item of size `a`,\niterate `i` f... | 54 | 1 | [] | 19 |
count-of-sub-multisets-with-bounded-sum | Optimized Dynamic Programming | optimized-dynamic-programming-by-jeffrey-b9eu | Intuition\nThis problem is similar to knapsack, so DP seems like a good approach.\n\n# Approach\nWe can try to approach this like a modified knapsack problem, w | jeffreyhu8 | NORMAL | 2023-10-14T16:01:00.712811+00:00 | 2023-10-14T16:07:58.818378+00:00 | 2,448 | false | # Intuition\nThis problem is similar to knapsack, so DP seems like a good approach.\n\n# Approach\nWe can try to approach this like a modified knapsack problem, where we take special care to account for duplicate elements. We can take care of duplicate elements by keeping a map from elements to their frequencies, then ... | 38 | 0 | ['Dynamic Programming', 'Java'] | 6 |
count-of-sub-multisets-with-bounded-sum | Simple Explanation for Optimized Knapsack | simple-explanation-for-optimized-knapsac-pl9k | PLEASE UPVOTE!\n\n\n# Intuition\n\nKnapsack DP - Count all the multisets with sum k (dp[k]) for all k <= r, then the answer is just sum(dp[l:r+1]). The challeng | dayoadeyemi | NORMAL | 2023-10-14T18:06:59.956306+00:00 | 2023-10-14T18:06:59.956338+00:00 | 1,008 | false | PLEASE UPVOTE!\n\n\n# Intuition\n\nKnapsack DP - Count all the multisets with sum `k` (`dp[k]`) for all `k <= r`, then the answer is just `sum(dp[l:r+1])`. The challenge is in optimizing the knapsack so t... | 35 | 0 | ['Dynamic Programming', 'Python3'] | 5 |
count-of-sub-multisets-with-bounded-sum | EASY DP! 3D -> 2D and explaining WHY we use sliding window | easy-dp-3d-2d-and-explaining-why-we-use-90dlc | Intuition\nClearly, there can only be $\sqrt{r}$ distinct numbers. The way to see this is to notice the smallest possible sum of $n$ distinct numbers is\n\large | gtsmarmy | NORMAL | 2023-10-14T22:37:25.473792+00:00 | 2023-10-20T17:14:53.607684+00:00 | 892 | false | # Intuition\nClearly, there can only be $\\sqrt{r}$ distinct numbers. The way to see this is to notice the smallest possible sum of $n$ distinct numbers is\n$$\\large 1+2+3+\\dotsc+m = \\frac{m(m+1)}{2}$$, therefore if $\\frac{m(m+1)}{2} \\leqslant r$ then $m \\leqslant O(\\sqrt{r})$\n\nWith this in mind, let\'s repres... | 16 | 0 | ['Math', 'Dynamic Programming', 'Sliding Window', 'C++'] | 4 |
count-of-sub-multisets-with-bounded-sum | Video Explanation (Intuition with proof of Amorized Time complexity) | video-explanation-intuition-with-proof-o-9e3a | Explanation\n\nClick here for the video\n\n# Code\n\nclass Solution {\npublic:\n static const int N = 2e4;\n int mod= 1e9+7;\n int cnt[N+1];\n int c | codingmohan | NORMAL | 2023-10-15T08:13:45.423612+00:00 | 2023-10-15T08:13:45.423628+00:00 | 527 | false | # Explanation\n\n[Click here for the video](https://youtu.be/V6AoVVxt18Y)\n\n# Code\n```\nclass Solution {\npublic:\n static const int N = 2e4;\n int mod= 1e9+7;\n int cnt[N+1];\n int countSubMultisets(vector<int>& a, int l, int r) {\n int dp[N+1];\n memset(cnt,0,sizeof(cnt));\n memset(... | 12 | 1 | ['C++'] | 2 |
count-of-sub-multisets-with-bounded-sum | ✅☑[C++/C/Java/Python/JavaScript] || DP || EXPLAINED🔥 | ccjavapythonjavascript-dp-explained-by-m-rrg6 | PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n1. Problem Context: The code aims to calculate the number of multisets with | MarkSPhilip31 | NORMAL | 2023-10-14T16:12:44.917909+00:00 | 2023-10-14T16:12:44.917929+00:00 | 1,510 | false | # *PLEASE UPVOTE IF IT HELPED*\n\n---\n\n\n# Approaches\n***(Also explained in the code)***\n\n1. **Problem Context:** The code aims to calculate the number of multisets with sums within a specified range [l, r] for a given set of integers.\n\n1. **Initialization:**\n\n - The code defines constants for modulo arithm... | 8 | 1 | ['Dynamic Programming', 'C', 'Sorting', 'C++', 'Java', 'Python3', 'JavaScript'] | 4 |
count-of-sub-multisets-with-bounded-sum | C++ recursive dp solution O(sqrt(r) * r) | c-recursive-dp-solution-osqrtr-r-by-dmit-6hew | We have at most 200 distinct elements so let\'s compress our array to array of pairs (a, c), where a - value, c - number of times it repeats in array\nLet\'s de | dmitrii_bokovikov | NORMAL | 2023-10-15T17:23:28.184441+00:00 | 2023-10-15T17:41:04.043828+00:00 | 391 | false | We have at most 200 distinct elements so let\'s compress our array to array of pairs (a, c), where a - value, c - number of times it repeats in array\nLet\'s define function get(i, s) - number of subsequences with sum less or equal than s starting from index i and tricky part of this problem is simplifying complexity o... | 6 | 0 | ['C++'] | 0 |
count-of-sub-multisets-with-bounded-sum | 🔥🔥🔥 C++ Solution 🔥🔥🔥 | c-solution-by-07dishwasherbob8-1j60 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe number of unique elements can be at most 200. We can optimize our dp using that use | 07dishwasherbob8 | NORMAL | 2023-10-14T21:00:00.178392+00:00 | 2023-10-14T21:00:00.178433+00:00 | 655 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe number of unique elements can be at most 200. We can optimize our dp using that use sliding window. We get this number by approximating sqrt(n). \n# Approach\ndp[i] is the number of unique sorted multisets of sum i lets say that we us... | 6 | 0 | ['Dynamic Programming', 'C++'] | 0 |
count-of-sub-multisets-with-bounded-sum | Nice optimisation trick on Coin Change Problem ( using 2 dp) | nice-optimisation-trick-on-coin-change-p-4oyh | Intuition\nFirst Observation one need to make is that # of distint element in array cannot exceed 200.\nUsing this observation we will make our dp (similar to c | i_m_karank | NORMAL | 2023-10-14T18:27:46.010314+00:00 | 2023-10-14T19:47:50.401308+00:00 | 338 | false | # Intuition\nFirst Observation one need to make is that # of distint element in array cannot exceed 200.\nUsing this observation we will make our dp (similar to coin change)\nlet dp[i][sumleft]=no of ways make Sumleft using index (i---n-1)\n# Approach\n Let\'s move to transitions\n dp[i][sumleft]=dp[i+1][sumleft] // do... | 4 | 1 | ['C++'] | 1 |
count-of-sub-multisets-with-bounded-sum | [Python3] Optimised knapsack DP with sliding window, <1s (faster than 100% at time of writing) | python3-optimised-knapsack-dp-with-slidi-s2pu | Note that this is intended to explain additional optimisations to the knapsack DP with sliding window solution method given in other solutions (e.g. lee215), wh | sveng101 | NORMAL | 2023-10-15T00:41:51.827096+00:00 | 2023-10-15T00:41:51.827122+00:00 | 169 | false | Note that this is intended to explain additional optimisations to the knapsack DP with sliding window solution method given in other solutions (e.g. lee215), which is not itself explained in detail here (although the code is commented with rationale behind individual steps). I would therefore recommend looking at other... | 3 | 0 | ['Python3'] | 1 |
count-of-sub-multisets-with-bounded-sum | Bottom-Up DP & Sliding Window to avoid TLE | bottom-up-dp-sliding-window-to-avoid-tle-6i1u | Intuition\n Describe your first thoughts on how to solve this problem. \nThe number of distinct elements (let it be m) the array can have is at most 200. It can | CelonyMire | NORMAL | 2023-10-14T20:03:51.927819+00:00 | 2023-10-16T19:16:08.057257+00:00 | 373 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe number of **distinct** elements (let it be `m`) the array can have is at most `200`. It can be verified by the sum of first `199` positive elements (`199*(199+1)/2=19900`, and then `0`). With this, we can develop a `O(sum*m)` solution... | 3 | 1 | ['Dynamic Programming', 'Sliding Window', 'C++'] | 0 |
count-of-sub-multisets-with-bounded-sum | DP + Sliding window [EXPLAINED] | dp-sliding-window-explained-by-r9n-w9vp | IntuitionWe need to count the number of subsets of a given list of integers that can sum to values between l and r. We also have to handle duplicates in the lis | r9n | NORMAL | 2025-01-15T22:40:06.475116+00:00 | 2025-01-15T22:40:06.475116+00:00 | 17 | false | # Intuition
We need to count the number of subsets of a given list of integers that can sum to values between l and r. We also have to handle duplicates in the list and perform efficient calculations using DP.
# Approach
I used DP where I maintain an array (dp) to keep track of the possible sums, and for each unique v... | 2 | 0 | ['Math', 'Dynamic Programming', 'Memoization', 'Sliding Window', 'Python3'] | 0 |
count-of-sub-multisets-with-bounded-sum | C++ solution | c-solution-by-pejmantheory-omal | 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 | pejmantheory | NORMAL | 2023-10-18T11:08:23.849303+00:00 | 2023-10-18T11:08:23.849332+00:00 | 178 | 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 |
count-of-sub-multisets-with-bounded-sum | Read this if you couldn't understand from any other solution. Comment if you still have a doubt. | read-this-if-you-couldnt-understand-from-48oa | Intuition\n Describe your first thoughts on how to solve this problem. \nI am assuming that you can solve question the simpler version of the problem where all | Adarsh-Roy | NORMAL | 2023-10-16T14:15:11.773895+00:00 | 2023-10-16T14:15:11.773913+00:00 | 227 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI am assuming that you can solve question the simpler version of the problem where all elements of the array are unique via dynamic programming considering the cases of picking or not picking the elements as we traverse the array, if not ... | 2 | 0 | ['Dynamic Programming', 'C++'] | 0 |
count-of-sub-multisets-with-bounded-sum | Easy DP Solution | easy-dp-solution-by-ming_91-ootr | Approach\n Describe your approach to solving the problem. \nCount occurrences of each distinct number in countArr.\n\ndp[i][sum] = dp[i+1][sum + k * countArr[i] | Ming_91 | NORMAL | 2023-10-14T16:30:17.674103+00:00 | 2023-10-14T16:30:17.674129+00:00 | 396 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\nCount occurrences of each distinct number in `countArr`.\n\n`dp[i][sum] = dp[i+1][sum + k * countArr[i].number]`, where k is in range `[0, countArr[i].count]`.\n\nIf `sum > r` we can early stop since all number are non-negative.\nIf `i == countArr.len... | 2 | 0 | ['Java'] | 2 |
count-of-sub-multisets-with-bounded-sum | K-Knapsack Problem || Detailed Explanation || Different Way to Think ||O(N*r) | k-knapsack-problem-detailed-explanation-84wnx | Intuition\n Describe your first thoughts on how to solve this problem. \n\nThis is a standard knapsack problem and a variation of 2585.\n\nIn general, knapsack | orekihoutarou | NORMAL | 2023-10-19T19:51:24.539934+00:00 | 2023-10-19T19:51:24.539961+00:00 | 73 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nThis is a standard knapsack problem and a variation of [2585](https://leetcode.com/problems/number-of-ways-to-earn-points/description/).\n\nIn general, knapsack DP problem is categorized into two sets: (1) 1-knapsack, with only 1 count ... | 1 | 0 | ['Python3'] | 0 |
count-of-sub-multisets-with-bounded-sum | The secret: faster than 100% | the-secret-faster-than-100-by-mbeceanu-szhr | Intuition\n Describe your first thoughts on how to solve this problem. \nThe dirty secret of this problem is that the distinct values in the array cannot be ver | mbeceanu | NORMAL | 2023-10-15T00:13:59.615903+00:00 | 2023-10-15T22:08:40.831528+00:00 | 107 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe dirty secret of this problem is that the distinct values in the array cannot be very many.\nIndeed, the sum of the first $n$ natural numbers is $\\frac {n(n+1)} 2$. It is given in the problem that the sum of all values is no more than... | 1 | 0 | ['Python3'] | 0 |
count-of-sub-multisets-with-bounded-sum | Counting Sub-Multisets with a Given Sum Range | counting-sub-multisets-with-a-given-sum-ascdg | IntuitionThe problem involves counting the number of multisets (subsets with repetitions allowed) that sum up within a given range [𝑙,𝑟].We need to efficiently | kuldeepchaudhary123 | NORMAL | 2025-03-19T12:41:39.606696+00:00 | 2025-03-19T12:41:39.606696+00:00 | 4 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem involves counting the number of multisets (subsets with repetitions allowed) that sum up within a given range [𝑙,𝑟].
We need to efficiently compute how many valid multisets exist that sum up to each value in this range.
Since... | 0 | 0 | ['Dynamic Programming', 'Sliding Window', 'C++'] | 0 |
count-of-sub-multisets-with-bounded-sum | 2902. Count of Sub-Multisets With Bounded Sum | 2902-count-of-sub-multisets-with-bounded-vtfh | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | G8xd0QPqTy | NORMAL | 2025-01-21T16:27:30.990927+00:00 | 2025-01-21T16:27:30.990927+00:00 | 5 | 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 | ['Python3'] | 0 |
count-of-sub-multisets-with-bounded-sum | ☕ Java solution | java-solution-by-barakamon-atr6 | null | Barakamon | NORMAL | 2025-01-11T09:54:11.782829+00:00 | 2025-01-11T09:54:11.782829+00:00 | 7 | false | 
```java []
class Solution {
private static final int MOD = (int) 1e9 + 7;
public int countSubMultisets(List<Integer> nums, int l, int r) {
int[] numsArray = nums.stream().mapToInt(... | 0 | 0 | ['Java'] | 0 |
count-of-sub-multisets-with-bounded-sum | Explained 38ms, beat 100% ; avoid tmp DP array for optimization | explained-38ms-beat-100-avoid-tmp-dp-arr-025q | IntuitionGroup identical numbers. Process each distinct number separately since total distinct numbers ≤ sqrt(sum). For each number, process all possible counts | ivangnilomedov | NORMAL | 2024-12-25T16:12:29.631802+00:00 | 2024-12-25T16:12:29.631802+00:00 | 7 | false | # Intuition
Group identical numbers. Process each distinct number separately since total distinct numbers ≤ sqrt(sum). For each number, process all possible counts (0 to max_count) using sliding window technique to group positions with same remainder efficiently.
# Approach
1. Group identical numbers and count occurre... | 0 | 0 | ['C++'] | 0 |
count-of-sub-multisets-with-bounded-sum | Python, knapsack dp solution with explanation | python-knapsack-dp-solution-with-explana-vs3n | knapsack dp\nThe problem ask that how many ways to fill up [l, r]size of knapsacks,\nand if numbers are the same, there are the same items.\ndp[i][j] is number | shun6096tw | NORMAL | 2024-11-25T07:48:49.122317+00:00 | 2024-11-25T07:48:49.122348+00:00 | 7 | false | ### knapsack dp\nThe problem ask that how many ways to fill up ```[l, r]```size of knapsacks,\nand if numbers are the same, there are the same items.\ndp[i][j] is number of ways to fill up size j of knapsack.\nwe have ```y``` of number ```x```,\ndp[i][j] = dp[i-1][j] + dp[i-1][j-x] + dp[i-1][j-2x] + ... + dp[i-1][j-y*x... | 0 | 0 | ['Prefix Sum', 'Python'] | 0 |
count-of-sub-multisets-with-bounded-sum | easy implementation | easy-implementation-by-ani_vishwakarma-vo0m | 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 | Ani_vishwakarma | NORMAL | 2024-10-24T09:48:27.136442+00:00 | 2024-10-24T09:48:27.136477+00:00 | 8 | 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)$$ --... | 0 | 0 | ['Dynamic Programming', 'C++'] | 0 |
count-of-sub-multisets-with-bounded-sum | Beginner Friendly Beats 90% | beginner-friendly-beats-90-by-l_u_c_a_s-h4ws | \n\n# Code\npython3 []\nfrom collections import Counter\nimport numpy as np\n\nclass Solution:\n def countSubMultisets(self, nums: List[int], l: int, r: int) | L_U_C_A_S | NORMAL | 2024-10-02T06:26:43.364384+00:00 | 2024-10-02T06:26:43.364409+00:00 | 9 | false | \n\n# Code\n```python3 []\nfrom collections import Counter\nimport numpy as np\n\nclass Solution:\n def countSubMultisets(self, nums: List[int], l: int, r: int) -> int:\n # Step 1: Define the modulo value for large numbers\n MOD = 10**9 + 7\n \n # Step 2: Count the frequency of each numbe... | 0 | 0 | ['Python3'] | 0 |
count-of-sub-multisets-with-bounded-sum | Knapsack DP + Pack Update Trick (Sliding Window) | knapsack-dp-pack-update-trick-sliding-wi-n6co | Intuition\nKnapsack DP. See also lee215\'s solution.\n\n \u274C(Low to High) One approach is given a dp[j] > 0, update dp[j+i], dp[j+2i], ... dp[j+ci]. But this | outerridgesavage | NORMAL | 2024-08-20T05:18:31.956685+00:00 | 2024-08-20T05:20:46.499342+00:00 | 20 | false | # Intuition\nKnapsack DP. See also [lee215\'s solution](https://leetcode.com/problems/count-of-sub-multisets-with-bounded-sum/solutions/4168129/c-python-knapsack-dp/).\n\n* \u274C(Low to High) One approach is given a `dp[j] > 0`, update `dp[j+i]`, `dp[j+2i]`, ... `dp[j+ci]`. But this leads to $$O(nr\\mathrm{max}(c))$$ ... | 0 | 0 | ['Dynamic Programming', 'Sliding Window', 'C++'] | 0 |
count-of-sub-multisets-with-bounded-sum | [Python] Numpy-powered O(NR) code (Beats 90%) | python-numpy-powered-onr-code-beats-90-b-mmay | Approach\n Describe your approach to solving the problem. \nThanks to numpy that is optimized for vector computation, you can get AC with $O(NR)$ code. Actually | nakanolab | NORMAL | 2024-07-07T08:09:37.042470+00:00 | 2024-07-07T08:14:38.654296+00:00 | 13 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\nThanks to numpy that is optimized for vector computation, you can get AC with $O(NR)$ code. Actually, it is pretty fast!\nAs an illustration, for `nums = [1, 3, 3]`, the code works as follows:\n- For `num = 1`, add a vector shifted right by 1 into `dp... | 0 | 0 | ['Python3'] | 0 |
count-of-sub-multisets-with-bounded-sum | JAVA SOLUTION | java-solution-by-danish_jamil-xmgr | Intuition\n\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- Tim | Danish_Jamil | NORMAL | 2024-06-29T06:11:30.109778+00:00 | 2024-06-29T06:11:30.109810+00:00 | 9 | false | # Intuition\n\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... | 0 | 0 | ['Java'] | 0 |
count-of-sub-multisets-with-bounded-sum | Thought process explained for interview. (C++) | thought-process-explained-for-interview-2j7c4 | Observations:\n- Order of number matter here, because 1,2,1 is same as 1,1,2. So we don\'t want duplicates.\n- For a particular number num, we can either take 1 | aaditya-pal | NORMAL | 2024-06-18T06:39:44.595834+00:00 | 2024-06-18T06:39:44.595865+00:00 | 40 | false | Observations:\n- Order of number matter here, because 1,2,1 is same as 1,1,2. So we don\'t want duplicates.\n- For a particular number num, we can either take 1 times, 2 times... frequency[num] times.\n- What if the required problem was to get ans for a particular sum.\n- It would be coin change problem, where order ma... | 0 | 0 | ['C++'] | 0 |
count-of-sub-multisets-with-bounded-sum | Alternative Fast Fourier Transform solution | alternative-fast-fourier-transform-solut-3937 | Intuition\nWe can use the Fast Fourier Transform (FFT) to determine the number of multisets having sum $k$ for all $k$ simultaneously by encoding the Knapsack p | mishai | NORMAL | 2024-06-13T18:24:45.261616+00:00 | 2024-06-13T18:24:45.261647+00:00 | 2 | false | # Intuition\nWe can use the [Fast Fourier Transform (FFT)](https://en.wikipedia.org/wiki/Fast_Fourier_transform) to determine the number of multisets having sum $k$ for all $k$ simultaneously by encoding the Knapsack problem as one of polynomial multiplication.\n\n# Approach\n<!-- Describe your approach to solving the ... | 0 | 0 | ['C++'] | 0 |
count-of-sub-multisets-with-bounded-sum | Beasts 100% users || JS || Fully explained | beasts-100-users-js-fully-explained-by-a-sqvf | \n# Code\n\nlet MOD = 1000000007; // Define the modulo value\nlet sumsMapPre = Array(20001).fill(0); // Initialize an array to store the prefix sum counts\nlet | aastha_b | NORMAL | 2024-04-19T20:08:04.400481+00:00 | 2024-04-19T20:10:09.051495+00:00 | 6 | false | \n# Code\n```\nlet MOD = 1000000007; // Define the modulo value\nlet sumsMapPre = Array(20001).fill(0); // Initialize an array to store the prefix sum counts\nlet sumsMapCur = Array(20001).fill(0); // Initi... | 0 | 0 | ['JavaScript'] | 0 |
count-of-sub-multisets-with-bounded-sum | Recursion->Memo->DP, not best solution, no explanation. | recursion-memo-dp-not-best-solution-no-e-q7m3 | Intuition\nIn order to understand DP better for DP newbie :)\n\n# Recursion (TLE)\n\nclass Solution {\n int mod = 1_0000_0000_7;\n public int countSubMult | jon371449 | NORMAL | 2024-04-11T17:04:14.076066+00:00 | 2024-04-11T17:04:14.076101+00:00 | 43 | false | # Intuition\nIn order to understand DP better for DP newbie :)\n\n# Recursion (TLE)\n```\nclass Solution {\n int mod = 1_0000_0000_7;\n public int countSubMultisets(List<Integer> nums, int l, int r) {\n Map<Integer, Integer> map = new HashMap<>();\n for (int num : nums) {\n map.put(num, m... | 0 | 0 | ['Dynamic Programming', 'Recursion', 'Memoization', 'Java'] | 0 |
count-of-sub-multisets-with-bounded-sum | Solution Like Never Before | solution-like-never-before-by-gp_777-gguc | 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 | GP_777 | NORMAL | 2024-04-07T14:29:11.964543+00:00 | 2024-04-07T14:29:11.964568+00:00 | 14 | 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)$$ --... | 0 | 0 | ['Java'] | 0 |
count-of-sub-multisets-with-bounded-sum | Python numpy solution. Simple logic without tricks | python-numpy-solution-simple-logic-witho-aimo | Intuition\nIdea is knapsack using dynamic programming. In normal knapsack we need to iterate through each number and either add it to the set, or not. Here, mul | omikad | NORMAL | 2024-03-29T20:43:43.997109+00:00 | 2024-03-29T20:51:23.277509+00:00 | 12 | false | # Intuition\nIdea is knapsack using dynamic programming. In normal knapsack we need to iterate through each number and either add it to the set, or not. Here, multiset definition makes us to use Counter first, to compress numbers with their frequences.\n\n\n# Approach\n\n`DP[i, sm]` is a number of ways to get a sum `sm... | 0 | 0 | ['Python3'] | 0 |
count-of-sub-multisets-with-bounded-sum | Python AC | python-ac-by-anonymous_k-s7ky | Intuition\n Describe your first thoughts on how to solve this problem. \nWhat are the options for an element \'a\', it can be selected from [0, c[a]] times.\nSi | anonymous_k | NORMAL | 2024-03-02T11:41:16.294668+00:00 | 2024-03-02T11:41:16.294699+00:00 | 20 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWhat are the options for an element \'a\', it can be selected from [0, c[a]] times.\nSimple DP is giving TLE, others have mentioned a logic to turn internal linear time options logic to constant time.\nWrite down f(i, s) and f(i, s+a), ge... | 0 | 0 | ['Python3'] | 1 |
count-of-sub-multisets-with-bounded-sum | C++ DP solution, I think pretty standard one. | c-dp-solution-i-think-pretty-standard-on-zbq9 | Intuition\nI first thought of too brute method.\n\n# Approach\nWith sort of brute-force method I passed all but ~15 samples.\nWhen I realized I don\'t know how | ohennel667 | NORMAL | 2024-02-01T08:07:11.532489+00:00 | 2024-02-01T08:07:11.532520+00:00 | 19 | false | # Intuition\nI first thought of too brute method.\n\n# Approach\nWith sort of brute-force method I passed all but ~15 samples.\nWhen I realized I don\'t know how to improve it further, I tried to think of completely different approach (DP).\n\n# Complexity\n- Time complexity: did not evaluate.\n\n- Space complexity: O(... | 0 | 0 | ['C++'] | 0 |
count-of-sub-multisets-with-bounded-sum | 13-Line Solution with DP in Python | 13-line-solution-with-dp-in-python-by-me-lv9o | Intuition\n Describe your first thoughts on how to solve this problem. \nNaive $O(NM)$ DP is too slow for the test size. However, the size of distinct numbers i | metaphysicalist | NORMAL | 2024-01-27T18:21:09.221822+00:00 | 2024-01-27T18:21:09.221849+00:00 | 49 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nNaive $O(NM)$ DP is too slow for the test size. However, the size of distinct numbers is up to $O(\\sqrt{N})$ only, so we try to find a DP solution that adds each distinct number in $O(N)$. As a result, the overall complexity is $O(N\\sqr... | 0 | 0 | ['Dynamic Programming', 'Sliding Window', 'Python3'] | 0 |
count-of-sub-multisets-with-bounded-sum | C++ | c-by-tinachien-t6io | \nusing LL = long long;\nLL M = 1e9+7;\nclass Solution {\n int count0 = 0;\n vector<pair<int, int>>arr;\npublic:\n int countSubMultisets(vector<int>& n | TinaChien | NORMAL | 2023-12-06T13:38:37.598755+00:00 | 2023-12-06T13:38:37.598783+00:00 | 6 | false | ```\nusing LL = long long;\nLL M = 1e9+7;\nclass Solution {\n int count0 = 0;\n vector<pair<int, int>>arr;\npublic:\n int countSubMultisets(vector<int>& nums, int l, int r) {\n unordered_map<int, int>Map;\n for(auto x : nums){\n if(x == 0)\n count0++;\n else\n... | 0 | 0 | [] | 0 |
count-of-sub-multisets-with-bounded-sum | Go - Dynamic Programming Knapsack + Prefix Sum Solution | go-dynamic-programming-knapsack-prefix-s-c5mx | Intuition\nThe problem equals to slightly modified Knapsack. It can be solved using the Dynamic Programming approach, very similar to Coin Change problem(s).\n\ | erni27 | NORMAL | 2023-11-04T14:36:43.042288+00:00 | 2023-11-04T14:36:43.042308+00:00 | 26 | false | # Intuition\nThe problem equals to slightly modified *Knapsack*. It can be solved using the *Dynamic Programming* approach, very similar to *Coin Change* problem(s).\n```\nfunc countSubMultisets(nums []int, l int, r int) int {\n\tmultiset := make(map[int]int)\n\tfor _, num := range nums {\n\t\tmultiset[num]++\n\t}\n\tm... | 0 | 0 | ['Dynamic Programming', 'Prefix Sum', 'Go'] | 0 |
count-of-sub-multisets-with-bounded-sum | C++ - Bottom up DP | c-bottom-up-dp-by-mumrocks-od2i | Intuition\nI first came up with top down DP but got OOM. Then looked into https://leetcode.com/problems/count-of-sub-multisets-with-bounded-sum/solutions/416806 | MumRocks | NORMAL | 2023-10-25T16:50:23.738805+00:00 | 2023-10-25T16:50:48.509734+00:00 | 43 | false | # Intuition\nI first came up with top down DP but got OOM. Then looked into https://leetcode.com/problems/count-of-sub-multisets-with-bounded-sum/solutions/4168064/optimized-dynamic-programming/ to implement bottom up DP solution. \n\n# Code\n```\nclass Solution {\n int MOD = 1000000007;\n \npublic:\n int count... | 0 | 0 | ['Dynamic Programming', 'C++'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.