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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
find-the-shortest-superstring | [Python3] travelling sales person (TSP) | python3-travelling-sales-person-tsp-by-y-zmhy | \n\nclass Solution:\n def shortestSuperstring(self, words: List[str]) -> str:\n n = len(words)\n graph = [[0]*n for _ in range(n)] # graph as a | ye15 | NORMAL | 2021-05-26T04:21:13.885708+00:00 | 2021-05-26T04:28:31.105650+00:00 | 884 | false | \n```\nclass Solution:\n def shortestSuperstring(self, words: List[str]) -> str:\n n = len(words)\n graph = [[0]*n for _ in range(n)] # graph as adjacency matrix \n \n for i in range(n):\n for j in range(n): \n if i != j: \n for k in range(len(... | 1 | 0 | ['Python3'] | 0 |
find-the-shortest-superstring | Java Simple and easy to understand solution, clean code with comments | java-simple-and-easy-to-understand-solut-wcj4 | PLEASE UPVOTE IF YOU LIKE THIS SOLUTION\n\n\n\n\nclass Solution {\n public String shortestSuperstring(String[] words) {\n int n = words.length;\n | satyaDcoder | NORMAL | 2021-05-25T04:46:19.314492+00:00 | 2021-05-25T04:46:19.314525+00:00 | 291 | false | **PLEASE UPVOTE IF YOU LIKE THIS SOLUTION**\n\n\n\n```\nclass Solution {\n public String shortestSuperstring(String[] words) {\n int n = words.length;\n \n int[][] overlaps = createOverlapGraph(words);\n \n int maskLen = 1 << n;\n \n //dp[mask][i] : maximum overlap of... | 1 | 2 | ['Java'] | 0 |
find-the-shortest-superstring | C++ simple solution using dynamic programming | c-simple-solution-using-dynamic-programm-lmjt | cpp\n#define PSI pair<string, int>\nclass Solution {\npublic:\n int calculateOverlap(string s, string pattern){\n for(int i=min((int)s.size(), (int)pa | sirkp19 | NORMAL | 2021-05-24T11:25:01.270581+00:00 | 2021-05-24T11:25:01.270610+00:00 | 288 | false | ```cpp\n#define PSI pair<string, int>\nclass Solution {\npublic:\n int calculateOverlap(string s, string pattern){\n for(int i=min((int)s.size(), (int)pattern.size()); i>0; i--){\n if(s.substr((int)s.size()-i)==pattern.substr(0, i)){\n return i;\n }\n }\n ret... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | Java | the best (Held-Karp ~97%, 17ms) and the worst solution (Greedy backtracking DFS, ~1000ms) | java-the-best-held-karp-97-17ms-and-the-5i1qi | Updated this by adding Solution 2. based on Held-Karp traveling salesman DP algorithm.\n\nSolution 2 - Held-Karp optimized to run for glueable words only (~17ms | prezes | NORMAL | 2021-05-24T03:07:08.167634+00:00 | 2021-05-24T21:10:37.330960+00:00 | 552 | false | Updated this by adding Solution 2. based on Held-Karp traveling salesman DP algorithm.\n\n**Solution 2 - Held-Karp optimized to run for glueable words only (~17ms)**\nThe incrementally improved solution uses Held-Karp-like algorithm (traveling salesman) with bitmask for efficiently iterating subsets of words.\nUnlike i... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | Rust translated DP solution | rust-translated-dp-solution-by-sugyan-6x3x | rust\nimpl Solution {\n pub fn shortest_superstring(words: Vec<String>) -> String {\n let n = words.len();\n let mut graph = vec![vec![0; n]; n | sugyan | NORMAL | 2021-05-24T02:07:40.327670+00:00 | 2021-05-24T02:07:40.327703+00:00 | 212 | false | ```rust\nimpl Solution {\n pub fn shortest_superstring(words: Vec<String>) -> String {\n let n = words.len();\n let mut graph = vec![vec![0; n]; n];\n for i in 0..n {\n for j in 0..n {\n if i != j {\n let mut k = words[j].len();\n w... | 1 | 0 | ['Rust'] | 0 |
find-the-shortest-superstring | [C++] Greedy merge 2 strings a time. Pick the 'best' pair according to heuristics. | c-greedy-merge-2-strings-a-time-pick-the-q142 | \nclass Solution {\n static bool cmp(vector<int>& a, vector<int>& b) {\n int ascore = a[0]-a[3]-a[4];\n int bscore = b[0]-b[3]-b[4];\n i | llc5pg | NORMAL | 2021-05-23T21:44:31.796421+00:00 | 2021-05-23T21:44:31.796466+00:00 | 247 | false | ```\nclass Solution {\n static bool cmp(vector<int>& a, vector<int>& b) {\n int ascore = a[0]-a[3]-a[4];\n int bscore = b[0]-b[3]-b[4];\n if (ascore>bscore) return true;\n if (ascore<bscore) return false;\n return (a[0]>b[0]);\n }\n void printvv(const vector<vector<int>>& vv)... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | Working one-liner bug | working-one-liner-bug-by-leaderboard-dw4l | I normally don\'t like providing percentage metrics, but this is true speed: 100% time (16 ms) and 97.69% space (14.1 MB). Credits for this goes to @DBabichev ( | leaderboard | NORMAL | 2021-05-23T20:30:57.680143+00:00 | 2021-05-23T20:31:44.087412+00:00 | 206 | false | I normally don\'t like providing percentage metrics, but this is true speed: 100% time (16 ms) and 97.69% space (14.1 MB). Credits for this goes to @DBabichev ([reference post](https://leetcode.com/problems/find-the-shortest-superstring/discuss/1225543/Python-dp-on-subsets-solution-3-solutions-%2B-oneliner-explained)).... | 1 | 0 | [] | 1 |
find-the-shortest-superstring | Rust Dynamic Programming | rust-dynamic-programming-by-michielbaird-01kz | The goal here is to covert the problem to the the Travelling Salesman Problem. We define and edge between 2 words as the amount of characters you need to add to | michielbaird | NORMAL | 2021-05-23T19:48:28.072099+00:00 | 2021-05-23T19:48:28.072146+00:00 | 387 | false | The goal here is to covert the problem to the the Travelling Salesman Problem. We define and edge between 2 words as the amount of characters you need to add to go from word1 to word2. Then we use a bitset to define which words are in the set.\n\n```\nuse std::usize;\n\n\nimpl Solution {\n \n fn recurse(\n ... | 1 | 0 | ['Dynamic Programming', 'Rust'] | 0 |
find-the-shortest-superstring | [c++] deep-first search with memorization | c-deep-first-search-with-memorization-by-xrqk | \nclass Solution {\npublic:\n string shortestSuperstring(vector<string>& words) {\n size_t n = words.size();\n vector<vector<int>> overlap(n, v | zonghao_li | NORMAL | 2021-05-23T18:58:38.321694+00:00 | 2021-05-23T18:58:38.321738+00:00 | 228 | false | ```\nclass Solution {\npublic:\n string shortestSuperstring(vector<string>& words) {\n size_t n = words.size();\n vector<vector<int>> overlap(n, vector<int>(n));\n for (size_t i = 0; i < n; ++i) \n for (size_t j = 0; j < n; ++j)\n update(i, j, words, overlap);\n ... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | 97.60% Time | 88.62% Space | C++ | 9760-time-8862-space-c-by-dibyajyotidhar-so3o | \nclass Solution {\npublic:\n \n string GetOverLapping(string a, string b){\n \n int M=0;\n \n int aplusb=0;\n \n | dibyajyotidhar | NORMAL | 2021-01-11T16:27:23.619084+00:00 | 2021-01-11T16:27:23.619132+00:00 | 355 | false | ```\nclass Solution {\npublic:\n \n string GetOverLapping(string a, string b){\n \n int M=0;\n \n int aplusb=0;\n \n int bplusa=0;\n \n //string ans="";\n \n for(int i=1;i<=min(a.length(),b.length());i++){\n if(a.compare(a.length... | 1 | 2 | [] | 1 |
find-the-shortest-superstring | C++ easy to understand backtracking | c-easy-to-understand-backtracking-by-use-jft5 | \n// Time: O(M! * ML)\n// Algo: Backtracking\n// 50/72 tests passed, TLE\n\nclass Solution {\npublic:\n // M: Number of strigs\n // L : combined length o | user8531d | NORMAL | 2020-12-03T01:17:19.426381+00:00 | 2020-12-03T01:17:19.426412+00:00 | 543 | false | ```\n// Time: O(M! * ML)\n// Algo: Backtracking\n// 50/72 tests passed, TLE\n\nclass Solution {\npublic:\n // M: Number of strigs\n // L : combined length of all strings\n // Time: O(ML)\n string strCompress(vector<string>& svec) {\n string current = svec[0];\n \n // Find if the postfi... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | bla bla bla | bla-bla-bla-by-mrghasita-gy5n | \nWe may assume that no string in A is substring of another string in A.\n\nRead this carefully, or face TLE.\nWhat this means is that, cost of adding current s | mrghasita | NORMAL | 2020-12-02T09:16:13.804855+00:00 | 2020-12-02T09:16:13.804887+00:00 | 291 | false | ```\nWe may assume that no string in A is substring of another string in A.\n```\nRead this carefully, or face TLE.\nWhat this means is that, cost of adding current string only depend on previous string added not whole sequence. i.e previous path doesn\'t matter, only last city matters. | 1 | 1 | [] | 0 |
find-the-shortest-superstring | Python DP solution | python-dp-solution-by-xing_yi-casi | \nclass Solution:\n def shortestSuperstring(self, A: List[str]) -> str:\n \n def overlap_length(a, b):\n for i in range(1, len(a)):\ | xing_yi | NORMAL | 2020-08-08T23:04:57.650402+00:00 | 2020-08-08T23:04:57.650437+00:00 | 387 | false | ```\nclass Solution:\n def shortestSuperstring(self, A: List[str]) -> str:\n \n def overlap_length(a, b):\n for i in range(1, len(a)):\n aa = a[i:]\n ll = len(aa)\n bb = b[:ll]\n if aa == bb:\n return ll\n ... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | [Python] 80ms A* algorithm | python-80ms-a-algorithm-by-tommmyk253-0mhn | Using ordinary TSP DP solution will try out every possible permutation and this is very time-consuming.\nSo I decided to use A algorithm to achieve some pruning | tommmyk253 | NORMAL | 2020-07-11T00:28:12.865670+00:00 | 2020-07-11T00:28:12.865704+00:00 | 362 | false | Using ordinary TSP DP solution will try out every possible permutation and this is very time-consuming.\nSo I decided to use A* algorithm to achieve some pruning.\n\nModel the directed graph:\n1. node: a pair of state and last inserted string\'s index, as (state, last)\n2. edge: represent the number of characters need ... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | (Easy to understand) Python - Recursion + Bitmask cache + overlaps | easy-to-understand-python-recursion-bitm-44wz | There are 3 key ideas in this solution.\n\n1. calculate overlap for each string concatenation, since we don\'t need to merge two full string to make a string co | ecsca | NORMAL | 2020-07-01T02:16:10.248725+00:00 | 2020-07-01T02:17:01.964039+00:00 | 464 | false | There are 3 key ideas in this solution.\n\n1. calculate overlap for each string concatenation, since we don\'t need to merge two full string to make a string contains both as a substring.\n\tex) "abcd", "cde" -> "abcde" contains both as a substring.\n\n2. We are going to use recursion to build every possible cases. Wha... | 1 | 0 | ['Recursion', 'Python'] | 0 |
find-the-shortest-superstring | C++ TSP Solution !!! | c-tsp-solution-by-kylewzk-8wv3 | \n string shortestSuperstring(vector<string>& A) {\n int N = A.size();\n vector<vector<int>> dp(1<<N, vector<int>(N, -1)), parent(1<<N, vector< | kylewzk | NORMAL | 2020-02-29T14:23:53.386305+00:00 | 2020-02-29T14:23:53.386357+00:00 | 421 | false | ```\n string shortestSuperstring(vector<string>& A) {\n int N = A.size();\n vector<vector<int>> dp(1<<N, vector<int>(N, -1)), parent(1<<N, vector<int>(N, -1)) , dist(N, vector<int>(N, 0));\n \n for(int i = 0; i < N; i++) {\n for(int j = 0; j < N; j++) {\n if(i ==... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | Just for whom looking for a C# solution | just-for-whom-looking-for-a-c-solution-b-k6rz | \n\npublic class Solution {\n public string ShortestSuperstring(string[] A) {\n int n = A.Length;\n int[,] graph = new int[n, n];\n \n | flyingingray | NORMAL | 2019-12-14T21:24:17.724772+00:00 | 2019-12-14T21:24:17.724806+00:00 | 182 | false | ```\n\npublic class Solution {\n public string ShortestSuperstring(string[] A) {\n int n = A.Length;\n int[,] graph = new int[n, n];\n \n // build the graph\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n graph[i, j] = calc(A[i], A[j]);\n... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | Python TSP, straightforward DP by using Bellman-Held-Karp algorithm | python-tsp-straightforward-dp-by-using-b-jvzb | Implement a straightforward Bellman\u2013Held\u2013Karp algorithm.\nIf we have A, B, C, D\nall the path contains 4 elements and ends with D can be written as:\n | liketheflower | NORMAL | 2019-11-27T18:28:50.325240+00:00 | 2019-11-27T19:42:18.794870+00:00 | 2,469 | false | Implement a straightforward Bellman\u2013Held\u2013Karp algorithm.\nIf we have A, B, C, D\nall the path contains 4 elements and ends with D can be written as:\n{A,B,C,D} ends with D\nIt can be generated from\n{A,B,C} ends with A + AD\n{A,B,C} ends with B + BD\n{A,B,C} ends with C+ CD\nPick up the path which has the mi... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | Modularized Travelling Salesman Problem Solution | modularized-travelling-salesman-problem-2w5r6 | Learned from Travelling Salesman Problem\n\nclass Solution {\n public String shortestSuperstring(String[] A) {\n int n = A.length;\n int[][] gr | lxhq | NORMAL | 2019-09-05T16:36:56.588499+00:00 | 2019-09-05T16:36:56.588536+00:00 | 1,259 | false | Learned from [Travelling Salesman Problem](https://www.youtube.com/watch?v=cY4HiiFHO1o)\n```\nclass Solution {\n public String shortestSuperstring(String[] A) {\n int n = A.length;\n int[][] graph = new int[n][n];\n for (int i = 0; i < n; i++) {\n for (int j = 0; j < n; j++) {\n ... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | c++ easy to understand with recursion and greedy approach | c-easy-to-understand-with-recursion-and-98iz1 | ```\nclass Solution {\n std::vector res;\npublic:\n string shortestSuperstring(vector& arr) {\n shortestSuperstringH(arr, arr.size());\n int | afflatus | NORMAL | 2019-02-23T17:57:41.376852+00:00 | 2019-02-23T17:57:41.376918+00:00 | 482 | false | ```\nclass Solution {\n std::vector<string> res;\npublic:\n string shortestSuperstring(vector<string>& arr) {\n shortestSuperstringH(arr, arr.size());\n int m = INT_MAX;\n int ind;\n for(int i=0;i<res.size();i++){\n if(res[i].size() < m){\n ind = i;\n m... | 1 | 1 | [] | 1 |
find-the-shortest-superstring | A slow but easy to understand Python solution | a-slow-but-easy-to-understand-python-sol-1l8d | \nfrom functools import lru_cache\nfrom typing import *\n\nclass Solution:\n def shortestSuperstring(self, strings: List[str]) -> str:\n """\n | senmenty | NORMAL | 2019-02-06T22:58:55.661004+00:00 | 2019-02-06T22:58:55.661047+00:00 | 319 | false | ```\nfrom functools import lru_cache\nfrom typing import *\n\nclass Solution:\n def shortestSuperstring(self, strings: List[str]) -> str:\n """\n Dynamic programming\n \n Reduce the problem of find the best concatenation for [1, 2, 3] to:\n - Choose 1 and recurse on [2, 3], and put... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | C++ Bottom up DP + KMP 20ms | c-bottom-up-dp-kmp-20ms-by-firejox-1sf5 | \nclass Solution {\n static int dp[4096][12];\n static int failure[12][20];\n static int cost[12][12];\n static int trace_table[4096][12];\n \npu | firejox | NORMAL | 2019-01-28T10:37:20.381051+00:00 | 2019-01-28T10:37:20.381121+00:00 | 354 | false | ```\nclass Solution {\n static int dp[4096][12];\n static int failure[12][20];\n static int cost[12][12];\n static int trace_table[4096][12];\n \npublic:\n string shortestSuperstring(vector<string>& A) {\n const int sz = A.size();\n const int dp_sz = 1 << sz;\n \n std::fill... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | BFS Solution with explanation | bfs-solution-with-explanation-by-zhassan-jzn6 | The problem is just a slight modification of the Find Shortest Path Visiting All Nodes problem. \nBelow are some thoughts that led me to that solution:\n Findin | zhassanb | NORMAL | 2018-11-19T00:42:10.642979+00:00 | 2018-11-19T00:42:10.643022+00:00 | 479 | false | The problem is just a slight modification of the [Find Shortest Path Visiting All Nodes](https://leetcode.com/problems/shortest-path-visiting-all-nodes/) problem. \nBelow are some thoughts that led me to that solution:\n* Finding the worst, but correct, superstring is easy, just concatenate all the strings. \n* Let\'s ... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | JavaScript Greedy Solution with clean explaination | javascript-greedy-solution-with-clean-ex-fq93 | \nvar shortestSuperstring = function(arr) {\n while (arr.length > 1) {\n let maxCommonLength = 0;\n let maxCommonString = arr[0] + arr[1];\n let maxCo | zidianlyu | NORMAL | 2018-11-18T08:10:45.402898+00:00 | 2018-11-18T08:10:45.402971+00:00 | 370 | false | ```\nvar shortestSuperstring = function(arr) {\n while (arr.length > 1) {\n let maxCommonLength = 0;\n let maxCommonString = arr[0] + arr[1];\n let maxCommonWords = [arr[0], arr[1]];\n for (let i = 0; i < arr.length - 1; i++) {\n for (let j = i + 1; j < arr.length; j++) {\n const {commonLength,... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | Complete search (DFS) with simple prunning | complete-search-dfs-with-simple-prunning-y5fe | Straightforward DFS with the following simple prunning strategy: for every string s we pre-calculate the length of the longest possible overlap between s and an | blackskygg | NORMAL | 2018-11-18T08:09:11.289589+00:00 | 2018-11-18T08:09:11.289633+00:00 | 554 | false | Straightforward DFS with the following simple prunning strategy: for every string `s` we pre-calculate the length of the longest possible overlap between `s` and any other strings. Then for every possible subset `A\'`of A, we could use the sum of the precalculated lengths as an upperbound to the total overlaps that can... | 1 | 0 | [] | 0 |
find-the-shortest-superstring | Simple Java Solution Greedy [Approximation Only, DP is the complete solution] | simple-java-solution-greedy-approximatio-x1ql | ```\nimport java.util.*;\n\nclass Solution {\n private Map.Entry maxOverlap(String a, String b) {\n int len = Math.min(a.length(), b.length());\n int max | mo0000 | NORMAL | 2018-11-18T04:38:18.058681+00:00 | 2018-11-18T04:38:18.058726+00:00 | 639 | false | ```\nimport java.util.*;\n\nclass Solution {\n private Map.Entry<Integer, String> maxOverlap(String a, String b) {\n int len = Math.min(a.length(), b.length());\n int max = -1;\n String s = a + b;\n for (int i = len; i > 0; i--) {\n String right = b.substring(0, i);\n if(a.endsWith(right)) {\n ... | 1 | 1 | [] | 4 |
find-the-shortest-superstring | 943. Find the Shortest Superstring | 943-find-the-shortest-superstring-by-g8x-r8rl | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | G8xd0QPqTy | NORMAL | 2025-01-02T03:39:32.956176+00:00 | 2025-01-02T03:39:32.956176+00:00 | 12 | 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 | ['Python'] | 0 |
find-the-shortest-superstring | Commented bitmasking dp solution beats 99.5% runtime and 90% memory | commented-bitmasking-dp-solution-beats-9-xml9 | ApproachStandard dp approach where states are a bitset representing which indices are included along with the last index and the value is the length of the shor | lovesbumblebees | NORMAL | 2025-01-01T23:53:05.547013+00:00 | 2025-01-01T23:53:05.547013+00:00 | 18 | false | # Approach
Standard dp approach where states are a bitset representing which indices are included along with the last index and the value is the length of the shortest possible string matching the state requirements. We then backtrace the dp to find a minimal sequence of strings that produces the optimal result.
The ... | 0 | 0 | ['Dynamic Programming', 'Bit Manipulation', 'C++'] | 0 |
find-the-shortest-superstring | Dijkstra + Bitmask AC | dijkstra-bitmask-ac-by-kalpit00-ybs1 | If TSP DP is too impossible for anyone to remember, this is a relatively easier approach
It uses Dijkstra with a Custom Node object which stores the index of | kalpit00 | NORMAL | 2024-12-22T23:41:22.961611+00:00 | 2024-12-22T23:41:22.961611+00:00 | 22 | false | If TSP DP is too impossible for anyone to remember, this is a relatively easier approach
It uses Dijkstra with a Custom `Node` object which stores the `index` of the word, a `bitmask` to determine which of the `n` words have been visited/added, a `cost` variable similar to the one used in Official solution and the ac... | 0 | 0 | ['Java'] | 0 |
find-the-shortest-superstring | Travelling Salesman Problem (TSP) | travelling-salesman-problem-tsp-by-brynn-0cfq | 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 | Brynner | NORMAL | 2024-11-14T12:38:53.090266+00:00 | 2024-11-14T12:38:53.090306+00:00 | 29 | 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 | ['Python3'] | 0 |
find-the-shortest-superstring | Bitmasking + DP + KMP | bitmasking-dp-kmp-by-adidala-divishath-r-5xm2 | Intuition\n1) First we need to know which one to pick before what, there 2^n possibilities\n2) Use bit masking so that we will know how many strings are alread | Adidala-Divishath-Reddy | NORMAL | 2024-11-02T14:50:24.389177+00:00 | 2024-11-02T14:50:24.389214+00:00 | 14 | false | # Intuition\n1) First we need to know which one to pick before what, there 2^n possibilities\n2) Use bit masking so that we will know how many strings are already used before we pick current string.\n\n# Approach\n1) First find common substring when we combine two strings into one. which would be suffix of one and pre... | 0 | 0 | ['C++'] | 0 |
find-the-shortest-superstring | C# DP | c-dp-by-everest911119-y7zk | Intuition\nDP\n\n# Approach\ndp[mask][i] := min distance to visit nodes (represented as a binary state s) once and only once and the path ends with node i.\n\n# | everest911119 | NORMAL | 2024-10-31T21:09:24.364408+00:00 | 2024-10-31T21:09:24.364448+00:00 | 4 | false | # Intuition\nDP\n\n# Approach\ndp[mask][i] := min distance to visit nodes (represented as a binary state s) once and only once and the path ends with node i.\n\n# Complexity\n- Time complexity:\nO(n^2*2^n)\n\n- Space complexity:\nO(n*2^n)\n\n# Code\n```csharp []\nusing System.Runtime.CompilerServices;\nusing System.Run... | 0 | 0 | ['Dynamic Programming', 'Bit Manipulation', 'C#'] | 0 |
find-the-shortest-superstring | Python3-459 ms Beats 66.48% | python3-459-ms-beats-6648-by-hassam_472-2fww | 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 | hassam_472 | NORMAL | 2024-10-19T13:06:39.743662+00:00 | 2024-10-19T13:06:39.743693+00:00 | 20 | 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 | ['Python3'] | 0 |
find-the-shortest-superstring | BackTracking+DP | backtrackingdp-by-linda2024-8tch | 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 | linda2024 | NORMAL | 2024-10-12T01:07:49.692409+00:00 | 2024-10-12T01:07:49.692436+00:00 | 4 | 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 | ['C#'] | 0 |
find-the-shortest-superstring | C++ Bitmask DP: maintain choice made at each step of the recursion. | c-bitmask-dp-maintain-choice-made-at-eac-uz56 | \n# Code\ncpp []\nint sufPref[12][12];\nint dp[12][1<<12];\nint choice[12][1<<12];\n\nclass Solution {\n private:\n\n int n;\n \n // length of t | pradyumnaym | NORMAL | 2024-10-09T20:22:39.823913+00:00 | 2024-10-09T20:28:31.538579+00:00 | 10 | false | \n# Code\n```cpp []\nint sufPref[12][12];\nint dp[12][1<<12];\nint choice[12][1<<12];\n\nclass Solution {\n private:\n\n int n;\n \n // length of the permutations\n int f(int last, int mask, vector<string> &words) {\n // if all strings added, no extra length\n if (mask == (1<<n) - 1) re... | 0 | 0 | ['C++'] | 0 |
find-the-shortest-superstring | String Matching || Bitmask || DP || Beats 100% | string-matching-bitmask-dp-beats-100-by-ow0e3 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nBitMask and dp\n\n# Complexity\n- Time complexity:\nO((1<<12)(13))\n\n- S | surajnishad930 | NORMAL | 2024-10-09T10:21:35.514290+00:00 | 2024-10-09T10:21:35.514331+00:00 | 9 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nBitMask and dp\n\n# Complexity\n- Time complexity:\nO((1<<12)*(13))\n\n- Space complexity:\nO((1<<12)*(13))\n\n# Code\n```cpp []\nclass Solution {\npublic:\n int overlap[13][13];\n int Max_overlap(string &a,string &b){... | 0 | 0 | ['C++'] | 0 |
patching-array | Solution + explanation | solution-explanation-by-stefanpochmann-89bf | Solution\n\n int minPatches(vector& nums, int n) {\n long miss = 1, added = 0, i = 0;\n while (miss <= n) {\n if (i < nums.size() && | stefanpochmann | NORMAL | 2016-01-27T05:07:57+00:00 | 2018-10-27T00:57:14.137408+00:00 | 64,164 | false | **Solution**\n\n int minPatches(vector<int>& nums, int n) {\n long miss = 1, added = 0, i = 0;\n while (miss <= n) {\n if (i < nums.size() && nums[i] <= miss) {\n miss += nums[i++];\n } else {\n miss += miss;\n added++;\n }\n... | 1,111 | 6 | [] | 99 |
patching-array | 🔥 🔥 🔥 💯 Easy to understand | Greedy Approach | Detailed Explanation 🔥 🔥 🔥 | easy-to-understand-greedy-approach-detai-ddek | To look into solutions to other problems visit my leetcode profle\n\n# Intuition\n\n- The code works like providing change with limited coin denominations. Supp | bhanu_bhakta | NORMAL | 2024-06-16T00:20:24.821547+00:00 | 2024-06-16T01:53:16.220375+00:00 | 41,216 | false | To look into solutions to other problems visit my [leetcode profle](https://leetcode.com/u/bhanu_bhakta/)\n\n# Intuition\n\n- The code works like providing change with limited coin denominations. Suppose you need to cover every amount up to \uD835\uDC5B cents. If you can\'t make exact change for a particular amount mis... | 251 | 2 | ['C++', 'Java', 'Go', 'Python3', 'Kotlin', 'JavaScript'] | 18 |
patching-array | C++, 8ms, greedy solution with explanation | c-8ms-greedy-solution-with-explanation-b-ht04 | show the algorithm with an example,\n\nlet nums=[1 2 5 6 20], n = 50.\n\nInitial value: with 0 nums, we can only get 0 maximumly.\n\nThen we need to get 1, sinc | dragonpw | NORMAL | 2016-05-14T17:47:35+00:00 | 2018-10-13T02:35:01.199312+00:00 | 13,355 | false | show the algorithm with an example,\n\nlet nums=[1 2 5 6 20], n = 50.\n\nInitial value: with 0 nums, we can only get 0 maximumly.\n\nThen we need to get 1, since nums[0]=1, then we can get 1 using [1]. now the maximum number we can get is 1. (actually, we can get all number no greater than the maximum number)\n\n nu... | 160 | 0 | [] | 16 |
patching-array | ✅LeetCode Hard in 6 mins 💯Beats 100% - Explained with [ Video ] - C++/Java/Python/JS - Arrays | leetcode-hard-in-6-mins-beats-100-explai-ld15 | \n\n\n\n# YouTube Video Explanation:\n\n **If you want a video for this question please write in the comments** \n\n https://www.youtube.com/watch?v=ujU-jeO1v-k | lancertech6 | NORMAL | 2024-06-16T04:51:05.832180+00:00 | 2024-06-16T09:39:10.488614+00:00 | 10,892 | false | \n\n\n\n# YouTube Video Explanation:\n\n<!-- **If you want a video for this question please write in the comments** -->\n\n<!-- https://www.youtube.com/watch?v=ujU-jeO1v-k -->\n\nhttp... | 91 | 1 | ['Array', 'Binary Search', 'Greedy', 'Python', 'C++', 'Java', 'JavaScript'] | 5 |
patching-array | [Python] 2 solutions: merge intervals + greedy, explained | python-2-solutions-merge-intervals-greed-mm2l | Solution 1\nLet as keep all possible numbers we can get as list of intervals, for example 0, 1, 2, 4, 5, 12 is [[0, 2], [4, 5], [12, 12]]. Then when we add new | dbabichev | NORMAL | 2021-08-29T08:09:58.574034+00:00 | 2021-08-29T08:09:58.574069+00:00 | 3,936 | false | #### Solution 1\nLet as keep all possible numbers we can get as list of intervals, for example `0, 1, 2, 4, 5, 12` is `[[0, 2], [4, 5], [12, 12]]`. Then when we add new number we can merge our intervals, using idea of Problem **0056**. What numbers we need to add next? We need to add number `3` in our example, the sma... | 79 | 5 | ['Greedy'] | 9 |
patching-array | Python O(n) with detailed explanation | python-on-with-detailed-explanation-by-y-2jlo | Initialize an empty list, keep adding new numbers from provided nums into this list, keep updating the coverage range and ensure a continus coverage range. If y | yuanzhi247012 | NORMAL | 2019-07-19T04:44:05.512209+00:00 | 2020-02-06T08:03:53.530327+00:00 | 2,667 | false | Initialize an empty list, keep adding new numbers from provided nums into this list, keep updating the coverage range and ensure a continus coverage range. If you do so, you only need to care about whether the newly added number will break the coverage range or not.\n\nSuppose 1~10 is already covered during this proces... | 57 | 0 | [] | 10 |
patching-array | 详细解释/Detailed Explanation with Example | xiang-xi-jie-shi-detailed-explanation-wi-i9h8 | \u4F8B\u5B50\uFF08Example\uFF09\n\n[1,2,3,5,10,50,70], n=100\n1. Seeing 1, we know [1,1] can be covered\n2. Seeing 2, we know [1,3] can be covered\n3. Similarly | lishichengyan | NORMAL | 2019-04-23T06:41:22.012744+00:00 | 2019-04-23T06:41:22.012815+00:00 | 2,293 | false | **\u4F8B\u5B50\uFF08Example\uFF09**\n\n[1,2,3,5,10,50,70], n=100\n1. Seeing 1, we know [1,1] can be covered\n2. Seeing 2, we know [1,3] can be covered\n3. Similarly for 3, [1,6] can be covered\n4. for 5, [1,11] can be covered\n5. for 10, [1, 21] can be covered\n6. for 50, however, we have to add a patch, if the patch i... | 42 | 0 | [] | 9 |
patching-array | ✅✅Fastest 💯💯 Efficient 💎💎Simplest Solution 🏃♂️🏃♂️DryRun🔥🔥 | fastest-efficient-simplest-solution-dryr-9ia0 | Thanks for checking out my solution.This post has been made with ❤ by Alok KhansaliDo Upvote if this helped 👍🎯Approach : Greedy 🤑Intuition 🔮Leetcode walo ne wee | TheCodeAlpha | NORMAL | 2024-06-16T08:00:45.565747+00:00 | 2025-01-07T16:58:50.074086+00:00 | 2,267 | false | #### Thanks for checking out my solution.
#### This post has been made with ❤ by [Alok Khansali](https://leetcode.com/u/TheCodeAlpha/)
### Do Upvote if this helped 👍
# 🎯Approach : Greedy 🤑
<!-- Describe your approach to solving the problem. -->
# Intuition 🔮
<!-- Describe your first thoughts on how to solve this ... | 39 | 0 | ['Array', 'Math', 'Greedy', 'C++', 'Java', 'Python3'] | 4 |
patching-array | C++ Simple and Easy Explained Solution, 7-Short-Lines | c-simple-and-easy-explained-solution-7-s-3x0q | Idea:\nEvery time count reaches a number that the next element in nums is greater than it, we need a patch.\nIf we add the number itself, count can be doubled b | yehudisk | NORMAL | 2021-08-29T12:10:35.212338+00:00 | 2021-08-29T12:10:35.212390+00:00 | 2,653 | false | **Idea:**\nEvery time `count` reaches a number that the next element in `nums` is greater than it, we need a patch.\nIf we add the number itself, `count` can be doubled because we can add the new number to any of the previous numbers.\nSo if `count` = 7, and the next number in `nums` is 10, if we add 7 to `nums` now we... | 37 | 2 | ['C'] | 5 |
patching-array | Rigorous Mathematical Proof | No Lucky Guess/Intuition | rigorous-mathematical-proof-no-lucky-gue-dtnt | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n\n# Algorithm (with explaination on an example)\n\nAt any point, if the n | prabhatjha26 | NORMAL | 2024-06-16T10:40:20.425486+00:00 | 2024-06-17T08:12:26.697671+00:00 | 840 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\n# Algorithm (with explaination on an example)\n\nAt any point, if the next item in the array is greater than (sum of all previous elements plus 1), then add (sum of all previous elements plus 1)\nin the array.\n\nInitializ... | 35 | 0 | ['Python'] | 3 |
patching-array | ✔✔ Patching Array. Simple Solution with Explanation | patching-array-simple-solution-with-expl-ljkb | \nint minPatches(vector<int>& nums, int n) {\n long miss = 1, added = 0, i = 0;\n while (miss <= n) {\n if (i < nums.size() && nums[i] <= miss) {\n | inomag | NORMAL | 2021-08-29T07:16:08.779881+00:00 | 2021-08-29T07:21:10.782490+00:00 | 1,679 | false | ```\nint minPatches(vector<int>& nums, int n) {\n long miss = 1, added = 0, i = 0;\n while (miss <= n) {\n if (i < nums.size() && nums[i] <= miss) {\n miss += nums[i++];\n } else {\n miss += miss;\n added++;\n }\n }\n return added;\n}\n```\n\nLet **miss*... | 29 | 10 | [] | 6 |
patching-array | Share my greedy solution by Java with simple explanation (time: 1 ms) | share-my-greedy-solution-by-java-with-si-u7uo | public static int minPatches(int[] nums, int n) {\n\t\tlong max = 0;\n\t\tint cnt = 0;\n\t\tfor (int i = 0; max < n;) {\n\t\t\tif (i >= nums.length || max < num | liqiwei | NORMAL | 2016-01-27T09:53:11+00:00 | 2018-10-02T05:40:35.766686+00:00 | 6,917 | false | public static int minPatches(int[] nums, int n) {\n\t\tlong max = 0;\n\t\tint cnt = 0;\n\t\tfor (int i = 0; max < n;) {\n\t\t\tif (i >= nums.length || max < nums[i] - 1) {\n\t\t\t\tmax += max + 1;\n\t\t\t\tcnt++;\n\t\t\t} else {\n\t\t\t\tmax += nums[i];\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t\treturn cnt;\n\t}\n\nThe var... | 27 | 1 | [] | 7 |
patching-array | 100% Beats | Easy to Understand | Detailed Step by Step Explaination | Greedy Approach | 100-beats-easy-to-understand-detailed-st-hdw9 | Problem Statement\nWe are given a sorted integer array nums and an integer n. Our task is to determine the minimum number of patches required to ensure that any | tanishqsingh | NORMAL | 2024-06-16T03:30:37.806551+00:00 | 2024-06-16T04:20:43.666166+00:00 | 4,303 | false | # Problem Statement\nWe are given a sorted integer array `nums` and an integer `n`. Our task is to determine the minimum number of patches required to ensure that any number in the range `[1, n]` can be formed by summing some elements from the array `nums`. Each element in `nums` can only be used once.\n\n# Highly Opti... | 17 | 0 | ['Greedy', 'C++', 'Java', 'Python3', 'JavaScript'] | 3 |
patching-array | A concrete example to work down the algorithm | a-concrete-example-to-work-down-the-algo-321o | \nThink it reversely: the maximum value we can form based on a given set of numsers?\nif for a give set [1, ..., k], and anything less or equal to k is already | winkee | NORMAL | 2020-06-12T12:19:11.063444+00:00 | 2020-06-12T12:28:54.994359+00:00 | 825 | false | \nThink it reversely: the maximum value we can form based on a given set of numsers?\nif for a give set [1, ..., k], and anything less or equal to k is already covered. then anything [k+1 .... k + k) must also be covered! In another word, if we have k and all value before k is covered, then the later part [k, 2*k) is a... | 17 | 0 | [] | 1 |
patching-array | 💯✅🔥Easy Java ,Python3 ,C++ Solution|| 0 ms ||≧◠‿◠≦✌ | easy-java-python3-c-solution-0-ms-_-by-s-urpt | Intuition\n Describe your first thoughts on how to solve this problem. \nThe idea behind this solution is to keep track of the largest number that can be repres | suyalneeraj09 | NORMAL | 2024-06-16T02:01:02.894764+00:00 | 2024-06-16T02:01:02.894789+00:00 | 2,327 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea behind this solution is to keep track of the largest number that can be represented using the given numbers and the patches added so far. We start with the first number in the nums array, and if it is greater than the current lar... | 14 | 0 | ['Array', 'C++', 'Java', 'Python3'] | 4 |
patching-array | My simple accepted C++ solution | my-simple-accepted-c-solution-by-violinv-2vqi | Idea: 1. Check the content if the current one is within sum +1, which is the total sum of all previous existing numbers. If yes, we proceed and update sum. If n | violinviolin | NORMAL | 2016-02-03T06:48:41+00:00 | 2016-02-03T06:48:41+00:00 | 3,853 | false | Idea: 1. Check the content if the current one is within sum +1, which is the total sum of all previous existing numbers. If yes, we proceed and update sum. If not, we patch one number that is within sum + 1. \n2. Keep updating the sum until it reaches n.\n \n\n\n\n\n\n int minPatches(vector<int>& nums, int n) {\n... | 14 | 0 | ['C++'] | 4 |
patching-array | Simple intuitive and well-explained solution accepted as best in C | simple-intuitive-and-well-explained-solu-d6rc | Before we hack this, we should be generous and think nothing about performance and try to come up with a sub-problem of it and then boot it from the beginning p | lhearen | NORMAL | 2016-02-22T11:39:36+00:00 | 2016-02-22T11:39:36+00:00 | 3,882 | false | Before we hack this, we should be generous and think nothing about performance and try to come up with a sub-problem of it and then boot it from the beginning point.\n\nSo before we truly get started, let's suppose we are in a state where we can reach <font color="#ff0000">**top**</font> by its sub-array nums[0...i] th... | 12 | 2 | ['Iterator'] | 7 |
patching-array | Beats 98% users.. efficient and easy to understand solution 🆒 | beats-98-users-efficient-and-easy-to-und-i27v | 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 | Aim_High_212 | NORMAL | 2024-06-16T03:21:52.417711+00:00 | 2024-06-16T03:21:52.417744+00:00 | 64 | 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)$$ --... | 11 | 0 | ['C', 'Python', 'C++', 'Java', 'Python3'] | 0 |
patching-array | [Greedy + Formal Proof + Tutorial] Patching Array | greedy-formal-proof-tutorial-patching-ar-agth | Topic : Greedy\nGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. | never_get_piped | NORMAL | 2024-06-16T01:41:30.228032+00:00 | 2024-06-21T04:51:12.483256+00:00 | 1,533 | false | **Topic** : Greedy\nGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. In these algorithms, decisions are made based on the information available at the current moment without considering the consequences of these decisions in ... | 11 | 0 | ['C', 'PHP', 'Python', 'Java', 'Go', 'JavaScript'] | 7 |
patching-array | Simple 9-line Python Solution | simple-9-line-python-solution-by-myliu-kgi9 | class Solution(object):\n def minPatches(self, nums, n):\n """\n :type nums: List[int]\n :type n: int\n :rtyp | myliu | NORMAL | 2016-04-18T06:21:29+00:00 | 2016-04-18T06:21:29+00:00 | 1,835 | false | class Solution(object):\n def minPatches(self, nums, n):\n """\n :type nums: List[int]\n :type n: int\n :rtype: int\n """\n miss, i, added = 1, 0, 0\n while miss <= n:\n if i < len(nums) and nums[i] <= miss:\n ... | 10 | 2 | ['Python'] | 2 |
patching-array | Python | Easy | python-easy-by-khosiyat-s12f | see the Successfully Accepted Submission\n\n# Code\n\nclass Solution(object):\n def minPatches(self, nums, n):\n ans = 0\n sum_val = 1\n | Khosiyat | NORMAL | 2024-06-16T05:12:04.634820+00:00 | 2024-06-16T05:12:04.634854+00:00 | 423 | false | [see the Successfully Accepted Submission](https://leetcode.com/problems/patching-array/submissions/1289786216/?envType=daily-question&envId=2024-06-16)\n\n# Code\n```\nclass Solution(object):\n def minPatches(self, nums, n):\n ans = 0\n sum_val = 1\n m = len(nums)\n i = 0\n\n whil... | 9 | 0 | ['Python3'] | 1 |
patching-array | NOT so hard || Easy to understand || Beginner friendly code || Python 3 | not-so-hard-easy-to-understand-beginner-avk59 | Intuition\n\nThe goal is to ensure that we can form all integers from 1 to n using a given array of positive integers (nums). If certain numbers are missing in | Saksham_chaudhary_2002 | NORMAL | 2024-06-16T01:05:08.810594+00:00 | 2024-06-16T01:05:08.810613+00:00 | 1,262 | false | # Intuition\n\nThe goal is to ensure that we can form all integers from 1 to n using a given array of positive integers (nums). If certain numbers are missing in the array to form the desired range, we need to determine the minimum number of additional integers (patches) required. The key insight here is that to cover ... | 9 | 1 | ['Array', 'Greedy', 'Python3'] | 5 |
patching-array | Simple C++ 12ms easy understanding O(n) | simple-c-12ms-easy-understanding-on-by-a-fl5u | class Solution {\n public:\n int minPatches(vector<int>& nums, int n) {\n if (n == 0) return 0;\n int num = nums.size();\n | algoguruz | NORMAL | 2016-01-27T17:12:05+00:00 | 2016-01-27T17:12:05+00:00 | 1,733 | false | class Solution {\n public:\n int minPatches(vector<int>& nums, int n) {\n if (n == 0) return 0;\n int num = nums.size();\n long reach = 0;\n int patch = 0;\n for (int i = 0; i < num; ){\n while (nums[i] > reach + 1){\n ... | 8 | 1 | [] | 1 |
patching-array | 1ms Java solution with explain | 1ms-java-solution-with-explain-by-codepl-2lne | public int minPatches(int[] nums, int n) {\n int index = 0;\n int addedCount = 0;\n long canReachTo = 0;\n while( canReachTo < n){\n | codeplexer | NORMAL | 2016-04-09T04:44:02+00:00 | 2016-04-09T04:44:02+00:00 | 2,915 | false | public int minPatches(int[] nums, int n) {\n int index = 0;\n int addedCount = 0;\n long canReachTo = 0;\n while( canReachTo < n){\n if( nums.length > index){\n int nextExisting = nums[index];\n if(nextExisting == canReachTo + 1){\n ... | 8 | 0 | [] | 4 |
patching-array | beats 100% | beats-100-by-vigneshreddy06-cyk5 | 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 | vigneshreddy06 | NORMAL | 2024-06-16T11:25:02.196442+00:00 | 2024-06-16T11:25:02.196468+00:00 | 48 | 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)$$ --... | 7 | 0 | ['C++', 'Python3'] | 0 |
patching-array | Easy to understand C++ solution beats 100% | easy-to-understand-c-solution-beats-100-vdb2c | Intuition\n Describe your first thoughts on how to solve this problem. \nIf the current sum < nums[i]-1, then we cannot create nums[i] using the sum, we need to | anuragkumar2608 | NORMAL | 2024-06-16T00:32:47.199359+00:00 | 2024-06-16T07:03:09.532173+00:00 | 1,129 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIf the current sum < nums[i]-1, then we cannot create nums[i] using the sum, we need to add sum+1 to the array,so next the sum becomes sum += sum + 1.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nEach time while a... | 7 | 0 | ['C++'] | 3 |
patching-array | Python solution faster than 97% with explanation | python-solution-faster-than-97-with-expl-vfs2 | \n you just need to figure out one thing:\n if you can sum up from 1 to 10, what happen if you have another number\n suppose this number is 2, then you can sum | flyingspa | NORMAL | 2021-05-20T14:12:36.353830+00:00 | 2021-05-20T14:14:22.299924+00:00 | 579 | false | \n* you just need to figure out one thing:\n* if you can sum up from 1 to 10, what happen if you have another number\n* suppose this number is 2, then you can sum up from 1 to 12\n* suppose this number is 11, then you can sum up from 1 to 21 \n* suppose this number is 12, then you can\'t get 11 and the consecutive sum ... | 7 | 0 | ['Python'] | 2 |
patching-array | [recommend for beginners]clean C++ implementation with detailed explanation | recommend-for-beginnersclean-c-implement-8zmr | we run the code on an example to illustrate the ideas:\n \n [1, miss) : the right miss is open field\n\n nums=[1,5,10] n=20\n\n in | rainbowsecret | NORMAL | 2016-02-04T01:49:53+00:00 | 2016-02-04T01:49:53+00:00 | 1,182 | false | we run the code on an example to illustrate the ideas:\n \n [1, miss) : the right miss is open field\n\n nums=[1,5,10] n=20\n\n initialize state : miss=1 i=0 size=3\n\nmiss=1 : i=0\n\n if find [number<=1] in nums { i++ } else add 1 to nums update miss+=1 [1,2)\... | 7 | 3 | [] | 2 |
patching-array | Java Solution, Beats 100.00% | java-solution-beats-10000-by-mohit-005-lz8y | Intuition \n\nThe problem requires adding the minimum number of patches (elements) to an array such that any number from 1 to n can be formed by the sum of some | Mohit-005 | NORMAL | 2024-06-16T02:43:22.704387+00:00 | 2024-06-16T02:43:22.704420+00:00 | 476 | false | # Intuition \n\nThe problem requires adding the minimum number of patches (elements) to an array such that any number from 1 to `n` can be formed by the sum of some elements in the array. The key is to iteratively ensure that every integer up to `n` can be constructed using the existing elements and the patches added s... | 6 | 0 | ['Java'] | 1 |
patching-array | ✅Simple and Easy Solution🔥 | simple-and-easy-solution-by-kg-profile-941u | Code\n\nclass Solution:\n def minPatches(self, nums: List[int], n: int) -> int:\n p = 0\n x = 1\n i = 0\n \n while x <= n: | KG-Profile | NORMAL | 2024-06-16T01:28:51.942305+00:00 | 2024-06-16T01:28:51.942322+00:00 | 35 | false | # Code\n```\nclass Solution:\n def minPatches(self, nums: List[int], n: int) -> int:\n p = 0\n x = 1\n i = 0\n \n while x <= n:\n if i < len(nums) and nums[i] <= x:\n x += nums[i]\n i += 1\n else:\n p += 1\n ... | 6 | 0 | ['Python3'] | 0 |
patching-array | Simple Easy to Understand Java Solution || Faster than 100% | simple-easy-to-understand-java-solution-go0ci | Logic\n\nIterating the nums[], and keeps adding them up, and we are getting a running sum. At any position, if nums[i] > sum+1, them we are sure we have to patc | rohitkumarsingh369 | NORMAL | 2021-08-29T16:17:57.453194+00:00 | 2021-08-29T16:20:02.049375+00:00 | 1,099 | false | **Logic**\n```\nIterating the nums[], and keeps adding them up, and we are getting a running sum. At any position, if nums[i] > sum+1, them we are sure we have to patch \na sum+1 because all nums before index i can\'t make sum+1 even add all of them up, and all nums after index i are all simply too large.\n```\n\n**Sol... | 6 | 0 | ['Java'] | 1 |
patching-array | [C++ | Python3 | JAVA] Easy Approach with simple solution | c-python3-java-easy-approach-with-simple-twi5 | APPROACH\n Let us assume that we are on some ith element of the array.\n We were successfullly able to create each and every number till the range arr[i]-1 by t | Maango16 | NORMAL | 2021-08-29T11:17:58.618318+00:00 | 2021-08-29T11:17:58.618359+00:00 | 502 | false | **APPROACH**\n* Let us assume that we are on some `i`th element of the array.\n* We were successfullly able to create each and every number till the range `arr[i]-1` by taking some numbers from arr in the range `0` to `i-1`. \n* Hence after choosing `arr[i]` we can create numbers from `0` to `arr[i]-1 + arr[i]` . \n\t*... | 6 | 2 | [] | 0 |
patching-array | *Java* here is my greedy version with brief explanations (1ms) | java-here-is-my-greedy-version-with-brie-cc91 | Greedy idea: add the maximum possible element whenever there is a gap\n\n public int minPatches(int[] nums, int n) {\n int count = 0;\n long pr | elementnotfoundexception | NORMAL | 2016-01-27T05:44:26+00:00 | 2016-01-27T05:44:26+00:00 | 2,070 | false | Greedy idea: add the maximum possible element whenever there is a gap\n\n public int minPatches(int[] nums, int n) {\n int count = 0;\n long priorSum = 0; // sum of elements prior to current index\n for(int i=0; i<nums.length; i++) {\n \tif(priorSum>=n) return count; // done\n \twh... | 6 | 0 | ['Java'] | 1 |
patching-array | Greedy solution in Python | greedy-solution-in-python-by-gavincode-88e5 | I used a greedy algorithm. When traversing through the given number list, consider each number as a goal and resource. When in the for loop for the ith number, | gavincode | NORMAL | 2016-01-27T07:10:22+00:00 | 2016-01-27T07:10:22+00:00 | 1,063 | false | I used a greedy algorithm. When traversing through the given number list, consider each number as a **goal** and **resource**. When in the for loop for the *ith* number, try to add some numbers so that you can represent every number in the range [ 1, nums[i] ). Then, add the *ith* number to your source for further loop... | 6 | 1 | [] | 0 |
patching-array | Patching Array - Solution Explanation and Code (Video Solution Available) | patching-array-solution-explanation-and-hpndr | Video SolutionIntuitionTo cover all numbers in the range [1, n] using the given array nums, we need to ensure that every integer in the range can be formed as t | CodeCrack7 | NORMAL | 2025-01-18T01:34:05.755715+00:00 | 2025-01-18T01:34:05.755715+00:00 | 74 | false | # Video Solution
[https://youtu.be/3XvfM0fOnjc?si=hz58mAXHPF62vVxq]()
# Intuition
To cover all numbers in the range `[1, n]` using the given array `nums`, we need to ensure that every integer in the range can be formed as the sum of one or more elements from `nums`. If `nums` lacks certain values necessary to achieve t... | 5 | 0 | ['Java'] | 0 |
patching-array | ✅5 lines only , Fastest Greedy Efficient Simplest Solution with DryRun in C++ | 5-lines-only-fastest-greedy-efficient-si-di9c | Intuition\n Describe your first thoughts on how to solve this problem. \n> First of all, we have to understand what we want.\n\nWe want that nums must have the | sidharthjain321 | NORMAL | 2024-06-16T21:56:29.223810+00:00 | 2024-06-16T22:22:11.258128+00:00 | 121 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n> First of all, we have to understand what we want.\n\nWe want that `nums` must have the required elements, so that there combinations of numbers will sum upto n, i.e., from `[1,n]` both inclusive.\n**At every step we will check , for eac... | 5 | 0 | ['Array', 'Greedy', 'Sorting', 'C++'] | 2 |
patching-array | 💯JAVA Solution Explained in HINDI | java-solution-explained-in-hindi-by-the_-cff7 | https://youtu.be/Qk1elv8QGj0\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote | The_elite | NORMAL | 2024-06-16T13:23:31.906410+00:00 | 2024-06-16T13:23:31.906524+00:00 | 301 | false | https://youtu.be/Qk1elv8QGj0\n\nFor explanation, please watch the above video and do like, share and subscribe the channel. \u2764\uFE0F Also, please do upvote the solution if you liked it.\n\n# Subscribe:- [ReelCoding](https://www.youtube.com/@reelcoding?sub_confirmation=1)\n\nSubscribe Goal:- 500\nCurrent Subscriber:... | 5 | 0 | ['Java'] | 0 |
patching-array | Very Easy C++ Solution with Video Explanation | very-easy-c-solution-with-video-explanat-65ym | Video Explanation\nhttps://youtu.be/YPY6EOfYANY?si=6NQWVso6wi1OiFtU\n# Complexity\n- Time complexity:O(nums.size())\n Add your time complexity here, e.g. O(n) \ | prajaktakap00r | NORMAL | 2024-06-16T04:26:55.934462+00:00 | 2024-06-16T04:26:55.934481+00:00 | 854 | false | # Video Explanation\nhttps://youtu.be/YPY6EOfYANY?si=6NQWVso6wi1OiFtU\n# Complexity\n- Time complexity:$$O(nums.size())$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int minPa... | 5 | 0 | ['C++'] | 2 |
patching-array | 💯✅🔥Easy Java ,Python3 ,C++ Solution|| 0 ms ||≧◠‿◠≦✌ | easy-java-python3-c-solution-0-ms-_-by-s-0ocj | Intuition\n Describe your first thoughts on how to solve this problem. \nThe idea behind this solution is to keep track of the largest number that can be repres | suyalneeraj09 | NORMAL | 2024-06-16T00:58:53.434035+00:00 | 2024-06-16T01:58:39.374160+00:00 | 160 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea behind this solution is to keep track of the largest number that can be represented using the given numbers and the patches added so far. We start with the first number in the nums array, and if it is greater than the current lar... | 5 | 0 | ['Array', 'C++', 'Java', 'Python3'] | 2 |
patching-array | Python || Math | python-math-by-in_sidious-6c8w | Intuition\nIf sum of all the numbers considered till now is x, we can form all the numbers from 1 to x. This condition is True every time.\n\n# Approach\nKeep a | iN_siDious | NORMAL | 2023-02-05T18:03:01.873190+00:00 | 2023-02-05T18:11:31.213254+00:00 | 537 | false | # Intuition\nIf sum of all the numbers considered till now is x, we can form all the numbers from 1 to x. This condition is True every time.\n\n# Approach\nKeep a variable limit which tracks till what max value we can form which is nothing but sum of all values considered till now.\nIf we encounter any number i.e. num ... | 5 | 0 | ['Math', 'Python', 'Python3'] | 1 |
patching-array | C++ 4ms 98% greedy solution w/ explanation | c-4ms-98-greedy-solution-w-explanation-b-uv5k | The idea is to greedily add the maximum missing number, and the numbers from nums once we can reach those numbers.\n\nWhy this works\n\nAssume we have nums = [1 | guccigang | NORMAL | 2019-08-21T00:26:46.038193+00:00 | 2020-01-27T19:35:20.495473+00:00 | 457 | false | The idea is to greedily add the maximum missing number, and the numbers from ```nums``` once we can reach those numbers.\n\n**Why this works**\n\nAssume we have ```nums = [1, 5, 10]``` and we want all numbers to 20. To start thing off, we need to look for a 1. We have a 1 in the array, so are we good. Then we look for ... | 5 | 0 | [] | 2 |
patching-array | 4ms C++ Greedy solution with explanation(极简代码+详细中文注释) | 4ms-c-greedy-solution-with-explanationji-jhza | \n/*\n\u9996\u5148\u53EF\u4EE5\u786E\u5B9A\u7684\u662F\uFF0C\nnums\u4E2D\u5FC5\u7136\u5305\u542B1\uFF0C\u5982\u679C\u4E0D\u5305\u542B1\uFF0C\u90A3\u4E48[1,n]\u8 | leetcoderchen | NORMAL | 2018-11-21T13:51:57.748816+00:00 | 2018-11-21T13:51:57.748856+00:00 | 831 | false | ```\n/*\n\u9996\u5148\u53EF\u4EE5\u786E\u5B9A\u7684\u662F\uFF0C\nnums\u4E2D\u5FC5\u7136\u5305\u542B1\uFF0C\u5982\u679C\u4E0D\u5305\u542B1\uFF0C\u90A3\u4E48[1,n]\u8FD9\u4E2A\u8303\u56F4\u4E2D\u76841\u5C31\u6CA1\u6CD5\u5B9E\u73B0\n\u5176\u6B21\u6570\u7EC4\u4E2D\u7684\u5143\u7D20\u4E0D\u80FD\u91CD\u590D\u4F7F\u7528\uFF0C\... | 5 | 0 | [] | 3 |
patching-array | Share my simple Java code | share-my-simple-java-code-by-mach7-ny9s | public class Solution {\n public int minPatches(int[] nums, int n) {\n int count = 0, i = 0;\n for (long covered=0; covered < n; ) | mach7 | NORMAL | 2016-02-03T08:57:25+00:00 | 2016-02-03T08:57:25+00:00 | 1,707 | false | public class Solution {\n public int minPatches(int[] nums, int n) {\n int count = 0, i = 0;\n for (long covered=0; covered < n; ) {\n if ((i<nums.length && nums[i]>covered+1) || i>=nums.length) { // at this moment, we need (covered+1), patch it.\n cov... | 5 | 0 | [] | 2 |
patching-array | C# Solution for Patching Array Problem | c-solution-for-patching-array-problem-by-wybg | Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind this solution is that we aim to cover the range of sums from 1 to | Aman_Raj_Sinha | NORMAL | 2024-06-16T12:28:02.586540+00:00 | 2024-06-16T12:28:02.586572+00:00 | 173 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this solution is that we aim to cover the range of sums from 1 to n using the elements in nums and additional patches if necessary. However, this solution uses a dynamic approach to iteratively extend the maximum sum ... | 4 | 0 | ['C#'] | 1 |
patching-array | BEATS 100% users with java | simple approach | beats-100-users-with-java-simple-approac-gjwt | \n\n\n# Approach\n Describe your approach to solving the problem. \n- The algorithm uses a while loop to iterate from 1 to n (inclusive) and checks if the curre | sukritisinha0717 | NORMAL | 2024-06-16T06:17:48.239514+00:00 | 2024-06-16T06:17:48.239552+00:00 | 58 | false | \n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- The algorithm uses a while loop to iterate from 1 to n (inclusive) and checks if the current miss value can be obtained using the... | 4 | 0 | ['Java'] | 1 |
patching-array | ✅ Easy C++ Solution | easy-c-solution-by-moheat-y510 | Code\n\nclass Solution {\npublic:\n int minPatches(vector<int>& nums, int n) {\n long long int sum = 0;\n int cnt = 0;\n int i = 0;\n | moheat | NORMAL | 2024-06-16T00:35:40.279309+00:00 | 2024-06-16T00:35:40.279327+00:00 | 613 | false | # Code\n```\nclass Solution {\npublic:\n int minPatches(vector<int>& nums, int n) {\n long long int sum = 0;\n int cnt = 0;\n int i = 0;\n while(sum < n)\n {\n if(i<nums.size() && nums[i] <= sum+1)\n {\n sum = sum + nums[i++];\n }\n ... | 4 | 0 | ['C++'] | 2 |
patching-array | 330: Solution with step by step explanation | 330-solution-with-step-by-step-explanati-roue | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n\nTo solve this problem, we can use a greedy approach. We start with a va | Marlen09 | NORMAL | 2023-03-01T05:44:13.323724+00:00 | 2023-03-01T05:44:13.323774+00:00 | 1,320 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n\nTo solve this problem, we can use a greedy approach. We start with a variable miss that represents the smallest number that cannot be formed by summing up any combination of the numbers in the array. Initially, miss is set... | 4 | 0 | ['Array', 'Greedy', 'Python', 'Python3'] | 3 |
patching-array | ✅C++ || Explained in Hinglish || Easy | c-explained-in-hinglish-easy-by-mayank88-yiq3 | \nclass Solution {\npublic:\n // Approach -\n // Ham kya karege ki ek reach variable lenge\n // Reach hame bataega ki kis number tak ham pohoch sakt | mayank888k | NORMAL | 2022-03-12T19:50:38.405919+00:00 | 2022-03-12T19:50:38.405948+00:00 | 461 | false | ```\nclass Solution {\npublic:\n // Approach -\n // Ham kya karege ki ek reach variable lenge\n // Reach hame bataega ki kis number tak ham pohoch sakte he jo hamare pass number he unse\n // Ham kya krege ki jab tak reach < n tab tak lopp chalaege\n \n // Agar reach < nums[i] to matlab abhi ham n... | 4 | 0 | ['Greedy', 'C'] | 2 |
patching-array | Concise Java Solution Faster than 100% with in depth explanation | concise-java-solution-faster-than-100-wi-c6y5 | Explaination:\nInput: [1, 5, 10]\n\nInitially, our reach is 0. Let\'s start the process of adding elts step by step:\n1) Adding 1st elt, doesn\'t make us miss e | 1_mohit_1 | NORMAL | 2021-09-13T04:56:52.864330+00:00 | 2021-09-13T04:56:52.864376+00:00 | 178 | false | Explaination:\nInput: [1, 5, 10]\n\nInitially, our **reach is 0**. Let\'s start the process of adding elts step by step:\n1) Adding 1st elt, doesn\'t make us miss elts. Since, reach is 0 and 1st element is 1. So Now **reach=1**.\n2) Now, adding 2nd elt(i.e. 5) makes us miss elts 2,3,4 since reach is only 1 till now. So... | 4 | 0 | [] | 1 |
patching-array | My 8 ms O(N) C++ code | my-8-ms-on-c-code-by-lejas-1hcg | The basic idea is to use "bound" to save the maximum number that can be generated with nums[0..i-1] and the added numbers (i.e. using nums[0..i-1] and the added | lejas | NORMAL | 2016-01-28T14:57:33+00:00 | 2016-01-28T14:57:33+00:00 | 803 | false | The basic idea is to use "bound" to save the maximum number that can be generated with nums[0..i-1] and the added numbers (i.e. using nums[0..i-1] and the added numbers, we can generate all the numbers in [1..bound]). If bound is less than n and nums[i] is larger than bound+1, then we need to add bound+1, which extend ... | 4 | 0 | [] | 0 |
patching-array | Python solution + clear explanation, o(|nums| + log n) | python-solution-clear-explanation-onums-4mlp5 | We want to form all numbers from 1 to n. Let's assume we already have the solutions for a smaller problem. We have an array to reach all numbers up to k, 1 <= k | pbarrera | NORMAL | 2016-10-28T12:13:31.934000+00:00 | 2016-10-28T12:13:31.934000+00:00 | 380 | false | We want to form all numbers from 1 to n. Let's assume we already have the solutions for a smaller problem. We have an array to reach all numbers up to k, 1 <= k < n, but not more. We have used nums<sub>0</sub> to nums<sub>i</sub> to create that array plus some patches. The next element is k+1. There is no way we can cr... | 4 | 0 | [] | 0 |
patching-array | Simple || Easy to Understand || Clear | simple-easy-to-understand-clear-by-kdhak-encw | 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 | kdhakal | NORMAL | 2024-10-07T04:55:22.529067+00:00 | 2024-10-07T04:55:22.529095+00:00 | 3 | 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 | ['Java'] | 0 |
patching-array | Why not you tried Prefix Sum !!! Greedy Approach O(logN) | why-not-you-tried-prefix-sum-greedy-appr-p06s | Intuition\nIf we have non-descending array [a, ....., b, c, ....., d] and the consecutive numbers formed from subarray [a, ....., b] are k and c >= k-1, then w | k_patil092 | NORMAL | 2024-06-16T11:11:59.465802+00:00 | 2024-06-16T11:11:59.465832+00:00 | 133 | false | # Intuition\nIf we have non-descending array $$[a, ....., b, c, ....., d]$$ and the consecutive numbers formed from subarray $$[a, ....., b]$$ are $$k$$ and $$c >= k-1$$, then we can ensure that the consecutive numbers formed from subarray $$[a, ....., b, c]$$ will be $$k+c$$.\n\nFor example,\nGiven $$nums = [1,2,3,6,... | 3 | 0 | ['Array', 'Greedy', 'C', 'Sorting', 'Prefix Sum', 'C++', 'Python3'] | 1 |
patching-array | Intuitive Approach with Observation and Dry Run ✅💯 | intuitive-approach-with-observation-and-pfw4n | Minimum Number of Patches\n\n## Problem Statement\nGiven a sorted array of positive integers nums and an integer n, you need to add the minimum number of patche | _Rishabh_96 | NORMAL | 2024-06-16T07:25:09.738551+00:00 | 2024-06-16T07:25:09.738570+00:00 | 100 | false | # Minimum Number of Patches\n\n## Problem Statement\nGiven a sorted array of positive integers `nums` and an integer `n`, you need to add the minimum number of patches to the array such that every number in the range `[1, n]` can be formed by the sum of some elements in the array.\n\n## Intuition\nTo solve the problem,... | 3 | 0 | ['Array', 'Greedy', 'C++'] | 1 |
patching-array | Binary Search || Code With Comments || Super Easy | binary-search-code-with-comments-super-e-5aad | Intuition\n\nThe code is well commented. This is the intuition for isPossible function in the code first read the comments in the code to get a better understan | vikalp_07 | NORMAL | 2024-06-16T06:08:06.416999+00:00 | 2024-06-16T06:08:06.417029+00:00 | 362 | false | # Intuition\n\n**The code is well commented. This is the intuition for isPossible function in the code first read the comments in the code to get a better understanding**\n\nThe isPossible Function in my code is based on the fact that if we have n consequtive numbers starting from 1,\nthen we can make each and every su... | 3 | 0 | ['Binary Search', 'C++'] | 1 |
patching-array | Simple solution in Java!! With dry run ✅✅✅✅ | simple-solution-in-java-with-dry-run-by-qkrbd | Intuition\nTo ensure all numbers from 1 to n can be formed using a given array nums, we can add the smallest missing number that cannot be formed so far. This m | PavanKumarMeesala | NORMAL | 2024-06-16T04:31:03.930446+00:00 | 2024-06-16T04:31:03.930486+00:00 | 66 | false | ## Intuition\nTo ensure all numbers from 1 to `n` can be formed using a given array `nums`, we can add the smallest missing number that cannot be formed so far. This minimizes the number of patches needed.\n\n## Approach\n1. **Initialize**: Start with the smallest missing number `patch` set to 1 and counters for total ... | 3 | 0 | ['Java'] | 0 |
patching-array | Easy Java Solution | Greedy | Beats 100 💯✅ | easy-java-solution-greedy-beats-100-by-s-ivls | Min Patches Problem\n\n## Problem Description\n\nGiven a sorted integer array nums and a positive integer n, your goal is to compute the minimum number of patch | shobhitkushwaha1406 | NORMAL | 2024-06-16T02:59:42.933390+00:00 | 2024-06-16T02:59:42.933423+00:00 | 440 | false | # Min Patches Problem\n\n## Problem Description\n\nGiven a sorted integer array `nums` and a positive integer `n`, your goal is to compute the minimum number of patches required to ensure that every integer in the range `[1, n]` can be formed as a sum of some elements from `nums`. A patch is an integer that you can add... | 3 | 0 | ['Array', 'Math', 'Greedy', 'Java'] | 2 |
patching-array | 🔥 🔥 🔥 💯 Easy to understand || Greedy Approach || Detailed Explanation || Python,C++,Java🔥 🔥 🔥 | easy-to-understand-greedy-approach-detai-ysou | Intuition\n Describe your first thoughts on how to solve this problem. \n The code works like providing change with limited coin denominations. Suppose you need | avinash_singh_13 | NORMAL | 2024-06-16T02:04:40.924111+00:00 | 2024-06-16T02:04:40.924139+00:00 | 27 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n* The code works like providing change with limited coin denominations. Suppose you need to cover every amount up to \uD835\uDC5B cents. If you can\'t make exact change for a particular amount miss, it indicates you lack a coin of value l... | 3 | 0 | ['C++', 'Java', 'Python3'] | 1 |
patching-array | 🔥Explained :🔥 🔥 c++🔥 🔥sorting 🔥 🔥100% faster🔥 🔥Tricky🔥💯💯 ⬆⬆⬆ | explained-c-sorting-100-faster-tricky-by-kycs | Intuition\n Describe your first thoughts on how to solve this problem. \n1. if we can make all number from 1 to n, and nums[i] = 5 => then we can make all numbe | sandipan103 | NORMAL | 2023-12-03T05:41:33.902741+00:00 | 2023-12-03T05:41:33.902771+00:00 | 139 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. if we can make all number from 1 to n, and nums[i] = 5 => then we can make all number from 1 to n+5\n\n2. if we can make (1 to n) then we need to make the next number i.e n+1;\n\n3. **Case-1 :** the current number (i.e `nums[i] > requ... | 3 | 0 | ['C++'] | 1 |
patching-array | TIME O(n), SPACE (1) || C++ || EASY TO UNDERSTAND || SHORT & SWEET | time-on-space-1-c-easy-to-understand-sho-ve4c | \n/*\ni E to [1,n]\nreach at ith day if nums[j]<=i+1 than add nums[j] into i else add i+1 in to ith day\n*/\nclass Solution {\npublic:\n int minPatches(vect | yash___sharma_ | NORMAL | 2023-03-28T06:35:29.130752+00:00 | 2023-03-28T06:35:38.188224+00:00 | 1,920 | false | ````\n/*\ni E to [1,n]\nreach at ith day if nums[j]<=i+1 than add nums[j] into i else add i+1 in to ith day\n*/\nclass Solution {\npublic:\n int minPatches(vector<int>& nums, int n) {\n long long sum = 0, cnt = 0, i = 0;\n while(sum<n){\n if(i<nums.size()&&nums[i]<=sum+1){\n ... | 3 | 0 | ['Array', 'Greedy', 'C', 'C++'] | 1 |
patching-array | Shortest Solution | C++ | greedy | shortest-solution-c-greedy-by-hritik_014-m95v | \nclass Solution {\npublic:\n int minPatches(vector<int>& nums, int n) {\n int patches = 0, i = 0, sz = nums.size();\n long count = 1;\n | hritik_01478 | NORMAL | 2022-10-13T04:28:25.664723+00:00 | 2022-10-13T04:28:25.664752+00:00 | 578 | false | ```\nclass Solution {\npublic:\n int minPatches(vector<int>& nums, int n) {\n int patches = 0, i = 0, sz = nums.size();\n long count = 1;\n while (count <= n) {\n \n if (i < sz && nums[i] <= count) \n count += nums[i++];\n \n else {\n ... | 3 | 0 | ['Greedy', 'C'] | 2 |
patching-array | Java | Simple solution | java-simple-solution-by-pulkitswami7-dukq | \n\nUPVOTE IF YOU FIND IT USEFUL\n\n\n\nclass Solution {\n public int minPatches(int[] nums, int n) {\n int patches = 0, i = 0;\n long val = 1; | pulkitswami7 | NORMAL | 2022-03-02T14:39:40.260421+00:00 | 2022-03-02T14:40:49.989591+00:00 | 180 | false | <hr>\n\n***UPVOTE IF YOU FIND IT USEFUL***\n<hr>\n\n```\nclass Solution {\n public int minPatches(int[] nums, int n) {\n int patches = 0, i = 0;\n long val = 1;\n \n while(val <= n){\n if(i < nums.length && nums[i] <= val){\n val += nums[i++];\n }\n ... | 3 | 0 | ['Array'] | 0 |
patching-array | Python, O(n), greedy | python-on-greedy-by-404akhan-trmi | Consider some prefix of our array and say the minimal number it can\'t achieve is minn, if the next number is greater than minn we won\'t be able to achieve min | 404akhan | NORMAL | 2021-11-06T11:18:34.967377+00:00 | 2021-11-06T11:18:34.967411+00:00 | 248 | false | Consider some prefix of our array and say the minimal number it can\'t achieve is `minn`, if the next number is greater than `minn` we won\'t be able to achieve `minn` at all (since all numbers are increasing and greater than `minn`) thus next number must be less or equal than `minn` if not so we need to patch with suc... | 3 | 0 | [] | 0 |
patching-array | [C++ Solution ] Detailed explanation -- greedy | c-solution-detailed-explanation-greedy-b-plwh | Let\'s start from an example, the given vector input = {1, 4, 6, 10}, n = 20;\n\nSince we are aimed to cover all the numbers in range [1, 20], let\'s consider t | charles1791 | NORMAL | 2021-06-26T08:23:27.788464+00:00 | 2023-02-26T01:16:21.475403+00:00 | 324 | false | Let\'s start from an example, the given vector input = {1, 4, 6, 10}, n = 20;\n\nSince we are aimed to cover all the numbers in range [1, 20], let\'s consider the elements one by one.\nWe may create an aiding vector<int> aid, which is used to contain all the elements that have been covered SO FAR.\nOf course, this "aid... | 3 | 1 | ['C++'] | 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.