title stringlengths 1 100 | titleSlug stringlengths 3 77 | Java int64 0 1 | Python3 int64 1 1 | content stringlengths 28 44.4k | voteCount int64 0 3.67k | question_content stringlengths 65 5k | question_hints stringclasses 970
values |
|---|---|---|---|---|---|---|---|
Python Top Down + Bottom Up | longest-palindromic-subsequence | 0 | 1 | # Approach\nTop Down is intuitive. Start from outside and work your way inside. Bottom Up is less intuitive, and is technically less efficient too since we check irrelevant states. For example, if the word is "bab", the Top Down approach would go from (0, 2) to (1,1). Whereas the Bottom Up approach would check (2,2), (... | 1 | Given a string `s`, find _the longest palindromic **subsequence**'s length in_ `s`.
A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
**Example 1:**
**Input:** s = "bbbab "
**Output:** 4
**Explanation:** On... | null |
Python3 Solution | longest-palindromic-subsequence | 0 | 1 | \n```\nclass Solution:\n def longestPalindromeSubseq(self, s: str) -> int:\n n=len(s)\n m=[[1 for x in range(n)] for y in range(n)]\n for ss in range(1,n):\n for i in range(n-ss):\n j=i+ss\n if s[i]==s[j] and ss==1:\n m[i][j]=2\n\n ... | 1 | Given a string `s`, find _the longest palindromic **subsequence**'s length in_ `s`.
A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
**Example 1:**
**Input:** s = "bbbab "
**Output:** 4
**Explanation:** On... | null |
Python solution | longest-palindromic-subsequence | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Given a string `s`, find _the longest palindromic **subsequence**'s length in_ `s`.
A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
**Example 1:**
**Input:** s = "bbbab "
**Output:** 4
**Explanation:** On... | null |
Solution | longest-palindromic-subsequence | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int longestPalindromeSubseq(string s){\n int m = s.size();\n\n int tab[3][m];\n for(int i = 0; i<m; i++){\n tab[0][i] = 0;\n tab[1][i] = 1;\n }\n int _2, _1, _0;\n for(int gap = 1; gap<m; gap++){\n _2 = ... | 1 | Given a string `s`, find _the longest palindromic **subsequence**'s length in_ `s`.
A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
**Example 1:**
**Input:** s = "bbbab "
**Output:** 4
**Explanation:** On... | null |
MOST OPTIMIZED PYTHON SOLUTION || BOTTOM-UP APPROACH | longest-palindromic-subsequence | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:$$O(N^2)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(N^2)$$\n<!-- Add your space complexity h... | 1 | Given a string `s`, find _the longest palindromic **subsequence**'s length in_ `s`.
A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
**Example 1:**
**Input:** s = "bbbab "
**Output:** 4
**Explanation:** On... | null |
MEMOIZATION || DP APPROACH | longest-palindromic-subsequence | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Given a string `s`, find _the longest palindromic **subsequence**'s length in_ `s`.
A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
**Example 1:**
**Input:** s = "bbbab "
**Output:** 4
**Explanation:** On... | null |
Python3 || Dynamic || Memoization || detailed explained | longest-palindromic-subsequence | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Using memoization**\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- we know for palindrome we have to check string reverse and original order if both same then it\'s palindrome.\n- now we want subsequence so fo... | 2 | Given a string `s`, find _the longest palindromic **subsequence**'s length in_ `s`.
A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
**Example 1:**
**Input:** s = "bbbab "
**Output:** 4
**Explanation:** On... | null |
Easy to follow DP with explanation | longest-palindromic-subsequence | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**$$Disclaimer:$$** I am aware this is not the optimal solution in terms of memory. However, in my opinion, it was one of the easier DP solutions to conceptualize.\n\nFor this problem, we are taking a DP approach with subsequences. As suc... | 4 | Given a string `s`, find _the longest palindromic **subsequence**'s length in_ `s`.
A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
**Example 1:**
**Input:** s = "bbbab "
**Output:** 4
**Explanation:** On... | null |
Python || Easy || DP || LCS Approach | longest-palindromic-subsequence | 0 | 1 | ```\n#Tabulation(Top-Down)\n#Time Complexity: O(n^2)\n#Space Complexity: O(n^2)\nclass Solution1:\n def longestPalindromeSubseq(self, s: str) -> int:\n n=len(s)\n s2=s[-1::-1]\n dp=[[0 for j in range(n+1)] for i in range(n+1)]\n for i in range(1,n+1):\n for j in range(1,n+1):\n... | 1 | Given a string `s`, find _the longest palindromic **subsequence**'s length in_ `s`.
A **subsequence** is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
**Example 1:**
**Input:** s = "bbbab "
**Output:** 4
**Explanation:** On... | null |
Python 3 || 4 lines, w/ map, accumulate || T/M: 99% / 6% | super-washing-machines | 0 | 1 | ```\nclass Solution:\n def findMinMoves(self, machines: List[int]) -> int:\n\n ave, rem = divmod(sum(machines),len(machines))\n if rem: return -1\n \n machines = [m - ave for m in machines]\n\n return max(*machines,*map(abs,(accumulate(machines))))\n```\n[https://leetcode.com/probl... | 3 | You have `n` super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any `m` (`1 <= m <= n`) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.
Given an integer array `machines` ... | null |
Python 3 || 4 lines, w/ explanation || T/M: 100% / 97% | super-washing-machines | 0 | 1 | Here\'s the plan:\n1. We can arrive at the final state `arr = [n,n,n..,n]` (where`n`is the average of the elements of`machine`) if and only if`n*len(arr) == sum(machines)`). If not, we return -1. \n2. If so, we revise the problem to a simpler, equivalent probem by decrementing each element in`machines`by`n`, which mean... | 5 | You have `n` super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any `m` (`1 <= m <= n`) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.
Given an integer array `machines` ... | null |
517: Solution with step by step explanation | super-washing-machines | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Get the length of the list \'machines\' and the sum of all the dresses.\n2. Check if the total number of dresses is evenly divisible by the number of machines. If not, it is not possible to make all the machines have the ... | 3 | You have `n` super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any `m` (`1 <= m <= n`) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.
Given an integer array `machines` ... | null |
Solution | super-washing-machines | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int findMinMoves(vector<int>& ms) {\n int n = ms.size();\n int sum = 0;\n for (int &i : ms) sum += i;\n if (sum % n) return -1;\n int t = sum / n;\n int ans = 0, toRight = 0;\n for (int &i : ms) {\n toRight = toRight +... | 1 | You have `n` super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any `m` (`1 <= m <= n`) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.
Given an integer array `machines` ... | null |
Four Line Code and easy to execute | super-washing-machines | 0 | 1 | # 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 | You have `n` super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any `m` (`1 <= m <= n`) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.
Given an integer array `machines` ... | null |
Python3 with explanation | super-washing-machines | 0 | 1 | # 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 | You have `n` super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any `m` (`1 <= m <= n`) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.
Given an integer array `machines` ... | null |
Greedy with explanation | super-washing-machines | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- toLeft[i]: machine in ith position pass number of clothes to it\'s left side\n- toRight[i]: machine in ith position pass number of clothes to it\'s right side\n- -toLeft[i]: machine in (i-1)th position pass number of clothes to it\'s ri... | 0 | You have `n` super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any `m` (`1 <= m <= n`) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.
Given an integer array `machines` ... | null |
Python (Simple Maths) | super-washing-machines | 0 | 1 | # 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 | You have `n` super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any `m` (`1 <= m <= n`) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.
Given an integer array `machines` ... | null |
python 3 | super-washing-machines | 0 | 1 | \n# Code\n```\nclass Solution:\n def findMinMoves(self, machines: List[int]) -> int:\n res = c = 0\n if (sum(machines)%len(machines)) != 0 :\n return -1\n a = int(sum(machines)/len(machines))\n for i in machines :\n diff = i - a\n c += diff\n re... | 0 | You have `n` super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any `m` (`1 <= m <= n`) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time.
Given an integer array `machines` ... | null |
simple iterative DP C++/Python||Beats 100.00% | coin-change-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIt is a standard DP question. Use simple DP in an iterative way to solve it\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe initial case is\n```\ndp[0][0]=1;//1 comb: no coins for no amount \n```\n# Complexity\n-... | 6 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the number of combinations that make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `0`.
You may assume that yo... | null |
MEMOIZATION || DP || SUPER EASY | coin-change-ii | 0 | 1 | # 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 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the number of combinations that make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `0`.
You may assume that yo... | null |
Python 59ms, beats 100% | coin-change-ii | 0 | 1 | # Code\n```\ndef change(self, amount: int, coins: List[int]) -> int:\n coins = [c for c in coins if c <= amount]\n dp = [0] * (amount + 1)\n dp[0] = 1\n coins.sort(reverse=True)\n\n for coin in coins:\n dp[coin] = 1\n for i in range(coin, amount - coin + 1):\n dp[i+coin] += dp[i]... | 2 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the number of combinations that make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `0`.
You may assume that yo... | null |
Python 6 lines | coin-change-ii | 0 | 1 | Not optimized\n\n# Code\n```\nclass Solution:\n def change(self, amount: int, coins: List[int]) -> int:\n @cache\n def foo(v, startIndex):\n if v == 0:\n return 1\n return reduce(add, [foo(v-coins[i], i) for i in range(startIndex, len(coins)) if coins[i] <= v], 0)\n... | 2 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the number of combinations that make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `0`.
You may assume that yo... | null |
MOST OPTIMIZED DP SOLUTION | coin-change-ii | 0 | 1 | # 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 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the number of combinations that make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `0`.
You may assume that yo... | null |
Python easy solutions | coin-change-ii | 0 | 1 | # Code\n```\nfrom typing import List\n\nclass Solution:\n def change(self, amount: int, coins: List[int]) -> int:\n dp = [0] * (amount + 1)\n dp[0] = 1\n\n for coin in coins:\n for i in range(coin, amount + 1):\n dp[i] += dp[i - coin]\n\n return dp[amount]\n``` | 1 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the number of combinations that make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `0`.
You may assume that yo... | null |
Easiest Solution|| Beats 100% | coin-change-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the number of combinations that make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `0`.
You may assume that yo... | null |
✅ 100% Dynamic Programming [VIDEO] - Optimal Solution | coin-change-ii | 1 | 1 | # Problem Understanding\n\nIn the "Coin Change II" problem, we are given coins of various denominations and a specific amount. Our task is to determine the number of different ways we can make up that amount using the coins. Unlike other coin change problems where we need to minimize or maximize the number of coins, he... | 91 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the number of combinations that make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `0`.
You may assume that yo... | null |
faster than 70.49% of Python3 submission || Easy Solution || Better Approach | coin-change-ii | 0 | 1 | ### Approach:-\nTwo lists prev and curr are created, each of length value + 1, initialized with zeros. These lists will be used to store the number of ways to make change for each value from 0 to value.\n\nFor the first denomination arr[0], the prev list is updated. If a particular value i is divisible by arr[0], it me... | 3 | You are given an integer array `coins` representing coins of different denominations and an integer `amount` representing a total amount of money.
Return _the number of combinations that make up that amount_. If that amount of money cannot be made up by any combination of the coins, return `0`.
You may assume that yo... | null |
Solution | random-flip-matrix | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n Solution(int m, int n) : rows(m), cols(n), total(m * n) {\n }\n vector<int> flip() {\n if (used.size() == total) return {};\n int index = rand() % total;\n while (used.count(index))\n index = ++index % total;\n used.insert(index);\n ... | 1 | There is an `m x n` binary grid `matrix` with all the values set `0` initially. Design an algorithm to randomly pick an index `(i, j)` where `matrix[i][j] == 0` and flips it to `1`. All the indices `(i, j)` where `matrix[i][j] == 0` should be equally likely to be returned.
Optimize your algorithm to minimize the numbe... | Keep prefix sums of both arrays. Can the difference between the prefix sums at an index help us? What happens if the difference between the two prefix sums at an index a is x, and x again at a different index b? This means that the sum of nums1 from index a + 1 to index b is equal to the sum of nums2 from index a + 1 t... |
Solution | random-flip-matrix | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n Solution(int m, int n) : rows(m), cols(n), total(m * n) {\n }\n vector<int> flip() {\n if (used.size() == total) return {};\n int index = rand() % total;\n while (used.count(index))\n index = ++index % total;\n used.insert(index);\n ... | 1 | A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns.
The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph.
The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n... | null |
python solution beats 90% | random-flip-matrix | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is to simulate a 2D grid of size m x n and flip a cell at random, with the restriction that a cell can only be flipped once. The solution uses a set to keep track of the cells that have been flipped, and uses the randint funct... | 1 | There is an `m x n` binary grid `matrix` with all the values set `0` initially. Design an algorithm to randomly pick an index `(i, j)` where `matrix[i][j] == 0` and flips it to `1`. All the indices `(i, j)` where `matrix[i][j] == 0` should be equally likely to be returned.
Optimize your algorithm to minimize the numbe... | Keep prefix sums of both arrays. Can the difference between the prefix sums at an index help us? What happens if the difference between the two prefix sums at an index a is x, and x again at a different index b? This means that the sum of nums1 from index a + 1 to index b is equal to the sum of nums2 from index a + 1 t... |
python solution beats 90% | random-flip-matrix | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is to simulate a 2D grid of size m x n and flip a cell at random, with the restriction that a cell can only be flipped once. The solution uses a set to keep track of the cells that have been flipped, and uses the randint funct... | 1 | A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns.
The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph.
The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n... | null |
Python3 Approach 1 with formal mathematical proof | random-flip-matrix | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition is to always partition the numbers into two parts: 1) the unvisited numbers and 2) the already visited numbers. Logically, the indices [0, `remain`] and [remain+1, `nRows`*`nCols`]) correspond to part 1 and part 2, respectiv... | 0 | There is an `m x n` binary grid `matrix` with all the values set `0` initially. Design an algorithm to randomly pick an index `(i, j)` where `matrix[i][j] == 0` and flips it to `1`. All the indices `(i, j)` where `matrix[i][j] == 0` should be equally likely to be returned.
Optimize your algorithm to minimize the numbe... | Keep prefix sums of both arrays. Can the difference between the prefix sums at an index help us? What happens if the difference between the two prefix sums at an index a is x, and x again at a different index b? This means that the sum of nums1 from index a + 1 to index b is equal to the sum of nums2 from index a + 1 t... |
Python3 Approach 1 with formal mathematical proof | random-flip-matrix | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition is to always partition the numbers into two parts: 1) the unvisited numbers and 2) the already visited numbers. Logically, the indices [0, `remain`] and [remain+1, `nRows`*`nCols`]) correspond to part 1 and part 2, respectiv... | 0 | A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns.
The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph.
The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n... | null |
An excellent solution | random-flip-matrix | 0 | 1 | # Intuition\n\n\n# Approach\nYou don\'t have to draw randomly, just pull out [i,j] with values of zero in order.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n\n# Code\n```\ncla... | 0 | There is an `m x n` binary grid `matrix` with all the values set `0` initially. Design an algorithm to randomly pick an index `(i, j)` where `matrix[i][j] == 0` and flips it to `1`. All the indices `(i, j)` where `matrix[i][j] == 0` should be equally likely to be returned.
Optimize your algorithm to minimize the numbe... | Keep prefix sums of both arrays. Can the difference between the prefix sums at an index help us? What happens if the difference between the two prefix sums at an index a is x, and x again at a different index b? This means that the sum of nums1 from index a + 1 to index b is equal to the sum of nums2 from index a + 1 t... |
An excellent solution | random-flip-matrix | 0 | 1 | # Intuition\n\n\n# Approach\nYou don\'t have to draw randomly, just pull out [i,j] with values of zero in order.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(1)$$\n\n# Code\n```\ncla... | 0 | A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns.
The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph.
The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n... | null |
No tricks. Python. Beats 98%. | random-flip-matrix | 0 | 1 | # Intuition\nIn this case, due to task size constraints, it is much more efficient to just track the already flipped indices, and let random fail-and-repeat from time to time.\n\nTo reduce the number of random calls, it makes sence to calculate the index as flattenned into one dimension.\n\n# Complexity\n- Time complex... | 0 | There is an `m x n` binary grid `matrix` with all the values set `0` initially. Design an algorithm to randomly pick an index `(i, j)` where `matrix[i][j] == 0` and flips it to `1`. All the indices `(i, j)` where `matrix[i][j] == 0` should be equally likely to be returned.
Optimize your algorithm to minimize the numbe... | Keep prefix sums of both arrays. Can the difference between the prefix sums at an index help us? What happens if the difference between the two prefix sums at an index a is x, and x again at a different index b? This means that the sum of nums1 from index a + 1 to index b is equal to the sum of nums2 from index a + 1 t... |
No tricks. Python. Beats 98%. | random-flip-matrix | 0 | 1 | # Intuition\nIn this case, due to task size constraints, it is much more efficient to just track the already flipped indices, and let random fail-and-repeat from time to time.\n\nTo reduce the number of random calls, it makes sence to calculate the index as flattenned into one dimension.\n\n# Complexity\n- Time complex... | 0 | A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns.
The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph.
The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n... | null |
Python3 - Switch Modes | random-flip-matrix | 0 | 1 | # Intuition\nFor very large sets, it makes sense to just maintain selected\nFor very small sets, it makes sense to just maintain available\nFor middle sized, it depends on how it is used\n\nSo, for a compromise, I just switch modes if more than half the positions are filled.\n\n# Code\n```\nimport secrets\nclass Soluti... | 0 | There is an `m x n` binary grid `matrix` with all the values set `0` initially. Design an algorithm to randomly pick an index `(i, j)` where `matrix[i][j] == 0` and flips it to `1`. All the indices `(i, j)` where `matrix[i][j] == 0` should be equally likely to be returned.
Optimize your algorithm to minimize the numbe... | Keep prefix sums of both arrays. Can the difference between the prefix sums at an index help us? What happens if the difference between the two prefix sums at an index a is x, and x again at a different index b? This means that the sum of nums1 from index a + 1 to index b is equal to the sum of nums2 from index a + 1 t... |
Python3 - Switch Modes | random-flip-matrix | 0 | 1 | # Intuition\nFor very large sets, it makes sense to just maintain selected\nFor very small sets, it makes sense to just maintain available\nFor middle sized, it depends on how it is used\n\nSo, for a compromise, I just switch modes if more than half the positions are filled.\n\n# Code\n```\nimport secrets\nclass Soluti... | 0 | A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns.
The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph.
The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n... | null |
97% faster soln | random-flip-matrix | 0 | 1 | # 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 | There is an `m x n` binary grid `matrix` with all the values set `0` initially. Design an algorithm to randomly pick an index `(i, j)` where `matrix[i][j] == 0` and flips it to `1`. All the indices `(i, j)` where `matrix[i][j] == 0` should be equally likely to be returned.
Optimize your algorithm to minimize the numbe... | Keep prefix sums of both arrays. Can the difference between the prefix sums at an index help us? What happens if the difference between the two prefix sums at an index a is x, and x again at a different index b? This means that the sum of nums1 from index a + 1 to index b is equal to the sum of nums2 from index a + 1 t... |
97% faster soln | random-flip-matrix | 0 | 1 | # 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 | A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns.
The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph.
The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n... | null |
[Python] Explained solution O(m*n) -> O(1) | random-flip-matrix | 0 | 1 | # Intuition\nThe idea is to first represent the matrix as a list of points `zeros = [(i, j) for i in range(m) for j in range(n)]`.\nNow we can keep track of picked points during flips by splitting the list into left and right parts. We\'ll store picked ones in the right part, while left part will be the source for new ... | 0 | There is an `m x n` binary grid `matrix` with all the values set `0` initially. Design an algorithm to randomly pick an index `(i, j)` where `matrix[i][j] == 0` and flips it to `1`. All the indices `(i, j)` where `matrix[i][j] == 0` should be equally likely to be returned.
Optimize your algorithm to minimize the numbe... | Keep prefix sums of both arrays. Can the difference between the prefix sums at an index help us? What happens if the difference between the two prefix sums at an index a is x, and x again at a different index b? This means that the sum of nums1 from index a + 1 to index b is equal to the sum of nums2 from index a + 1 t... |
[Python] Explained solution O(m*n) -> O(1) | random-flip-matrix | 0 | 1 | # Intuition\nThe idea is to first represent the matrix as a list of points `zeros = [(i, j) for i in range(m) for j in range(n)]`.\nNow we can keep track of picked points during flips by splitting the list into left and right parts. We\'ll store picked ones in the right part, while left part will be the source for new ... | 0 | A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns.
The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph.
The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n... | null |
If used take next available, 95% speed | random-flip-matrix | 0 | 1 | Runtime: 48 ms, faster than 95.00% of Python3 online submissions for Random Flip Matrix.\nMemory Usage: 14.7 MB, less than 59.17% of Python3 online submissions for Random Flip Matrix.\n```\nfrom random import randint\nclass Solution:\n\n def __init__(self, n_rows: int, n_cols: int):\n self.rows = n_rows\n ... | 2 | There is an `m x n` binary grid `matrix` with all the values set `0` initially. Design an algorithm to randomly pick an index `(i, j)` where `matrix[i][j] == 0` and flips it to `1`. All the indices `(i, j)` where `matrix[i][j] == 0` should be equally likely to be returned.
Optimize your algorithm to minimize the numbe... | Keep prefix sums of both arrays. Can the difference between the prefix sums at an index help us? What happens if the difference between the two prefix sums at an index a is x, and x again at a different index b? This means that the sum of nums1 from index a + 1 to index b is equal to the sum of nums2 from index a + 1 t... |
If used take next available, 95% speed | random-flip-matrix | 0 | 1 | Runtime: 48 ms, faster than 95.00% of Python3 online submissions for Random Flip Matrix.\nMemory Usage: 14.7 MB, less than 59.17% of Python3 online submissions for Random Flip Matrix.\n```\nfrom random import randint\nclass Solution:\n\n def __init__(self, n_rows: int, n_cols: int):\n self.rows = n_rows\n ... | 2 | A game on an **undirected** graph is played by two players, Mouse and Cat, who alternate turns.
The graph is given as follows: `graph[a]` is a list of all nodes `b` such that `ab` is an edge of the graph.
The mouse starts at node `1` and goes first, the cat starts at node `2` and goes second, and there is a hole at n... | null |
C++ || PYTHON || O(N) || 0ms || Beats 100% | detect-capital | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- First we can simply check whether all of the characters are capital or not, we can use ascii value to check this.\n\n- If all are not capital, then we can check the ... | 2 | We define the usage of capitals in a word to be right when one of the following cases holds:
* All letters in this word are capitals, like `"USA "`.
* All letters in this word are not capitals, like `"leetcode "`.
* Only the first letter in this word is capital, like `"Google "`.
Given a string `word`, return `... | null |
Solution | detect-capital | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n bool detectCapitalUse(string word) {\n int count = count_if(word.begin(), word.end(), [](char c){ return isupper(c); });\n return count == word.length() || count == 0 || (count == 1 && isupper(word[0]));\n }\n};\n```\n\n```Python3 []\nclass Solution:\n def d... | 1 | We define the usage of capitals in a word to be right when one of the following cases holds:
* All letters in this word are capitals, like `"USA "`.
* All letters in this word are not capitals, like `"leetcode "`.
* Only the first letter in this word is capital, like `"Google "`.
Given a string `word`, return `... | null |
python one liner :) | detect-capital | 0 | 1 | c0ck funny haha\n\n# Code\n```\nclass Solution:\n def detectCapitalUse(self, c0ck: str) -> bool:\n return c0ck.isupper() or c0ck.islower() or c0ck.istitle()\n``` | 1 | We define the usage of capitals in a word to be right when one of the following cases holds:
* All letters in this word are capitals, like `"USA "`.
* All letters in this word are not capitals, like `"leetcode "`.
* Only the first letter in this word is capital, like `"Google "`.
Given a string `word`, return `... | null |
Solution | longest-uncommon-subsequence-i | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int findLUSlength(string a, string b) {\n if (a == b) {\n return -1;\n }\n return max(a.length(), b.length());\n }\n};\n```\n\n```Python3 []\nclass Solution:\n def findLUSlength(self, a: str, b: str) -> int:\n return -1 if a == b else max(... | 1 | Given two strings `a` and `b`, return _the length of the **longest uncommon subsequence** between_ `a` _and_ `b`. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between two strings is a string that is a **subsequence of one but not the other**.
A **subsequence** of a stri... | null |
[Python 3] One-liner - Greedy - Simple solution | longest-uncommon-subsequence-i | 0 | 1 | # 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)$$ --... | 4 | Given two strings `a` and `b`, return _the length of the **longest uncommon subsequence** between_ `a` _and_ `b`. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between two strings is a string that is a **subsequence of one but not the other**.
A **subsequence** of a stri... | null |
521: Time 91.38% and Space 94.99%, Solution with step by step explanation | longest-uncommon-subsequence-i | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Check if both strings a and b are equal. If they are, then there is no uncommon subsequence between them, so return -1.\n2. If both strings a and b are not equal, then the longest uncommon subsequence would be the longest... | 5 | Given two strings `a` and `b`, return _the length of the **longest uncommon subsequence** between_ `a` _and_ `b`. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between two strings is a string that is a **subsequence of one but not the other**.
A **subsequence** of a stri... | null |
Python3||O(1) | longest-uncommon-subsequence-i | 0 | 1 | # 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- O(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- O(1)\n<!-- Add your space complexity here, ... | 2 | Given two strings `a` and `b`, return _the length of the **longest uncommon subsequence** between_ `a` _and_ `b`. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between two strings is a string that is a **subsequence of one but not the other**.
A **subsequence** of a stri... | null |
python two lines | easy | longest-uncommon-subsequence-i | 0 | 1 | ```\nclass Solution:\n def findLUSlength(self, a: str, b: str) -> int:\n if a==b:return -1\n else:return max(len(a),len(b))\n```\n**if you like do upvote !** | 6 | Given two strings `a` and `b`, return _the length of the **longest uncommon subsequence** between_ `a` _and_ `b`. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between two strings is a string that is a **subsequence of one but not the other**.
A **subsequence** of a stri... | null |
Longest Uncommon Subsequence via String Comparison | longest-uncommon-subsequence-i | 0 | 1 | # Approach\n1. If both strings, a and b, are equal, it means there\'s no uncommon subsequence, so you set output to -1.\n2. Otherwise, if the strings are not equal, the longest uncommon subsequence would be the length of the longer string between a and b. You return the length of the longer string as the output.\n\n# C... | 0 | Given two strings `a` and `b`, return _the length of the **longest uncommon subsequence** between_ `a` _and_ `b`. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between two strings is a string that is a **subsequence of one but not the other**.
A **subsequence** of a stri... | null |
very easy solution for begginers | longest-uncommon-subsequence-i | 0 | 1 | # 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 | Given two strings `a` and `b`, return _the length of the **longest uncommon subsequence** between_ `a` _and_ `b`. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between two strings is a string that is a **subsequence of one but not the other**.
A **subsequence** of a stri... | null |
sergey solution | longest-uncommon-subsequence-i | 0 | 1 | # 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 | Given two strings `a` and `b`, return _the length of the **longest uncommon subsequence** between_ `a` _and_ `b`. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between two strings is a string that is a **subsequence of one but not the other**.
A **subsequence** of a stri... | null |
Easy Loop Solution | longest-uncommon-subsequence-i | 0 | 1 | # 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 | Given two strings `a` and `b`, return _the length of the **longest uncommon subsequence** between_ `a` _and_ `b`. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between two strings is a string that is a **subsequence of one but not the other**.
A **subsequence** of a stri... | null |
Solution | longest-uncommon-subsequence-ii | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int findLUSlength(vector<string>& strs) {\n if (strs.empty()) return -1;\n int rst = -1;\n for(auto i = 0; i < strs.size(); ++i) {\n int j = 0;\n for(; j < strs.size(); ++j) {\n if(i==j) continue;\n if(LCS... | 1 | Given an array of strings `strs`, return _the length of the **longest uncommon subsequence** between them_. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between an array of strings is a string that is a **subsequence of one string but not the others**.
A **subsequence**... | null |
522: Solution with step by step explanation | longest-uncommon-subsequence-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. We define a helper function isSubsequence(a: str, b: str) -> bool that returns True if string a is a subsequence of string b, and False otherwise. We do this by iterating through each character in b, and for each characte... | 4 | Given an array of strings `strs`, return _the length of the **longest uncommon subsequence** between them_. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between an array of strings is a string that is a **subsequence of one string but not the others**.
A **subsequence**... | null |
98% TC and 78% SC easy python solution | longest-uncommon-subsequence-ii | 0 | 1 | ```\ndef findLUSlength(self, strs: List[str]) -> int:\n\tdef isSub(x, y):\n\t\ti = j = 0\n\t\twhile(i<len(x) and j<len(y)):\n\t\t\tif(x[i] == y[j]):\n\t\t\t\ti += 1\n\t\t\tj += 1\n\t\treturn i == len(x)\n\tstrs.sort(key = len, reverse = True)\n\tn = len(strs)\n\tfor i in range(n):\n\t\tp = 1\n\t\tfor j in range(n):\n\t... | 1 | Given an array of strings `strs`, return _the length of the **longest uncommon subsequence** between them_. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between an array of strings is a string that is a **subsequence of one string but not the others**.
A **subsequence**... | null |
✅Bruet force solution || python | longest-uncommon-subsequence-ii | 0 | 1 | # Code\n```\nclass Solution:\n\n def sub(self,s,i,temp,d):\n if(i==len(s)):\n d[temp[:]]=d.get(temp[:],0)+1\n return \n self.sub(s,i+1,temp+s[i],d)\n self.sub(s,i+1,temp,d)\n return \n\n def findLUSlength(self, strs: List[str]) -> int:\n d={}\n for s... | 0 | Given an array of strings `strs`, return _the length of the **longest uncommon subsequence** between them_. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between an array of strings is a string that is a **subsequence of one string but not the others**.
A **subsequence**... | null |
Concise and short implementation in Python, Faster than 98% | longest-uncommon-subsequence-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSimilar to the previuos [problem](https://leetcode.com/problems/longest-uncommon-subsequence-i/), the answer is the longest string that is not a subsequence of another strings. So, a string that appears more than once is not an answer. \n... | 0 | Given an array of strings `strs`, return _the length of the **longest uncommon subsequence** between them_. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between an array of strings is a string that is a **subsequence of one string but not the others**.
A **subsequence**... | null |
BRUTE FORCE SOLUTION | longest-uncommon-subsequence-ii | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBRUTE FORCE \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUSING LCS\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N^2)\n\n- Space complexity:\n<!-- Add your space ... | 0 | Given an array of strings `strs`, return _the length of the **longest uncommon subsequence** between them_. If the longest uncommon subsequence does not exist, return `-1`.
An **uncommon subsequence** between an array of strings is a string that is a **subsequence of one string but not the others**.
A **subsequence**... | null |
Solution | longest-word-in-dictionary-through-deleting | 1 | 1 | ```C++ []\n\nclass Solution {\npublic:\n string findLongestWord(string s, vector<string>& dictionary) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n string res;\n for (int i = 0; i < dictionary.size();i++)\n {\n string &word = dictionary[i];\n\n if (... | 1 | Given a string `s` and a string array `dictionary`, return _the longest string in the dictionary that can be formed by deleting some of the given string characters_. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the emp... | null |
python3 two solutions | longest-word-in-dictionary-through-deleting | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | Given a string `s` and a string array `dictionary`, return _the longest string in the dictionary that can be formed by deleting some of the given string characters_. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the emp... | null |
524: Solution with step by step explanation | longest-word-in-dictionary-through-deleting | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Sort the dictionary d in descending order by length and then lexicographically.\n2. Traverse through the dictionary d using a for loop.\n3. Set a variable i to 0 to keep track of the index of the current character in the ... | 6 | Given a string `s` and a string array `dictionary`, return _the longest string in the dictionary that can be formed by deleting some of the given string characters_. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the emp... | null |
Python simple straight-forward solution | longest-word-in-dictionary-through-deleting | 0 | 1 | ```\nclass Solution:\n def findLongestWord(self, s: str, dictionary: list[str]) -> str:\n def is_valid(string): # can we get the string by deleting some letters or not\n i = 0\n for letter in s:\n if letter == string[i]:\n i += 1\n if i =... | 4 | Given a string `s` and a string array `dictionary`, return _the longest string in the dictionary that can be formed by deleting some of the given string characters_. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the emp... | null |
Python. very clear and simplistic solution. | longest-word-in-dictionary-through-deleting | 0 | 1 | ```\nclass Solution:\n def findLongestWord(self, s: str, d: List[str]) -> str:\n def is_subseq(main: str, sub: str) -> bool:\n i, j, m, n = 0, 0, len(main), len(sub)\n while i < m and j < n and n - j >= m - i:\n if main[i] == sub[j]:\n i += 1\n ... | 6 | Given a string `s` and a string array `dictionary`, return _the longest string in the dictionary that can be formed by deleting some of the given string characters_. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the emp... | null |
Python N pointers + Bucket sort O(m * n) Solution | longest-word-in-dictionary-through-deleting | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWhile traversing through the string, increment pointers that each correspond to the current position of the word. When a pointer reaches the end of a word, add it to a complete list that is used later in the bucket sort.\n\n# Approach\n<!... | 0 | Given a string `s` and a string array `dictionary`, return _the longest string in the dictionary that can be formed by deleting some of the given string characters_. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the emp... | null |
✅Easy Solution Brute Force Approach✅ | longest-word-in-dictionary-through-deleting | 0 | 1 | # 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 | Given a string `s` and a string array `dictionary`, return _the longest string in the dictionary that can be formed by deleting some of the given string characters_. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the emp... | null |
Python3 | 2 Pointers approach | longest-word-in-dictionary-through-deleting | 0 | 1 | # Approach\n<!-- Describe your approach to solving the problem. --> Iterate every dictionary string within s, increase the index of s if element matches or not, increase index for dictionary string only if element matches, maintain a count(here j, index for dict string is acting as count as well), if count equals to le... | 0 | Given a string `s` and a string array `dictionary`, return _the longest string in the dictionary that can be formed by deleting some of the given string characters_. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the emp... | null |
720. Longest Word in Dictionary | longest-word-in-dictionary-through-deleting | 0 | 1 | # 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 | Given a string `s` and a string array `dictionary`, return _the longest string in the dictionary that can be formed by deleting some of the given string characters_. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no possible result, return the emp... | null |
Solution | contiguous-array | 1 | 1 | ```C++ []\nclass Solution {\npublic:\n int findMaxLength(vector<int>& nums) {\n int n = nums.size();\n vector<int> vect(2 * n + 1, INT_MIN);\n vect[nums.size()] = -1;\n int totalSum = 0, maxLength = 0;\n for (int i = 0; i < n; i++)\n {\n totalSum += (nums[i] == 0 ... | 2 | Given a binary array `nums`, return _the maximum length of a contiguous subarray with an equal number of_ `0` _and_ `1`.
**Example 1:**
**Input:** nums = \[0,1\]
**Output:** 2
**Explanation:** \[0, 1\] is the longest contiguous subarray with an equal number of 0 and 1.
**Example 2:**
**Input:** nums = \[0,1,0\]
**O... | null |
525: Solution with step by step explanation | contiguous-array | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize a dictionary count with a key-value pair of 0: -1 to keep track of the count difference of 0\'s and 1\'s and the index at which it occurs.\n2. Initialize two variables, max_len and count_diff to keep track of t... | 5 | Given a binary array `nums`, return _the maximum length of a contiguous subarray with an equal number of_ `0` _and_ `1`.
**Example 1:**
**Input:** nums = \[0,1\]
**Output:** 2
**Explanation:** \[0, 1\] is the longest contiguous subarray with an equal number of 0 and 1.
**Example 2:**
**Input:** nums = \[0,1,0\]
**O... | null |
Python Permutation Solution | beautiful-arrangement | 0 | 1 | It took a while for me to understand this question. In the end all they were asking for was to find a permutation of n numbers that satisfy **one of these conditions**. EIther the number at index + 1 is divisible by the index + 1 or index + 1 is divisible by the number. Also a much better example would have been to sh... | 32 | Suppose you have `n` integers labeled `1` through `n`. A permutation of those `n` integers `perm` (**1-indexed**) is considered a **beautiful arrangement** if for every `i` (`1 <= i <= n`), **either** of the following is true:
* `perm[i]` is divisible by `i`.
* `i` is divisible by `perm[i]`.
Given an integer `n`,... | null |
526: Space 98.18%, Solution with step by step explanation | beautiful-arrangement | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Create a list of integers from 1 to n using the range function and the list constructor. Assign it to the variable nums.\n2. Initialize a variable count to 0.\n3. Define a nested function backtrack which takes an integer ... | 4 | Suppose you have `n` integers labeled `1` through `n`. A permutation of those `n` integers `perm` (**1-indexed**) is considered a **beautiful arrangement** if for every `i` (`1 <= i <= n`), **either** of the following is true:
* `perm[i]` is divisible by `i`.
* `i` is divisible by `perm[i]`.
Given an integer `n`,... | null |
Python Backtracking, the more intuitive way | beautiful-arrangement | 0 | 1 | ```\nclass Solution:\n def countArrangement(self, n: int) -> int:\n self.count = 0\n self.backtrack(n, 1, [])\n return self.count\n \n def backtrack(self, N, idx, temp):\n if len(temp) == N:\n self.count += 1\n return\n \n for i in range(1, N+... | 8 | Suppose you have `n` integers labeled `1` through `n`. A permutation of those `n` integers `perm` (**1-indexed**) is considered a **beautiful arrangement** if for every `i` (`1 <= i <= n`), **either** of the following is true:
* `perm[i]` is divisible by `i`.
* `i` is divisible by `perm[i]`.
Given an integer `n`,... | null |
Python solution, easy and clear | beautiful-arrangement | 0 | 1 | ```\ndef countArrangement(self, n: int) -> int:\n\tdef solve(i):\n\t\tif(i == n+1):\n\t\t\tans[0] += 1\n\t\t\treturn\n\t\tfor j in range(1, n+1):\n\t\t\tb = 1<<j\n\t\t\tif(vis[0] & b == 0):\n\t\t\t\tif not(i%j and j%i):\n\t\t\t\t\tvis[0] = vis[0] | b\n\t\t\t\t\tsolve(i+1)\n\t\t\t\t\tvis[0] = vis[0] ^ b\n\tvis = [0]\n\t... | 1 | Suppose you have `n` integers labeled `1` through `n`. A permutation of those `n` integers `perm` (**1-indexed**) is considered a **beautiful arrangement** if for every `i` (`1 <= i <= n`), **either** of the following is true:
* `perm[i]` is divisible by `i`.
* `i` is divisible by `perm[i]`.
Given an integer `n`,... | null |
Statistically sound solution beating most in runtime and space time compelxity. | random-pick-with-weight | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nTreat the indices as the possible values of a random variable X. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nUsing the weights, first compute the PMF of X and then its CDF. \n\nGenerate a random value betw... | 1 | You are given a **0-indexed** array of positive integers `w` where `w[i]` describes the **weight** of the `ith` index.
You need to implement the function `pickIndex()`, which **randomly** picks an index in the range `[0, w.length - 1]` (**inclusive**) and returns it. The **probability** of picking an index `i` is `w[i... | We can transform the linked list to an array this should ease things up After transforming the linked list to an array it becomes as easy as swapping two integers in an array then rebuilding the linked list |
Statistically sound solution beating most in runtime and space time compelxity. | random-pick-with-weight | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nTreat the indices as the possible values of a random variable X. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nUsing the weights, first compute the PMF of X and then its CDF. \n\nGenerate a random value betw... | 1 | Given an array of integers `nums`, sort the array in ascending order and return it.
You must solve the problem **without using any built-in** functions in `O(nlog(n))` time complexity and with the smallest space complexity possible.
**Example 1:**
**Input:** nums = \[5,2,3,1\]
**Output:** \[1,2,3,5\]
**Explanation:*... | null |
[Python] Roblox VO Preparation: Prefix Sum + Binary Search / Dichotomy Solution | random-pick-with-weight | 0 | 1 | Someone says Roblox Virtual Onsite interview includes problems like **528. Random Pick with Weight** in this post [\nRoblox Virtual Onsite | 2 Questions | 1 Hard](https://leetcode.com/discuss/interview-question/1139472/roblox-virtual-onsite-2-questions-1-hardhttp://). So I\'m doing it for practice. Wish me good luck! F... | 2 | You are given a **0-indexed** array of positive integers `w` where `w[i]` describes the **weight** of the `ith` index.
You need to implement the function `pickIndex()`, which **randomly** picks an index in the range `[0, w.length - 1]` (**inclusive**) and returns it. The **probability** of picking an index `i` is `w[i... | We can transform the linked list to an array this should ease things up After transforming the linked list to an array it becomes as easy as swapping two integers in an array then rebuilding the linked list |
[Python] Roblox VO Preparation: Prefix Sum + Binary Search / Dichotomy Solution | random-pick-with-weight | 0 | 1 | Someone says Roblox Virtual Onsite interview includes problems like **528. Random Pick with Weight** in this post [\nRoblox Virtual Onsite | 2 Questions | 1 Hard](https://leetcode.com/discuss/interview-question/1139472/roblox-virtual-onsite-2-questions-1-hardhttp://). So I\'m doing it for practice. Wish me good luck! F... | 2 | Given an array of integers `nums`, sort the array in ascending order and return it.
You must solve the problem **without using any built-in** functions in `O(nlog(n))` time complexity and with the smallest space complexity possible.
**Example 1:**
**Input:** nums = \[5,2,3,1\]
**Output:** \[1,2,3,5\]
**Explanation:*... | null |
Python 3 simple solution | random-pick-with-weight | 0 | 1 | **Hello fellow coders! \uD83D\uDE00**\n\nI decided to post this solution because the other python3 solutions, while genius and better, were a bit hard to read. Here is the breakdown of my solution:\n* Normalize the weights in the initial w list to ratios \n\t* i.e. [1,3,3,1] --> [1 / 8, 3 / 8, 3 / 8, 1 / 8], for a gen... | 87 | You are given a **0-indexed** array of positive integers `w` where `w[i]` describes the **weight** of the `ith` index.
You need to implement the function `pickIndex()`, which **randomly** picks an index in the range `[0, w.length - 1]` (**inclusive**) and returns it. The **probability** of picking an index `i` is `w[i... | We can transform the linked list to an array this should ease things up After transforming the linked list to an array it becomes as easy as swapping two integers in an array then rebuilding the linked list |
Python 3 simple solution | random-pick-with-weight | 0 | 1 | **Hello fellow coders! \uD83D\uDE00**\n\nI decided to post this solution because the other python3 solutions, while genius and better, were a bit hard to read. Here is the breakdown of my solution:\n* Normalize the weights in the initial w list to ratios \n\t* i.e. [1,3,3,1] --> [1 / 8, 3 / 8, 3 / 8, 1 / 8], for a gen... | 87 | Given an array of integers `nums`, sort the array in ascending order and return it.
You must solve the problem **without using any built-in** functions in `O(nlog(n))` time complexity and with the smallest space complexity possible.
**Example 1:**
**Input:** nums = \[5,2,3,1\]
**Output:** \[1,2,3,5\]
**Explanation:*... | null |
[Python] 6 lines Solution with Statistics Explanation | random-pick-with-weight | 0 | 1 | In Statistics, if we nomalize (divided by `self.cdf[-1]`) the `self.cdf` into [0, 1], it\'s actually a discrete distribution `cdf ` sequence. For cdf function, given `X` you can get the Cumulative Probability (aka percentile) of `X`, which is `Prob(x <= X) `.\n\nThe question is actually asking to generate random sample... | 33 | You are given a **0-indexed** array of positive integers `w` where `w[i]` describes the **weight** of the `ith` index.
You need to implement the function `pickIndex()`, which **randomly** picks an index in the range `[0, w.length - 1]` (**inclusive**) and returns it. The **probability** of picking an index `i` is `w[i... | We can transform the linked list to an array this should ease things up After transforming the linked list to an array it becomes as easy as swapping two integers in an array then rebuilding the linked list |
[Python] 6 lines Solution with Statistics Explanation | random-pick-with-weight | 0 | 1 | In Statistics, if we nomalize (divided by `self.cdf[-1]`) the `self.cdf` into [0, 1], it\'s actually a discrete distribution `cdf ` sequence. For cdf function, given `X` you can get the Cumulative Probability (aka percentile) of `X`, which is `Prob(x <= X) `.\n\nThe question is actually asking to generate random sample... | 33 | Given an array of integers `nums`, sort the array in ascending order and return it.
You must solve the problem **without using any built-in** functions in `O(nlog(n))` time complexity and with the smallest space complexity possible.
**Example 1:**
**Input:** nums = \[5,2,3,1\]
**Output:** \[1,2,3,5\]
**Explanation:*... | null |
Python, Simple, 6-line code || 97.65% faster || Runtime: 186 ms | random-pick-with-weight | 0 | 1 | Runtime: 186 ms, faster than 97.65%\nMemory Usage: 18.1 MB, less than 82.77%\n\n\tclass Solution(object):\n\t\tdef __init__(self, w):\n\t\t\t"""\n\t\t\t:type w: List[int]\n\t\t\t"""\n\t\t\t#Cumulative sum\n\t\t\tself.list = [0] * len(w)\n\n\t\t\ts = 0\n\t\t\tfor i, n in enumerate(w):\n\t\t\t\ts += n\n\t\t\t\tself.list[... | 2 | You are given a **0-indexed** array of positive integers `w` where `w[i]` describes the **weight** of the `ith` index.
You need to implement the function `pickIndex()`, which **randomly** picks an index in the range `[0, w.length - 1]` (**inclusive**) and returns it. The **probability** of picking an index `i` is `w[i... | We can transform the linked list to an array this should ease things up After transforming the linked list to an array it becomes as easy as swapping two integers in an array then rebuilding the linked list |
Python, Simple, 6-line code || 97.65% faster || Runtime: 186 ms | random-pick-with-weight | 0 | 1 | Runtime: 186 ms, faster than 97.65%\nMemory Usage: 18.1 MB, less than 82.77%\n\n\tclass Solution(object):\n\t\tdef __init__(self, w):\n\t\t\t"""\n\t\t\t:type w: List[int]\n\t\t\t"""\n\t\t\t#Cumulative sum\n\t\t\tself.list = [0] * len(w)\n\n\t\t\ts = 0\n\t\t\tfor i, n in enumerate(w):\n\t\t\t\ts += n\n\t\t\t\tself.list[... | 2 | Given an array of integers `nums`, sort the array in ascending order and return it.
You must solve the problem **without using any built-in** functions in `O(nlog(n))` time complexity and with the smallest space complexity possible.
**Example 1:**
**Input:** nums = \[5,2,3,1\]
**Output:** \[1,2,3,5\]
**Explanation:*... | null |
Better than 96.5% | random-pick-with-weight | 0 | 1 | Should be O(N) init and O(1) for pickIndex and O(1) extra space as the list will always be constant size. Definitely went a different approach than the solution\n```\nclass Solution:\n\n def __init__(self, w: List[int]):\n self.li = []\n ma = sum(w)\n \n for i, weight in enumerate(w):\n ... | 8 | You are given a **0-indexed** array of positive integers `w` where `w[i]` describes the **weight** of the `ith` index.
You need to implement the function `pickIndex()`, which **randomly** picks an index in the range `[0, w.length - 1]` (**inclusive**) and returns it. The **probability** of picking an index `i` is `w[i... | We can transform the linked list to an array this should ease things up After transforming the linked list to an array it becomes as easy as swapping two integers in an array then rebuilding the linked list |
Better than 96.5% | random-pick-with-weight | 0 | 1 | Should be O(N) init and O(1) for pickIndex and O(1) extra space as the list will always be constant size. Definitely went a different approach than the solution\n```\nclass Solution:\n\n def __init__(self, w: List[int]):\n self.li = []\n ma = sum(w)\n \n for i, weight in enumerate(w):\n ... | 8 | Given an array of integers `nums`, sort the array in ascending order and return it.
You must solve the problem **without using any built-in** functions in `O(nlog(n))` time complexity and with the smallest space complexity possible.
**Example 1:**
**Input:** nums = \[5,2,3,1\]
**Output:** \[1,2,3,5\]
**Explanation:*... | null |
528: Solution with step by step explanation | random-pick-with-weight | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define an __init__ method that initializes the class with two instance variables:\nprefix_sum: A list that stores the cumulative sum of weights up to each index in w. Initialize prefix_sum to an empty list.\ntotal_sum: Th... | 3 | You are given a **0-indexed** array of positive integers `w` where `w[i]` describes the **weight** of the `ith` index.
You need to implement the function `pickIndex()`, which **randomly** picks an index in the range `[0, w.length - 1]` (**inclusive**) and returns it. The **probability** of picking an index `i` is `w[i... | We can transform the linked list to an array this should ease things up After transforming the linked list to an array it becomes as easy as swapping two integers in an array then rebuilding the linked list |
528: Solution with step by step explanation | random-pick-with-weight | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define an __init__ method that initializes the class with two instance variables:\nprefix_sum: A list that stores the cumulative sum of weights up to each index in w. Initialize prefix_sum to an empty list.\ntotal_sum: Th... | 3 | Given an array of integers `nums`, sort the array in ascending order and return it.
You must solve the problem **without using any built-in** functions in `O(nlog(n))` time complexity and with the smallest space complexity possible.
**Example 1:**
**Input:** nums = \[5,2,3,1\]
**Output:** \[1,2,3,5\]
**Explanation:*... | null |
Python | BinarySearch for accumulation of weight | random-pick-with-weight | 0 | 1 | ```python\nfrom bisect import bisect_left\nfrom itertools import accumulate\nfrom random import random\n\nclass Solution:\n def __init__(self, w):\n self.acc = list(accumulate(w))\n\n def pickIndex(self) -> int:\n return bisect_left(self.acc, random() * self.acc[-1])\n``` | 2 | You are given a **0-indexed** array of positive integers `w` where `w[i]` describes the **weight** of the `ith` index.
You need to implement the function `pickIndex()`, which **randomly** picks an index in the range `[0, w.length - 1]` (**inclusive**) and returns it. The **probability** of picking an index `i` is `w[i... | We can transform the linked list to an array this should ease things up After transforming the linked list to an array it becomes as easy as swapping two integers in an array then rebuilding the linked list |
Python | BinarySearch for accumulation of weight | random-pick-with-weight | 0 | 1 | ```python\nfrom bisect import bisect_left\nfrom itertools import accumulate\nfrom random import random\n\nclass Solution:\n def __init__(self, w):\n self.acc = list(accumulate(w))\n\n def pickIndex(self) -> int:\n return bisect_left(self.acc, random() * self.acc[-1])\n``` | 2 | Given an array of integers `nums`, sort the array in ascending order and return it.
You must solve the problem **without using any built-in** functions in `O(nlog(n))` time complexity and with the smallest space complexity possible.
**Example 1:**
**Input:** nums = \[5,2,3,1\]
**Output:** \[1,2,3,5\]
**Explanation:*... | null |
Python3 solution || quibler7 | random-pick-with-weight | 0 | 1 | # Code\n```\nclass Solution:\n\n def __init__(self, w: List[int]):\n sum = 0\n self.a = []\n for i in w:\n sum += i\n self.a.append(sum)\n self.total = sum \n \n\n def pickIndex(self) -> int:\n ran = random.randint(1, self.total)\n return bise... | 2 | You are given a **0-indexed** array of positive integers `w` where `w[i]` describes the **weight** of the `ith` index.
You need to implement the function `pickIndex()`, which **randomly** picks an index in the range `[0, w.length - 1]` (**inclusive**) and returns it. The **probability** of picking an index `i` is `w[i... | We can transform the linked list to an array this should ease things up After transforming the linked list to an array it becomes as easy as swapping two integers in an array then rebuilding the linked list |
Python3 solution || quibler7 | random-pick-with-weight | 0 | 1 | # Code\n```\nclass Solution:\n\n def __init__(self, w: List[int]):\n sum = 0\n self.a = []\n for i in w:\n sum += i\n self.a.append(sum)\n self.total = sum \n \n\n def pickIndex(self) -> int:\n ran = random.randint(1, self.total)\n return bise... | 2 | Given an array of integers `nums`, sort the array in ascending order and return it.
You must solve the problem **without using any built-in** functions in `O(nlog(n))` time complexity and with the smallest space complexity possible.
**Example 1:**
**Input:** nums = \[5,2,3,1\]
**Output:** \[1,2,3,5\]
**Explanation:*... | null |
99.8% faster solution in Python with explanation | random-pick-with-weight | 0 | 1 | We get a random number (float) from 0 to 1.\nThen, we add the weights cumulatively and divide by the sum of weights and maintain them in an array.\nExample:\nw: [1,2,3,4]\nSum of wlwments in w: 10\nNormalised weights: [0.1, 0.2, 0.3, 0.4]\nCumulative weights: [0.1, 0.3, 0.6, 1.0]\n\nNow, we get a random number between ... | 6 | You are given a **0-indexed** array of positive integers `w` where `w[i]` describes the **weight** of the `ith` index.
You need to implement the function `pickIndex()`, which **randomly** picks an index in the range `[0, w.length - 1]` (**inclusive**) and returns it. The **probability** of picking an index `i` is `w[i... | We can transform the linked list to an array this should ease things up After transforming the linked list to an array it becomes as easy as swapping two integers in an array then rebuilding the linked list |
99.8% faster solution in Python with explanation | random-pick-with-weight | 0 | 1 | We get a random number (float) from 0 to 1.\nThen, we add the weights cumulatively and divide by the sum of weights and maintain them in an array.\nExample:\nw: [1,2,3,4]\nSum of wlwments in w: 10\nNormalised weights: [0.1, 0.2, 0.3, 0.4]\nCumulative weights: [0.1, 0.3, 0.6, 1.0]\n\nNow, we get a random number between ... | 6 | Given an array of integers `nums`, sort the array in ascending order and return it.
You must solve the problem **without using any built-in** functions in `O(nlog(n))` time complexity and with the smallest space complexity possible.
**Example 1:**
**Input:** nums = \[5,2,3,1\]
**Output:** \[1,2,3,5\]
**Explanation:*... | null |
529: Solution with step by step explanation | minesweeper | 0 | 1 | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Check if the clicked cell is a mine.\n2. Define directions to search for mines using a list of tuples containing the row and column offsets.\n3. Define a helper function countMines that takes the row and column of a cell ... | 3 | Let's play the minesweeper game ([Wikipedia](https://en.wikipedia.org/wiki/Minesweeper_(video_game)), [online game](http://minesweeperonline.com))!
You are given an `m x n` char matrix `board` representing the game board where:
* `'M'` represents an unrevealed mine,
* `'E'` represents an unrevealed empty square,
... | null |
✅[Python] Simple and Clean, beats 88%✅ | minesweeper | 0 | 1 | ### Please upvote if you find this helpful. \u270C\n<img src="https://assets.leetcode.com/users/images/b8e25620-d320-420a-ae09-94c7453bd033_1678818986.7001078.jpeg" alt="Cute Robot - Stable diffusion" width="200"/>\n\n# Intuition\nThe Minesweeper problem involves updating a game board based on a given click position. T... | 3 | Let's play the minesweeper game ([Wikipedia](https://en.wikipedia.org/wiki/Minesweeper_(video_game)), [online game](http://minesweeperonline.com))!
You are given an `m x n` char matrix `board` representing the game board where:
* `'M'` represents an unrevealed mine,
* `'E'` represents an unrevealed empty square,
... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.