slug_name
stringlengths
6
77
meta_info
dict
id
stringlengths
1
4
difficulty
stringclasses
3 values
pretty_content
listlengths
0
1
solutions
listlengths
2
121
prompt
stringlengths
107
1.32k
generator_code
stringclasses
1 value
convert_online
stringclasses
1 value
convert_offline
stringclasses
1 value
evaluate_offline
stringclasses
1 value
entry_point
stringclasses
1 value
test_cases
stringclasses
1 value
acRate
float64
13.8
89.6
Difficulty Level
stringclasses
5 values
demons
stringclasses
1 value
transpose-matrix
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a 2D integer array <code>matrix</code>, return <em>the <strong>transpose</strong> of</em> <code>matrix</code>.</p>\n\n<p>The <strong>transpose</strong> of a matrix is the matrix flipped over its main diagonal, switching the matrix&#39;s row and column indices.</p>\n\n<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/02/10/hint_transpose.png\" style=\"width: 600px; height: 197px;\" /></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> matrix = [[1,2,3],[4,5,6],[7,8,9]]\n<strong>Output:</strong> [[1,4,7],[2,5,8],[3,6,9]]\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> matrix = [[1,2,3],[4,5,6]]\n<strong>Output:</strong> [[1,4],[2,5],[3,6]]\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>m == matrix.length</code></li>\n\t<li><code>n == matrix[i].length</code></li>\n\t<li><code>1 &lt;= m, n &lt;= 1000</code></li>\n\t<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>\n\t<li><code>-10<sup>9</sup> &lt;= matrix[i][j] &lt;= 10<sup>9</sup></code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "867", "questionId": "898", "questionTitle": "Transpose Matrix", "questionTitleSlug": "transpose-matrix", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"277.4K\", \"totalSubmission\": \"418.7K\", \"totalAcceptedRaw\": 277362, \"totalSubmissionRaw\": 418716, \"acRate\": \"66.2%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Matrix", "slug": "matrix" }, { "name": "Simulation", "slug": "simulation" } ] } } }
867
Easy
[ "Given a 2D integer array matrix, return the transpose of matrix.\n\nThe transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.\n\n\n\n \nExample 1:\n\nInput: matrix = [[1,2,3],[4,5,6],[7,8,9]]\nOutput: [[1,4,7],[2,5,8],[3,6,9]]\n\n\nExample 2:\n\nInput: m...
[ { "hash": 3583749731329360400, "runtime": "77ms", "solution": "class Solution:\n def transpose(self, matrix: List[List[int]]) -> List[List[int]]:\n result = [[0 for _ in range(len(matrix))] for _ in range(len(matrix[0]))]\n for i in range(len(matrix)):\n for j in range(len(ma...
class Solution(object): def transpose(self, matrix): """ :type matrix: List[List[int]] :rtype: List[List[int]] """
None
None
None
None
None
None
66.2
Level 1
Hi
longest-subarray-with-maximum-bitwise-and
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an integer array <code>nums</code> of size <code>n</code>.</p>\n\n<p>Consider a <strong>non-empty</strong> subarray from <code>nums</code> that has the <strong>maximum</strong> possible <strong>bitwise AND</strong>.</p>\n\n<ul>\n\t<li>In other words, let <code>k</code> be the maximum value of the bitwise AND of <strong>any</strong> subarray of <code>nums</code>. Then, only subarrays with a bitwise AND equal to <code>k</code> should be considered.</li>\n</ul>\n\n<p>Return <em>the length of the <strong>longest</strong> such subarray</em>.</p>\n\n<p>The bitwise AND of an array is the bitwise AND of all the numbers in it.</p>\n\n<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,2,3,3,2,2]\n<strong>Output:</strong> 2\n<strong>Explanation:</strong>\nThe maximum possible bitwise AND of a subarray is 3.\nThe longest subarray with that value is [3,3], so we return 2.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,2,3,4]\n<strong>Output:</strong> 1\n<strong>Explanation:</strong>\nThe maximum possible bitwise AND of a subarray is 4.\nThe longest subarray with that value is [4], so we return 1.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2419", "questionId": "2503", "questionTitle": "Longest Subarray With Maximum Bitwise AND", "questionTitleSlug": "longest-subarray-with-maximum-bitwise-and", "similarQuestions": "[{\"title\": \"Number of Different Integers in a String\", \"titleSlug\": \"number-of-different-integers-in-a-string\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Remove Colored Pieces if Both Neighbors are the Same Color\", \"titleSlug\": \"remove-colored-pieces-if-both-neighbors-are-the-same-color\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Count Number of Maximum Bitwise-OR Subsets\", \"titleSlug\": \"count-number-of-maximum-bitwise-or-subsets\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Smallest Subarrays With Maximum Bitwise OR\", \"titleSlug\": \"smallest-subarrays-with-maximum-bitwise-or\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"25.8K\", \"totalSubmission\": \"52.4K\", \"totalAcceptedRaw\": 25762, \"totalSubmissionRaw\": 52417, \"acRate\": \"49.1%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Bit Manipulation", "slug": "bit-manipulation" }, { "name": "Brainteaser", "slug": "brainteaser" } ] } } }
2419
Medium
[ "You are given an integer array nums of size n.\n\nConsider a non-empty subarray from nums that has the maximum possible bitwise AND.\n\n\n\tIn other words, let k be the maximum value of the bitwise AND of any subarray of nums. Then, only subarrays with a bitwise AND equal to k should be considered.\n\n\nReturn the...
[ { "hash": -3421560099714726000, "runtime": "702ms", "solution": "class Solution(object):\n def longestSubarray(self, nums):\n max_and = max(nums)\n left, right = 0, 0\n res = 0\n while right < len(nums):\n if nums[right] & max_and == max_and:\n ri...
class Solution(object): def longestSubarray(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
49.1
Level 3
Hi
construct-string-with-repeat-limit
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a string <code>s</code> and an integer <code>repeatLimit</code>. Construct a new string <code>repeatLimitedString</code> using the characters of <code>s</code> such that no letter appears <strong>more than</strong> <code>repeatLimit</code> times <strong>in a row</strong>. You do <strong>not</strong> have to use all characters from <code>s</code>.</p>\n\n<p>Return <em>the <strong>lexicographically largest</strong> </em><code>repeatLimitedString</code> <em>possible</em>.</p>\n\n<p>A string <code>a</code> is <strong>lexicographically larger</strong> than a string <code>b</code> if in the first position where <code>a</code> and <code>b</code> differ, string <code>a</code> has a letter that appears later in the alphabet than the corresponding letter in <code>b</code>. If the first <code>min(a.length, b.length)</code> characters do not differ, then the longer string is the lexicographically larger one.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;cczazcc&quot;, repeatLimit = 3\n<strong>Output:</strong> &quot;zzcccac&quot;\n<strong>Explanation:</strong> We use all of the characters from s to construct the repeatLimitedString &quot;zzcccac&quot;.\nThe letter &#39;a&#39; appears at most 1 time in a row.\nThe letter &#39;c&#39; appears at most 3 times in a row.\nThe letter &#39;z&#39; appears at most 2 times in a row.\nHence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.\nThe string is the lexicographically largest repeatLimitedString possible so we return &quot;zzcccac&quot;.\nNote that the string &quot;zzcccca&quot; is lexicographically larger but the letter &#39;c&#39; appears more than 3 times in a row, so it is not a valid repeatLimitedString.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;aababab&quot;, repeatLimit = 2\n<strong>Output:</strong> &quot;bbabaa&quot;\n<strong>Explanation:</strong> We use only some of the characters from s to construct the repeatLimitedString &quot;bbabaa&quot;. \nThe letter &#39;a&#39; appears at most 2 times in a row.\nThe letter &#39;b&#39; appears at most 2 times in a row.\nHence, no letter appears more than repeatLimit times in a row and the string is a valid repeatLimitedString.\nThe string is the lexicographically largest repeatLimitedString possible so we return &quot;bbabaa&quot;.\nNote that the string &quot;bbabaaa&quot; is lexicographically larger but the letter &#39;a&#39; appears more than 2 times in a row, so it is not a valid repeatLimitedString.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= repeatLimit &lt;= s.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>s</code> consists of lowercase English letters.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2182", "questionId": "2300", "questionTitle": "Construct String With Repeat Limit", "questionTitleSlug": "construct-string-with-repeat-limit", "similarQuestions": "[{\"title\": \"Rearrange String k Distance Apart\", \"titleSlug\": \"rearrange-string-k-distance-apart\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"19.5K\", \"totalSubmission\": \"36.6K\", \"totalAcceptedRaw\": 19485, \"totalSubmissionRaw\": 36650, \"acRate\": \"53.2%\"}", "topicTags": [ { "name": "String", "slug": "string" }, { "name": "Greedy", "slug": "greedy" }, { "name": "Heap (Priority Queue)", "slug": "heap-priority-queue" }, { "name": "Counting", "slug": "counting" } ] } } }
2182
Medium
[ "You are given a string s and an integer repeatLimit. Construct a new string repeatLimitedString using the characters of s such that no letter appears more than repeatLimit times in a row. You do not have to use all characters from s.\n\nReturn the lexicographically largest repeatLimitedString possible.\n\nA string...
[ { "hash": 7153174541604722000, "runtime": "508ms", "solution": "class Solution(object):\n def repeatLimitedString(self, s, repeatLimit):\n \"\"\"\n :type s: str\n :type repeatLimit: int\n :rtype: str\n \"\"\"\n counts = Counter(s)\n heap = [(26 - ord(l...
class Solution(object): def repeatLimitedString(self, s, repeatLimit): """ :type s: str :type repeatLimit: int :rtype: str """
None
None
None
None
None
None
53.2
Level 3
Hi
most-visited-sector-in-a-circular-track
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer <code>n</code> and an integer array <code>rounds</code>. We have a circular track which consists of <code>n</code> sectors labeled from <code>1</code> to <code>n</code>. A marathon will be held on this track, the marathon consists of <code>m</code> rounds. The <code>i<sup>th</sup></code> round starts at sector <code>rounds[i - 1]</code> and ends at sector <code>rounds[i]</code>. For example, round 1 starts at sector <code>rounds[0]</code> and ends at sector <code>rounds[1]</code></p>\n\n<p>Return <em>an array of the most visited sectors</em> sorted in <strong>ascending</strong> order.</p>\n\n<p>Notice that you circulate the track in ascending order of sector numbers in the counter-clockwise direction (See the first example).</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/08/14/tmp.jpg\" style=\"width: 433px; height: 341px;\" />\n<pre>\n<strong>Input:</strong> n = 4, rounds = [1,3,1,2]\n<strong>Output:</strong> [1,2]\n<strong>Explanation:</strong> The marathon starts at sector 1. The order of the visited sectors is as follows:\n1 --&gt; 2 --&gt; 3 (end of round 1) --&gt; 4 --&gt; 1 (end of round 2) --&gt; 2 (end of round 3 and the marathon)\nWe can see that both sectors 1 and 2 are visited twice and they are the most visited sectors. Sectors 3 and 4 are visited only once.</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 2, rounds = [2,1,2,1,2,1,2,1,2]\n<strong>Output:</strong> [2]\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 7, rounds = [1,3,5,7]\n<strong>Output:</strong> [1,2,3,4,5,6,7]\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= n &lt;= 100</code></li>\n\t<li><code>1 &lt;= m &lt;= 100</code></li>\n\t<li><code>rounds.length == m + 1</code></li>\n\t<li><code>1 &lt;= rounds[i] &lt;= n</code></li>\n\t<li><code>rounds[i] != rounds[i + 1]</code> for <code>0 &lt;= i &lt; m</code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "1560", "questionId": "1682", "questionTitle": "Most Visited Sector in a Circular Track", "questionTitleSlug": "most-visited-sector-in-a-circular-track", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"29.8K\", \"totalSubmission\": \"51K\", \"totalAcceptedRaw\": 29804, \"totalSubmissionRaw\": 50956, \"acRate\": \"58.5%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Simulation", "slug": "simulation" } ] } } }
1560
Easy
[ "Given an integer n and an integer array rounds. We have a circular track which consists of n sectors labeled from 1 to n. A marathon will be held on this track, the marathon consists of m rounds. The ith round starts at sector rounds[i - 1] and ends at sector rounds[i]. For example, round 1 starts at sector rounds...
[ { "hash": -9101536571257537000, "runtime": "52ms", "solution": "class Solution(object):\n def mostVisited(self, n, rounds):\n \"\"\"\n :type n: int\n :type rounds: List[int]\n :rtype: List[int]\n \"\"\"\n sectors = [0 for _ in range(n)]\n max_usage = 0...
class Solution(object): def mostVisited(self, n, rounds): """ :type n: int :type rounds: List[int] :rtype: List[int] """
None
None
None
None
None
None
58.5
Level 1
Hi
build-array-where-you-can-find-the-maximum-exactly-k-comparisons
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given three integers <code>n</code>, <code>m</code> and <code>k</code>. Consider the following algorithm to find the maximum element of an array of positive integers:</p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/04/02/e.png\" style=\"width: 424px; height: 372px;\" />\n<p>You should build the array arr which has the following properties:</p>\n\n<ul>\n\t<li><code>arr</code> has exactly <code>n</code> integers.</li>\n\t<li><code>1 &lt;= arr[i] &lt;= m</code> where <code>(0 &lt;= i &lt; n)</code>.</li>\n\t<li>After applying the mentioned algorithm to <code>arr</code>, the value <code>search_cost</code> is equal to <code>k</code>.</li>\n</ul>\n\n<p>Return <em>the number of ways</em> to build the array <code>arr</code> under the mentioned conditions. As the answer may grow large, the answer <strong>must be</strong> computed modulo <code>10<sup>9</sup> + 7</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 2, m = 3, k = 1\n<strong>Output:</strong> 6\n<strong>Explanation:</strong> The possible arrays are [1, 1], [2, 1], [2, 2], [3, 1], [3, 2] [3, 3]\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 5, m = 2, k = 3\n<strong>Output:</strong> 0\n<strong>Explanation:</strong> There are no possible arrays that satisfy the mentioned conditions.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 9, m = 1, k = 1\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> The only possible array is [1, 1, 1, 1, 1, 1, 1, 1, 1]\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= n &lt;= 50</code></li>\n\t<li><code>1 &lt;= m &lt;= 100</code></li>\n\t<li><code>0 &lt;= k &lt;= n</code></li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "1420", "questionId": "1535", "questionTitle": "Build Array Where You Can Find The Maximum Exactly K Comparisons", "questionTitleSlug": "build-array-where-you-can-find-the-maximum-exactly-k-comparisons", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"60.9K\", \"totalSubmission\": \"87.4K\", \"totalAcceptedRaw\": 60896, \"totalSubmissionRaw\": 87418, \"acRate\": \"69.7%\"}", "topicTags": [ { "name": "Dynamic Programming", "slug": "dynamic-programming" }, { "name": "Prefix Sum", "slug": "prefix-sum" } ] } } }
1420
Hard
[ "You are given three integers n, m and k. Consider the following algorithm to find the maximum element of an array of positive integers:\n\nYou should build the array arr which has the following properties:\n\n\n\tarr has exactly n integers.\n\t1 <= arr[i] <= m where (0 <= i < n).\n\tAfter applying the mentioned al...
[ { "hash": 1046999169287209300, "runtime": "1084ms", "solution": "class Solution(object):\n def numOfArrays(self, n, m, k):\n \"\"\"\n :type n: int\n :type m: int\n :type k: intå\n :rtype: int\n \"\"\"\n mod = 10**9+7\n dp = [[[0 for _ in range(k...
class Solution(object): def numOfArrays(self, n, m, k): """ :type n: int :type m: int :type k: int :rtype: int """
None
None
None
None
None
None
69.7
Level 5
Hi
find-duplicate-subtrees
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given the <code>root</code>&nbsp;of a binary tree, return all <strong>duplicate subtrees</strong>.</p>\n\n<p>For each kind of duplicate subtrees, you only need to return the root node of any <b>one</b> of them.</p>\n\n<p>Two trees are <strong>duplicate</strong> if they have the <strong>same structure</strong> with the <strong>same node values</strong>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/08/16/e1.jpg\" style=\"width: 450px; height: 354px;\" />\n<pre>\n<strong>Input:</strong> root = [1,2,3,4,null,2,4,null,null,4]\n<strong>Output:</strong> [[2,4],[4]]\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/08/16/e2.jpg\" style=\"width: 321px; height: 201px;\" />\n<pre>\n<strong>Input:</strong> root = [2,1,1]\n<strong>Output:</strong> [[1]]\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/08/16/e33.jpg\" style=\"width: 450px; height: 303px;\" />\n<pre>\n<strong>Input:</strong> root = [2,2,2,3,null,3,null]\n<strong>Output:</strong> [[2,3],[3]]\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li>The number of the nodes in the tree will be in the range <code>[1, 5000]</code></li>\n\t<li><code>-200 &lt;= Node.val &lt;= 200</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "652", "questionId": "652", "questionTitle": "Find Duplicate Subtrees", "questionTitleSlug": "find-duplicate-subtrees", "similarQuestions": "[{\"title\": \"Serialize and Deserialize Binary Tree\", \"titleSlug\": \"serialize-and-deserialize-binary-tree\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Serialize and Deserialize BST\", \"titleSlug\": \"serialize-and-deserialize-bst\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Construct String from Binary Tree\", \"titleSlug\": \"construct-string-from-binary-tree\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Delete Duplicate Folders in System\", \"titleSlug\": \"delete-duplicate-folders-in-system\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"248.1K\", \"totalSubmission\": \"418.8K\", \"totalAcceptedRaw\": 248118, \"totalSubmissionRaw\": 418821, \"acRate\": \"59.2%\"}", "topicTags": [ { "name": "Hash Table", "slug": "hash-table" }, { "name": "Tree", "slug": "tree" }, { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Binary Tree", "slug": "binary-tree" } ] } } }
652
Medium
[ "Given the root of a binary tree, return all duplicate subtrees.\n\nFor each kind of duplicate subtrees, you only need to return the root node of any one of them.\n\nTwo trees are duplicate if they have the same structure with the same node values.\n\n \nExample 1:\n\nInput: root = [1,2,3,4,null,2,4,null,null,4]\nO...
[ { "hash": -8463940226813298000, "runtime": "74ms", "solution": "# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution(object):\n def f...
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution(object): def findDuplicateSubtrees(self, root): """ :type root: TreeNode :rtype: List[TreeNode] """
None
None
None
None
None
None
59.2
Level 3
Hi
furthest-point-from-origin
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a string <code>moves</code> of length <code>n</code> consisting only of characters <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, and <code>&#39;_&#39;</code>. The string represents your movement on a number line starting from the origin <code>0</code>.</p>\n\n<p>In the <code>i<sup>th</sup></code> move, you can choose one of the following directions:</p>\n\n<ul>\n\t<li>move to the left if <code>moves[i] = &#39;L&#39;</code> or <code>moves[i] = &#39;_&#39;</code></li>\n\t<li>move to the right if <code>moves[i] = &#39;R&#39;</code> or <code>moves[i] = &#39;_&#39;</code></li>\n</ul>\n\n<p>Return <em>the <strong>distance from the origin</strong> of the <strong>furthest</strong> point you can get to after </em><code>n</code><em> moves</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> moves = &quot;L_RL__R&quot;\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> The furthest point we can reach from the origin 0 is point -3 through the following sequence of moves &quot;LLRLLLR&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> moves = &quot;_R__LL_&quot;\n<strong>Output:</strong> 5\n<strong>Explanation:</strong> The furthest point we can reach from the origin 0 is point -5 through the following sequence of moves &quot;LRLLLLL&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> moves = &quot;_______&quot;\n<strong>Output:</strong> 7\n<strong>Explanation:</strong> The furthest point we can reach from the origin 0 is point 7 through the following sequence of moves &quot;RRRRRRR&quot;.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= moves.length == n &lt;= 50</code></li>\n\t<li><code>moves</code> consists only of characters <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code> and <code>&#39;_&#39;</code>.</li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "2833", "questionId": "3019", "questionTitle": "Furthest Point From Origin", "questionTitleSlug": "furthest-point-from-origin", "similarQuestions": "[{\"title\": \"Robot Return to Origin\", \"titleSlug\": \"robot-return-to-origin\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"39.6K\", \"totalSubmission\": \"62.6K\", \"totalAcceptedRaw\": 39583, \"totalSubmissionRaw\": 62559, \"acRate\": \"63.3%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Counting", "slug": "counting" } ] } } }
2833
Easy
[ "You are given a string moves of length n consisting only of characters 'L', 'R', and '_'. The string represents your movement on a number line starting from the origin 0.\n\nIn the ith move, you can choose one of the following directions:\n\n\n\tmove to the left if moves[i] = 'L' or moves[i] = '_'\n\tmove to the r...
[ { "hash": -6596906841188622000, "runtime": "25ms", "solution": "class Solution(object):\n def furthestDistanceFromOrigin(self, moves):\n \"\"\"\n :type moves: str\n :rtype: int\n \"\"\"\n return moves.count(\"_\") + abs(moves.count(\"L\") - moves.count(\"R\"))\n ...
class Solution(object): def furthestDistanceFromOrigin(self, moves): """ :type moves: str :rtype: int """
None
None
None
None
None
None
63.3
Level 1
Hi
tuple-with-same-product
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an array <code>nums</code> of <strong>distinct</strong> positive integers, return <em>the number of tuples </em><code>(a, b, c, d)</code><em> such that </em><code>a * b = c * d</code><em> where </em><code>a</code><em>, </em><code>b</code><em>, </em><code>c</code><em>, and </em><code>d</code><em> are elements of </em><code>nums</code><em>, and </em><code>a != b != c != d</code><em>.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [2,3,4,6]\n<strong>Output:</strong> 8\n<strong>Explanation:</strong> There are 8 valid tuples:\n(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6,2,4,3)\n(3,4,2,6) , (4,3,2,6) , (3,4,6,2) , (4,3,6,2)\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,2,4,5,10]\n<strong>Output:</strong> 16\n<strong>Explanation:</strong> There are 16 valid tuples:\n(1,10,2,5) , (1,10,5,2) , (10,1,2,5) , (10,1,5,2)\n(2,5,1,10) , (2,5,10,1) , (5,2,1,10) , (5,2,10,1)\n(2,10,4,5) , (2,10,5,4) , (10,2,4,5) , (10,2,5,4)\n(4,5,2,10) , (4,5,10,2) , (5,4,2,10) , (5,4,10,2)\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 1000</code></li>\n\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>\n\t<li>All elements in <code>nums</code> are <strong>distinct</strong>.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1726", "questionId": "1364", "questionTitle": "Tuple with Same Product", "questionTitleSlug": "tuple-with-same-product", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"26K\", \"totalSubmission\": \"42.6K\", \"totalAcceptedRaw\": 25953, \"totalSubmissionRaw\": 42588, \"acRate\": \"60.9%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" } ] } } }
1726
Medium
[ "Given an array nums of distinct positive integers, return the number of tuples (a, b, c, d) such that a * b = c * d where a, b, c, and d are elements of nums, and a != b != c != d.\n\n \nExample 1:\n\nInput: nums = [2,3,4,6]\nOutput: 8\nExplanation: There are 8 valid tuples:\n(2,6,3,4) , (2,6,4,3) , (6,2,3,4) , (6...
[ { "hash": 1665776563962331600, "runtime": "775ms", "solution": "# \n\nclass Solution(object):\n\tdef tupleSameProduct(self, nums):\n\t\tnums = sorted(nums)\n\t\tcount = collections.Counter()\n\t\tres = 0\n\t\tfor i, n in enumerate(nums):\n\t\t\tfor j in range(i):\n\t\t\t\tp = nums[j] * n\n\t\t\t\tres +=...
class Solution(object): def tupleSameProduct(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
60.9
Level 2
Hi
make-array-strictly-increasing
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given two integer arrays&nbsp;<code>arr1</code> and <code>arr2</code>, return the minimum number of operations (possibly zero) needed&nbsp;to make <code>arr1</code> strictly increasing.</p>\n\n<p>In one operation, you can choose two indices&nbsp;<code>0 &lt;=&nbsp;i &lt; arr1.length</code>&nbsp;and&nbsp;<code>0 &lt;= j &lt; arr2.length</code>&nbsp;and do the assignment&nbsp;<code>arr1[i] = arr2[j]</code>.</p>\n\n<p>If there is no way to make&nbsp;<code>arr1</code>&nbsp;strictly increasing,&nbsp;return&nbsp;<code>-1</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> Replace <code>5</code> with <code>2</code>, then <code>arr1 = [1, 2, 3, 6, 7]</code>.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr1 = [1,5,3,6,7], arr2 = [4,3,1]\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> Replace <code>5</code> with <code>3</code> and then replace <code>3</code> with <code>4</code>. <code>arr1 = [1, 3, 4, 6, 7]</code>.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]\n<strong>Output:</strong> -1\n<strong>Explanation:</strong> You can&#39;t make <code>arr1</code> strictly increasing.</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= arr1.length, arr2.length &lt;= 2000</code></li>\n\t<li><code>0 &lt;= arr1[i], arr2[i] &lt;= 10^9</code></li>\n</ul>\n\n<p>&nbsp;</p>\n", "difficulty": "Hard", "questionFrontendId": "1187", "questionId": "1290", "questionTitle": "Make Array Strictly Increasing", "questionTitleSlug": "make-array-strictly-increasing", "similarQuestions": "[{\"title\": \"Make Array Non-decreasing or Non-increasing\", \"titleSlug\": \"make-array-non-decreasing-or-non-increasing\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"55.5K\", \"totalSubmission\": \"94.7K\", \"totalAcceptedRaw\": 55549, \"totalSubmissionRaw\": 94745, \"acRate\": \"58.6%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Binary Search", "slug": "binary-search" }, { "name": "Dynamic Programming", "slug": "dynamic-programming" }, { "name": "Sorting", "slug": "sorting" } ] } } }
1187
Hard
[ "Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing.\n\nIn one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and do the assignment arr1[i] = arr2[j].\n\nIf there is no way to make arr1 strictly...
[ { "hash": 1704261424397139700, "runtime": "446ms", "solution": "class Solution(object):\n def makeArrayIncreasing(self, arr1, arr2):\n dp = {-1: 0}\n arr2.sort()\n n = len(arr2)\n\n for i in range(len(arr1)):\n new_dp = {}\n for prev in dp:\n ...
class Solution(object): def makeArrayIncreasing(self, arr1, arr2): """ :type arr1: List[int] :type arr2: List[int] :rtype: int """
None
None
None
None
None
None
58.6
Level 5
Hi
eliminate-maximum-number-of-monsters
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are playing a video game where you are defending your city from a group of <code>n</code> monsters. You are given a <strong>0-indexed</strong> integer array <code>dist</code> of size <code>n</code>, where <code>dist[i]</code> is the <strong>initial distance</strong> in kilometers of the <code>i<sup>th</sup></code> monster from the city.</p>\n\n<p>The monsters walk toward the city at a <strong>constant</strong> speed. The speed of each monster is given to you in an integer array <code>speed</code> of size <code>n</code>, where <code>speed[i]</code> is the speed of the <code>i<sup>th</sup></code> monster in kilometers per minute.</p>\n\n<p>You have a weapon that, once fully charged, can eliminate a <strong>single</strong> monster. However, the weapon takes <strong>one minute</strong> to charge. The weapon is fully charged at the very start.</p>\n\n<p>You lose when any monster reaches your city. If a monster reaches the city at the exact moment the weapon is fully charged, it counts as a <strong>loss</strong>, and the game ends before you can use your weapon.</p>\n\n<p>Return <em>the <strong>maximum</strong> number of monsters that you can eliminate before you lose, or </em><code>n</code><em> if you can eliminate all the monsters before they reach the city.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> dist = [1,3,4], speed = [1,1,1]\n<strong>Output:</strong> 3\n<strong>Explanation:</strong>\nIn the beginning, the distances of the monsters are [1,3,4]. You eliminate the first monster.\nAfter a minute, the distances of the monsters are [X,2,3]. You eliminate the second monster.\nAfter a minute, the distances of the monsters are [X,X,2]. You eliminate the third monster.\nAll 3 monsters can be eliminated.</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> dist = [1,1,2,3], speed = [1,1,1,1]\n<strong>Output:</strong> 1\n<strong>Explanation:</strong>\nIn the beginning, the distances of the monsters are [1,1,2,3]. You eliminate the first monster.\nAfter a minute, the distances of the monsters are [X,0,1,2], so you lose.\nYou can only eliminate 1 monster.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> dist = [3,2,4], speed = [5,3,2]\n<strong>Output:</strong> 1\n<strong>Explanation:</strong>\nIn the beginning, the distances of the monsters are [3,2,4]. You eliminate the first monster.\nAfter a minute, the distances of the monsters are [X,0,2], so you lose.\nYou can only eliminate 1 monster.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>n == dist.length == speed.length</code></li>\n\t<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= dist[i], speed[i] &lt;= 10<sup>5</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1921", "questionId": "2049", "questionTitle": "Eliminate Maximum Number of Monsters", "questionTitleSlug": "eliminate-maximum-number-of-monsters", "similarQuestions": "[{\"title\": \"Minimum Health to Beat Game\", \"titleSlug\": \"minimum-health-to-beat-game\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Minimum Time to Kill All Monsters\", \"titleSlug\": \"minimum-time-to-kill-all-monsters\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"102.2K\", \"totalSubmission\": \"199.2K\", \"totalAcceptedRaw\": 102240, \"totalSubmissionRaw\": 199211, \"acRate\": \"51.3%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Greedy", "slug": "greedy" }, { "name": "Sorting", "slug": "sorting" } ] } } }
1921
Medium
[ "You are playing a video game where you are defending your city from a group of n monsters. You are given a 0-indexed integer array dist of size n, where dist[i] is the initial distance in kilometers of the ith monster from the city.\n\nThe monsters walk toward the city at a constant speed. The speed of each monste...
[ { "hash": 6681520657314349000, "runtime": "951ms", "solution": "class Solution(object):\n def eliminateMaximum(self, dist, speed):\n \"\"\"\n :type dist: List[int]\n :type speed: List[int]\n :rtype: int\n time[i] for ith monster to reach equals = dist[i]/speed[i]\n ...
class Solution(object): def eliminateMaximum(self, dist, speed): """ :type dist: List[int] :type speed: List[int] :rtype: int """
None
None
None
None
None
None
51.3
Level 3
Hi
maximum-number-of-moves-in-a-grid
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> <code>m x n</code> matrix <code>grid</code> consisting of <strong>positive</strong> integers.</p>\n\n<p>You can start at <strong>any</strong> cell in the first column of the matrix, and traverse the grid in the following way:</p>\n\n<ul>\n\t<li>From a cell <code>(row, col)</code>, you can move to any of the cells: <code>(row - 1, col + 1)</code>, <code>(row, col + 1)</code> and <code>(row + 1, col + 1)</code> such that the value of the cell you move to, should be <strong>strictly</strong> bigger than the value of the current cell.</li>\n</ul>\n\n<p>Return <em>the <strong>maximum</strong> number of <strong>moves</strong> that you can perform.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2023/04/11/yetgriddrawio-10.png\" style=\"width: 201px; height: 201px;\" />\n<pre>\n<strong>Input:</strong> grid = [[2,4,3,5],[5,4,9,3],[3,4,2,11],[10,9,13,15]]\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> We can start at the cell (0, 0) and make the following moves:\n- (0, 0) -&gt; (0, 1).\n- (0, 1) -&gt; (1, 2).\n- (1, 2) -&gt; (2, 3).\nIt can be shown that it is the maximum number of moves that can be made.</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2023/04/12/yetgrid4drawio.png\" />\n<strong>Input:</strong> grid = [[3,2,4],[2,1,9],[1,1,7]]\n<strong>Output:</strong> 0\n<strong>Explanation:</strong> Starting from any cell in the first column we cannot perform any moves.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>m == grid.length</code></li>\n\t<li><code>n == grid[i].length</code></li>\n\t<li><code>2 &lt;= m, n &lt;= 1000</code></li>\n\t<li><code>4 &lt;= m * n &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= grid[i][j] &lt;= 10<sup>6</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2684", "questionId": "2794", "questionTitle": "Maximum Number of Moves in a Grid", "questionTitleSlug": "maximum-number-of-moves-in-a-grid", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"21K\", \"totalSubmission\": \"46.7K\", \"totalAcceptedRaw\": 21015, \"totalSubmissionRaw\": 46659, \"acRate\": \"45.0%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Dynamic Programming", "slug": "dynamic-programming" }, { "name": "Matrix", "slug": "matrix" } ] } } }
2684
Medium
[ "You are given a 0-indexed m x n matrix grid consisting of positive integers.\n\nYou can start at any cell in the first column of the matrix, and traverse the grid in the following way:\n\n\n\tFrom a cell (row, col), you can move to any of the cells: (row - 1, col + 1), (row, col + 1) and (row + 1, col + 1) such th...
[ { "hash": -1174346403093790500, "runtime": "4539ms", "solution": "class Solution(object):\n def maxMoves(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n rows = len(grid)\n cols = len(grid[0])\n visit = set()\n direct...
class Solution(object): def maxMoves(self, grid): """ :type grid: List[List[int]] :rtype: int """
None
None
None
None
None
None
45
Level 4
Hi
count-the-number-of-complete-components
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an integer <code>n</code>. There is an <strong>undirected</strong> graph with <code>n</code> vertices, numbered from <code>0</code> to <code>n - 1</code>. You are given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting vertices <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>\n\n<p>Return <em>the number of <strong>complete connected components</strong> of the graph</em>.</p>\n\n<p>A <strong>connected component</strong> is a subgraph of a graph in which there exists a path between any two vertices, and no vertex of the subgraph shares an edge with a vertex outside of the subgraph.</p>\n\n<p>A connected component is said to be <b>complete</b> if there exists an edge between every pair of its vertices.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<p><strong class=\"example\"><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2023/04/11/screenshot-from-2023-04-11-23-31-23.png\" style=\"width: 671px; height: 270px;\" /></strong></p>\n\n<pre>\n<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[1,2],[3,4]]\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> From the picture above, one can see that all of the components of this graph are complete.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<p><strong class=\"example\"><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2023/04/11/screenshot-from-2023-04-11-23-32-00.png\" style=\"width: 671px; height: 270px;\" /></strong></p>\n\n<pre>\n<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[1,2],[3,4],[3,5]]\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> The component containing vertices 0, 1, and 2 is complete since there is an edge between every pair of two vertices. On the other hand, the component containing vertices 3, 4, and 5 is not complete since there is no edge between vertices 4 and 5. Thus, the number of complete components in this graph is 1.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= n &lt;= 50</code></li>\n\t<li><code>0 &lt;= edges.length &lt;= n * (n - 1) / 2</code></li>\n\t<li><code>edges[i].length == 2</code></li>\n\t<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt;= n - 1</code></li>\n\t<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>\n\t<li>There are no repeated edges.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2685", "questionId": "2793", "questionTitle": "Count the Number of Complete Components", "questionTitleSlug": "count-the-number-of-complete-components", "similarQuestions": "[{\"title\": \"Number of Connected Components in an Undirected Graph\", \"titleSlug\": \"number-of-connected-components-in-an-undirected-graph\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"20.1K\", \"totalSubmission\": \"31K\", \"totalAcceptedRaw\": 20121, \"totalSubmissionRaw\": 30987, \"acRate\": \"64.9%\"}", "topicTags": [ { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Breadth-First Search", "slug": "breadth-first-search" }, { "name": "Graph", "slug": "graph" } ] } } }
2685
Medium
[ "You are given an integer n. There is an undirected graph with n vertices, numbered from 0 to n - 1. You are given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting vertices ai and bi.\n\nReturn the number of complete connected components of the graph.\n\nA c...
[ { "hash": -722085971743297300, "runtime": "516ms", "solution": "class Solution(object):\n def countCompleteComponents(self, n, edges):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"\n\n ad = defaultdict(list)\n for i,j in...
class Solution(object): def countCompleteComponents(self, n, edges): """ :type n: int :type edges: List[List[int]] :rtype: int """
None
None
None
None
None
None
64.9
Level 2
Hi
last-day-where-you-can-still-cross
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There is a <strong>1-based</strong> binary matrix where <code>0</code> represents land and <code>1</code> represents water. You are given integers <code>row</code> and <code>col</code> representing the number of rows and columns in the matrix, respectively.</p>\n\n<p>Initially on day <code>0</code>, the <strong>entire</strong> matrix is <strong>land</strong>. However, each day a new cell becomes flooded with <strong>water</strong>. You are given a <strong>1-based</strong> 2D array <code>cells</code>, where <code>cells[i] = [r<sub>i</sub>, c<sub>i</sub>]</code> represents that on the <code>i<sup>th</sup></code> day, the cell on the <code>r<sub>i</sub><sup>th</sup></code> row and <code>c<sub>i</sub><sup>th</sup></code> column (<strong>1-based</strong> coordinates) will be covered with <strong>water</strong> (i.e., changed to <code>1</code>).</p>\n\n<p>You want to find the <strong>last</strong> day that it is possible to walk from the <strong>top</strong> to the <strong>bottom</strong> by only walking on land cells. You can start from <strong>any</strong> cell in the top row and end at <strong>any</strong> cell in the bottom row. You can only travel in the<strong> four</strong> cardinal directions (left, right, up, and down).</p>\n\n<p>Return <em>the <strong>last</strong> day where it is possible to walk from the <strong>top</strong> to the <strong>bottom</strong> by only walking on land cells</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/07/27/1.png\" style=\"width: 624px; height: 162px;\" />\n<pre>\n<strong>Input:</strong> row = 2, col = 2, cells = [[1,1],[2,1],[1,2],[2,2]]\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> The above image depicts how the matrix changes each day starting from day 0.\nThe last day where it is possible to cross from top to bottom is on day 2.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/07/27/2.png\" style=\"width: 504px; height: 178px;\" />\n<pre>\n<strong>Input:</strong> row = 2, col = 2, cells = [[1,1],[1,2],[2,1],[2,2]]\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> The above image depicts how the matrix changes each day starting from day 0.\nThe last day where it is possible to cross from top to bottom is on day 1.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/07/27/3.png\" style=\"width: 666px; height: 167px;\" />\n<pre>\n<strong>Input:</strong> row = 3, col = 3, cells = [[1,2],[2,1],[3,3],[2,2],[1,1],[1,3],[2,3],[3,2],[3,1]]\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> The above image depicts how the matrix changes each day starting from day 0.\nThe last day where it is possible to cross from top to bottom is on day 3.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= row, col &lt;= 2 * 10<sup>4</sup></code></li>\n\t<li><code>4 &lt;= row * col &lt;= 2 * 10<sup>4</sup></code></li>\n\t<li><code>cells.length == row * col</code></li>\n\t<li><code>1 &lt;= r<sub>i</sub> &lt;= row</code></li>\n\t<li><code>1 &lt;= c<sub>i</sub> &lt;= col</code></li>\n\t<li>All the values of <code>cells</code> are <strong>unique</strong>.</li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "1970", "questionId": "2101", "questionTitle": "Last Day Where You Can Still Cross", "questionTitleSlug": "last-day-where-you-can-still-cross", "similarQuestions": "[{\"title\": \"Bricks Falling When Hit\", \"titleSlug\": \"bricks-falling-when-hit\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Escape the Spreading Fire\", \"titleSlug\": \"escape-the-spreading-fire\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"53.4K\", \"totalSubmission\": \"85.6K\", \"totalAcceptedRaw\": 53365, \"totalSubmissionRaw\": 85585, \"acRate\": \"62.4%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Binary Search", "slug": "binary-search" }, { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Breadth-First Search", "slug": "breadth-first-search" }, { "name": "Union Find", "slug": "union-find" }, { "name": "Matrix", "slug": "matrix" } ] } } }
1970
Hard
[ "There is a 1-based binary matrix where 0 represents land and 1 represents water. You are given integers row and col representing the number of rows and columns in the matrix, respectively.\n\nInitially on day 0, the entire matrix is land. However, each day a new cell becomes flooded with water. You are given a 1-b...
[ { "hash": 1876004084844619000, "runtime": "2397ms", "solution": "class Solution(object):\n def latestDayToCross(self, row, col, cells):\n \"\"\"\n :type row: int\n :type col: int\n :type cells: List[List[int]]\n :rtype: int\n \"\"\"\n left, right = 1, ...
class Solution(object): def latestDayToCross(self, row, col, cells): """ :type row: int :type col: int :type cells: List[List[int]] :rtype: int """
None
None
None
None
None
None
62.4
Level 5
Hi
maximum-twin-sum-of-a-linked-list
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>In a linked list of size <code>n</code>, where <code>n</code> is <strong>even</strong>, the <code>i<sup>th</sup></code> node (<strong>0-indexed</strong>) of the linked list is known as the <strong>twin</strong> of the <code>(n-1-i)<sup>th</sup></code> node, if <code>0 &lt;= i &lt;= (n / 2) - 1</code>.</p>\n\n<ul>\n\t<li>For example, if <code>n = 4</code>, then node <code>0</code> is the twin of node <code>3</code>, and node <code>1</code> is the twin of node <code>2</code>. These are the only nodes with twins for <code>n = 4</code>.</li>\n</ul>\n\n<p>The <strong>twin sum </strong>is defined as the sum of a node and its twin.</p>\n\n<p>Given the <code>head</code> of a linked list with even length, return <em>the <strong>maximum twin sum</strong> of the linked list</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/12/03/eg1drawio.png\" style=\"width: 250px; height: 70px;\" />\n<pre>\n<strong>Input:</strong> head = [5,4,2,1]\n<strong>Output:</strong> 6\n<strong>Explanation:</strong>\nNodes 0 and 1 are the twins of nodes 3 and 2, respectively. All have twin sum = 6.\nThere are no other nodes with twins in the linked list.\nThus, the maximum twin sum of the linked list is 6. \n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/12/03/eg2drawio.png\" style=\"width: 250px; height: 70px;\" />\n<pre>\n<strong>Input:</strong> head = [4,2,2,3]\n<strong>Output:</strong> 7\n<strong>Explanation:</strong>\nThe nodes with twins present in this linked list are:\n- Node 0 is the twin of node 3 having a twin sum of 4 + 3 = 7.\n- Node 1 is the twin of node 2 having a twin sum of 2 + 2 = 4.\nThus, the maximum twin sum of the linked list is max(7, 4) = 7. \n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/12/03/eg3drawio.png\" style=\"width: 200px; height: 88px;\" />\n<pre>\n<strong>Input:</strong> head = [1,100000]\n<strong>Output:</strong> 100001\n<strong>Explanation:</strong>\nThere is only one node with a twin in the linked list having twin sum of 1 + 100000 = 100001.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li>The number of nodes in the list is an <strong>even</strong> integer in the range <code>[2, 10<sup>5</sup>]</code>.</li>\n\t<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2130", "questionId": "2236", "questionTitle": "Maximum Twin Sum of a Linked List", "questionTitleSlug": "maximum-twin-sum-of-a-linked-list", "similarQuestions": "[{\"title\": \"Reverse Linked List\", \"titleSlug\": \"reverse-linked-list\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Palindrome Linked List\", \"titleSlug\": \"palindrome-linked-list\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Middle of the Linked List\", \"titleSlug\": \"middle-of-the-linked-list\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"219.1K\", \"totalSubmission\": \"269.1K\", \"totalAcceptedRaw\": 219111, \"totalSubmissionRaw\": 269101, \"acRate\": \"81.4%\"}", "topicTags": [ { "name": "Linked List", "slug": "linked-list" }, { "name": "Two Pointers", "slug": "two-pointers" }, { "name": "Stack", "slug": "stack" } ] } } }
2130
Medium
[ "In a linked list of size n, where n is even, the ith node (0-indexed) of the linked list is known as the twin of the (n-1-i)th node, if 0 <= i <= (n / 2) - 1.\n\n\n\tFor example, if n = 4, then node 0 is the twin of node 3, and node 1 is the twin of node 2. These are the only nodes with twins for n = 4.\n\n\nThe t...
[ { "hash": -4065389821841659400, "runtime": "560ms", "solution": "# Definition for singly-linked list.\n# class ListNode(object):\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution(object):\n def pairSum(self, head):\n \"\"\"\n ...
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def pairSum(self, head): """ :type head: Optional[ListNode] :rtype: int """
None
None
None
None
None
None
81.4
Level 2
Hi
shortest-bridge
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an <code>n x n</code> binary matrix <code>grid</code> where <code>1</code> represents land and <code>0</code> represents water.</p>\n\n<p>An <strong>island</strong> is a 4-directionally connected group of <code>1</code>&#39;s not connected to any other <code>1</code>&#39;s. There are <strong>exactly two islands</strong> in <code>grid</code>.</p>\n\n<p>You may change <code>0</code>&#39;s to <code>1</code>&#39;s to connect the two islands to form <strong>one island</strong>.</p>\n\n<p>Return <em>the smallest number of </em><code>0</code><em>&#39;s you must flip to connect the two islands</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> grid = [[0,1],[1,0]]\n<strong>Output:</strong> 1\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> grid = [[0,1,0],[0,0,0],[0,0,1]]\n<strong>Output:</strong> 2\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]\n<strong>Output:</strong> 1\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>n == grid.length == grid[i].length</code></li>\n\t<li><code>2 &lt;= n &lt;= 100</code></li>\n\t<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>\n\t<li>There are exactly two islands in <code>grid</code>.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "934", "questionId": "971", "questionTitle": "Shortest Bridge", "questionTitleSlug": "shortest-bridge", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"191.1K\", \"totalSubmission\": \"331.2K\", \"totalAcceptedRaw\": 191103, \"totalSubmissionRaw\": 331187, \"acRate\": \"57.7%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Breadth-First Search", "slug": "breadth-first-search" }, { "name": "Matrix", "slug": "matrix" } ] } } }
934
Medium
[ "You are given an n x n binary matrix grid where 1 represents land and 0 represents water.\n\nAn island is a 4-directionally connected group of 1's not connected to any other 1's. There are exactly two islands in grid.\n\nYou may change 0's to 1's to connect the two islands to form one island.\n\nReturn the smalles...
[ { "hash": 1074915850534305800, "runtime": "1600ms", "solution": "import numpy as np\nclass Solution(object):\n def shortestBridge(self, grid):\n \n #find first island\n first_loc = (0, 0)\n for row_num, row in enumerate(grid):\n for col_num, location in enumerat...
class Solution(object): def shortestBridge(self, grid): """ :type grid: List[List[int]] :rtype: int """
None
None
None
None
None
None
57.7
Level 3
Hi
vowel-spellchecker
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a <code>wordlist</code>, we want to implement a spellchecker that converts a query word into a correct word.</p>\n\n<p>For a given <code>query</code> word, the spell checker handles two categories of spelling mistakes:</p>\n\n<ul>\n\t<li>Capitalization: If the query matches a word in the wordlist (<strong>case-insensitive</strong>), then the query word is returned with the same case as the case in the wordlist.\n\n\t<ul>\n\t\t<li>Example: <code>wordlist = [&quot;yellow&quot;]</code>, <code>query = &quot;YellOw&quot;</code>: <code>correct = &quot;yellow&quot;</code></li>\n\t\t<li>Example: <code>wordlist = [&quot;Yellow&quot;]</code>, <code>query = &quot;yellow&quot;</code>: <code>correct = &quot;Yellow&quot;</code></li>\n\t\t<li>Example: <code>wordlist = [&quot;yellow&quot;]</code>, <code>query = &quot;yellow&quot;</code>: <code>correct = &quot;yellow&quot;</code></li>\n\t</ul>\n\t</li>\n\t<li>Vowel Errors: If after replacing the vowels <code>(&#39;a&#39;, &#39;e&#39;, &#39;i&#39;, &#39;o&#39;, &#39;u&#39;)</code> of the query word with any vowel individually, it matches a word in the wordlist (<strong>case-insensitive</strong>), then the query word is returned with the same case as the match in the wordlist.\n\t<ul>\n\t\t<li>Example: <code>wordlist = [&quot;YellOw&quot;]</code>, <code>query = &quot;yollow&quot;</code>: <code>correct = &quot;YellOw&quot;</code></li>\n\t\t<li>Example: <code>wordlist = [&quot;YellOw&quot;]</code>, <code>query = &quot;yeellow&quot;</code>: <code>correct = &quot;&quot;</code> (no match)</li>\n\t\t<li>Example: <code>wordlist = [&quot;YellOw&quot;]</code>, <code>query = &quot;yllw&quot;</code>: <code>correct = &quot;&quot;</code> (no match)</li>\n\t</ul>\n\t</li>\n</ul>\n\n<p>In addition, the spell checker operates under the following precedence rules:</p>\n\n<ul>\n\t<li>When the query exactly matches a word in the wordlist (<strong>case-sensitive</strong>), you should return the same word back.</li>\n\t<li>When the query matches a word up to capitlization, you should return the first such match in the wordlist.</li>\n\t<li>When the query matches a word up to vowel errors, you should return the first such match in the wordlist.</li>\n\t<li>If the query has no matches in the wordlist, you should return the empty string.</li>\n</ul>\n\n<p>Given some <code>queries</code>, return a list of words <code>answer</code>, where <code>answer[i]</code> is the correct word for <code>query = queries[i]</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<pre><strong>Input:</strong> wordlist = [\"KiTe\",\"kite\",\"hare\",\"Hare\"], queries = [\"kite\",\"Kite\",\"KiTe\",\"Hare\",\"HARE\",\"Hear\",\"hear\",\"keti\",\"keet\",\"keto\"]\n<strong>Output:</strong> [\"kite\",\"KiTe\",\"KiTe\",\"Hare\",\"hare\",\"\",\"\",\"KiTe\",\"\",\"KiTe\"]\n</pre><p><strong class=\"example\">Example 2:</strong></p>\n<pre><strong>Input:</strong> wordlist = [\"yellow\"], queries = [\"YellOw\"]\n<strong>Output:</strong> [\"yellow\"]\n</pre>\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= wordlist.length, queries.length &lt;= 5000</code></li>\n\t<li><code>1 &lt;= wordlist[i].length, queries[i].length &lt;= 7</code></li>\n\t<li><code>wordlist[i]</code> and <code>queries[i]</code> consist only of only English letters.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "966", "questionId": "1006", "questionTitle": "Vowel Spellchecker", "questionTitleSlug": "vowel-spellchecker", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"40.1K\", \"totalSubmission\": \"78.1K\", \"totalAcceptedRaw\": 40116, \"totalSubmissionRaw\": 78066, \"acRate\": \"51.4%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" }, { "name": "String", "slug": "string" } ] } } }
966
Medium
[ "Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word.\n\nFor a given query word, the spell checker handles two categories of spelling mistakes:\n\n\n\tCapitalization: If the query matches a word in the wordlist (case-insensitive), then the query word is returned with...
[ { "hash": -2293509480545781200, "runtime": "495ms", "solution": "class Solution(object):\n def mixVowels(self, word):\n if word == \"\":\n return [\"\"]\n\n word = word.lower()\n\n vowels = {sym : 1 for sym in [\"a\", \"e\", \"i\", \"o\", \"u\"]}\n\n pos = None\...
class Solution(object): def spellchecker(self, wordlist, queries): """ :type wordlist: List[str] :type queries: List[str] :rtype: List[str] """
None
None
None
None
None
None
51.4
Level 3
Hi
flood-fill
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>An image is represented by an <code>m x n</code> integer grid <code>image</code> where <code>image[i][j]</code> represents the pixel value of the image.</p>\n\n<p>You are also given three integers <code>sr</code>, <code>sc</code>, and <code>color</code>. You should perform a <strong>flood fill</strong> on the image starting from the pixel <code>image[sr][sc]</code>.</p>\n\n<p>To perform a <strong>flood fill</strong>, consider the starting pixel, plus any pixels connected <strong>4-directionally</strong> to the starting pixel of the same color as the starting pixel, plus any pixels connected <strong>4-directionally</strong> to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with <code>color</code>.</p>\n\n<p>Return <em>the modified image after performing the flood fill</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/06/01/flood1-grid.jpg\" style=\"width: 613px; height: 253px;\" />\n<pre>\n<strong>Input:</strong> image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2\n<strong>Output:</strong> [[2,2,2],[2,2,0],[2,0,1]]\n<strong>Explanation:</strong> From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color.\nNote the bottom corner is not colored 2, because it is not 4-directionally connected to the starting pixel.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0\n<strong>Output:</strong> [[0,0,0],[0,0,0]]\n<strong>Explanation:</strong> The starting pixel is already colored 0, so no changes are made to the image.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>m == image.length</code></li>\n\t<li><code>n == image[i].length</code></li>\n\t<li><code>1 &lt;= m, n &lt;= 50</code></li>\n\t<li><code>0 &lt;= image[i][j], color &lt; 2<sup>16</sup></code></li>\n\t<li><code>0 &lt;= sr &lt; m</code></li>\n\t<li><code>0 &lt;= sc &lt; n</code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "733", "questionId": "733", "questionTitle": "Flood Fill", "questionTitleSlug": "flood-fill", "similarQuestions": "[{\"title\": \"Island Perimeter\", \"titleSlug\": \"island-perimeter\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"821.9K\", \"totalSubmission\": \"1.3M\", \"totalAcceptedRaw\": 821944, \"totalSubmissionRaw\": 1298474, \"acRate\": \"63.3%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Breadth-First Search", "slug": "breadth-first-search" }, { "name": "Matrix", "slug": "matrix" } ] } } }
733
Easy
[ "An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image.\n\nYou are also given three integers sr, sc, and color. You should perform a flood fill on the image starting from the pixel image[sr][sc].\n\nTo perform a flood fill, consider the starting pixel, plus...
[ { "hash": 582061128299372200, "runtime": "54ms", "solution": "class Solution:\n def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:\n \n rows, cols = len(image), len(image[0])\n\n def dfs(row, col, prev):\n if row < 0 or col < ...
class Solution(object): def floodFill(self, image, sr, sc, color): """ :type image: List[List[int]] :type sr: int :type sc: int :type color: int :rtype: List[List[int]] """
None
None
None
None
None
None
63.3
Level 1
Hi
paint-house-iii
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p>\n\n<p>A neighborhood is a maximal group of continuous houses that are painted with the same color.</p>\n\n<ul>\n\t<li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li>\n</ul>\n\n<p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p>\n\n<ul>\n\t<li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li>\n\t<li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li>\n</ul>\n\n<p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3\n<strong>Output:</strong> 9\n<strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1]\nThis array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}].\nCost of paint all houses (1 + 1 + 1 + 1 + 5) = 9.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3\n<strong>Output:</strong> 11\n<strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2]\nThis array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. \nCost of paint the first and last house (10 + 1) = 11.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3\n<strong>Output:</strong> -1\n<strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>m == houses.length == cost.length</code></li>\n\t<li><code>n == cost[i].length</code></li>\n\t<li><code>1 &lt;= m &lt;= 100</code></li>\n\t<li><code>1 &lt;= n &lt;= 20</code></li>\n\t<li><code>1 &lt;= target &lt;= m</code></li>\n\t<li><code>0 &lt;= houses[i] &lt;= n</code></li>\n\t<li><code>1 &lt;= cost[i][j] &lt;= 10<sup>4</sup></code></li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "1473", "questionId": "1583", "questionTitle": "Paint House III", "questionTitleSlug": "paint-house-iii", "similarQuestions": "[{\"title\": \"Number of Distinct Roll Sequences\", \"titleSlug\": \"number-of-distinct-roll-sequences\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"59.4K\", \"totalSubmission\": \"97.2K\", \"totalAcceptedRaw\": 59399, \"totalSubmissionRaw\": 97185, \"acRate\": \"61.1%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Dynamic Programming", "slug": "dynamic-programming" } ] } } }
1473
Hard
[ "There is a row of m houses in a small city, each house must be painted with one of the n colors (labeled from 1 to n), some houses that have been painted last summer should not be painted again.\n\nA neighborhood is a maximal group of continuous houses that are painted with the same color.\n\n\n\tFor example: hous...
[ { "hash": 1014433388814471400, "runtime": "379ms", "solution": "class Solution(object):\n def minCost(self, houses, cost, m, n, target):\n \"\"\"\n :type houses: List[int]\n :type cost: List[List[int]]\n :type m: int\n :type n: int\n :type target: int\n ...
class Solution(object): def minCost(self, houses, cost, m, n, target): """ :type houses: List[int] :type cost: List[List[int]] :type m: int :type n: int :type target: int :rtype: int """
None
None
None
None
None
None
61.1
Level 5
Hi
subarrays-distinct-element-sum-of-squares-ii
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed </strong>integer array <code>nums</code>.</p>\n\n<p>The <strong>distinct count</strong> of a subarray of <code>nums</code> is defined as:</p>\n\n<ul>\n\t<li>Let <code>nums[i..j]</code> be a subarray of <code>nums</code> consisting of all the indices from <code>i</code> to <code>j</code> such that <code>0 &lt;= i &lt;= j &lt; nums.length</code>. Then the number of distinct values in <code>nums[i..j]</code> is called the distinct count of <code>nums[i..j]</code>.</li>\n</ul>\n\n<p>Return <em>the sum of the <strong>squares</strong> of <strong>distinct counts</strong> of all subarrays of </em><code>nums</code>.</p>\n\n<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>\n\n<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>\n\n<p>&nbsp;</p>\n<p><strong>Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,2,1]\n<strong>Output:</strong> 15\n<strong>Explanation:</strong> Six possible subarrays are:\n[1]: 1 distinct value\n[2]: 1 distinct value\n[1]: 1 distinct value\n[1,2]: 2 distinct values\n[2,1]: 2 distinct values\n[1,2,1]: 2 distinct values\nThe sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> + 2<sup>2</sup> = 15.\n</pre>\n\n<p><strong>Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [2,2]\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> Three possible subarrays are:\n[2]: 1 distinct value\n[2]: 1 distinct value\n[2,2]: 1 distinct value\nThe sum of the squares of the distinct counts in all subarrays is equal to 1<sup>2</sup> + 1<sup>2</sup> + 1<sup>2</sup> = 3.</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "2916", "questionId": "3139", "questionTitle": "Subarrays Distinct Element Sum of Squares II", "questionTitleSlug": "subarrays-distinct-element-sum-of-squares-ii", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"1.9K\", \"totalSubmission\": \"11.6K\", \"totalAcceptedRaw\": 1930, \"totalSubmissionRaw\": 11610, \"acRate\": \"16.6%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Dynamic Programming", "slug": "dynamic-programming" }, { "name": "Binary Indexed Tree", "slug": "binary-indexed-tree" }, { "name": "Segment Tree", "slug": "segment-tree" } ] } } }
2916
Hard
[ "You are given a 0-indexed integer array nums.\n\nThe distinct count of a subarray of nums is defined as:\n\n\n\tLet nums[i..j] be a subarray of nums consisting of all the indices from i to j such that 0 <= i <= j < nums.length. Then the number of distinct values in nums[i..j] is called the distinct count of nums[i...
[ { "hash": -3106681045057277400, "runtime": "4192ms", "solution": "# Time: O(nlogn)\n# Space: O(n)\n\nimport collections\nfrom sortedcontainers import SortedList\n\n\n# math, fenwick tree\nclass Solution(object):\n def sumCounts(self, nums):\n \"\"\"\n :type nums: List[int]\n :rt...
class Solution(object): def sumCounts(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
16.6
Level 5
Hi
maximum-enemy-forts-that-can-be-captured
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> integer array <code>forts</code> of length <code>n</code> representing the positions of several forts. <code>forts[i]</code> can be <code>-1</code>, <code>0</code>, or <code>1</code> where:</p>\n\n<ul>\n\t<li><code>-1</code> represents there is <strong>no fort</strong> at the <code>i<sup>th</sup></code> position.</li>\n\t<li><code>0</code> indicates there is an <strong>enemy</strong> fort at the <code>i<sup>th</sup></code> position.</li>\n\t<li><code>1</code> indicates the fort at the <code>i<sup>th</sup></code> the position is under your command.</li>\n</ul>\n\n<p>Now you have decided to move your army from one of your forts at position <code>i</code> to an empty position <code>j</code> such that:</p>\n\n<ul>\n\t<li><code>0 &lt;= i, j &lt;= n - 1</code></li>\n\t<li>The army travels over enemy forts <strong>only</strong>. Formally, for all <code>k</code> where <code>min(i,j) &lt; k &lt; max(i,j)</code>, <code>forts[k] == 0.</code></li>\n</ul>\n\n<p>While moving the army, all the enemy forts that come in the way are <strong>captured</strong>.</p>\n\n<p>Return<em> the <strong>maximum</strong> number of enemy forts that can be captured</em>. In case it is <strong>impossible</strong> to move your army, or you do not have any fort under your command, return <code>0</code><em>.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> forts = [1,0,0,-1,0,0,0,0,1]\n<strong>Output:</strong> 4\n<strong>Explanation:</strong>\n- Moving the army from position 0 to position 3 captures 2 enemy forts, at 1 and 2.\n- Moving the army from position 8 to position 3 captures 4 enemy forts.\nSince 4 is the maximum number of enemy forts that can be captured, we return 4.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> forts = [0,0,1,-1]\n<strong>Output:</strong> 0\n<strong>Explanation:</strong> Since no enemy fort can be captured, 0 is returned.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= forts.length &lt;= 1000</code></li>\n\t<li><code>-1 &lt;= forts[i] &lt;= 1</code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "2511", "questionId": "2602", "questionTitle": "Maximum Enemy Forts That Can Be Captured", "questionTitleSlug": "maximum-enemy-forts-that-can-be-captured", "similarQuestions": "[{\"title\": \"Max Consecutive Ones\", \"titleSlug\": \"max-consecutive-ones\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Max Consecutive Ones III\", \"titleSlug\": \"max-consecutive-ones-iii\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"20K\", \"totalSubmission\": \"52.9K\", \"totalAcceptedRaw\": 20023, \"totalSubmissionRaw\": 52863, \"acRate\": \"37.9%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Two Pointers", "slug": "two-pointers" } ] } } }
2511
Easy
[ "You are given a 0-indexed integer array forts of length n representing the positions of several forts. forts[i] can be -1, 0, or 1 where:\n\n\n\t-1 represents there is no fort at the ith position.\n\t0 indicates there is an enemy fort at the ith position.\n\t1 indicates the fort at the ith the position is under yo...
[ { "hash": -579443881711432800, "runtime": "23ms", "solution": "class Solution(object):\n def captureForts(self, forts):\n current_fort = 0\n index = 0\n max_step = 0\n while index < len(forts) and forts[index] == 0:\n index += 1\n if index < len(forts):\n...
class Solution(object): def captureForts(self, forts): """ :type forts: List[int] :rtype: int """
None
None
None
None
None
None
37.9
Level 1
Hi
reformat-phone-number
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a phone number as a string <code>number</code>. <code>number</code> consists of digits, spaces <code>&#39; &#39;</code>, and/or dashes <code>&#39;-&#39;</code>.</p>\n\n<p>You would like to reformat the phone number in a certain manner. Firstly, <strong>remove</strong> all spaces and dashes. Then, <strong>group</strong> the digits from left to right into blocks of length 3 <strong>until</strong> there are 4 or fewer digits. The final digits are then grouped as follows:</p>\n\n<ul>\n\t<li>2 digits: A single block of length 2.</li>\n\t<li>3 digits: A single block of length 3.</li>\n\t<li>4 digits: Two blocks of length 2 each.</li>\n</ul>\n\n<p>The blocks are then joined by dashes. Notice that the reformatting process should <strong>never</strong> produce any blocks of length 1 and produce <strong>at most</strong> two blocks of length 2.</p>\n\n<p>Return <em>the phone number after formatting.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> number = &quot;1-23-45 6&quot;\n<strong>Output:</strong> &quot;123-456&quot;\n<strong>Explanation:</strong> The digits are &quot;123456&quot;.\nStep 1: There are more than 4 digits, so group the next 3 digits. The 1st block is &quot;123&quot;.\nStep 2: There are 3 digits remaining, so put them in a single block of length 3. The 2nd block is &quot;456&quot;.\nJoining the blocks gives &quot;123-456&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> number = &quot;123 4-567&quot;\n<strong>Output:</strong> &quot;123-45-67&quot;\n<strong>Explanation: </strong>The digits are &quot;1234567&quot;.\nStep 1: There are more than 4 digits, so group the next 3 digits. The 1st block is &quot;123&quot;.\nStep 2: There are 4 digits left, so split them into two blocks of length 2. The blocks are &quot;45&quot; and &quot;67&quot;.\nJoining the blocks gives &quot;123-45-67&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> number = &quot;123 4-5678&quot;\n<strong>Output:</strong> &quot;123-456-78&quot;\n<strong>Explanation:</strong> The digits are &quot;12345678&quot;.\nStep 1: The 1st block is &quot;123&quot;.\nStep 2: The 2nd block is &quot;456&quot;.\nStep 3: There are 2 digits left, so put them in a single block of length 2. The 3rd block is &quot;78&quot;.\nJoining the blocks gives &quot;123-456-78&quot;.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= number.length &lt;= 100</code></li>\n\t<li><code>number</code> consists of digits and the characters <code>&#39;-&#39;</code> and <code>&#39; &#39;</code>.</li>\n\t<li>There are at least <strong>two</strong> digits in <code>number</code>.</li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "1694", "questionId": "1812", "questionTitle": "Reformat Phone Number", "questionTitleSlug": "reformat-phone-number", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"34.7K\", \"totalSubmission\": \"53.3K\", \"totalAcceptedRaw\": 34708, \"totalSubmissionRaw\": 53348, \"acRate\": \"65.1%\"}", "topicTags": [ { "name": "String", "slug": "string" } ] } } }
1694
Easy
[ "You are given a phone number as a string number. number consists of digits, spaces ' ', and/or dashes '-'.\n\nYou would like to reformat the phone number in a certain manner. Firstly, remove all spaces and dashes. Then, group the digits from left to right into blocks of length 3 until there are 4 or fewer digits. ...
[ { "hash": 6068240841880591000, "runtime": "18ms", "solution": "class Solution(object):\n def reformatNumber(self, number):\n \"\"\"\n :type number: str\n :rtype: str\n \"\"\"\n number = number.replace('-', '').replace(' ', '')\n end = out = ''\n if (le...
class Solution(object): def reformatNumber(self, number): """ :type number: str :rtype: str """
None
None
None
None
None
None
65.1
Level 1
Hi
maximum-number-of-events-that-can-be-attended
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an array of <code>events</code> where <code>events[i] = [startDay<sub>i</sub>, endDay<sub>i</sub>]</code>. Every event <code>i</code> starts at <code>startDay<sub>i</sub></code><sub> </sub>and ends at <code>endDay<sub>i</sub></code>.</p>\n\n<p>You can attend an event <code>i</code> at any day <code>d</code> where <code>startTime<sub>i</sub> &lt;= d &lt;= endTime<sub>i</sub></code>. You can only attend one event at any time <code>d</code>.</p>\n\n<p>Return <em>the maximum number of events you can attend</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/02/05/e1.png\" style=\"width: 400px; height: 267px;\" />\n<pre>\n<strong>Input:</strong> events = [[1,2],[2,3],[3,4]]\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> You can attend all the three events.\nOne way to attend them all is as shown.\nAttend the first event on day 1.\nAttend the second event on day 2.\nAttend the third event on day 3.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> events= [[1,2],[2,3],[3,4],[1,2]]\n<strong>Output:</strong> 4\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= events.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>events[i].length == 2</code></li>\n\t<li><code>1 &lt;= startDay<sub>i</sub> &lt;= endDay<sub>i</sub> &lt;= 10<sup>5</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1353", "questionId": "1478", "questionTitle": "Maximum Number of Events That Can Be Attended", "questionTitleSlug": "maximum-number-of-events-that-can-be-attended", "similarQuestions": "[{\"title\": \"Maximum Number of Events That Can Be Attended II\", \"titleSlug\": \"maximum-number-of-events-that-can-be-attended-ii\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Maximum Earnings From Taxi\", \"titleSlug\": \"maximum-earnings-from-taxi\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Meeting Rooms III\", \"titleSlug\": \"meeting-rooms-iii\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"80.2K\", \"totalSubmission\": \"248.4K\", \"totalAcceptedRaw\": 80160, \"totalSubmissionRaw\": 248410, \"acRate\": \"32.3%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Greedy", "slug": "greedy" }, { "name": "Sorting", "slug": "sorting" }, { "name": "Heap (Priority Queue)", "slug": "heap-priority-queue" } ] } } }
1353
Medium
[ "You are given an array of events where events[i] = [startDayi, endDayi]. Every event i starts at startDayi and ends at endDayi.\n\nYou can attend an event i at any day d where startTimei <= d <= endTimei. You can only attend one event at any time d.\n\nReturn the maximum number of events you can attend.\n\n \nExam...
[ { "hash": 8246130093067929000, "runtime": "1094ms", "solution": "class Solution(object):\n def maxEvents(self, events):\n \"\"\"\n :type events: List[List[int]]\n :rtype: int\n \"\"\"\n n = len(events)\n # Sort by startDay(needed), endDay (minimizes number of...
class Solution(object): def maxEvents(self, events): """ :type events: List[List[int]] :rtype: int """
None
None
None
None
None
None
32.3
Level 4
Hi
sequential-digits
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>An&nbsp;integer has <em>sequential digits</em> if and only if each digit in the number is one more than the previous digit.</p>\n\n<p>Return a <strong>sorted</strong> list of all the integers&nbsp;in the range <code>[low, high]</code>&nbsp;inclusive that have sequential digits.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<pre><strong>Input:</strong> low = 100, high = 300\n<strong>Output:</strong> [123,234]\n</pre><p><strong class=\"example\">Example 2:</strong></p>\n<pre><strong>Input:</strong> low = 1000, high = 13000\n<strong>Output:</strong> [1234,2345,3456,4567,5678,6789,12345]\n</pre>\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>10 &lt;= low &lt;= high &lt;= 10^9</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1291", "questionId": "1212", "questionTitle": "Sequential Digits", "questionTitleSlug": "sequential-digits", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"92.5K\", \"totalSubmission\": \"151K\", \"totalAcceptedRaw\": 92549, \"totalSubmissionRaw\": 150963, \"acRate\": \"61.3%\"}", "topicTags": [ { "name": "Enumeration", "slug": "enumeration" } ] } } }
1291
Medium
[ "An integer has sequential digits if and only if each digit in the number is one more than the previous digit.\n\nReturn a sorted list of all the integers in the range [low, high] inclusive that have sequential digits.\n\n \nExample 1:\nInput: low = 100, high = 300\nOutput: [123,234]\nExample 2:\nInput: low = 1000,...
[ { "hash": 58507498279725410, "runtime": "17ms", "solution": "class Solution(object):\n def sequentialDigits(self, low, high):\n digit=\"123456789\"\n output=[]\n low_l=len(str(low))\n high_l=len(str(high))\n for x in range(low_l,high_l+1):\n for y in rang...
class Solution(object): def sequentialDigits(self, low, high): """ :type low: int :type high: int :rtype: List[int] """
None
None
None
None
None
None
61.3
Level 2
Hi
leaf-similar-trees
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Consider all the leaves of a binary tree, from&nbsp;left to right order, the values of those&nbsp;leaves form a <strong>leaf value sequence</strong><em>.</em></p>\n\n<p><img alt=\"\" src=\"https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/16/tree.png\" style=\"width: 400px; height: 336px;\" /></p>\n\n<p>For example, in the given tree above, the leaf value sequence is <code>(6, 7, 4, 9, 8)</code>.</p>\n\n<p>Two binary trees are considered <em>leaf-similar</em>&nbsp;if their leaf value sequence is the same.</p>\n\n<p>Return <code>true</code> if and only if the two given trees with head nodes <code>root1</code> and <code>root2</code> are leaf-similar.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-1.jpg\" style=\"width: 600px; height: 237px;\" />\n<pre>\n<strong>Input:</strong> root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8]\n<strong>Output:</strong> true\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-2.jpg\" style=\"width: 300px; height: 110px;\" />\n<pre>\n<strong>Input:</strong> root1 = [1,2,3], root2 = [1,3,2]\n<strong>Output:</strong> false\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li>The number of nodes in each tree will be in the range <code>[1, 200]</code>.</li>\n\t<li>Both of the given trees will have values in the range <code>[0, 200]</code>.</li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "872", "questionId": "904", "questionTitle": "Leaf-Similar Trees", "questionTitleSlug": "leaf-similar-trees", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"309.5K\", \"totalSubmission\": \"455.7K\", \"totalAcceptedRaw\": 309484, \"totalSubmissionRaw\": 455684, \"acRate\": \"67.9%\"}", "topicTags": [ { "name": "Tree", "slug": "tree" }, { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Binary Tree", "slug": "binary-tree" } ] } } }
872
Easy
[ "Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence.\n\n\n\nFor example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8).\n\nTwo binary trees are considered leaf-similar if their leaf value sequence is the same.\n\nReturn tru...
[ { "hash": -4278622175997991000, "runtime": "9ms", "solution": "# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution(object):\n def le...
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution(object): def leafSimilar(self, root1, root2): """ :type root1: TreeNode :type root2: TreeNode :rtype: bool """
None
None
None
None
None
None
67.9
Level 1
Hi
the-k-strongest-values-in-an-array
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an array of integers <code>arr</code> and an integer <code>k</code>.</p>\n\n<p>A value <code>arr[i]</code> is said to be stronger than a value <code>arr[j]</code> if <code>|arr[i] - m| &gt; |arr[j] - m|</code> where <code>m</code> is the <strong>median</strong> of the array.<br />\nIf <code>|arr[i] - m| == |arr[j] - m|</code>, then <code>arr[i]</code> is said to be stronger than <code>arr[j]</code> if <code>arr[i] &gt; arr[j]</code>.</p>\n\n<p>Return <em>a list of the strongest <code>k</code></em> values in the array. return the answer <strong>in any arbitrary order</strong>.</p>\n\n<p><strong>Median</strong> is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position <code>((n - 1) / 2)</code> in the sorted list <strong>(0-indexed)</strong>.</p>\n\n<ul>\n\t<li>For <code>arr = [6, -3, 7, 2, 11]</code>, <code>n = 5</code> and the median is obtained by sorting the array <code>arr = [-3, 2, 6, 7, 11]</code> and the median is <code>arr[m]</code> where <code>m = ((5 - 1) / 2) = 2</code>. The median is <code>6</code>.</li>\n\t<li>For <code>arr = [-7, 22, 17,&thinsp;3]</code>, <code>n = 4</code> and the median is obtained by sorting the array <code>arr = [-7, 3, 17, 22]</code> and the median is <code>arr[m]</code> where <code>m = ((4 - 1) / 2) = 1</code>. The median is <code>3</code>.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [1,2,3,4,5], k = 2\n<strong>Output:</strong> [5,1]\n<strong>Explanation:</strong> Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also <strong>accepted</strong> answer.\nPlease note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 &gt; 1.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [1,1,3,5,5], k = 2\n<strong>Output:</strong> [5,5]\n<strong>Explanation:</strong> Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5].\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> arr = [6,7,11,7,6,8], k = 5\n<strong>Output:</strong> [11,8,6,6,7]\n<strong>Explanation:</strong> Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7].\nAny permutation of [11,8,6,6,7] is <strong>accepted</strong>.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>-10<sup>5</sup> &lt;= arr[i] &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= k &lt;= arr.length</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1471", "questionId": "1581", "questionTitle": "The k Strongest Values in an Array", "questionTitleSlug": "the-k-strongest-values-in-an-array", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"36.3K\", \"totalSubmission\": \"59.6K\", \"totalAcceptedRaw\": 36254, \"totalSubmissionRaw\": 59577, \"acRate\": \"60.9%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Two Pointers", "slug": "two-pointers" }, { "name": "Sorting", "slug": "sorting" } ] } } }
1471
Medium
[ "Given an array of integers arr and an integer k.\n\nA value arr[i] is said to be stronger than a value arr[j] if |arr[i] - m| > |arr[j] - m| where m is the median of the array.\nIf |arr[i] - m| == |arr[j] - m|, then arr[i] is said to be stronger than arr[j] if arr[i] > arr[j].\n\nReturn a list of the strongest k v...
[ { "hash": 5667168738932668000, "runtime": "1119ms", "solution": "class Solution(object):\n def getStrongest(self, arr, k):\n \"\"\"\n :type arr: List[int]\n :type k: int\n :rtype: List[int]\n \"\"\"\n mid = sorted(arr)[(len(arr)-1)//2] # O(nlogn)\n sor...
class Solution(object): def getStrongest(self, arr, k): """ :type arr: List[int] :type k: int :rtype: List[int] """
None
None
None
None
None
None
60.9
Level 2
Hi
count-of-matches-in-tournament
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an integer <code>n</code>, the number of teams in a tournament that has strange rules:</p>\n\n<ul>\n\t<li>If the current number of teams is <strong>even</strong>, each team gets paired with another team. A total of <code>n / 2</code> matches are played, and <code>n / 2</code> teams advance to the next round.</li>\n\t<li>If the current number of teams is <strong>odd</strong>, one team randomly advances in the tournament, and the rest gets paired. A total of <code>(n - 1) / 2</code> matches are played, and <code>(n - 1) / 2 + 1</code> teams advance to the next round.</li>\n</ul>\n\n<p>Return <em>the number of matches played in the tournament until a winner is decided.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 7\n<strong>Output:</strong> 6\n<strong>Explanation:</strong> Details of the tournament: \n- 1st Round: Teams = 7, Matches = 3, and 4 teams advance.\n- 2nd Round: Teams = 4, Matches = 2, and 2 teams advance.\n- 3rd Round: Teams = 2, Matches = 1, and 1 team is declared the winner.\nTotal number of matches = 3 + 2 + 1 = 6.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 14\n<strong>Output:</strong> 13\n<strong>Explanation:</strong> Details of the tournament:\n- 1st Round: Teams = 14, Matches = 7, and 7 teams advance.\n- 2nd Round: Teams = 7, Matches = 3, and 4 teams advance.\n- 3rd Round: Teams = 4, Matches = 2, and 2 teams advance.\n- 4th Round: Teams = 2, Matches = 1, and 1 team is declared the winner.\nTotal number of matches = 7 + 3 + 2 + 1 = 13.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= n &lt;= 200</code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "1688", "questionId": "1806", "questionTitle": "Count of Matches in Tournament", "questionTitleSlug": "count-of-matches-in-tournament", "similarQuestions": "[{\"title\": \"Count Distinct Numbers on Board\", \"titleSlug\": \"count-distinct-numbers-on-board\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"213K\", \"totalSubmission\": \"248K\", \"totalAcceptedRaw\": 212959, \"totalSubmissionRaw\": 248020, \"acRate\": \"85.9%\"}", "topicTags": [ { "name": "Math", "slug": "math" }, { "name": "Simulation", "slug": "simulation" } ] } } }
1688
Easy
[ "You are given an integer n, the number of teams in a tournament that has strange rules:\n\n\n\tIf the current number of teams is even, each team gets paired with another team. A total of n / 2 matches are played, and n / 2 teams advance to the next round.\n\tIf the current number of teams is odd, one team randomly...
[ { "hash": 6585473495532557000, "runtime": "16ms", "solution": "class Solution(object): \n #THIRUNEELAN A-717823I604\n def numberOfMatches(self, n):\n m=0\n while(n>1):\n if n%2==0: \n m+=(n//2)\n else:\n m+=(n//2)+1\n n//=2\n return m \n " ...
class Solution(object): def numberOfMatches(self, n): """ :type n: int :rtype: int """
None
None
None
None
None
None
85.9
Level 1
Hi
critical-connections-in-a-network
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There are <code>n</code> servers numbered from <code>0</code> to <code>n - 1</code> connected by undirected server-to-server <code>connections</code> forming a network where <code>connections[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> represents a connection between servers <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>. Any server can reach other servers directly or indirectly through the network.</p>\n\n<p>A <em>critical connection</em> is a connection that, if removed, will make some servers unable to reach some other server.</p>\n\n<p>Return all critical connections in the network in any order.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2019/09/03/1537_ex1_2.png\" style=\"width: 198px; height: 248px;\" />\n<pre>\n<strong>Input:</strong> n = 4, connections = [[0,1],[1,2],[2,0],[1,3]]\n<strong>Output:</strong> [[1,3]]\n<strong>Explanation:</strong> [[3,1]] is also accepted.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 2, connections = [[0,1]]\n<strong>Output:</strong> [[0,1]]\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>\n\t<li><code>n - 1 &lt;= connections.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt;= n - 1</code></li>\n\t<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>\n\t<li>There are no repeated connections.</li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "1192", "questionId": "1300", "questionTitle": "Critical Connections in a Network", "questionTitleSlug": "critical-connections-in-a-network", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"216.4K\", \"totalSubmission\": \"390.7K\", \"totalAcceptedRaw\": 216429, \"totalSubmissionRaw\": 390655, \"acRate\": \"55.4%\"}", "topicTags": [ { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Graph", "slug": "graph" }, { "name": "Biconnected Component", "slug": "biconnected-component" } ] } } }
1192
Hard
[ "There are n servers numbered from 0 to n - 1 connected by undirected server-to-server connections forming a network where connections[i] = [ai, bi] represents a connection between servers ai and bi. Any server can reach other servers directly or indirectly through the network.\n\nA critical connection is a connect...
[ { "hash": -601411781323412500, "runtime": "1799ms", "solution": "\nfrom collections import defaultdict\nclass Solution(object):\n def criticalConnections(self, n, connections):\n \"\"\"\n :type n: int\n :type connections: List[List[int]]\n :rtype: List[List[int]]\n ...
class Solution(object): def criticalConnections(self, n, connections): """ :type n: int :type connections: List[List[int]] :rtype: List[List[int]] """
None
None
None
None
None
None
55.4
Level 5
Hi
generate-a-string-with-characters-that-have-odd-counts
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an&nbsp;integer <code>n</code>, <em>return a string with <code>n</code>&nbsp;characters such that each character in such string occurs <strong>an odd number of times</strong></em>.</p>\n\n<p>The returned string must contain only lowercase English letters. If there are multiples valid strings, return <strong>any</strong> of them. &nbsp;</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 4\n<strong>Output:</strong> &quot;pppz&quot;\n<strong>Explanation:</strong> &quot;pppz&quot; is a valid string since the character &#39;p&#39; occurs three times and the character &#39;z&#39; occurs once. Note that there are many other valid strings such as &quot;ohhh&quot; and &quot;love&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 2\n<strong>Output:</strong> &quot;xy&quot;\n<strong>Explanation:</strong> &quot;xy&quot; is a valid string since the characters &#39;x&#39; and &#39;y&#39; occur once. Note that there are many other valid strings such as &quot;ag&quot; and &quot;ur&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 7\n<strong>Output:</strong> &quot;holasss&quot;\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= n &lt;= 500</code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "1374", "questionId": "1490", "questionTitle": "Generate a String With Characters That Have Odd Counts", "questionTitleSlug": "generate-a-string-with-characters-that-have-odd-counts", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"87K\", \"totalSubmission\": \"112K\", \"totalAcceptedRaw\": 86952, \"totalSubmissionRaw\": 112050, \"acRate\": \"77.6%\"}", "topicTags": [ { "name": "String", "slug": "string" } ] } } }
1374
Easy
[ "Given an integer n, return a string with n characters such that each character in such string occurs an odd number of times.\n\nThe returned string must contain only lowercase English letters. If there are multiples valid strings, return any of them.  \n\n \nExample 1:\n\nInput: n = 4\nOutput: \"pppz\"\nExplanatio...
[ { "hash": 2530253890720957400, "runtime": "16ms", "solution": "class Solution(object):\n def generateTheString(self, n):\n \"\"\"\n :type n: int\n :rtype: str\n \"\"\"\n if n%2!=0:\n return 'x'*n\n return ('x'*(n-1))+'y'" }, { "hash": 26344...
class Solution(object): def generateTheString(self, n): """ :type n: int :rtype: str """
None
None
None
None
None
None
77.6
Level 1
Hi
next-greater-element-ii
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a circular integer array <code>nums</code> (i.e., the next element of <code>nums[nums.length - 1]</code> is <code>nums[0]</code>), return <em>the <strong>next greater number</strong> for every element in</em> <code>nums</code>.</p>\n\n<p>The <strong>next greater number</strong> of a number <code>x</code> is the first greater number to its traversing-order next in the array, which means you could search circularly to find its next greater number. If it doesn&#39;t exist, return <code>-1</code> for this number.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,2,1]\n<strong>Output:</strong> [2,-1,2]\nExplanation: The first 1&#39;s next greater number is 2; \nThe number 2 can&#39;t find next greater number. \nThe second 1&#39;s next greater number needs to search circularly, which is also 2.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,2,3,4,3]\n<strong>Output:</strong> [2,3,4,-1,4]\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>\n\t<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "503", "questionId": "503", "questionTitle": "Next Greater Element II", "questionTitleSlug": "next-greater-element-ii", "similarQuestions": "[{\"title\": \"Next Greater Element I\", \"titleSlug\": \"next-greater-element-i\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Next Greater Element III\", \"titleSlug\": \"next-greater-element-iii\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"352.6K\", \"totalSubmission\": \"555.3K\", \"totalAcceptedRaw\": 352641, \"totalSubmissionRaw\": 555306, \"acRate\": \"63.5%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Stack", "slug": "stack" }, { "name": "Monotonic Stack", "slug": "monotonic-stack" } ] } } }
503
Medium
[ "Given a circular integer array nums (i.e., the next element of nums[nums.length - 1] is nums[0]), return the next greater number for every element in nums.\n\nThe next greater number of a number x is the first greater number to its traversing-order next in the array, which means you could search circularly to find...
[ { "hash": 6830494461926446000, "runtime": "1969ms", "solution": "class Solution(object):\n def nextGreaterElements(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n\n n = len(nums)\n ans = []\n dic = {}\n\n def next_gr...
class Solution(object): def nextGreaterElements(self, nums): """ :type nums: List[int] :rtype: List[int] """
None
None
None
None
None
None
63.5
Level 2
Hi
count-beautiful-substrings-ii
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a string <code>s</code> and a positive integer <code>k</code>.</p>\n\n<p>Let <code>vowels</code> and <code>consonants</code> be the number of vowels and consonants in a string.</p>\n\n<p>A string is <strong>beautiful</strong> if:</p>\n\n<ul>\n\t<li><code>vowels == consonants</code>.</li>\n\t<li><code>(vowels * consonants) % k == 0</code>, in other terms the multiplication of <code>vowels</code> and <code>consonants</code> is divisible by <code>k</code>.</li>\n</ul>\n\n<p>Return <em>the number of <strong>non-empty beautiful substrings</strong> in the given string</em> <code>s</code>.</p>\n\n<p>A <strong>substring</strong> is a contiguous sequence of characters in a string.</p>\n\n<p><strong>Vowel letters</strong> in English are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>.</p>\n\n<p><strong>Consonant letters</strong> in English are every letter except vowels.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;baeyh&quot;, k = 2\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> There are 2 beautiful substrings in the given string.\n- Substring &quot;b<u>aeyh</u>&quot;, vowels = 2 ([&quot;a&quot;,e&quot;]), consonants = 2 ([&quot;y&quot;,&quot;h&quot;]).\nYou can see that string &quot;aeyh&quot; is beautiful as vowels == consonants and vowels * consonants % k == 0.\n- Substring &quot;<u>baey</u>h&quot;, vowels = 2 ([&quot;a&quot;,e&quot;]), consonants = 2 ([&quot;b&quot;,&quot;y&quot;]).\nYou can see that string &quot;baey&quot; is beautiful as vowels == consonants and vowels * consonants % k == 0.\nIt can be shown that there are only 2 beautiful substrings in the given string.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;abba&quot;, k = 1\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> There are 3 beautiful substrings in the given string.\n- Substring &quot;<u>ab</u>ba&quot;, vowels = 1 ([&quot;a&quot;]), consonants = 1 ([&quot;b&quot;]).\n- Substring &quot;ab<u>ba</u>&quot;, vowels = 1 ([&quot;a&quot;]), consonants = 1 ([&quot;b&quot;]).\n- Substring &quot;<u>abba</u>&quot;, vowels = 2 ([&quot;a&quot;,&quot;a&quot;]), consonants = 2 ([&quot;b&quot;,&quot;b&quot;]).\nIt can be shown that there are only 3 beautiful substrings in the given string.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;bcdf&quot;, k = 1\n<strong>Output:</strong> 0\n<strong>Explanation:</strong> There are no beautiful substrings in the given string.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li>\n\t<li><code>1 &lt;= k &lt;= 1000</code></li>\n\t<li><code>s</code> consists of only English lowercase letters.</li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "2949", "questionId": "3208", "questionTitle": "Count Beautiful Substrings II", "questionTitleSlug": "count-beautiful-substrings-ii", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"3.3K\", \"totalSubmission\": \"14K\", \"totalAcceptedRaw\": 3305, \"totalSubmissionRaw\": 13976, \"acRate\": \"23.6%\"}", "topicTags": [ { "name": "Hash Table", "slug": "hash-table" }, { "name": "Math", "slug": "math" }, { "name": "String", "slug": "string" }, { "name": "Number Theory", "slug": "number-theory" }, { "name": "Prefix Sum", "slug": "prefix-sum" } ] } } }
2949
Hard
[ "You are given a string s and a positive integer k.\n\nLet vowels and consonants be the number of vowels and consonants in a string.\n\nA string is beautiful if:\n\n\n\tvowels == consonants.\n\t(vowels * consonants) % k == 0, in other terms the multiplication of vowels and consonants is divisible by k.\n\n\nReturn ...
[ { "hash": -6897588075069215000, "runtime": "215ms", "solution": "class Solution(object):\n def beautifulSubstrings(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n \n vow = set()\n for c in 'aeiou':\n vow.a...
class Solution(object): def beautifulSubstrings(self, s, k): """ :type s: str :type k: int :rtype: int """
None
None
None
None
None
None
23.6
Level 5
Hi
number-of-valid-words-in-a-sentence
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A sentence consists of lowercase letters (<code>&#39;a&#39;</code> to <code>&#39;z&#39;</code>), digits (<code>&#39;0&#39;</code> to <code>&#39;9&#39;</code>), hyphens (<code>&#39;-&#39;</code>), punctuation marks (<code>&#39;!&#39;</code>, <code>&#39;.&#39;</code>, and <code>&#39;,&#39;</code>), and spaces (<code>&#39; &#39;</code>) only. Each sentence can be broken down into <strong>one or more tokens</strong> separated by one or more spaces <code>&#39; &#39;</code>.</p>\n\n<p>A token is a valid word if <strong>all three</strong> of the following are true:</p>\n\n<ul>\n\t<li>It only contains lowercase letters, hyphens, and/or punctuation (<strong>no</strong> digits).</li>\n\t<li>There is <strong>at most one</strong> hyphen <code>&#39;-&#39;</code>. If present, it <strong>must</strong> be surrounded by lowercase characters (<code>&quot;a-b&quot;</code> is valid, but <code>&quot;-ab&quot;</code> and <code>&quot;ab-&quot;</code> are not valid).</li>\n\t<li>There is <strong>at most one</strong> punctuation mark. If present, it <strong>must</strong> be at the <strong>end</strong> of the token (<code>&quot;ab,&quot;</code>, <code>&quot;cd!&quot;</code>, and <code>&quot;.&quot;</code> are valid, but <code>&quot;a!b&quot;</code> and <code>&quot;c.,&quot;</code> are not valid).</li>\n</ul>\n\n<p>Examples of valid words include <code>&quot;a-b.&quot;</code>, <code>&quot;afad&quot;</code>, <code>&quot;ba-c&quot;</code>, <code>&quot;a!&quot;</code>, and <code>&quot;!&quot;</code>.</p>\n\n<p>Given a string <code>sentence</code>, return <em>the <strong>number</strong> of valid words in </em><code>sentence</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> sentence = &quot;<u>cat</u> <u>and</u> <u>dog</u>&quot;\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> The valid words in the sentence are &quot;cat&quot;, &quot;and&quot;, and &quot;dog&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> sentence = &quot;!this 1-s b8d!&quot;\n<strong>Output:</strong> 0\n<strong>Explanation:</strong> There are no valid words in the sentence.\n&quot;!this&quot; is invalid because it starts with a punctuation mark.\n&quot;1-s&quot; and &quot;b8d&quot; are invalid because they contain digits.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> sentence = &quot;<u>alice</u> <u>and</u> <u>bob</u> <u>are</u> <u>playing</u> stone-game10&quot;\n<strong>Output:</strong> 5\n<strong>Explanation:</strong> The valid words in the sentence are &quot;alice&quot;, &quot;and&quot;, &quot;bob&quot;, &quot;are&quot;, and &quot;playing&quot;.\n&quot;stone-game10&quot; is invalid because it contains digits.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= sentence.length &lt;= 1000</code></li>\n\t<li><code>sentence</code> only contains lowercase English letters, digits, <code>&#39; &#39;</code>, <code>&#39;-&#39;</code>, <code>&#39;!&#39;</code>, <code>&#39;.&#39;</code>, and <code>&#39;,&#39;</code>.</li>\n\t<li>There will be at least&nbsp;<code>1</code> token.</li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "2047", "questionId": "2173", "questionTitle": "Number of Valid Words in a Sentence", "questionTitleSlug": "number-of-valid-words-in-a-sentence", "similarQuestions": "[{\"title\": \"Maximum Number of Words Found in Sentences\", \"titleSlug\": \"maximum-number-of-words-found-in-sentences\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"25.9K\", \"totalSubmission\": \"89.6K\", \"totalAcceptedRaw\": 25923, \"totalSubmissionRaw\": 89586, \"acRate\": \"28.9%\"}", "topicTags": [ { "name": "String", "slug": "string" } ] } } }
2047
Easy
[ "A sentence consists of lowercase letters ('a' to 'z'), digits ('0' to '9'), hyphens ('-'), punctuation marks ('!', '.', and ','), and spaces (' ') only. Each sentence can be broken down into one or more tokens separated by one or more spaces ' '.\n\nA token is a valid word if all three of the following are true:\n...
[ { "hash": 2241118792219098400, "runtime": "43ms", "solution": "class Solution(object):\n def countValidWords(self, sentence):\n \"\"\"\n :type sentence: str\n :rtype: int\n \"\"\"\n splitSentence = sentence.split(' ')\n count = 0\n\n for item in splitS...
class Solution(object): def countValidWords(self, sentence): """ :type sentence: str :rtype: int """
None
None
None
None
None
None
28.9
Level 1
Hi
evaluate-the-bracket-pairs-of-a-string
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a string <code>s</code> that contains some bracket pairs, with each pair containing a <strong>non-empty</strong> key.</p>\n\n<ul>\n\t<li>For example, in the string <code>&quot;(name)is(age)yearsold&quot;</code>, there are <strong>two</strong> bracket pairs that contain the keys <code>&quot;name&quot;</code> and <code>&quot;age&quot;</code>.</li>\n</ul>\n\n<p>You know the values of a wide range of keys. This is represented by a 2D string array <code>knowledge</code> where each <code>knowledge[i] = [key<sub>i</sub>, value<sub>i</sub>]</code> indicates that key <code>key<sub>i</sub></code> has a value of <code>value<sub>i</sub></code>.</p>\n\n<p>You are tasked to evaluate <strong>all</strong> of the bracket pairs. When you evaluate a bracket pair that contains some key <code>key<sub>i</sub></code>, you will:</p>\n\n<ul>\n\t<li>Replace <code>key<sub>i</sub></code> and the bracket pair with the key&#39;s corresponding <code>value<sub>i</sub></code>.</li>\n\t<li>If you do not know the value of the key, you will replace <code>key<sub>i</sub></code> and the bracket pair with a question mark <code>&quot;?&quot;</code> (without the quotation marks).</li>\n</ul>\n\n<p>Each key will appear at most once in your <code>knowledge</code>. There will not be any nested brackets in <code>s</code>.</p>\n\n<p>Return <em>the resulting string after evaluating <strong>all</strong> of the bracket pairs.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;(name)is(age)yearsold&quot;, knowledge = [[&quot;name&quot;,&quot;bob&quot;],[&quot;age&quot;,&quot;two&quot;]]\n<strong>Output:</strong> &quot;bobistwoyearsold&quot;\n<strong>Explanation:</strong>\nThe key &quot;name&quot; has a value of &quot;bob&quot;, so replace &quot;(name)&quot; with &quot;bob&quot;.\nThe key &quot;age&quot; has a value of &quot;two&quot;, so replace &quot;(age)&quot; with &quot;two&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;hi(name)&quot;, knowledge = [[&quot;a&quot;,&quot;b&quot;]]\n<strong>Output:</strong> &quot;hi?&quot;\n<strong>Explanation:</strong> As you do not know the value of the key &quot;name&quot;, replace &quot;(name)&quot; with &quot;?&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;(a)(a)(a)aaa&quot;, knowledge = [[&quot;a&quot;,&quot;yes&quot;]]\n<strong>Output:</strong> &quot;yesyesyesaaa&quot;\n<strong>Explanation:</strong> The same key can appear multiple times.\nThe key &quot;a&quot; has a value of &quot;yes&quot;, so replace all occurrences of &quot;(a)&quot; with &quot;yes&quot;.\nNotice that the &quot;a&quot;s not in a bracket pair are not evaluated.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>0 &lt;= knowledge.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>knowledge[i].length == 2</code></li>\n\t<li><code>1 &lt;= key<sub>i</sub>.length, value<sub>i</sub>.length &lt;= 10</code></li>\n\t<li><code>s</code> consists of lowercase English letters and round brackets <code>&#39;(&#39;</code> and <code>&#39;)&#39;</code>.</li>\n\t<li>Every open bracket <code>&#39;(&#39;</code> in <code>s</code> will have a corresponding close bracket <code>&#39;)&#39;</code>.</li>\n\t<li>The key in each bracket pair of <code>s</code> will be non-empty.</li>\n\t<li>There will not be any nested bracket pairs in <code>s</code>.</li>\n\t<li><code>key<sub>i</sub></code> and <code>value<sub>i</sub></code> consist of lowercase English letters.</li>\n\t<li>Each <code>key<sub>i</sub></code> in <code>knowledge</code> is unique.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1807", "questionId": "1934", "questionTitle": "Evaluate the Bracket Pairs of a String", "questionTitleSlug": "evaluate-the-bracket-pairs-of-a-string", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"27.8K\", \"totalSubmission\": \"41.8K\", \"totalAcceptedRaw\": 27845, \"totalSubmissionRaw\": 41795, \"acRate\": \"66.6%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" }, { "name": "String", "slug": "string" } ] } } }
1807
Medium
[ "You are given a string s that contains some bracket pairs, with each pair containing a non-empty key.\n\n\n\tFor example, in the string \"(name)is(age)yearsold\", there are two bracket pairs that contain the keys \"name\" and \"age\".\n\n\nYou know the values of a wide range of keys. This is represented by a 2D st...
[ { "hash": -1583645388600584400, "runtime": "918ms", "solution": "\nclass Solution(object):\n def evaluate(self, s, knowledge):\n \"\"\"\n :type s: str\n :type knowledge: List[List[str]]\n :rtype: str\n \"\"\"\n knowledgeDict = {}\n for k in knowledge:\...
class Solution(object): def evaluate(self, s, knowledge): """ :type s: str :type knowledge: List[List[str]] :rtype: str """
None
None
None
None
None
None
66.6
Level 2
Hi
minimize-string-length
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a <strong>0-indexed</strong> string <code>s</code>, repeatedly perform the following operation <strong>any</strong> number of times:</p>\n\n<ul>\n\t<li>Choose an index <code>i</code> in the string, and let <code>c</code> be the character in position <code>i</code>. <strong>Delete</strong> the <strong>closest occurrence</strong> of <code>c</code> to the <strong>left</strong> of <code>i</code> (if any) and the <strong>closest occurrence</strong> of <code>c</code> to the <strong>right</strong> of <code>i</code> (if any).</li>\n</ul>\n\n<p>Your task is to <strong>minimize</strong> the length of <code>s</code> by performing the above operation any number of times.</p>\n\n<p>Return <em>an integer denoting the length of the <strong>minimized</strong> string.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;aaabc&quot;\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> In this example, s is &quot;aaabc&quot;. We can start by selecting the character &#39;a&#39; at index 1. We then remove the closest &#39;a&#39; to the left of index 1, which is at index 0, and the closest &#39;a&#39; to the right of index 1, which is at index 2. After this operation, the string becomes &quot;abc&quot;. Any further operation we perform on the string will leave it unchanged. Therefore, the length of the minimized string is 3.</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;cbbd&quot;\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> For this we can start with character &#39;b&#39; at index 1. There is no occurrence of &#39;b&#39; to the left of index 1, but there is one to the right at index 2, so we delete the &#39;b&#39; at index 2. The string becomes &quot;cbd&quot; and further operations will leave it unchanged. Hence, the minimized length is 3.&nbsp;\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;dddaaa&quot;\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> For this, we can start with the character &#39;d&#39; at index 1. The closest occurrence of a &#39;d&#39; to its left is at index 0, and the closest occurrence of a &#39;d&#39; to its right is at index 2. We delete both index 0 and 2, so the string becomes &quot;daaa&quot;. In the new string, we can select the character &#39;a&#39; at index 2. The closest occurrence of an &#39;a&#39; to its left is at index 1, and the closest occurrence of an &#39;a&#39; to its right is at index 3. We delete both of them, and the string becomes &quot;da&quot;. We cannot minimize this further, so the minimized length is 2.\n</pre>\n\n<div class=\"notranslate\" style=\"all: initial;\">&nbsp;</div>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= s.length &lt;= 100</code></li>\n\t<li><code>s</code> contains only lowercase English letters</li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "2716", "questionId": "2825", "questionTitle": "Minimize String Length", "questionTitleSlug": "minimize-string-length", "similarQuestions": "[{\"title\": \"Remove All Adjacent Duplicates In String\", \"titleSlug\": \"remove-all-adjacent-duplicates-in-string\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Remove All Adjacent Duplicates in String II\", \"titleSlug\": \"remove-all-adjacent-duplicates-in-string-ii\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"40.6K\", \"totalSubmission\": \"54.6K\", \"totalAcceptedRaw\": 40627, \"totalSubmissionRaw\": 54556, \"acRate\": \"74.5%\"}", "topicTags": [ { "name": "Hash Table", "slug": "hash-table" }, { "name": "String", "slug": "string" } ] } } }
2716
Easy
[ "Given a 0-indexed string s, repeatedly perform the following operation any number of times:\n\n\n\tChoose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the left of i (if any) and the closest occurrence of c to the right of i (if any).\n\n\nYour task is to...
[ { "hash": 8948336514608136000, "runtime": "11ms", "solution": "class Solution(object):\n def minimizedStringLength(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n return len(set(s))" }, { "hash": -3155303585815383000, "runtime": "27ms", "s...
class Solution(object): def minimizedStringLength(self, s): """ :type s: str :rtype: int """
None
None
None
None
None
None
74.5
Level 1
Hi
minimum-index-sum-of-two-lists
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given two arrays of strings <code>list1</code> and <code>list2</code>, find the <strong>common strings with the least index sum</strong>.</p>\n\n<p>A <strong>common string</strong> is a string that appeared in both <code>list1</code> and <code>list2</code>.</p>\n\n<p>A <strong>common string with the least index sum</strong> is a common string such that if it appeared at <code>list1[i]</code> and <code>list2[j]</code> then <code>i + j</code> should be the minimum value among all the other <strong>common strings</strong>.</p>\n\n<p>Return <em>all the <strong>common strings with the least index sum</strong></em>. Return the answer in <strong>any order</strong>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> list1 = [&quot;Shogun&quot;,&quot;Tapioca Express&quot;,&quot;Burger King&quot;,&quot;KFC&quot;], list2 = [&quot;Piatti&quot;,&quot;The Grill at Torrey Pines&quot;,&quot;Hungry Hunter Steakhouse&quot;,&quot;Shogun&quot;]\n<strong>Output:</strong> [&quot;Shogun&quot;]\n<strong>Explanation:</strong> The only common string is &quot;Shogun&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> list1 = [&quot;Shogun&quot;,&quot;Tapioca Express&quot;,&quot;Burger King&quot;,&quot;KFC&quot;], list2 = [&quot;KFC&quot;,&quot;Shogun&quot;,&quot;Burger King&quot;]\n<strong>Output:</strong> [&quot;Shogun&quot;]\n<strong>Explanation:</strong> The common string with the least index sum is &quot;Shogun&quot; with index sum = (0 + 1) = 1.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> list1 = [&quot;happy&quot;,&quot;sad&quot;,&quot;good&quot;], list2 = [&quot;sad&quot;,&quot;happy&quot;,&quot;good&quot;]\n<strong>Output:</strong> [&quot;sad&quot;,&quot;happy&quot;]\n<strong>Explanation:</strong> There are three common strings:\n&quot;happy&quot; with index sum = (0 + 1) = 1.\n&quot;sad&quot; with index sum = (1 + 0) = 1.\n&quot;good&quot; with index sum = (2 + 2) = 4.\nThe strings with the least index sum are &quot;sad&quot; and &quot;happy&quot;.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= list1.length, list2.length &lt;= 1000</code></li>\n\t<li><code>1 &lt;= list1[i].length, list2[i].length &lt;= 30</code></li>\n\t<li><code>list1[i]</code> and <code>list2[i]</code> consist of spaces <code>&#39; &#39;</code> and English letters.</li>\n\t<li>All the strings of <code>list1</code> are <strong>unique</strong>.</li>\n\t<li>All the strings of <code>list2</code> are <strong>unique</strong>.</li>\n\t<li>There is at least a common string between <code>list1</code> and <code>list2</code>.</li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "599", "questionId": "599", "questionTitle": "Minimum Index Sum of Two Lists", "questionTitleSlug": "minimum-index-sum-of-two-lists", "similarQuestions": "[{\"title\": \"Intersection of Two Linked Lists\", \"titleSlug\": \"intersection-of-two-linked-lists\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"210K\", \"totalSubmission\": \"385K\", \"totalAcceptedRaw\": 210019, \"totalSubmissionRaw\": 384972, \"acRate\": \"54.6%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" }, { "name": "String", "slug": "string" } ] } } }
599
Easy
[ "Given two arrays of strings list1 and list2, find the common strings with the least index sum.\n\nA common string is a string that appeared in both list1 and list2.\n\nA common string with the least index sum is a common string such that if it appeared at list1[i] and list2[j] then i + j should be the minimum valu...
[ { "hash": -5156716125672930000, "runtime": "248ms", "solution": "class Solution(object):\n def findRestaurant(self, list1, list2):\n \n matched = {}\n \n for i in range(len(list1)):\n \n if list1[i] in list2:\n sum = i + list2.index(list1[i])\n...
class Solution(object): def findRestaurant(self, list1, list2): """ :type list1: List[str] :type list2: List[str] :rtype: List[str] """
None
None
None
None
None
None
54.6
Level 1
Hi
rectangle-overlap
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>An axis-aligned rectangle is represented as a list <code>[x1, y1, x2, y2]</code>, where <code>(x1, y1)</code> is the coordinate of its bottom-left corner, and <code>(x2, y2)</code> is the coordinate of its top-right corner. Its top and bottom edges are parallel to the X-axis, and its left and right edges are parallel to the Y-axis.</p>\n\n<p>Two rectangles overlap if the area of their intersection is <strong>positive</strong>. To be clear, two rectangles that only touch at the corner or edges do not overlap.</p>\n\n<p>Given two axis-aligned rectangles <code>rec1</code> and <code>rec2</code>, return <code>true</code><em> if they overlap, otherwise return </em><code>false</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<pre><strong>Input:</strong> rec1 = [0,0,2,2], rec2 = [1,1,3,3]\n<strong>Output:</strong> true\n</pre><p><strong class=\"example\">Example 2:</strong></p>\n<pre><strong>Input:</strong> rec1 = [0,0,1,1], rec2 = [1,0,2,1]\n<strong>Output:</strong> false\n</pre><p><strong class=\"example\">Example 3:</strong></p>\n<pre><strong>Input:</strong> rec1 = [0,0,1,1], rec2 = [2,2,3,3]\n<strong>Output:</strong> false\n</pre>\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>rec1.length == 4</code></li>\n\t<li><code>rec2.length == 4</code></li>\n\t<li><code>-10<sup>9</sup> &lt;= rec1[i], rec2[i] &lt;= 10<sup>9</sup></code></li>\n\t<li><code>rec1</code> and <code>rec2</code> represent a valid rectangle with a non-zero area.</li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "836", "questionId": "866", "questionTitle": "Rectangle Overlap", "questionTitleSlug": "rectangle-overlap", "similarQuestions": "[{\"title\": \"Rectangle Area\", \"titleSlug\": \"rectangle-area\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"135.3K\", \"totalSubmission\": \"304.8K\", \"totalAcceptedRaw\": 135292, \"totalSubmissionRaw\": 304837, \"acRate\": \"44.4%\"}", "topicTags": [ { "name": "Math", "slug": "math" }, { "name": "Geometry", "slug": "geometry" } ] } } }
836
Easy
[ "An axis-aligned rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) is the coordinate of its bottom-left corner, and (x2, y2) is the coordinate of its top-right corner. Its top and bottom edges are parallel to the X-axis, and its left and right edges are parallel to the Y-axis.\n\nTwo rectangles ov...
[ { "hash": -4945409994630263000, "runtime": "7ms", "solution": "class Solution(object):\n def isRectangleOverlap(self, rec1, rec2):\n \"\"\"\n :type rec1: List[int]\n :type rec2: List[int]\n :rtype: bool\n \"\"\"\n if rec1[0] < rec2[2] and rec1[1] < rec2[3] an...
class Solution(object): def isRectangleOverlap(self, rec1, rec2): """ :type rec1: List[int] :type rec2: List[int] :rtype: bool """
None
None
None
None
None
None
44.4
Level 1
Hi
sign-of-the-product-of-an-array
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There is a function <code>signFunc(x)</code> that returns:</p>\n\n<ul>\n\t<li><code>1</code> if <code>x</code> is positive.</li>\n\t<li><code>-1</code> if <code>x</code> is negative.</li>\n\t<li><code>0</code> if <code>x</code> is equal to <code>0</code>.</li>\n</ul>\n\n<p>You are given an integer array <code>nums</code>. Let <code>product</code> be the product of all values in the array <code>nums</code>.</p>\n\n<p>Return <code>signFunc(product)</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [-1,-2,-3,-4,3,2,1]\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> The product of all values in the array is 144, and signFunc(144) = 1\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,5,0,2,-3]\n<strong>Output:</strong> 0\n<strong>Explanation:</strong> The product of all values in the array is 0, and signFunc(0) = 0\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [-1,1,-1,1,-1]\n<strong>Output:</strong> -1\n<strong>Explanation:</strong> The product of all values in the array is -1, and signFunc(-1) = -1\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 1000</code></li>\n\t<li><code>-100 &lt;= nums[i] &lt;= 100</code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "1822", "questionId": "1950", "questionTitle": "Sign of the Product of an Array", "questionTitleSlug": "sign-of-the-product-of-an-array", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"310K\", \"totalSubmission\": \"474.8K\", \"totalAcceptedRaw\": 309987, \"totalSubmissionRaw\": 474832, \"acRate\": \"65.3%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Math", "slug": "math" } ] } } }
1822
Easy
[ "There is a function signFunc(x) that returns:\n\n\n\t1 if x is positive.\n\t-1 if x is negative.\n\t0 if x is equal to 0.\n\n\nYou are given an integer array nums. Let product be the product of all values in the array nums.\n\nReturn signFunc(product).\n\n \nExample 1:\n\nInput: nums = [-1,-2,-3,-4,3,2,1]\nOutput:...
[ { "hash": 7369142928569103000, "runtime": "37ms", "solution": "class Solution:\n def arraySign(self, nums):\n result = 1\n for num in nums:\n result *= num\n if result < 0:\n return -1\n if result == 0:\n return 0\n if result > 0:\n ...
class Solution(object): def arraySign(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
65.3
Level 1
Hi
kth-largest-element-in-a-stream
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Design a class to find the <code>k<sup>th</sup></code> largest element in a stream. Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p>\n\n<p>Implement <code>KthLargest</code> class:</p>\n\n<ul>\n\t<li><code>KthLargest(int k, int[] nums)</code> Initializes the object with the integer <code>k</code> and the stream of integers <code>nums</code>.</li>\n\t<li><code>int add(int val)</code> Appends the integer <code>val</code> to the stream and returns the element representing the <code>k<sup>th</sup></code> largest element in the stream.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input</strong>\n[&quot;KthLargest&quot;, &quot;add&quot;, &quot;add&quot;, &quot;add&quot;, &quot;add&quot;, &quot;add&quot;]\n[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]\n<strong>Output</strong>\n[null, 4, 5, 5, 8, 8]\n\n<strong>Explanation</strong>\nKthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);\nkthLargest.add(3); // return 4\nkthLargest.add(5); // return 5\nkthLargest.add(10); // return 5\nkthLargest.add(9); // return 8\nkthLargest.add(4); // return 8\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>\n\t<li><code>0 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>\n\t<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>\n\t<li><code>-10<sup>4</sup> &lt;= val &lt;= 10<sup>4</sup></code></li>\n\t<li>At most <code>10<sup>4</sup></code> calls will be made to <code>add</code>.</li>\n\t<li>It is guaranteed that there will be at least <code>k</code> elements in the array when you search for the <code>k<sup>th</sup></code> element.</li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "703", "questionId": "789", "questionTitle": "Kth Largest Element in a Stream", "questionTitleSlug": "kth-largest-element-in-a-stream", "similarQuestions": "[{\"title\": \"Kth Largest Element in an Array\", \"titleSlug\": \"kth-largest-element-in-an-array\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Finding MK Average\", \"titleSlug\": \"finding-mk-average\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Sequentially Ordinal Rank Tracker\", \"titleSlug\": \"sequentially-ordinal-rank-tracker\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"481.3K\", \"totalSubmission\": \"850.3K\", \"totalAcceptedRaw\": 481261, \"totalSubmissionRaw\": 850324, \"acRate\": \"56.6%\"}", "topicTags": [ { "name": "Tree", "slug": "tree" }, { "name": "Design", "slug": "design" }, { "name": "Binary Search Tree", "slug": "binary-search-tree" }, { "name": "Heap (Priority Queue)", "slug": "heap-priority-queue" }, { "name": "Binary Tree", "slug": "binary-tree" }, { "name": "Data Stream", "slug": "data-stream" } ] } } }
703
Easy
[ "Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.\n\nImplement KthLargest class:\n\n\n\tKthLargest(int k, int[] nums) Initializes the object with the integer k and the stream of integers nums.\n\tint add(int val) A...
[ { "hash": -2736596448868835000, "runtime": "362ms", "solution": "class KthLargest:\n\n def __init__(self, k: int, nums: List[int]):\n nums.sort()\n self.k=k\n self.stream=nums\n while len(self.stream)>k:\n self.stream.pop(0)\n\n def add(self, val: int) -> int...
class KthLargest(object): def __init__(self, k, nums): """ :type k: int :type nums: List[int] """ def add(self, val): """ :type val: int :rtype: int """ # Your KthLargest object will be instantiated and called as such: # obj = KthLargest(k, nums) # param_1 = obj.add(val)
None
None
None
None
None
None
56.6
Level 1
Hi
incremental-memory-leak
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given two integers <code>memory1</code> and <code>memory2</code> representing the available memory in bits on two memory sticks. There is currently a faulty program running that consumes an increasing amount of memory every second.</p>\n\n<p>At the <code>i<sup>th</sup></code> second (starting from 1), <code>i</code> bits of memory are allocated to the stick with <strong>more available memory</strong> (or from the first memory stick if both have the same available memory). If neither stick has at least <code>i</code> bits of available memory, the program <strong>crashes</strong>.</p>\n\n<p>Return <em>an array containing </em><code>[crashTime, memory1<sub>crash</sub>, memory2<sub>crash</sub>]</code><em>, where </em><code>crashTime</code><em> is the time (in seconds) when the program crashed and </em><code>memory1<sub>crash</sub></code><em> and </em><code>memory2<sub>crash</sub></code><em> are the available bits of memory in the first and second sticks respectively</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> memory1 = 2, memory2 = 2\n<strong>Output:</strong> [3,1,0]\n<strong>Explanation:</strong> The memory is allocated as follows:\n- At the 1<sup>st</sup> second, 1 bit of memory is allocated to stick 1. The first stick now has 1 bit of available memory.\n- At the 2<sup>nd</sup> second, 2 bits of memory are allocated to stick 2. The second stick now has 0 bits of available memory.\n- At the 3<sup>rd</sup> second, the program crashes. The sticks have 1 and 0 bits available respectively.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> memory1 = 8, memory2 = 11\n<strong>Output:</strong> [6,0,4]\n<strong>Explanation:</strong> The memory is allocated as follows:\n- At the 1<sup>st</sup> second, 1 bit of memory is allocated to stick 2. The second stick now has 10 bit of available memory.\n- At the 2<sup>nd</sup> second, 2 bits of memory are allocated to stick 2. The second stick now has 8 bits of available memory.\n- At the 3<sup>rd</sup> second, 3 bits of memory are allocated to stick 1. The first stick now has 5 bits of available memory.\n- At the 4<sup>th</sup> second, 4 bits of memory are allocated to stick 2. The second stick now has 4 bits of available memory.\n- At the 5<sup>th</sup> second, 5 bits of memory are allocated to stick 1. The first stick now has 0 bits of available memory.\n- At the 6<sup>th</sup> second, the program crashes. The sticks have 0 and 4 bits available respectively.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 &lt;= memory1, memory2 &lt;= 2<sup>31</sup> - 1</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1860", "questionId": "1971", "questionTitle": "Incremental Memory Leak", "questionTitleSlug": "incremental-memory-leak", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"17.5K\", \"totalSubmission\": \"24.3K\", \"totalAcceptedRaw\": 17535, \"totalSubmissionRaw\": 24341, \"acRate\": \"72.0%\"}", "topicTags": [ { "name": "Simulation", "slug": "simulation" } ] } } }
1860
Medium
[ "You are given two integers memory1 and memory2 representing the available memory in bits on two memory sticks. There is currently a faulty program running that consumes an increasing amount of memory every second.\n\nAt the ith second (starting from 1), i bits of memory are allocated to the stick with more availab...
[ { "hash": -427686844496398400, "runtime": "258ms", "solution": "class Solution(object):\n def memLeak(self, memory1, memory2):\n i = 1\n while max(memory1,memory2)>=i:\n if memory1 >= memory2:\n memory1 -= i\n elif memory1 < memory2:\n ...
class Solution(object): def memLeak(self, memory1, memory2): """ :type memory1: int :type memory2: int :rtype: List[int] """
None
None
None
None
None
None
72
Level 2
Hi
longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an array of integers <code>nums</code> and an integer <code>limit</code>, return the size of the longest <strong>non-empty</strong> subarray such that the absolute difference between any two elements of this subarray is less than or equal to <code>limit</code><em>.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [8,2,4,7], limit = 4\n<strong>Output:</strong> 2 \n<strong>Explanation:</strong> All subarrays are: \n[8] with maximum absolute diff |8-8| = 0 &lt;= 4.\n[8,2] with maximum absolute diff |8-2| = 6 &gt; 4. \n[8,2,4] with maximum absolute diff |8-2| = 6 &gt; 4.\n[8,2,4,7] with maximum absolute diff |8-2| = 6 &gt; 4.\n[2] with maximum absolute diff |2-2| = 0 &lt;= 4.\n[2,4] with maximum absolute diff |2-4| = 2 &lt;= 4.\n[2,4,7] with maximum absolute diff |2-7| = 5 &gt; 4.\n[4] with maximum absolute diff |4-4| = 0 &lt;= 4.\n[4,7] with maximum absolute diff |4-7| = 3 &lt;= 4.\n[7] with maximum absolute diff |7-7| = 0 &lt;= 4. \nTherefore, the size of the longest subarray is 2.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [10,1,2,4,7,2], limit = 5\n<strong>Output:</strong> 4 \n<strong>Explanation:</strong> The subarray [2,4,7,2] is the longest since the maximum absolute diff is |2-7| = 5 &lt;= 5.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [4,2,2,2,4,4,2,2], limit = 0\n<strong>Output:</strong> 3\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>\n\t<li><code>0 &lt;= limit &lt;= 10<sup>9</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1438", "questionId": "1549", "questionTitle": "Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit", "questionTitleSlug": "longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit", "similarQuestions": "[{\"title\": \"Partition Array Such That Maximum Difference Is K\", \"titleSlug\": \"partition-array-such-that-maximum-difference-is-k\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Count Subarrays With Fixed Bounds\", \"titleSlug\": \"count-subarrays-with-fixed-bounds\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"119.4K\", \"totalSubmission\": \"244.2K\", \"totalAcceptedRaw\": 119360, \"totalSubmissionRaw\": 244175, \"acRate\": \"48.9%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Queue", "slug": "queue" }, { "name": "Sliding Window", "slug": "sliding-window" }, { "name": "Heap (Priority Queue)", "slug": "heap-priority-queue" }, { "name": "Ordered Set", "slug": "ordered-set" }, { "name": "Monotonic Queue", "slug": "monotonic-queue" } ] } } }
1438
Medium
[ "Given an array of integers nums and an integer limit, return the size of the longest non-empty subarray such that the absolute difference between any two elements of this subarray is less than or equal to limit.\n\n \nExample 1:\n\nInput: nums = [8,2,4,7], limit = 4\nOutput: 2 \nExplanation: All subarrays are: \n[...
[ { "hash": 4379590527409481000, "runtime": "511ms", "solution": "class Solution(object):\n def longestSubarray(self, nums, limit):\n \"\"\"\n :type nums: List[int]\n :type limit: int\n :rtype: int\n \"\"\"\n l = 0\n res = 1\n min_arr = collection...
class Solution(object): def longestSubarray(self, nums, limit): """ :type nums: List[int] :type limit: int :rtype: int """
None
None
None
None
None
None
48.9
Level 3
Hi
construct-smallest-number-from-di-string
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> string <code>pattern</code> of length <code>n</code> consisting of the characters <code>&#39;I&#39;</code> meaning <strong>increasing</strong> and <code>&#39;D&#39;</code> meaning <strong>decreasing</strong>.</p>\n\n<p>A <strong>0-indexed</strong> string <code>num</code> of length <code>n + 1</code> is created using the following conditions:</p>\n\n<ul>\n\t<li><code>num</code> consists of the digits <code>&#39;1&#39;</code> to <code>&#39;9&#39;</code>, where each digit is used <strong>at most</strong> once.</li>\n\t<li>If <code>pattern[i] == &#39;I&#39;</code>, then <code>num[i] &lt; num[i + 1]</code>.</li>\n\t<li>If <code>pattern[i] == &#39;D&#39;</code>, then <code>num[i] &gt; num[i + 1]</code>.</li>\n</ul>\n\n<p>Return <em>the lexicographically <strong>smallest</strong> possible string </em><code>num</code><em> that meets the conditions.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> pattern = &quot;IIIDIDDD&quot;\n<strong>Output:</strong> &quot;123549876&quot;\n<strong>Explanation:\n</strong>At indices 0, 1, 2, and 4 we must have that num[i] &lt; num[i+1].\nAt indices 3, 5, 6, and 7 we must have that num[i] &gt; num[i+1].\nSome possible values of num are &quot;245639871&quot;, &quot;135749862&quot;, and &quot;123849765&quot;.\nIt can be proven that &quot;123549876&quot; is the smallest possible num that meets the conditions.\nNote that &quot;123414321&quot; is not possible because the digit &#39;1&#39; is used more than once.</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> pattern = &quot;DDD&quot;\n<strong>Output:</strong> &quot;4321&quot;\n<strong>Explanation:</strong>\nSome possible values of num are &quot;9876&quot;, &quot;7321&quot;, and &quot;8742&quot;.\nIt can be proven that &quot;4321&quot; is the smallest possible num that meets the conditions.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= pattern.length &lt;= 8</code></li>\n\t<li><code>pattern</code> consists of only the letters <code>&#39;I&#39;</code> and <code>&#39;D&#39;</code>.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2375", "questionId": "2456", "questionTitle": "Construct Smallest Number From DI String", "questionTitleSlug": "construct-smallest-number-from-di-string", "similarQuestions": "[{\"title\": \"DI String Match\", \"titleSlug\": \"di-string-match\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"27.8K\", \"totalSubmission\": \"37K\", \"totalAcceptedRaw\": 27783, \"totalSubmissionRaw\": 37004, \"acRate\": \"75.1%\"}", "topicTags": [ { "name": "String", "slug": "string" }, { "name": "Backtracking", "slug": "backtracking" }, { "name": "Stack", "slug": "stack" }, { "name": "Greedy", "slug": "greedy" } ] } } }
2375
Medium
[ "You are given a 0-indexed string pattern of length n consisting of the characters 'I' meaning increasing and 'D' meaning decreasing.\n\nA 0-indexed string num of length n + 1 is created using the following conditions:\n\n\n\tnum consists of the digits '1' to '9', where each digit is used at most once.\n\tIf patter...
[ { "hash": -8920411412713249000, "runtime": "19ms", "solution": "class Solution:\n def smallestNumber(self, pattern):\n stack = []\n ans = []\n\n for i in range(len(pattern) + 1):\n stack.append(i + 1)\n\n if i == len(pattern) or pattern[i] == 'I':\n ...
class Solution(object): def smallestNumber(self, pattern): """ :type pattern: str :rtype: str """
None
None
None
None
None
None
75.1
Level 2
Hi
encrypt-and-decrypt-strings
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a character array <code>keys</code> containing <strong>unique</strong> characters and a string array <code>values</code> containing strings of length 2. You are also given another string array <code>dictionary</code> that contains all permitted original strings after decryption. You should implement a data structure that can encrypt or decrypt a <strong>0-indexed</strong> string.</p>\n\n<p>A string is <strong>encrypted</strong> with the following process:</p>\n\n<ol>\n\t<li>For each character <code>c</code> in the string, we find the index <code>i</code> satisfying <code>keys[i] == c</code> in <code>keys</code>.</li>\n\t<li>Replace <code>c</code> with <code>values[i]</code> in the string.</li>\n</ol>\n\n<p>Note that in case a character of the string is <strong>not present</strong> in <code>keys</code>, the encryption process cannot be carried out, and an empty string <code>&quot;&quot;</code> is returned.</p>\n\n<p>A string is <strong>decrypted</strong> with the following process:</p>\n\n<ol>\n\t<li>For each substring <code>s</code> of length 2 occurring at an even index in the string, we find an <code>i</code> such that <code>values[i] == s</code>. If there are multiple valid <code>i</code>, we choose <strong>any</strong> one of them. This means a string could have multiple possible strings it can decrypt to.</li>\n\t<li>Replace <code>s</code> with <code>keys[i]</code> in the string.</li>\n</ol>\n\n<p>Implement the <code>Encrypter</code> class:</p>\n\n<ul>\n\t<li><code>Encrypter(char[] keys, String[] values, String[] dictionary)</code> Initializes the <code>Encrypter</code> class with <code>keys, values</code>, and <code>dictionary</code>.</li>\n\t<li><code>String encrypt(String word1)</code> Encrypts <code>word1</code> with the encryption process described above and returns the encrypted string.</li>\n\t<li><code>int decrypt(String word2)</code> Returns the number of possible strings <code>word2</code> could decrypt to that also appear in <code>dictionary</code>.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input</strong>\n[&quot;Encrypter&quot;, &quot;encrypt&quot;, &quot;decrypt&quot;]\n[[[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&quot;ei&quot;, &quot;zf&quot;, &quot;ei&quot;, &quot;am&quot;], [&quot;abcd&quot;, &quot;acbd&quot;, &quot;adbc&quot;, &quot;badc&quot;, &quot;dacb&quot;, &quot;cadb&quot;, &quot;cbda&quot;, &quot;abad&quot;]], [&quot;abcd&quot;], [&quot;eizfeiam&quot;]]\n<strong>Output</strong>\n[null, &quot;eizfeiam&quot;, 2]\n\n<strong>Explanation</strong>\nEncrypter encrypter = new Encrypter([[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;], [&quot;ei&quot;, &quot;zf&quot;, &quot;ei&quot;, &quot;am&quot;], [&quot;abcd&quot;, &quot;acbd&quot;, &quot;adbc&quot;, &quot;badc&quot;, &quot;dacb&quot;, &quot;cadb&quot;, &quot;cbda&quot;, &quot;abad&quot;]);\nencrypter.encrypt(&quot;abcd&quot;); // return &quot;eizfeiam&quot;. \n&nbsp; // &#39;a&#39; maps to &quot;ei&quot;, &#39;b&#39; maps to &quot;zf&quot;, &#39;c&#39; maps to &quot;ei&quot;, and &#39;d&#39; maps to &quot;am&quot;.\nencrypter.decrypt(&quot;eizfeiam&quot;); // return 2. \n // &quot;ei&quot; can map to &#39;a&#39; or &#39;c&#39;, &quot;zf&quot; maps to &#39;b&#39;, and &quot;am&quot; maps to &#39;d&#39;. \n // Thus, the possible strings after decryption are &quot;abad&quot;, &quot;cbad&quot;, &quot;abcd&quot;, and &quot;cbcd&quot;. \n // 2 of those strings, &quot;abad&quot; and &quot;abcd&quot;, appear in dictionary, so the answer is 2.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= keys.length == values.length &lt;= 26</code></li>\n\t<li><code>values[i].length == 2</code></li>\n\t<li><code>1 &lt;= dictionary.length &lt;= 100</code></li>\n\t<li><code>1 &lt;= dictionary[i].length &lt;= 100</code></li>\n\t<li>All <code>keys[i]</code> and <code>dictionary[i]</code> are <strong>unique</strong>.</li>\n\t<li><code>1 &lt;= word1.length &lt;= 2000</code></li>\n\t<li><code>1 &lt;= word2.length &lt;= 200</code></li>\n\t<li>All <code>word1[i]</code> appear in <code>keys</code>.</li>\n\t<li><code>word2.length</code> is even.</li>\n\t<li><code>keys</code>, <code>values[i]</code>, <code>dictionary[i]</code>, <code>word1</code>, and <code>word2</code> only contain lowercase English letters.</li>\n\t<li>At most <code>200</code> calls will be made to <code>encrypt</code> and <code>decrypt</code> <strong>in total</strong>.</li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "2227", "questionId": "1433", "questionTitle": "Encrypt and Decrypt Strings", "questionTitleSlug": "encrypt-and-decrypt-strings", "similarQuestions": "[{\"title\": \"Implement Trie (Prefix Tree)\", \"titleSlug\": \"implement-trie-prefix-tree\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Word Search II\", \"titleSlug\": \"word-search-ii\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Implement Trie II (Prefix Tree)\", \"titleSlug\": \"implement-trie-ii-prefix-tree\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"11.4K\", \"totalSubmission\": \"28.6K\", \"totalAcceptedRaw\": 11368, \"totalSubmissionRaw\": 28615, \"acRate\": \"39.7%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" }, { "name": "String", "slug": "string" }, { "name": "Design", "slug": "design" }, { "name": "Trie", "slug": "trie" } ] } } }
2227
Hard
[ "You are given a character array keys containing unique characters and a string array values containing strings of length 2. You are also given another string array dictionary that contains all permitted original strings after decryption. You should implement a data structure that can encrypt or decrypt a 0-indexed...
[ { "hash": -4725060558562722000, "runtime": "453ms", "solution": "class Encrypter(object):\n\n def __init__(self, keys, values, dictionary):\n \"\"\"\n :type keys: List[str]\n :type values: List[str]\n :type dictionary: List[str]\n \"\"\"\n self.encDict = {}\n...
class Encrypter(object): def __init__(self, keys, values, dictionary): """ :type keys: List[str] :type values: List[str] :type dictionary: List[str] """ def encrypt(self, word1): """ :type word1: str :rtype: str """ def decrypt(self, word2): """ :type word2: str :rtype: int """ # Your Encrypter object will be instantiated and called as such: # obj = Encrypter(keys, values, dictionary) # param_1 = obj.encrypt(word1) # param_2 = obj.decrypt(word2)
None
None
None
None
None
None
39.7
Level 5
Hi
count-number-of-maximum-bitwise-or-subsets
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer array <code>nums</code>, find the <strong>maximum</strong> possible <strong>bitwise OR</strong> of a subset of <code>nums</code> and return <em>the <strong>number of different non-empty subsets</strong> with the maximum bitwise OR</em>.</p>\n\n<p>An array <code>a</code> is a <strong>subset</strong> of an array <code>b</code> if <code>a</code> can be obtained from <code>b</code> by deleting some (possibly zero) elements of <code>b</code>. Two subsets are considered <strong>different</strong> if the indices of the elements chosen are different.</p>\n\n<p>The bitwise OR of an array <code>a</code> is equal to <code>a[0] <strong>OR</strong> a[1] <strong>OR</strong> ... <strong>OR</strong> a[a.length - 1]</code> (<strong>0-indexed</strong>).</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [3,1]\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> The maximum possible bitwise OR of a subset is 3. There are 2 subsets with a bitwise OR of 3:\n- [3]\n- [3,1]\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [2,2,2]\n<strong>Output:</strong> 7\n<strong>Explanation:</strong> All non-empty subsets of [2,2,2] have a bitwise OR of 2. There are 2<sup>3</sup> - 1 = 7 total subsets.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [3,2,1,5]\n<strong>Output:</strong> 6\n<strong>Explanation:</strong> The maximum possible bitwise OR of a subset is 7. There are 6 subsets with a bitwise OR of 7:\n- [3,5]\n- [3,1,5]\n- [3,2,5]\n- [3,2,1,5]\n- [2,5]\n- [2,1,5]</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 16</code></li>\n\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2044", "questionId": "2170", "questionTitle": "Count Number of Maximum Bitwise-OR Subsets", "questionTitleSlug": "count-number-of-maximum-bitwise-or-subsets", "similarQuestions": "[{\"title\": \"Subsets\", \"titleSlug\": \"subsets\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Largest Combination With Bitwise AND Greater Than Zero\", \"titleSlug\": \"largest-combination-with-bitwise-and-greater-than-zero\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Longest Subarray With Maximum Bitwise AND\", \"titleSlug\": \"longest-subarray-with-maximum-bitwise-and\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"24.9K\", \"totalSubmission\": \"32.4K\", \"totalAcceptedRaw\": 24874, \"totalSubmissionRaw\": 32439, \"acRate\": \"76.7%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Backtracking", "slug": "backtracking" }, { "name": "Bit Manipulation", "slug": "bit-manipulation" } ] } } }
2044
Medium
[ "Given an integer array nums, find the maximum possible bitwise OR of a subset of nums and return the number of different non-empty subsets with the maximum bitwise OR.\n\nAn array a is a subset of an array b if a can be obtained from b by deleting some (possibly zero) elements of b. Two subsets are considered diff...
[ { "hash": 5221556856487852000, "runtime": "20ms", "solution": "class Solution(object):\n def countMaxOrSubsets(self, A):\n dp = collections.Counter([0])\n for a in A:\n for k, v in dp.items():\n dp[k | a] += v\n return dp[max(dp)]...
class Solution(object): def countMaxOrSubsets(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
76.7
Level 2
Hi
max-pair-sum-in-an-array
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You have to find the <strong>maximum</strong> sum of a pair of numbers from <code>nums</code> such that the maximum <strong>digit </strong>in both numbers are equal.</p>\n\n<p>Return <em>the maximum sum or</em> <code>-1</code><em> if no such pair exists</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [51,71,17,24,42]\n<strong>Output:</strong> 88\n<strong>Explanation:</strong> \nFor i = 1 and j = 2, nums[i] and nums[j] have equal maximum digits with a pair sum of 71 + 17 = 88. \nFor i = 3 and j = 4, nums[i] and nums[j] have equal maximum digits with a pair sum of 24 + 42 = 66.\nIt can be shown that there are no other pairs with equal maximum digits, so the answer is 88.</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,2,3,4]\n<strong>Output:</strong> -1\n<strong>Explanation:</strong> No pair exists in nums with equal maximum digits.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= nums.length &lt;= 100</code></li>\n\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "2815", "questionId": "2902", "questionTitle": "Max Pair Sum in an Array", "questionTitleSlug": "max-pair-sum-in-an-array", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"37.7K\", \"totalSubmission\": \"65.5K\", \"totalAcceptedRaw\": 37663, \"totalSubmissionRaw\": 65487, \"acRate\": \"57.5%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" } ] } } }
2815
Easy
[ "You are given a 0-indexed integer array nums. You have to find the maximum sum of a pair of numbers from nums such that the maximum digit in both numbers are equal.\n\nReturn the maximum sum or -1 if no such pair exists.\n\n \nExample 1:\n\nInput: nums = [51,71,17,24,42]\nOutput: 88\nExplanation: \nFor i = 1 and j...
[ { "hash": 4305591979644368400, "runtime": "89ms", "solution": "class Solution(object):\n def maxSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n dic = {}\n answer = 0\n for num in nums:\n max_value = int(max(str(num...
class Solution(object): def maxSum(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
57.5
Level 1
Hi
house-robber
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and <b>it will automatically contact the police if two adjacent houses were broken into on the same night</b>.</p>\n\n<p>Given an integer array <code>nums</code> representing the amount of money of each house, return <em>the maximum amount of money you can rob tonight <b>without alerting the police</b></em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,2,3,1]\n<strong>Output:</strong> 4\n<strong>Explanation:</strong> Rob house 1 (money = 1) and then rob house 3 (money = 3).\nTotal amount you can rob = 1 + 3 = 4.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [2,7,9,3,1]\n<strong>Output:</strong> 12\n<strong>Explanation:</strong> Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).\nTotal amount you can rob = 2 + 9 + 1 = 12.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 100</code></li>\n\t<li><code>0 &lt;= nums[i] &lt;= 400</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "198", "questionId": "198", "questionTitle": "House Robber", "questionTitleSlug": "house-robber", "similarQuestions": "[{\"title\": \"Maximum Product Subarray\", \"titleSlug\": \"maximum-product-subarray\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"House Robber II\", \"titleSlug\": \"house-robber-ii\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Paint House\", \"titleSlug\": \"paint-house\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Paint Fence\", \"titleSlug\": \"paint-fence\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"House Robber III\", \"titleSlug\": \"house-robber-iii\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Non-negative Integers without Consecutive Ones\", \"titleSlug\": \"non-negative-integers-without-consecutive-ones\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Coin Path\", \"titleSlug\": \"coin-path\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Delete and Earn\", \"titleSlug\": \"delete-and-earn\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Solving Questions With Brainpower\", \"titleSlug\": \"solving-questions-with-brainpower\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Count Number of Ways to Place Houses\", \"titleSlug\": \"count-number-of-ways-to-place-houses\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"House Robber IV\", \"titleSlug\": \"house-robber-iv\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Mice and Cheese\", \"titleSlug\": \"mice-and-cheese\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Largest Element in an Array after Merge Operations\", \"titleSlug\": \"largest-element-in-an-array-after-merge-operations\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"1.9M\", \"totalSubmission\": \"3.8M\", \"totalAcceptedRaw\": 1880533, \"totalSubmissionRaw\": 3751040, \"acRate\": \"50.1%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Dynamic Programming", "slug": "dynamic-programming" } ] } } }
198
Medium
[ "You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken i...
[ { "hash": -8413701205599297000, "runtime": "14ms", "solution": "class Solution(object):\n def rob(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n prev = curr = max_sum = 0\n for n in nums:\n tmp = curr\n curr = max...
class Solution(object): def rob(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
50.1
Level 3
Hi
bricks-falling-when-hit
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an <code>m x n</code> binary <code>grid</code>, where each <code>1</code> represents a brick and <code>0</code> represents an empty space. A brick is <strong>stable</strong> if:</p>\n\n<ul>\n\t<li>It is directly connected to the top of the grid, or</li>\n\t<li>At least one other brick in its four adjacent cells is <strong>stable</strong>.</li>\n</ul>\n\n<p>You are also given an array <code>hits</code>, which is a sequence of erasures we want to apply. Each time we want to erase the brick at the location <code>hits[i] = (row<sub>i</sub>, col<sub>i</sub>)</code>. The brick on that location&nbsp;(if it exists) will disappear. Some other bricks may no longer be stable because of that erasure and will <strong>fall</strong>. Once a brick falls, it is <strong>immediately</strong> erased from the <code>grid</code> (i.e., it does not land on other stable bricks).</p>\n\n<p>Return <em>an array </em><code>result</code><em>, where each </em><code>result[i]</code><em> is the number of bricks that will <strong>fall</strong> after the </em><code>i<sup>th</sup></code><em> erasure is applied.</em></p>\n\n<p><strong>Note</strong> that an erasure may refer to a location with no brick, and if it does, no bricks drop.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> grid = [[1,0,0,0],[1,1,1,0]], hits = [[1,0]]\n<strong>Output:</strong> [2]\n<strong>Explanation: </strong>Starting with the grid:\n[[1,0,0,0],\n [<u>1</u>,1,1,0]]\nWe erase the underlined brick at (1,0), resulting in the grid:\n[[1,0,0,0],\n [0,<u>1</u>,<u>1</u>,0]]\nThe two underlined bricks are no longer stable as they are no longer connected to the top nor adjacent to another stable brick, so they will fall. The resulting grid is:\n[[1,0,0,0],\n [0,0,0,0]]\nHence the result is [2].\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> grid = [[1,0,0,0],[1,1,0,0]], hits = [[1,1],[1,0]]\n<strong>Output:</strong> [0,0]\n<strong>Explanation: </strong>Starting with the grid:\n[[1,0,0,0],\n [1,<u>1</u>,0,0]]\nWe erase the underlined brick at (1,1), resulting in the grid:\n[[1,0,0,0],\n [1,0,0,0]]\nAll remaining bricks are still stable, so no bricks fall. The grid remains the same:\n[[1,0,0,0],\n [<u>1</u>,0,0,0]]\nNext, we erase the underlined brick at (1,0), resulting in the grid:\n[[1,0,0,0],\n [0,0,0,0]]\nOnce again, all remaining bricks are still stable, so no bricks fall.\nHence the result is [0,0].\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>m == grid.length</code></li>\n\t<li><code>n == grid[i].length</code></li>\n\t<li><code>1 &lt;= m, n &lt;= 200</code></li>\n\t<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code>.</li>\n\t<li><code>1 &lt;= hits.length &lt;= 4 * 10<sup>4</sup></code></li>\n\t<li><code>hits[i].length == 2</code></li>\n\t<li><code>0 &lt;= x<sub>i&nbsp;</sub>&lt;= m - 1</code></li>\n\t<li><code>0 &lt;=&nbsp;y<sub>i</sub> &lt;= n - 1</code></li>\n\t<li>All <code>(x<sub>i</sub>, y<sub>i</sub>)</code> are unique.</li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "803", "questionId": "821", "questionTitle": "Bricks Falling When Hit", "questionTitleSlug": "bricks-falling-when-hit", "similarQuestions": "[{\"title\": \"Last Day Where You Can Still Cross\", \"titleSlug\": \"last-day-where-you-can-still-cross\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Number of Ways to Build Sturdy Brick Wall\", \"titleSlug\": \"number-of-ways-to-build-sturdy-brick-wall\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"29.2K\", \"totalSubmission\": \"84.5K\", \"totalAcceptedRaw\": 29218, \"totalSubmissionRaw\": 84543, \"acRate\": \"34.6%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Union Find", "slug": "union-find" }, { "name": "Matrix", "slug": "matrix" } ] } } }
803
Hard
[ "You are given an m x n binary grid, where each 1 represents a brick and 0 represents an empty space. A brick is stable if:\n\n\n\tIt is directly connected to the top of the grid, or\n\tAt least one other brick in its four adjacent cells is stable.\n\n\nYou are also given an array hits, which is a sequence of erasu...
[ { "hash": -3634959599803926000, "runtime": "2703ms", "solution": "class Solution(object):\n def hitBricks(self, grid, hits):\n \"\"\"\n :type grid: List[List[int]]\n :type hits: List[List[int]]\n :rtype: List[int]\n \"\"\"\n\n dirs = [[1,0], [0,1], [-1,0], [0...
class Solution(object): def hitBricks(self, grid, hits): """ :type grid: List[List[int]] :type hits: List[List[int]] :rtype: List[int] """
None
None
None
None
None
None
34.6
Level 5
Hi
minimum-time-to-collect-all-apples-in-a-tree
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an undirected tree consisting of <code>n</code> vertices numbered from <code>0</code> to <code>n-1</code>, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. <em>Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at <strong>vertex 0</strong> and coming back to this vertex.</em></p>\n\n<p>The edges of the undirected tree are given in the array <code>edges</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> means that exists an edge connecting the vertices <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>. Additionally, there is a boolean array <code>hasApple</code>, where <code>hasApple[i] = true</code> means that vertex <code>i</code> has an apple; otherwise, it does not have any apple.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/04/23/min_time_collect_apple_1.png\" style=\"width: 300px; height: 212px;\" />\n<pre>\n<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,true,true,false]\n<strong>Output:</strong> 8 \n<strong>Explanation:</strong> The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows. \n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/04/23/min_time_collect_apple_2.png\" style=\"width: 300px; height: 212px;\" />\n<pre>\n<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,false,true,false]\n<strong>Output:</strong> 6\n<strong>Explanation:</strong> The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows. \n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,false,false,false,false,false]\n<strong>Output:</strong> 0\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>\n\t<li><code>edges.length == n - 1</code></li>\n\t<li><code>edges[i].length == 2</code></li>\n\t<li><code>0 &lt;= a<sub>i</sub> &lt; b<sub>i</sub> &lt;= n - 1</code></li>\n\t<li><code>hasApple.length == n</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1443", "questionId": "1554", "questionTitle": "Minimum Time to Collect All Apples in a Tree", "questionTitleSlug": "minimum-time-to-collect-all-apples-in-a-tree", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"106.9K\", \"totalSubmission\": \"171.6K\", \"totalAcceptedRaw\": 106944, \"totalSubmissionRaw\": 171561, \"acRate\": \"62.3%\"}", "topicTags": [ { "name": "Hash Table", "slug": "hash-table" }, { "name": "Tree", "slug": "tree" }, { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Breadth-First Search", "slug": "breadth-first-search" } ] } } }
1443
Medium
[ "Given an undirected tree consisting of n vertices numbered from 0 to n-1, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. Return the minimum time in seconds you have to spend to collect all apples in the tree, starting at vertex 0 and coming back to this vertex.\n\nTh...
[ { "hash": 6323627901980146000, "runtime": "533ms", "solution": "class Solution(object):\n def minTime(self, n, edges, hasApple):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :type hasApple: List[bool]\n :rtype: int\n \"\"\"\n ad = defaultdi...
class Solution(object): def minTime(self, n, edges, hasApple): """ :type n: int :type edges: List[List[int]] :type hasApple: List[bool] :rtype: int """
None
None
None
None
None
None
62.3
Level 2
Hi
increment-submatrices-by-one
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a positive integer <code>n</code>, indicating that we initially have an <code>n x n</code>&nbsp;<strong>0-indexed</strong> integer matrix <code>mat</code> filled with zeroes.</p>\n\n<p>You are also given a 2D integer array <code>query</code>. For each <code>query[i] = [row1<sub>i</sub>, col1<sub>i</sub>, row2<sub>i</sub>, col2<sub>i</sub>]</code>, you should do the following operation:</p>\n\n<ul>\n\t<li>Add <code>1</code> to <strong>every element</strong> in the submatrix with the <strong>top left</strong> corner <code>(row1<sub>i</sub>, col1<sub>i</sub>)</code> and the <strong>bottom right</strong> corner <code>(row2<sub>i</sub>, col2<sub>i</sub>)</code>. That is, add <code>1</code> to <code>mat[x][y]</code> for all <code>row1<sub>i</sub> &lt;= x &lt;= row2<sub>i</sub></code> and <code>col1<sub>i</sub> &lt;= y &lt;= col2<sub>i</sub></code>.</li>\n</ul>\n\n<p>Return<em> the matrix</em> <code>mat</code><em> after performing every query.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/11/24/p2example11.png\" style=\"width: 531px; height: 121px;\" />\n<pre>\n<strong>Input:</strong> n = 3, queries = [[1,1,2,2],[0,0,1,1]]\n<strong>Output:</strong> [[1,1,0],[1,2,1],[0,1,1]]\n<strong>Explanation:</strong> The diagram above shows the initial matrix, the matrix after the first query, and the matrix after the second query.\n- In the first query, we add 1 to every element in the submatrix with the top left corner (1, 1) and bottom right corner (2, 2).\n- In the second query, we add 1 to every element in the submatrix with the top left corner (0, 0) and bottom right corner (1, 1).\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/11/24/p2example22.png\" style=\"width: 261px; height: 82px;\" />\n<pre>\n<strong>Input:</strong> n = 2, queries = [[0,0,1,1]]\n<strong>Output:</strong> [[1,1],[1,1]]\n<strong>Explanation:</strong> The diagram above shows the initial matrix and the matrix after the first query.\n- In the first query we add 1 to every element in the matrix.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= n &lt;= 500</code></li>\n\t<li><code>1 &lt;= queries.length &lt;= 10<sup>4</sup></code></li>\n\t<li><code>0 &lt;= row1<sub>i</sub> &lt;= row2<sub>i</sub> &lt; n</code></li>\n\t<li><code>0 &lt;= col1<sub>i</sub> &lt;= col2<sub>i</sub> &lt; n</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2536", "questionId": "2625", "questionTitle": "Increment Submatrices by One", "questionTitleSlug": "increment-submatrices-by-one", "similarQuestions": "[{\"title\": \"Range Sum Query 2D - Mutable\", \"titleSlug\": \"range-sum-query-2d-mutable\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Count Positions on Street With Required Brightness\", \"titleSlug\": \"count-positions-on-street-with-required-brightness\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"19.2K\", \"totalSubmission\": \"39.7K\", \"totalAcceptedRaw\": 19184, \"totalSubmissionRaw\": 39698, \"acRate\": \"48.3%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Matrix", "slug": "matrix" }, { "name": "Prefix Sum", "slug": "prefix-sum" } ] } } }
2536
Medium
[ "You are given a positive integer n, indicating that we initially have an n x n 0-indexed integer matrix mat filled with zeroes.\n\nYou are also given a 2D integer array query. For each query[i] = [row1i, col1i, row2i, col2i], you should do the following operation:\n\n\n\tAdd 1 to every element in the submatrix wit...
[ { "hash": 822886350135179100, "runtime": "1950ms", "solution": "class Solution(object):\n def rangeAddQueries(self, n, queries):\n \"\"\"\n :type n: int\n :type queries: List[List[int]]\n :rtype: List[List[int]]\n \"\"\"\n \n arr = [[0]*(n) for _ in ra...
class Solution(object): def rangeAddQueries(self, n, queries): """ :type n: int :type queries: List[List[int]] :rtype: List[List[int]] """
None
None
None
None
None
None
48.3
Level 3
Hi
minimum-sum-of-mountain-triplets-i
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of integers.</p>\n\n<p>A triplet of indices <code>(i, j, k)</code> is a <strong>mountain</strong> if:</p>\n\n<ul>\n\t<li><code>i &lt; j &lt; k</code></li>\n\t<li><code>nums[i] &lt; nums[j]</code> and <code>nums[k] &lt; nums[j]</code></li>\n</ul>\n\n<p>Return <em>the <strong>minimum possible sum</strong> of a mountain triplet of</em> <code>nums</code>. <em>If no such triplet exists, return</em> <code>-1</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [8,6,1,5,3]\n<strong>Output:</strong> 9\n<strong>Explanation:</strong> Triplet (2, 3, 4) is a mountain triplet of sum 9 since: \n- 2 &lt; 3 &lt; 4\n- nums[2] &lt; nums[3] and nums[4] &lt; nums[3]\nAnd the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [5,4,8,7,10,2]\n<strong>Output:</strong> 13\n<strong>Explanation:</strong> Triplet (1, 3, 5) is a mountain triplet of sum 13 since: \n- 1 &lt; 3 &lt; 5\n- nums[1] &lt; nums[3] and nums[5] &lt; nums[3]\nAnd the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [6,5,4,3,4,5]\n<strong>Output:</strong> -1\n<strong>Explanation:</strong> It can be shown that there are no mountain triplets in nums.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>3 &lt;= nums.length &lt;= 50</code></li>\n\t<li><code>1 &lt;= nums[i] &lt;= 50</code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "2908", "questionId": "3176", "questionTitle": "Minimum Sum of Mountain Triplets I", "questionTitleSlug": "minimum-sum-of-mountain-triplets-i", "similarQuestions": "[{\"title\": \"3Sum\", \"titleSlug\": \"3sum\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Number of Arithmetic Triplets\", \"titleSlug\": \"number-of-arithmetic-triplets\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Maximum Value of an Ordered Triplet I\", \"titleSlug\": \"maximum-value-of-an-ordered-triplet-i\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"26.9K\", \"totalSubmission\": \"42.5K\", \"totalAcceptedRaw\": 26864, \"totalSubmissionRaw\": 42499, \"acRate\": \"63.2%\"}", "topicTags": [ { "name": "Array", "slug": "array" } ] } } }
2908
Easy
[ "You are given a 0-indexed array nums of integers.\n\nA triplet of indices (i, j, k) is a mountain if:\n\n\n\ti < j < k\n\tnums[i] < nums[j] and nums[k] < nums[j]\n\n\nReturn the minimum possible sum of a mountain triplet of nums. If no such triplet exists, return -1.\n\n \nExample 1:\n\nInput: nums = [8,6,1,5,3]\n...
[ { "hash": 2049191256262178600, "runtime": "92ms", "solution": "class Solution(object):\n def minimumSum(self, nums):\n min_sum = 151\n for i in range(len(nums)):\n for j in range(i + 1, len(nums)):\n for k in range(j + 1, len(nums)):\n if num...
class Solution(object): def minimumSum(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
63.2
Level 1
Hi
reverse-nodes-in-even-length-groups
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given the <code>head</code> of a linked list.</p>\n\n<p>The nodes in the linked list are <strong>sequentially</strong> assigned to <strong>non-empty</strong> groups whose lengths form the sequence of the natural numbers (<code>1, 2, 3, 4, ...</code>). The <strong>length</strong> of a group is the number of nodes assigned to it. In other words,</p>\n\n<ul>\n\t<li>The <code>1<sup>st</sup></code> node is assigned to the first group.</li>\n\t<li>The <code>2<sup>nd</sup></code> and the <code>3<sup>rd</sup></code> nodes are assigned to the second group.</li>\n\t<li>The <code>4<sup>th</sup></code>, <code>5<sup>th</sup></code>, and <code>6<sup>th</sup></code> nodes are assigned to the third group, and so on.</li>\n</ul>\n\n<p>Note that the length of the last group may be less than or equal to <code>1 + the length of the second to last group</code>.</p>\n\n<p><strong>Reverse</strong> the nodes in each group with an <strong>even</strong> length, and return <em>the</em> <code>head</code> <em>of the modified linked list</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/10/25/eg1.png\" style=\"width: 699px; height: 124px;\" />\n<pre>\n<strong>Input:</strong> head = [5,2,6,3,9,1,7,3,8,4]\n<strong>Output:</strong> [5,6,2,3,9,1,4,8,3,7]\n<strong>Explanation:</strong>\n- The length of the first group is 1, which is odd, hence no reversal occurs.\n- The length of the second group is 2, which is even, hence the nodes are reversed.\n- The length of the third group is 3, which is odd, hence no reversal occurs.\n- The length of the last group is 4, which is even, hence the nodes are reversed.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/10/25/eg2.png\" style=\"width: 284px; height: 114px;\" />\n<pre>\n<strong>Input:</strong> head = [1,1,0,6]\n<strong>Output:</strong> [1,0,1,6]\n<strong>Explanation:</strong>\n- The length of the first group is 1. No reversal occurs.\n- The length of the second group is 2. The nodes are reversed.\n- The length of the last group is 1. No reversal occurs.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/11/17/ex3.png\" style=\"width: 348px; height: 114px;\" />\n<pre>\n<strong>Input:</strong> head = [1,1,0,6,5]\n<strong>Output:</strong> [1,0,1,5,6]\n<strong>Explanation:</strong>\n- The length of the first group is 1. No reversal occurs.\n- The length of the second group is 2. The nodes are reversed.\n- The length of the last group is 2. The nodes are reversed.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li>The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>\n\t<li><code>0 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2074", "questionId": "2196", "questionTitle": "Reverse Nodes in Even Length Groups", "questionTitleSlug": "reverse-nodes-in-even-length-groups", "similarQuestions": "[{\"title\": \"Reverse Nodes in k-Group\", \"titleSlug\": \"reverse-nodes-in-k-group\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Reverse Linked List\", \"titleSlug\": \"reverse-linked-list\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"20.8K\", \"totalSubmission\": \"37.2K\", \"totalAcceptedRaw\": 20800, \"totalSubmissionRaw\": 37198, \"acRate\": \"55.9%\"}", "topicTags": [ { "name": "Linked List", "slug": "linked-list" } ] } } }
2074
Medium
[ "You are given the head of a linked list.\n\nThe nodes in the linked list are sequentially assigned to non-empty groups whose lengths form the sequence of the natural numbers (1, 2, 3, 4, ...). The length of a group is the number of nodes assigned to it. In other words,\n\n\n\tThe 1st node is assigned to the first ...
[ { "hash": -6953931825597435000, "runtime": "2070ms", "solution": "\"\"\"\n1. We can think of each of these groups of nodes as separate linked lists. So, for each of these groups applying an in-place linked list reversal solves the original problem. \n\n2. Also, we’ll need to modify the terminating condi...
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution(object): def reverseEvenLengthGroups(self, head): """ :type head: Optional[ListNode] :rtype: Optional[ListNode] """
None
None
None
None
None
None
55.9
Level 3
Hi
finding-mk-average
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given two integers, <code>m</code> and <code>k</code>, and a stream of integers. You are tasked to implement a data structure that calculates the <strong>MKAverage</strong> for the stream.</p>\n\n<p>The <strong>MKAverage</strong> can be calculated using these steps:</p>\n\n<ol>\n\t<li>If the number of the elements in the stream is less than <code>m</code> you should consider the <strong>MKAverage</strong> to be <code>-1</code>. Otherwise, copy the last <code>m</code> elements of the stream to a separate container.</li>\n\t<li>Remove the smallest <code>k</code> elements and the largest <code>k</code> elements from the container.</li>\n\t<li>Calculate the average value for the rest of the elements <strong>rounded down to the nearest integer</strong>.</li>\n</ol>\n\n<p>Implement the <code>MKAverage</code> class:</p>\n\n<ul>\n\t<li><code>MKAverage(int m, int k)</code> Initializes the <strong>MKAverage</strong> object with an empty stream and the two integers <code>m</code> and <code>k</code>.</li>\n\t<li><code>void addElement(int num)</code> Inserts a new element <code>num</code> into the stream.</li>\n\t<li><code>int calculateMKAverage()</code> Calculates and returns the <strong>MKAverage</strong> for the current stream <strong>rounded down to the nearest integer</strong>.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input</strong>\n[&quot;MKAverage&quot;, &quot;addElement&quot;, &quot;addElement&quot;, &quot;calculateMKAverage&quot;, &quot;addElement&quot;, &quot;calculateMKAverage&quot;, &quot;addElement&quot;, &quot;addElement&quot;, &quot;addElement&quot;, &quot;calculateMKAverage&quot;]\n[[3, 1], [3], [1], [], [10], [], [5], [5], [5], []]\n<strong>Output</strong>\n[null, null, null, -1, null, 3, null, null, null, 5]\n\n<strong>Explanation</strong>\n<code>MKAverage obj = new MKAverage(3, 1); \nobj.addElement(3); // current elements are [3]\nobj.addElement(1); // current elements are [3,1]\nobj.calculateMKAverage(); // return -1, because m = 3 and only 2 elements exist.\nobj.addElement(10); // current elements are [3,1,10]\nobj.calculateMKAverage(); // The last 3 elements are [3,1,10].\n // After removing smallest and largest 1 element the container will be [3].\n // The average of [3] equals 3/1 = 3, return 3\nobj.addElement(5); // current elements are [3,1,10,5]\nobj.addElement(5); // current elements are [3,1,10,5,5]\nobj.addElement(5); // current elements are [3,1,10,5,5,5]\nobj.calculateMKAverage(); // The last 3 elements are [5,5,5].\n // After removing smallest and largest 1 element the container will be [5].\n // The average of [5] equals 5/1 = 5, return 5\n</code></pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>3 &lt;= m &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= k*2 &lt; m</code></li>\n\t<li><code>1 &lt;= num &lt;= 10<sup>5</sup></code></li>\n\t<li>At most <code>10<sup>5</sup></code> calls will be made to <code>addElement</code> and <code>calculateMKAverage</code>.</li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "1825", "questionId": "1953", "questionTitle": "Finding MK Average", "questionTitleSlug": "finding-mk-average", "similarQuestions": "[{\"title\": \"Find Median from Data Stream\", \"titleSlug\": \"find-median-from-data-stream\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Kth Largest Element in a Stream\", \"titleSlug\": \"kth-largest-element-in-a-stream\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Sequentially Ordinal Rank Tracker\", \"titleSlug\": \"sequentially-ordinal-rank-tracker\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"12.9K\", \"totalSubmission\": \"35.5K\", \"totalAcceptedRaw\": 12909, \"totalSubmissionRaw\": 35541, \"acRate\": \"36.3%\"}", "topicTags": [ { "name": "Design", "slug": "design" }, { "name": "Queue", "slug": "queue" }, { "name": "Heap (Priority Queue)", "slug": "heap-priority-queue" }, { "name": "Data Stream", "slug": "data-stream" }, { "name": "Ordered Set", "slug": "ordered-set" } ] } } }
1825
Hard
[ "You are given two integers, m and k, and a stream of integers. You are tasked to implement a data structure that calculates the MKAverage for the stream.\n\nThe MKAverage can be calculated using these steps:\n\n\n\tIf the number of the elements in the stream is less than m you should consider the MKAverage to be -...
[ { "hash": 2041835027533204000, "runtime": "1291ms", "solution": "from sortedcontainers import SortedList\nclass MKAverage(object):\n\n def __init__(self, m, k):\n \"\"\"\n :type m: int\n :type k: int\n \"\"\"\n self.m = m\n self.k = k\n self.minList = ...
class MKAverage(object): def __init__(self, m, k): """ :type m: int :type k: int """ def addElement(self, num): """ :type num: int :rtype: None """ def calculateMKAverage(self): """ :rtype: int """ # Your MKAverage object will be instantiated and called as such: # obj = MKAverage(m, k) # obj.addElement(num) # param_2 = obj.calculateMKAverage()
None
None
None
None
None
None
36.3
Level 5
Hi
sum-of-nodes-with-even-valued-grandparent
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given the <code>root</code> of a binary tree, return <em>the sum of values of nodes with an <strong>even-valued grandparent</strong></em>. If there are no nodes with an <strong>even-valued grandparent</strong>, return <code>0</code>.</p>\n\n<p>A <strong>grandparent</strong> of a node is the parent of its parent if it exists.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/08/10/even1-tree.jpg\" style=\"width: 504px; height: 302px;\" />\n<pre>\n<strong>Input:</strong> root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]\n<strong>Output:</strong> 18\n<strong>Explanation:</strong> The red nodes are the nodes with even-value grandparent while the blue nodes are the even-value grandparents.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/08/10/even2-tree.jpg\" style=\"width: 64px; height: 65px;\" />\n<pre>\n<strong>Input:</strong> root = [1]\n<strong>Output:</strong> 0\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>\n\t<li><code>1 &lt;= Node.val &lt;= 100</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1315", "questionId": "1243", "questionTitle": "Sum of Nodes with Even-Valued Grandparent", "questionTitleSlug": "sum-of-nodes-with-even-valued-grandparent", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"141.6K\", \"totalSubmission\": \"165.6K\", \"totalAcceptedRaw\": 141563, \"totalSubmissionRaw\": 165553, \"acRate\": \"85.5%\"}", "topicTags": [ { "name": "Tree", "slug": "tree" }, { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Breadth-First Search", "slug": "breadth-first-search" }, { "name": "Binary Tree", "slug": "binary-tree" } ] } } }
1315
Medium
[ "Given the root of a binary tree, return the sum of values of nodes with an even-valued grandparent. If there are no nodes with an even-valued grandparent, return 0.\n\nA grandparent of a node is the parent of its parent if it exists.\n\n \nExample 1:\n\nInput: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]\nOu...
[ { "hash": 3271317099610890000, "runtime": "96ms", "solution": "# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution(object):\n def su...
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution(object): def sumEvenGrandparent(self, root): """ :type root: TreeNode :rtype: int """
None
None
None
None
None
None
85.5
Level 2
Hi
maximum-number-of-k-divisible-components
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. You are given the integer <code>n</code> and a 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>\n\n<p>You are also given a <strong>0-indexed</strong> integer array <code>values</code> of length <code>n</code>, where <code>values[i]</code> is the <strong>value</strong> associated with the <code>i<sup>th</sup></code> node, and an integer <code>k</code>.</p>\n\n<p>A <strong>valid split</strong> of the tree is obtained by removing any set of edges, possibly empty, from the tree such that the resulting components all have values that are divisible by <code>k</code>, where the <strong>value of a connected component</strong> is the sum of the values of its nodes.</p>\n\n<p>Return <em>the <strong>maximum number of components</strong> in any valid split</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2023/08/07/example12-cropped2svg.jpg\" style=\"width: 1024px; height: 453px;\" />\n<pre>\n<strong>Input:</strong> n = 5, edges = [[0,2],[1,2],[1,3],[2,4]], values = [1,8,1,4,4], k = 6\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> We remove the edge connecting node 1 with 2. The resulting split is valid because:\n- The value of the component containing nodes 1 and 3 is values[1] + values[3] = 12.\n- The value of the component containing nodes 0, 2, and 4 is values[0] + values[2] + values[4] = 6.\nIt can be shown that no other valid split has more than 2 connected components.</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2023/08/07/example21svg-1.jpg\" style=\"width: 999px; height: 338px;\" />\n<pre>\n<strong>Input:</strong> n = 7, edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [3,0,6,1,5,2,1], k = 3\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> We remove the edge connecting node 0 with 2, and the edge connecting node 0 with 1. The resulting split is valid because:\n- The value of the component containing node 0 is values[0] = 3.\n- The value of the component containing nodes 2, 5, and 6 is values[2] + values[5] + values[6] = 9.\n- The value of the component containing nodes 1, 3, and 4 is values[1] + values[3] + values[4] = 6.\nIt can be shown that no other valid split has more than 3 connected components.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li>\n\t<li><code>edges.length == n - 1</code></li>\n\t<li><code>edges[i].length == 2</code></li>\n\t<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>\n\t<li><code>values.length == n</code></li>\n\t<li><code>0 &lt;= values[i] &lt;= 10<sup>9</sup></code></li>\n\t<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>\n\t<li>Sum of <code>values</code> is divisible by <code>k</code>.</li>\n\t<li>The input is generated such that <code>edges</code> represents a valid tree.</li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "2872", "questionId": "3058", "questionTitle": "Maximum Number of K-Divisible Components", "questionTitleSlug": "maximum-number-of-k-divisible-components", "similarQuestions": "[{\"title\": \"Create Components With Same Value\", \"titleSlug\": \"create-components-with-same-value\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"9.1K\", \"totalSubmission\": \"15.2K\", \"totalAcceptedRaw\": 9121, \"totalSubmissionRaw\": 15205, \"acRate\": \"60.0%\"}", "topicTags": [ { "name": "Dynamic Programming", "slug": "dynamic-programming" }, { "name": "Tree", "slug": "tree" }, { "name": "Depth-First Search", "slug": "depth-first-search" } ] } } }
2872
Hard
[ "There is an undirected tree with n nodes labeled from 0 to n - 1. You are given the integer n and a 2D integer array edges of length n - 1, where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.\n\nYou are also given a 0-indexed integer array values of length n, where values...
[ { "hash": -4931007637798567000, "runtime": "1212ms", "solution": "class Solution(object):\n def maxKDivisibleComponents(self, n, edges, values, k):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :type values: List[int]\n :type k: int\n :rtype: int\n...
class Solution(object): def maxKDivisibleComponents(self, n, edges, values, k): """ :type n: int :type edges: List[List[int]] :type values: List[int] :type k: int :rtype: int """
None
None
None
None
None
None
60
Level 5
Hi
n-ary-tree-postorder-traversal
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given the <code>root</code> of an n-ary tree, return <em>the postorder traversal of its nodes&#39; values</em>.</p>\n\n<p>Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img src=\"https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png\" style=\"width: 100%; max-width: 300px;\" />\n<pre>\n<strong>Input:</strong> root = [1,null,3,2,4,null,5,6]\n<strong>Output:</strong> [5,6,3,2,4,1]\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png\" style=\"width: 296px; height: 241px;\" />\n<pre>\n<strong>Input:</strong> root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]\n<strong>Output:</strong> [2,6,14,11,7,3,12,8,4,13,9,10,5,1]\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>\n\t<li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li>\n\t<li>The height of the n-ary tree is less than or equal to <code>1000</code>.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong>Follow up:</strong> Recursive solution is trivial, could you do it iteratively?</p>\n", "difficulty": "Easy", "questionFrontendId": "590", "questionId": "776", "questionTitle": "N-ary Tree Postorder Traversal", "questionTitleSlug": "n-ary-tree-postorder-traversal", "similarQuestions": "[{\"title\": \"Binary Tree Postorder Traversal\", \"titleSlug\": \"binary-tree-postorder-traversal\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"N-ary Tree Level Order Traversal\", \"titleSlug\": \"n-ary-tree-level-order-traversal\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"N-ary Tree Preorder Traversal\", \"titleSlug\": \"n-ary-tree-preorder-traversal\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"241.9K\", \"totalSubmission\": \"311.6K\", \"totalAcceptedRaw\": 241861, \"totalSubmissionRaw\": 311575, \"acRate\": \"77.6%\"}", "topicTags": [ { "name": "Stack", "slug": "stack" }, { "name": "Tree", "slug": "tree" }, { "name": "Depth-First Search", "slug": "depth-first-search" } ] } } }
590
Easy
[ "Given the root of an n-ary tree, return the postorder traversal of its nodes' values.\n\nNary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)\n\n \nExample 1:\n\nInput: root = [1,null,3,2,4,null,5,6]\nOutput: [5,6,3,2,4,1]...
[ { "hash": -5933833584734762000, "runtime": "34ms", "solution": "\"\"\"\n# Definition for a Node.\nclass Node:\n def __init__(self, val=None, children=None):\n self.val = val\n self.children = children\n\"\"\"\n\nclass Solution:\n def postorder(self, root: 'Node') -> List[int]:\n ...
""" # Definition for a Node. class Node(object): def __init__(self, val=None, children=None): self.val = val self.children = children """ class Solution(object): def postorder(self, root): """ :type root: Node :rtype: List[int] """
None
None
None
None
None
None
77.6
Level 1
Hi
longest-absolute-file-path
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture:</p>\n\n<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/08/28/mdir.jpg\" style=\"width: 681px; height: 322px;\" /></p>\n\n<p>Here, we have <code>dir</code> as the only directory in the root. <code>dir</code> contains two subdirectories, <code>subdir1</code> and <code>subdir2</code>. <code>subdir1</code> contains a file <code>file1.ext</code> and subdirectory <code>subsubdir1</code>. <code>subdir2</code> contains a subdirectory <code>subsubdir2</code>, which contains a file <code>file2.ext</code>.</p>\n\n<p>In text form, it looks like this (with ⟶ representing the tab character):</p>\n\n<pre>\ndir\n⟶ subdir1\n⟶ ⟶ file1.ext\n⟶ ⟶ subsubdir1\n⟶ subdir2\n⟶ ⟶ subsubdir2\n⟶ ⟶ ⟶ file2.ext\n</pre>\n\n<p>If we were to write this representation in code, it will look like this: <code>&quot;dir\\n\\tsubdir1\\n\\t\\tfile1.ext\\n\\t\\tsubsubdir1\\n\\tsubdir2\\n\\t\\tsubsubdir2\\n\\t\\t\\tfile2.ext&quot;</code>. Note that the <code>&#39;\\n&#39;</code> and <code>&#39;\\t&#39;</code> are the new-line and tab characters.</p>\n\n<p>Every file and directory has a unique <strong>absolute path</strong> in the file system, which is the order of directories that must be opened to reach the file/directory itself, all concatenated by <code>&#39;/&#39;s</code>. Using the above example, the <strong>absolute path</strong> to <code>file2.ext</code> is <code>&quot;dir/subdir2/subsubdir2/file2.ext&quot;</code>. Each directory name consists of letters, digits, and/or spaces. Each file name is of the form <code>name.extension</code>, where <code>name</code> and <code>extension</code> consist of letters, digits, and/or spaces.</p>\n\n<p>Given a string <code>input</code> representing the file system in the explained format, return <em>the length of the <strong>longest absolute path</strong> to a <strong>file</strong> in the abstracted file system</em>. If there is no file in the system, return <code>0</code>.</p>\n\n<p><strong>Note</strong> that the testcases are generated such that the file system is valid and no file or directory name has length 0.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/08/28/dir1.jpg\" style=\"width: 401px; height: 202px;\" />\n<pre>\n<strong>Input:</strong> input = &quot;dir\\n\\tsubdir1\\n\\tsubdir2\\n\\t\\tfile.ext&quot;\n<strong>Output:</strong> 20\n<strong>Explanation:</strong> We have only one file, and the absolute path is &quot;dir/subdir2/file.ext&quot; of length 20.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/08/28/dir2.jpg\" style=\"width: 641px; height: 322px;\" />\n<pre>\n<strong>Input:</strong> input = &quot;dir\\n\\tsubdir1\\n\\t\\tfile1.ext\\n\\t\\tsubsubdir1\\n\\tsubdir2\\n\\t\\tsubsubdir2\\n\\t\\t\\tfile2.ext&quot;\n<strong>Output:</strong> 32\n<strong>Explanation:</strong> We have two files:\n&quot;dir/subdir1/file1.ext&quot; of length 21\n&quot;dir/subdir2/subsubdir2/file2.ext&quot; of length 32.\nWe return 32 since it is the longest absolute path to a file.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> input = &quot;a&quot;\n<strong>Output:</strong> 0\n<strong>Explanation:</strong> We do not have any files, just a single directory named &quot;a&quot;.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= input.length &lt;= 10<sup>4</sup></code></li>\n\t<li><code>input</code> may contain lowercase or uppercase English letters, a new line character <code>&#39;\\n&#39;</code>, a tab character <code>&#39;\\t&#39;</code>, a dot <code>&#39;.&#39;</code>, a space <code>&#39; &#39;</code>, and digits.</li>\n\t<li>All file and directory names have <strong>positive</strong> length.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "388", "questionId": "388", "questionTitle": "Longest Absolute File Path", "questionTitleSlug": "longest-absolute-file-path", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"147.8K\", \"totalSubmission\": \"315K\", \"totalAcceptedRaw\": 147845, \"totalSubmissionRaw\": 314970, \"acRate\": \"46.9%\"}", "topicTags": [ { "name": "String", "slug": "string" }, { "name": "Stack", "slug": "stack" }, { "name": "Depth-First Search", "slug": "depth-first-search" } ] } } }
388
Medium
[ "Suppose we have a file system that stores both files and directories. An example of one system is represented in the following picture:\n\n\n\nHere, we have dir as the only directory in the root. dir contains two subdirectories, subdir1 and subdir2. subdir1 contains a file file1.ext and subdirectory subsubdir1. su...
[ { "hash": 845471166128700300, "runtime": "18ms", "solution": "class Solution(object):\n def lengthLongestPath(self, input):\n \"\"\"\n :type input: str\n :rtype: int\n \"\"\"\n \n input = str(input)\n new_input = input.replace(\"\\t\", \"\")\n\n ...
class Solution(object): def lengthLongestPath(self, input): """ :type input: str :rtype: int """
None
None
None
None
None
None
46.9
Level 4
Hi
remove-duplicate-letters
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a string <code>s</code>, remove duplicate letters so that every letter appears once and only once. You must make sure your result is <span data-keyword=\"lexicographically-smaller-string\"><strong>the smallest in lexicographical order</strong></span> among all possible results.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;bcabc&quot;\n<strong>Output:</strong> &quot;abc&quot;\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;cbacdcbc&quot;\n<strong>Output:</strong> &quot;acdb&quot;\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= s.length &lt;= 10<sup>4</sup></code></li>\n\t<li><code>s</code> consists of lowercase English letters.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong>Note:</strong> This question is the same as 1081: <a href=\"https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/\" target=\"_blank\">https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/</a></p>\n", "difficulty": "Medium", "questionFrontendId": "316", "questionId": "316", "questionTitle": "Remove Duplicate Letters", "questionTitleSlug": "remove-duplicate-letters", "similarQuestions": "[{\"title\": \"Smallest K-Length Subsequence With Occurrences of a Letter\", \"titleSlug\": \"smallest-k-length-subsequence-with-occurrences-of-a-letter\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"310.4K\", \"totalSubmission\": \"633.3K\", \"totalAcceptedRaw\": 310400, \"totalSubmissionRaw\": 633295, \"acRate\": \"49.0%\"}", "topicTags": [ { "name": "String", "slug": "string" }, { "name": "Stack", "slug": "stack" }, { "name": "Greedy", "slug": "greedy" }, { "name": "Monotonic Stack", "slug": "monotonic-stack" } ] } } }
316
Medium
[ "Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.\n\n \nExample 1:\n\nInput: s = \"bcabc\"\nOutput: \"abc\"\n\n\nExample 2:\n\nInput: s = \"cbacdcbc\"\nOutput: \"acdb\"\n\n\...
[ { "hash": -1373001803551996000, "runtime": "76ms", "solution": "class Solution(object):\n def removeDuplicateLetters(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n\n if len(s) == 0:\n return \"\"\n\n count = Counter(s)\n\n first ...
class Solution(object): def removeDuplicateLetters(self, s): """ :type s: str :rtype: str """
None
None
None
None
None
None
49
Level 3
Hi
trim-a-binary-search-tree
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given the <code>root</code> of a binary search tree and the lowest and highest boundaries as <code>low</code> and <code>high</code>, trim the tree so that all its elements lies in <code>[low, high]</code>. Trimming the tree should <strong>not</strong> change the relative structure of the elements that will remain in the tree (i.e., any node&#39;s descendant should remain a descendant). It can be proven that there is a <strong>unique answer</strong>.</p>\n\n<p>Return <em>the root of the trimmed binary search tree</em>. Note that the root may change depending on the given bounds.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/09/09/trim1.jpg\" style=\"width: 450px; height: 126px;\" />\n<pre>\n<strong>Input:</strong> root = [1,0,2], low = 1, high = 2\n<strong>Output:</strong> [1,null,2]\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2020/09/09/trim2.jpg\" style=\"width: 450px; height: 277px;\" />\n<pre>\n<strong>Input:</strong> root = [3,0,4,null,2,null,null,1], low = 1, high = 3\n<strong>Output:</strong> [3,2,null,1]\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>\n\t<li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li>\n\t<li>The value of each node in the tree is <strong>unique</strong>.</li>\n\t<li><code>root</code> is guaranteed to be a valid binary search tree.</li>\n\t<li><code>0 &lt;= low &lt;= high &lt;= 10<sup>4</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "669", "questionId": "669", "questionTitle": "Trim a Binary Search Tree", "questionTitleSlug": "trim-a-binary-search-tree", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"280.6K\", \"totalSubmission\": \"423.4K\", \"totalAcceptedRaw\": 280650, \"totalSubmissionRaw\": 423434, \"acRate\": \"66.3%\"}", "topicTags": [ { "name": "Tree", "slug": "tree" }, { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Binary Search Tree", "slug": "binary-search-tree" }, { "name": "Binary Tree", "slug": "binary-tree" } ] } } }
669
Medium
[ "Given the root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all its elements lies in [low, high]. Trimming the tree should not change the relative structure of the elements that will remain in the tree (i.e., any node's descendant should remain a descendant)....
[ { "hash": 1965115353698872000, "runtime": "40ms", "solution": "# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution(object):\n def tr...
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution(object): def trimBST(self, root, low, high): """ :type root: TreeNode :type low: int :type high: int :rtype: TreeNode """
None
None
None
None
None
None
66.3
Level 2
Hi
grid-game
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> 2D array <code>grid</code> of size <code>2 x n</code>, where <code>grid[r][c]</code> represents the number of points at position <code>(r, c)</code> on the matrix. Two robots are playing a game on this matrix.</p>\n\n<p>Both robots initially start at <code>(0, 0)</code> and want to reach <code>(1, n-1)</code>. Each robot may only move to the <strong>right</strong> (<code>(r, c)</code> to <code>(r, c + 1)</code>) or <strong>down </strong>(<code>(r, c)</code> to <code>(r + 1, c)</code>).</p>\n\n<p>At the start of the game, the <strong>first</strong> robot moves from <code>(0, 0)</code> to <code>(1, n-1)</code>, collecting all the points from the cells on its path. For all cells <code>(r, c)</code> traversed on the path, <code>grid[r][c]</code> is set to <code>0</code>. Then, the <strong>second</strong> robot moves from <code>(0, 0)</code> to <code>(1, n-1)</code>, collecting the points on its path. Note that their paths may intersect with one another.</p>\n\n<p>The <strong>first</strong> robot wants to <strong>minimize</strong> the number of points collected by the <strong>second</strong> robot. In contrast, the <strong>second </strong>robot wants to <strong>maximize</strong> the number of points it collects. If both robots play <strong>optimally</strong>, return <em>the <b>number of points</b> collected by the <strong>second</strong> robot.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/09/08/a1.png\" style=\"width: 388px; height: 103px;\" />\n<pre>\n<strong>Input:</strong> grid = [[2,5,4],[1,5,1]]\n<strong>Output:</strong> 4\n<strong>Explanation:</strong> The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.\nThe cells visited by the first robot are set to 0.\nThe second robot will collect 0 + 0 + 4 + 0 = 4 points.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/09/08/a2.png\" style=\"width: 384px; height: 105px;\" />\n<pre>\n<strong>Input:</strong> grid = [[3,3,1],[8,5,2]]\n<strong>Output:</strong> 4\n<strong>Explanation:</strong> The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.\nThe cells visited by the first robot are set to 0.\nThe second robot will collect 0 + 3 + 1 + 0 = 4 points.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/09/08/a3.png\" style=\"width: 493px; height: 103px;\" />\n<pre>\n<strong>Input:</strong> grid = [[1,3,1,15],[1,3,3,1]]\n<strong>Output:</strong> 7\n<strong>Explanation: </strong>The optimal path taken by the first robot is shown in red, and the optimal path taken by the second robot is shown in blue.\nThe cells visited by the first robot are set to 0.\nThe second robot will collect 0 + 1 + 3 + 3 + 0 = 7 points.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>grid.length == 2</code></li>\n\t<li><code>n == grid[r].length</code></li>\n\t<li><code>1 &lt;= n &lt;= 5 * 10<sup>4</sup></code></li>\n\t<li><code>1 &lt;= grid[r][c] &lt;= 10<sup>5</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2017", "questionId": "2145", "questionTitle": "Grid Game", "questionTitleSlug": "grid-game", "similarQuestions": "[{\"title\": \"Minimum Penalty for a Shop\", \"titleSlug\": \"minimum-penalty-for-a-shop\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"20.8K\", \"totalSubmission\": \"46.2K\", \"totalAcceptedRaw\": 20811, \"totalSubmissionRaw\": 46245, \"acRate\": \"45.0%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Matrix", "slug": "matrix" }, { "name": "Prefix Sum", "slug": "prefix-sum" } ] } } }
2017
Medium
[ "You are given a 0-indexed 2D array grid of size 2 x n, where grid[r][c] represents the number of points at position (r, c) on the matrix. Two robots are playing a game on this matrix.\n\nBoth robots initially start at (0, 0) and want to reach (1, n-1). Each robot may only move to the right ((r, c) to (r, c + 1)) o...
[ { "hash": 7181152869152928000, "runtime": "1388ms", "solution": "class Solution(object):\n def gridGame(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n return self.minSum(grid)\n \n\n def minSum(self, grid):\n topSums = [0] ...
class Solution(object): def gridGame(self, grid): """ :type grid: List[List[int]] :rtype: int """
None
None
None
None
None
None
45
Level 4
Hi
number-of-smooth-descent-periods-of-a-stock
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an integer array <code>prices</code> representing the daily price history of a stock, where <code>prices[i]</code> is the stock price on the <code>i<sup>th</sup></code> day.</p>\n\n<p>A <strong>smooth descent period</strong> of a stock consists of <strong>one or more contiguous</strong> days such that the price on each day is <strong>lower</strong> than the price on the <strong>preceding day</strong> by <strong>exactly</strong> <code>1</code>. The first day of the period is exempted from this rule.</p>\n\n<p>Return <em>the number of <strong>smooth descent periods</strong></em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> prices = [3,2,1,4]\n<strong>Output:</strong> 7\n<strong>Explanation:</strong> There are 7 smooth descent periods:\n[3], [2], [1], [4], [3,2], [2,1], and [3,2,1]\nNote that a period with one day is a smooth descent period by the definition.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> prices = [8,6,7,7]\n<strong>Output:</strong> 4\n<strong>Explanation:</strong> There are 4 smooth descent periods: [8], [6], [7], and [7]\nNote that [8,6] is not a smooth descent period as 8 - 6 &ne; 1.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> prices = [1]\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> There is 1 smooth descent period: [1]\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= prices.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= prices[i] &lt;= 10<sup>5</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2110", "questionId": "2233", "questionTitle": "Number of Smooth Descent Periods of a Stock", "questionTitleSlug": "number-of-smooth-descent-periods-of-a-stock", "similarQuestions": "[{\"title\": \"Subarray Product Less Than K\", \"titleSlug\": \"subarray-product-less-than-k\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Number of Valid Subarrays\", \"titleSlug\": \"number-of-valid-subarrays\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Number of Zero-Filled Subarrays\", \"titleSlug\": \"number-of-zero-filled-subarrays\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"33.1K\", \"totalSubmission\": \"56.5K\", \"totalAcceptedRaw\": 33054, \"totalSubmissionRaw\": 56497, \"acRate\": \"58.5%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Math", "slug": "math" }, { "name": "Dynamic Programming", "slug": "dynamic-programming" } ] } } }
2110
Medium
[ "You are given an integer array prices representing the daily price history of a stock, where prices[i] is the stock price on the ith day.\n\nA smooth descent period of a stock consists of one or more contiguous days such that the price on each day is lower than the price on the preceding day by exactly 1. The firs...
[ { "hash": -5480989276640249000, "runtime": "890ms", "solution": "class Solution(object):\n def getDescentPeriods(self, prices):\n \"\"\"\n :type prices: List[int]\n :rtype: int\n \"\"\"\n\n if len(prices) == 0 or len(prices) == 1:\n return len(prices)\n ...
class Solution(object): def getDescentPeriods(self, prices): """ :type prices: List[int] :rtype: int """
None
None
None
None
None
None
58.5
Level 3
Hi
count-the-number-of-good-subarrays
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of <strong>good</strong> subarrays of</em> <code>nums</code>.</p>\n\n<p>A subarray <code>arr</code> is <strong>good</strong> if it there are <strong>at least </strong><code>k</code> pairs of indices <code>(i, j)</code> such that <code>i &lt; j</code> and <code>arr[i] == arr[j]</code>.</p>\n\n<p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,1,1,1,1], k = 10\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> The only good subarray is the array nums itself.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [3,1,4,3,2,2,4], k = 2\n<strong>Output:</strong> 4\n<strong>Explanation:</strong> There are 4 different good subarrays:\n- [3,1,4,3,2,2] that has 2 pairs.\n- [3,1,4,3,2,2,4] that has 3 pairs.\n- [1,4,3,2,2,4] that has 2 pairs.\n- [4,3,2,2,4] that has 2 pairs.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= nums[i], k &lt;= 10<sup>9</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2537", "questionId": "2626", "questionTitle": "Count the Number of Good Subarrays", "questionTitleSlug": "count-the-number-of-good-subarrays", "similarQuestions": "[{\"title\": \"Count Number of Homogenous Substrings\", \"titleSlug\": \"count-number-of-homogenous-substrings\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Maximum Sum of Distinct Subarrays With Length K\", \"titleSlug\": \"maximum-sum-of-distinct-subarrays-with-length-k\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"16.3K\", \"totalSubmission\": \"33.3K\", \"totalAcceptedRaw\": 16315, \"totalSubmissionRaw\": 33252, \"acRate\": \"49.1%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" }, { "name": "Sliding Window", "slug": "sliding-window" } ] } } }
2537
Medium
[ "Given an integer array nums and an integer k, return the number of good subarrays of nums.\n\nA subarray arr is good if it there are at least k pairs of indices (i, j) such that i < j and arr[i] == arr[j].\n\nA subarray is a contiguous non-empty sequence of elements within an array.\n\n \nExample 1:\n\nInput: nums...
[ { "hash": 8907563275418299000, "runtime": "533ms", "solution": "from collections import defaultdict\nclass Solution(object):\n def countGood(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n # sliding window?\n el...
class Solution(object): def countGood(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """
None
None
None
None
None
None
49.1
Level 3
Hi
k-similar-strings
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Strings <code>s1</code> and <code>s2</code> are <code>k</code><strong>-similar</strong> (for some non-negative integer <code>k</code>) if we can swap the positions of two letters in <code>s1</code> exactly <code>k</code> times so that the resulting string equals <code>s2</code>.</p>\n\n<p>Given two anagrams <code>s1</code> and <code>s2</code>, return the smallest <code>k</code> for which <code>s1</code> and <code>s2</code> are <code>k</code><strong>-similar</strong>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> s1 = &quot;ab&quot;, s2 = &quot;ba&quot;\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> The two string are 1-similar because we can use one swap to change s1 to s2: &quot;ab&quot; --&gt; &quot;ba&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> s1 = &quot;abc&quot;, s2 = &quot;bca&quot;\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> The two strings are 2-similar because we can use two swaps to change s1 to s2: &quot;abc&quot; --&gt; &quot;bac&quot; --&gt; &quot;bca&quot;.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= s1.length &lt;= 20</code></li>\n\t<li><code>s2.length == s1.length</code></li>\n\t<li><code>s1</code> and <code>s2</code> contain only lowercase letters from the set <code>{&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;}</code>.</li>\n\t<li><code>s2</code> is an anagram of <code>s1</code>.</li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "854", "questionId": "884", "questionTitle": "K-Similar Strings", "questionTitleSlug": "k-similar-strings", "similarQuestions": "[{\"title\": \"Couples Holding Hands\", \"titleSlug\": \"couples-holding-hands\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"43.7K\", \"totalSubmission\": \"109.7K\", \"totalAcceptedRaw\": 43677, \"totalSubmissionRaw\": 109712, \"acRate\": \"39.8%\"}", "topicTags": [ { "name": "String", "slug": "string" }, { "name": "Breadth-First Search", "slug": "breadth-first-search" } ] } } }
854
Hard
[ "Strings s1 and s2 are k-similar (for some non-negative integer k) if we can swap the positions of two letters in s1 exactly k times so that the resulting string equals s2.\n\nGiven two anagrams s1 and s2, return the smallest k for which s1 and s2 are k-similar.\n\n \nExample 1:\n\nInput: s1 = \"ab\", s2 = \"ba\"\n...
[ { "hash": -4129624075386658000, "runtime": "109ms", "solution": "from collections import deque\nclass Solution(object):\n def kSimilarity(self, s1, s2):\n \"\"\"\n :type s1: str\n :type s2: str\n :rtype: int\n \"\"\"\n queue = deque()\n queue.append(s1...
class Solution(object): def kSimilarity(self, s1, s2): """ :type s1: str :type s2: str :rtype: int """
None
None
None
None
None
None
39.8
Level 5
Hi
faulty-keyboard
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Your laptop keyboard is faulty, and whenever you type a character <code>&#39;i&#39;</code> on it, it reverses the string that you have written. Typing other characters works as expected.</p>\n\n<p>You are given a <strong>0-indexed</strong> string <code>s</code>, and you type each character of <code>s</code> using your faulty keyboard.</p>\n\n<p>Return <em>the final string that will be present on your laptop screen.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;string&quot;\n<strong>Output:</strong> &quot;rtsng&quot;\n<strong>Explanation:</strong> \nAfter typing first character, the text on the screen is &quot;s&quot;.\nAfter the second character, the text is &quot;st&quot;. \nAfter the third character, the text is &quot;str&quot;.\nSince the fourth character is an &#39;i&#39;, the text gets reversed and becomes &quot;rts&quot;.\nAfter the fifth character, the text is &quot;rtsn&quot;. \nAfter the sixth character, the text is &quot;rtsng&quot;. \nTherefore, we return &quot;rtsng&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;poiinter&quot;\n<strong>Output:</strong> &quot;ponter&quot;\n<strong>Explanation:</strong> \nAfter the first character, the text on the screen is &quot;p&quot;.\nAfter the second character, the text is &quot;po&quot;. \nSince the third character you type is an &#39;i&#39;, the text gets reversed and becomes &quot;op&quot;. \nSince the fourth character you type is an &#39;i&#39;, the text gets reversed and becomes &quot;po&quot;.\nAfter the fifth character, the text is &quot;pon&quot;.\nAfter the sixth character, the text is &quot;pont&quot;. \nAfter the seventh character, the text is &quot;ponte&quot;. \nAfter the eighth character, the text is &quot;ponter&quot;. \nTherefore, we return &quot;ponter&quot;.</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= s.length &lt;= 100</code></li>\n\t<li><code>s</code> consists of lowercase English letters.</li>\n\t<li><code>s[0] != &#39;i&#39;</code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "2810", "questionId": "2886", "questionTitle": "Faulty Keyboard", "questionTitleSlug": "faulty-keyboard", "similarQuestions": "[{\"title\": \"Reverse Vowels of a String\", \"titleSlug\": \"reverse-vowels-of-a-string\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Reverse String II\", \"titleSlug\": \"reverse-string-ii\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Reverse Only Letters\", \"titleSlug\": \"reverse-only-letters\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"53.1K\", \"totalSubmission\": \"64.5K\", \"totalAcceptedRaw\": 53080, \"totalSubmissionRaw\": 64455, \"acRate\": \"82.4%\"}", "topicTags": [ { "name": "String", "slug": "string" }, { "name": "Simulation", "slug": "simulation" } ] } } }
2810
Easy
[ "Your laptop keyboard is faulty, and whenever you type a character 'i' on it, it reverses the string that you have written. Typing other characters works as expected.\n\nYou are given a 0-indexed string s, and you type each character of s using your faulty keyboard.\n\nReturn the final string that will be present o...
[ { "hash": 4324306972056862700, "runtime": "15ms", "solution": "class Solution(object):\n def finalString(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n output = \"\"\n\n for x in s:\n if x == \"i\":\n output = output[::-1...
class Solution(object): def finalString(self, s): """ :type s: str :rtype: str """
None
None
None
None
None
None
82.4
Level 1
Hi
minimum-score-triangulation-of-polygon
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You have a convex <code>n</code>-sided polygon where each vertex has an integer value. You are given an integer array <code>values</code> where <code>values[i]</code> is the value of the <code>i<sup>th</sup></code> vertex (i.e., <strong>clockwise order</strong>).</p>\n\n<p>You will <strong>triangulate</strong> the polygon into <code>n - 2</code> triangles. For each triangle, the value of that triangle is the product of the values of its vertices, and the total score of the triangulation is the sum of these values over all <code>n - 2</code> triangles in the triangulation.</p>\n\n<p>Return <em>the smallest possible total score that you can achieve with some triangulation of the polygon</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/02/25/shape1.jpg\" style=\"width: 201px; height: 133px;\" />\n<pre>\n<strong>Input:</strong> values = [1,2,3]\n<strong>Output:</strong> 6\n<strong>Explanation:</strong> The polygon is already triangulated, and the score of the only triangle is 6.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/02/25/shape2.jpg\" style=\"width: 446px; height: 163px;\" />\n<pre>\n<strong>Input:</strong> values = [3,7,4,5]\n<strong>Output:</strong> 144\n<strong>Explanation:</strong> There are two triangulations, with possible scores: 3*7*5 + 4*5*7 = 245, or 3*4*5 + 3*4*7 = 144.\nThe minimum score is 144.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/02/25/shape3.jpg\" style=\"width: 207px; height: 163px;\" />\n<pre>\n<strong>Input:</strong> values = [1,3,1,4,1,5]\n<strong>Output:</strong> 13\n<strong>Explanation:</strong> The minimum score triangulation has score 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>n == values.length</code></li>\n\t<li><code>3 &lt;= n &lt;= 50</code></li>\n\t<li><code>1 &lt;= values[i] &lt;= 100</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1039", "questionId": "1111", "questionTitle": "Minimum Score Triangulation of Polygon", "questionTitleSlug": "minimum-score-triangulation-of-polygon", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"43.9K\", \"totalSubmission\": \"75.5K\", \"totalAcceptedRaw\": 43855, \"totalSubmissionRaw\": 75488, \"acRate\": \"58.1%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Dynamic Programming", "slug": "dynamic-programming" } ] } } }
1039
Medium
[ "You have a convex n-sided polygon where each vertex has an integer value. You are given an integer array values where values[i] is the value of the ith vertex (i.e., clockwise order).\n\nYou will triangulate the polygon into n - 2 triangles. For each triangle, the value of that triangle is the product of the value...
[ { "hash": -8562891751538853000, "runtime": "60ms", "solution": "class Solution(object):\n def minScoreTriangulation(self, values):\n \"\"\"\n :type values: List[int]\n :rtype: int\n \"\"\"\n if len(values) < 3:\n return 0\n elif len(values) == 3: \...
class Solution(object): def minScoreTriangulation(self, values): """ :type values: List[int] :rtype: int """
None
None
None
None
None
None
58.1
Level 3
Hi
minimize-maximum-of-array
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> array <code>nums</code> comprising of <code>n</code> non-negative integers.</p>\n\n<p>In one operation, you must:</p>\n\n<ul>\n\t<li>Choose an integer <code>i</code> such that <code>1 &lt;= i &lt; n</code> and <code>nums[i] &gt; 0</code>.</li>\n\t<li>Decrease <code>nums[i]</code> by 1.</li>\n\t<li>Increase <code>nums[i - 1]</code> by 1.</li>\n</ul>\n\n<p>Return<em> the <strong>minimum</strong> possible value of the <strong>maximum</strong> integer of </em><code>nums</code><em> after performing <strong>any</strong> number of operations</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [3,7,1,6]\n<strong>Output:</strong> 5\n<strong>Explanation:</strong>\nOne set of optimal operations is as follows:\n1. Choose i = 1, and nums becomes [4,6,1,6].\n2. Choose i = 3, and nums becomes [4,6,2,5].\n3. Choose i = 1, and nums becomes [5,5,2,5].\nThe maximum integer of nums is 5. It can be shown that the maximum number cannot be less than 5.\nTherefore, we return 5.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [10,1]\n<strong>Output:</strong> 10\n<strong>Explanation:</strong>\nIt is optimal to leave nums as is, and since 10 is the maximum value, we return 10.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>n == nums.length</code></li>\n\t<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>\n\t<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2439", "questionId": "2530", "questionTitle": "Minimize Maximum of Array", "questionTitleSlug": "minimize-maximum-of-array", "similarQuestions": "[{\"title\": \"Maximum Candies Allocated to K Children\", \"titleSlug\": \"maximum-candies-allocated-to-k-children\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Minimum Speed to Arrive on Time\", \"titleSlug\": \"minimum-speed-to-arrive-on-time\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Minimum Time to Complete Trips\", \"titleSlug\": \"minimum-time-to-complete-trips\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"76.5K\", \"totalSubmission\": \"163.7K\", \"totalAcceptedRaw\": 76507, \"totalSubmissionRaw\": 163737, \"acRate\": \"46.7%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Binary Search", "slug": "binary-search" }, { "name": "Dynamic Programming", "slug": "dynamic-programming" }, { "name": "Greedy", "slug": "greedy" }, { "name": "Prefix Sum", "slug": "prefix-sum" } ] } } }
2439
Medium
[ "You are given a 0-indexed array nums comprising of n non-negative integers.\n\nIn one operation, you must:\n\n\n\tChoose an integer i such that 1 <= i < n and nums[i] > 0.\n\tDecrease nums[i] by 1.\n\tIncrease nums[i - 1] by 1.\n\n\nReturn the minimum possible value of the maximum integer of nums after performing ...
[ { "hash": 8875177997201507000, "runtime": "994ms", "solution": "class Solution(object):\n def minimizeArrayValue(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n # nums, n non-negtive integers 0,1,2..\n # nums[i]>0,1<=i<n, nums[i]--,nums...
class Solution(object): def minimizeArrayValue(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
46.7
Level 4
Hi
monotone-increasing-digits
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>An integer has <strong>monotone increasing digits</strong> if and only if each pair of adjacent digits <code>x</code> and <code>y</code> satisfy <code>x &lt;= y</code>.</p>\n\n<p>Given an integer <code>n</code>, return <em>the largest number that is less than or equal to </em><code>n</code><em> with <strong>monotone increasing digits</strong></em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 10\n<strong>Output:</strong> 9\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 1234\n<strong>Output:</strong> 1234\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 332\n<strong>Output:</strong> 299\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 &lt;= n &lt;= 10<sup>9</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "738", "questionId": "738", "questionTitle": "Monotone Increasing Digits", "questionTitleSlug": "monotone-increasing-digits", "similarQuestions": "[{\"title\": \"Remove K Digits\", \"titleSlug\": \"remove-k-digits\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"49.4K\", \"totalSubmission\": \"103.8K\", \"totalAcceptedRaw\": 49431, \"totalSubmissionRaw\": 103828, \"acRate\": \"47.6%\"}", "topicTags": [ { "name": "Math", "slug": "math" }, { "name": "Greedy", "slug": "greedy" } ] } } }
738
Medium
[ "An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x <= y.\n\nGiven an integer n, return the largest number that is less than or equal to n with monotone increasing digits.\n\n \nExample 1:\n\nInput: n = 10\nOutput: 9\n\n\nExample 2:\n\nInput: n = 1234\nOutput: 12...
[ { "hash": 361004318906945200, "runtime": "14ms", "solution": "class Solution(object):\n def monotoneIncreasingDigits(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n def r(n):\n #print(n)\n if n<9:\n return n\n ...
class Solution(object): def monotoneIncreasingDigits(self, n): """ :type n: int :rtype: int """
None
None
None
None
None
None
47.6
Level 3
Hi
sort-integers-by-the-power-value
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>The power of an integer <code>x</code> is defined as the number of steps needed to transform <code>x</code> into <code>1</code> using the following steps:</p>\n\n<ul>\n\t<li>if <code>x</code> is even then <code>x = x / 2</code></li>\n\t<li>if <code>x</code> is odd then <code>x = 3 * x + 1</code></li>\n</ul>\n\n<p>For example, the power of <code>x = 3</code> is <code>7</code> because <code>3</code> needs <code>7</code> steps to become <code>1</code> (<code>3 --&gt; 10 --&gt; 5 --&gt; 16 --&gt; 8 --&gt; 4 --&gt; 2 --&gt; 1</code>).</p>\n\n<p>Given three integers <code>lo</code>, <code>hi</code> and <code>k</code>. The task is to sort all integers in the interval <code>[lo, hi]</code> by the power value in <strong>ascending order</strong>, if two or more integers have <strong>the same</strong> power value sort them by <strong>ascending order</strong>.</p>\n\n<p>Return the <code>k<sup>th</sup></code> integer in the range <code>[lo, hi]</code> sorted by the power value.</p>\n\n<p>Notice that for any integer <code>x</code> <code>(lo &lt;= x &lt;= hi)</code> it is <strong>guaranteed</strong> that <code>x</code> will transform into <code>1</code> using these steps and that the power of <code>x</code> is will <strong>fit</strong> in a 32-bit signed integer.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> lo = 12, hi = 15, k = 2\n<strong>Output:</strong> 13\n<strong>Explanation:</strong> The power of 12 is 9 (12 --&gt; 6 --&gt; 3 --&gt; 10 --&gt; 5 --&gt; 16 --&gt; 8 --&gt; 4 --&gt; 2 --&gt; 1)\nThe power of 13 is 9\nThe power of 14 is 17\nThe power of 15 is 17\nThe interval sorted by the power value [12,13,14,15]. For k = 2 answer is the second element which is 13.\nNotice that 12 and 13 have the same power value and we sorted them in ascending order. Same for 14 and 15.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> lo = 7, hi = 11, k = 4\n<strong>Output:</strong> 7\n<strong>Explanation:</strong> The power array corresponding to the interval [7, 8, 9, 10, 11] is [16, 3, 19, 6, 14].\nThe interval sorted by power is [8, 10, 11, 7, 9].\nThe fourth number in the sorted array is 7.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= lo &lt;= hi &lt;= 1000</code></li>\n\t<li><code>1 &lt;= k &lt;= hi - lo + 1</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1387", "questionId": "1488", "questionTitle": "Sort Integers by The Power Value", "questionTitleSlug": "sort-integers-by-the-power-value", "similarQuestions": "[{\"title\": \"Find Score of an Array After Marking All Elements\", \"titleSlug\": \"find-score-of-an-array-after-marking-all-elements\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"86.1K\", \"totalSubmission\": \"123K\", \"totalAcceptedRaw\": 86058, \"totalSubmissionRaw\": 123037, \"acRate\": \"69.9%\"}", "topicTags": [ { "name": "Dynamic Programming", "slug": "dynamic-programming" }, { "name": "Memoization", "slug": "memoization" }, { "name": "Sorting", "slug": "sorting" } ] } } }
1387
Medium
[ "The power of an integer x is defined as the number of steps needed to transform x into 1 using the following steps:\n\n\n\tif x is even then x = x / 2\n\tif x is odd then x = 3 * x + 1\n\n\nFor example, the power of x = 3 is 7 because 3 needs 7 steps to become 1 (3 --> 10 --> 5 --> 16 --> 8 --> 4 --> 2 --> 1).\n\n...
[ { "hash": 5824880556434299000, "runtime": "46ms", "solution": "class Solution(object):\n moose = [0, 0, 1, 7, 2, 5, 8, 16, 3, 19, 6, 14, 9, 9, 17, 17, 4, 12, 20, 20, 7, 7, 15, 15, 10, 23, 10, 111, 18, 18, 18, 106, 5, 26, 13, 13, 21, 21, 21, 34, 8, 109, 8, 29, 16, 16, 16, 104, 11, 24, 24, 24, 11, 11, ...
class Solution(object): def getKth(self, lo, hi, k): """ :type lo: int :type hi: int :type k: int :rtype: int """
None
None
None
None
None
None
69.9
Level 2
Hi
html-entity-parser
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p><strong>HTML entity parser</strong> is the parser that takes HTML code as input and replace all the entities of the special characters by the characters itself.</p>\n\n<p>The special characters and their entities for HTML are:</p>\n\n<ul>\n\t<li><strong>Quotation Mark:</strong> the entity is <code>&amp;quot;</code> and symbol character is <code>&quot;</code>.</li>\n\t<li><strong>Single Quote Mark:</strong> the entity is <code>&amp;apos;</code> and symbol character is <code>&#39;</code>.</li>\n\t<li><strong>Ampersand:</strong> the entity is <code>&amp;amp;</code> and symbol character is <code>&amp;</code>.</li>\n\t<li><strong>Greater Than Sign:</strong> the entity is <code>&amp;gt;</code> and symbol character is <code>&gt;</code>.</li>\n\t<li><strong>Less Than Sign:</strong> the entity is <code>&amp;lt;</code> and symbol character is <code>&lt;</code>.</li>\n\t<li><strong>Slash:</strong> the entity is <code>&amp;frasl;</code> and symbol character is <code>/</code>.</li>\n</ul>\n\n<p>Given the input <code>text</code> string to the HTML parser, you have to implement the entity parser.</p>\n\n<p>Return <em>the text after replacing the entities by the special characters</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> text = &quot;&amp;amp; is an HTML entity but &amp;ambassador; is not.&quot;\n<strong>Output:</strong> &quot;&amp; is an HTML entity but &amp;ambassador; is not.&quot;\n<strong>Explanation:</strong> The parser will replace the &amp;amp; entity by &amp;\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> text = &quot;and I quote: &amp;quot;...&amp;quot;&quot;\n<strong>Output:</strong> &quot;and I quote: \\&quot;...\\&quot;&quot;\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= text.length &lt;= 10<sup>5</sup></code></li>\n\t<li>The string may contain any possible characters out of all the 256 ASCII characters.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1410", "questionId": "1526", "questionTitle": "HTML Entity Parser", "questionTitleSlug": "html-entity-parser", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"23K\", \"totalSubmission\": \"45.1K\", \"totalAcceptedRaw\": 23013, \"totalSubmissionRaw\": 45059, \"acRate\": \"51.1%\"}", "topicTags": [ { "name": "Hash Table", "slug": "hash-table" }, { "name": "String", "slug": "string" } ] } } }
1410
Medium
[ "HTML entity parser is the parser that takes HTML code as input and replace all the entities of the special characters by the characters itself.\n\nThe special characters and their entities for HTML are:\n\n\n\tQuotation Mark: the entity is &quot; and symbol character is \".\n\tSingle Quote Mark: the entity is &apo...
[ { "hash": 3894061621552926700, "runtime": "50ms", "solution": "import re\n\nclass Solution(object):\n def entityParser(self, text):\n \"\"\"\n :type text: str\n :rtype: str\n \"\"\"\n\n my_list_of_tup = [('&quot;','\"'), ('&apos;',\"'\"), ('&gt;','>'), ('&lt;', '<')...
class Solution(object): def entityParser(self, text): """ :type text: str :rtype: str """
None
None
None
None
None
None
51.1
Level 3
Hi
count-servers-that-communicate
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a map of a server center, represented as a <code>m * n</code> integer matrix&nbsp;<code>grid</code>, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column.<br />\n<br />\nReturn the number of servers&nbsp;that communicate with any other server.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2019/11/14/untitled-diagram-6.jpg\" style=\"width: 202px; height: 203px;\" /></p>\n\n<pre>\n<strong>Input:</strong> grid = [[1,0],[0,1]]\n<strong>Output:</strong> 0\n<b>Explanation:</b>&nbsp;No servers can communicate with others.</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<p><strong><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2019/11/13/untitled-diagram-4.jpg\" style=\"width: 203px; height: 203px;\" /></strong></p>\n\n<pre>\n<strong>Input:</strong> grid = [[1,0],[1,1]]\n<strong>Output:</strong> 3\n<b>Explanation:</b>&nbsp;All three servers can communicate with at least one other server.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2019/11/14/untitled-diagram-1-3.jpg\" style=\"width: 443px; height: 443px;\" /></p>\n\n<pre>\n<strong>Input:</strong> grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]\n<strong>Output:</strong> 4\n<b>Explanation:</b>&nbsp;The two servers in the first row can communicate with each other. The two servers in the third column can communicate with each other. The server at right bottom corner can&#39;t communicate with any other server.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>m == grid.length</code></li>\n\t<li><code>n == grid[i].length</code></li>\n\t<li><code>1 &lt;= m &lt;= 250</code></li>\n\t<li><code>1 &lt;= n &lt;= 250</code></li>\n\t<li><code>grid[i][j] == 0 or 1</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1267", "questionId": "1396", "questionTitle": "Count Servers that Communicate", "questionTitleSlug": "count-servers-that-communicate", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"56K\", \"totalSubmission\": \"93.1K\", \"totalAcceptedRaw\": 55962, \"totalSubmissionRaw\": 93137, \"acRate\": \"60.1%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Breadth-First Search", "slug": "breadth-first-search" }, { "name": "Union Find", "slug": "union-find" }, { "name": "Matrix", "slug": "matrix" }, { "name": "Counting", "slug": "counting" } ] } } }
1267
Medium
[ "You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column.\n\nReturn the number of servers that communicate with any ot...
[ { "hash": -5399892682740378000, "runtime": "388ms", "solution": "class Solution(object):\n def countServers(self, grid):\n n, m = len(grid), len(grid[0])\n res = 0\n for i in range(n):\n c = 0\n for j in range(m):\n if grid[i][j]:\n ...
class Solution(object): def countServers(self, grid): """ :type grid: List[List[int]] :rtype: int """
None
None
None
None
None
None
60.1
Level 2
Hi
max-difference-you-can-get-from-changing-an-integer
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an integer <code>num</code>. You will apply the following steps exactly <strong>two</strong> times:</p>\n\n<ul>\n\t<li>Pick a digit <code>x (0 &lt;= x &lt;= 9)</code>.</li>\n\t<li>Pick another digit <code>y (0 &lt;= y &lt;= 9)</code>. The digit <code>y</code> can be equal to <code>x</code>.</li>\n\t<li>Replace all the occurrences of <code>x</code> in the decimal representation of <code>num</code> by <code>y</code>.</li>\n\t<li>The new integer <strong>cannot</strong> have any leading zeros, also the new integer <strong>cannot</strong> be 0.</li>\n</ul>\n\n<p>Let <code>a</code> and <code>b</code> be the results of applying the operations to <code>num</code> the first and second times, respectively.</p>\n\n<p>Return <em>the max difference</em> between <code>a</code> and <code>b</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> num = 555\n<strong>Output:</strong> 888\n<strong>Explanation:</strong> The first time pick x = 5 and y = 9 and store the new integer in a.\nThe second time pick x = 5 and y = 1 and store the new integer in b.\nWe have now a = 999 and b = 111 and max difference = 888\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> num = 9\n<strong>Output:</strong> 8\n<strong>Explanation:</strong> The first time pick x = 9 and y = 9 and store the new integer in a.\nThe second time pick x = 9 and y = 1 and store the new integer in b.\nWe have now a = 9 and b = 1 and max difference = 8\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= num &lt;= 10</code><sup>8</sup></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1432", "questionId": "1529", "questionTitle": "Max Difference You Can Get From Changing an Integer", "questionTitleSlug": "max-difference-you-can-get-from-changing-an-integer", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"16.7K\", \"totalSubmission\": \"40.8K\", \"totalAcceptedRaw\": 16713, \"totalSubmissionRaw\": 40838, \"acRate\": \"40.9%\"}", "topicTags": [ { "name": "Math", "slug": "math" }, { "name": "Greedy", "slug": "greedy" } ] } } }
1432
Medium
[ "You are given an integer num. You will apply the following steps exactly two times:\n\n\n\tPick a digit x (0 <= x <= 9).\n\tPick another digit y (0 <= y <= 9). The digit y can be equal to x.\n\tReplace all the occurrences of x in the decimal representation of num by y.\n\tThe new integer cannot have any leading ze...
[ { "hash": 8116359169056432000, "runtime": "18ms", "solution": "class Solution(object):\n def maxDiff(self, num):\n \"\"\"\n :type num: int\n :rtype: int\n \"\"\"\n string = [letter for letter in str(num)]\n a = []\n b = []\n changed = False\n ...
class Solution(object): def maxDiff(self, num): """ :type num: int :rtype: int """
None
None
None
None
None
None
40.9
Level 4
Hi
minimum-number-of-swaps-to-make-the-string-balanced
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> string <code>s</code> of <strong>even</strong> length <code>n</code>. The string consists of <strong>exactly</strong> <code>n / 2</code> opening brackets <code>&#39;[&#39;</code> and <code>n / 2</code> closing brackets <code>&#39;]&#39;</code>.</p>\n\n<p>A string is called <strong>balanced</strong> if and only if:</p>\n\n<ul>\n\t<li>It is the empty string, or</li>\n\t<li>It can be written as <code>AB</code>, where both <code>A</code> and <code>B</code> are <strong>balanced</strong> strings, or</li>\n\t<li>It can be written as <code>[C]</code>, where <code>C</code> is a <strong>balanced</strong> string.</li>\n</ul>\n\n<p>You may swap the brackets at <strong>any</strong> two indices <strong>any</strong> number of times.</p>\n\n<p>Return <em>the <strong>minimum</strong> number of swaps to make </em><code>s</code> <em><strong>balanced</strong></em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;][][&quot;\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> You can make the string balanced by swapping index 0 with index 3.\nThe resulting string is &quot;[[]]&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;]]][[[&quot;\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> You can do the following to make the string balanced:\n- Swap index 0 with index 4. s = &quot;[]][][&quot;.\n- Swap index 1 with index 5. s = &quot;[[][]]&quot;.\nThe resulting string is &quot;[[][]]&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;[]&quot;\n<strong>Output:</strong> 0\n<strong>Explanation:</strong> The string is already balanced.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>n == s.length</code></li>\n\t<li><code>2 &lt;= n &lt;= 10<sup>6</sup></code></li>\n\t<li><code>n</code> is even.</li>\n\t<li><code>s[i]</code> is either <code>&#39;[&#39; </code>or <code>&#39;]&#39;</code>.</li>\n\t<li>The number of opening brackets <code>&#39;[&#39;</code> equals <code>n / 2</code>, and the number of closing brackets <code>&#39;]&#39;</code> equals <code>n / 2</code>.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1963", "questionId": "2095", "questionTitle": "Minimum Number of Swaps to Make the String Balanced", "questionTitleSlug": "minimum-number-of-swaps-to-make-the-string-balanced", "similarQuestions": "[{\"title\": \"Remove Invalid Parentheses\", \"titleSlug\": \"remove-invalid-parentheses\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Minimum Add to Make Parentheses Valid\", \"titleSlug\": \"minimum-add-to-make-parentheses-valid\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Minimum Remove to Make Valid Parentheses\", \"titleSlug\": \"minimum-remove-to-make-valid-parentheses\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Minimum Insertions to Balance a Parentheses String\", \"titleSlug\": \"minimum-insertions-to-balance-a-parentheses-string\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"59.3K\", \"totalSubmission\": \"84.1K\", \"totalAcceptedRaw\": 59261, \"totalSubmissionRaw\": 84108, \"acRate\": \"70.5%\"}", "topicTags": [ { "name": "Two Pointers", "slug": "two-pointers" }, { "name": "String", "slug": "string" }, { "name": "Stack", "slug": "stack" }, { "name": "Greedy", "slug": "greedy" } ] } } }
1963
Medium
[ "You are given a 0-indexed string s of even length n. The string consists of exactly n / 2 opening brackets '[' and n / 2 closing brackets ']'.\n\nA string is called balanced if and only if:\n\n\n\tIt is the empty string, or\n\tIt can be written as AB, where both A and B are balanced strings, or\n\tIt can be writte...
[ { "hash": -2056377538428982000, "runtime": "240ms", "solution": "class Solution(object):\n def minSwaps(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n # dp ?\n # dp[i][j] = dp[i+1][j-1] if s[i+1] == [ AND S[j-1] = ]\n # ]. [. => +1\n ...
class Solution(object): def minSwaps(self, s): """ :type s: str :rtype: int """
None
None
None
None
None
None
70.5
Level 2
Hi
reachable-nodes-in-subdivided-graph
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an undirected graph (the <strong>&quot;original graph&quot;</strong>) with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. You decide to <strong>subdivide</strong> each edge in the graph into a chain of nodes, with the number of new nodes varying between each edge.</p>\n\n<p>The graph is given as a 2D array of <code>edges</code> where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, cnt<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the original graph, and <code>cnt<sub>i</sub></code> is the total number of new nodes that you will <strong>subdivide</strong> the edge into. Note that <code>cnt<sub>i</sub> == 0</code> means you will not subdivide the edge.</p>\n\n<p>To <strong>subdivide</strong> the edge <code>[u<sub>i</sub>, v<sub>i</sub>]</code>, replace it with <code>(cnt<sub>i</sub> + 1)</code> new edges and <code>cnt<sub>i</sub></code> new nodes. The new nodes are <code>x<sub>1</sub></code>, <code>x<sub>2</sub></code>, ..., <code>x<sub>cnt<sub>i</sub></sub></code>, and the new edges are <code>[u<sub>i</sub>, x<sub>1</sub>]</code>, <code>[x<sub>1</sub>, x<sub>2</sub>]</code>, <code>[x<sub>2</sub>, x<sub>3</sub>]</code>, ..., <code>[x<sub>cnt<sub>i</sub>-1</sub>, x<sub>cnt<sub>i</sub></sub>]</code>, <code>[x<sub>cnt<sub>i</sub></sub>, v<sub>i</sub>]</code>.</p>\n\n<p>In this <strong>new graph</strong>, you want to know how many nodes are <strong>reachable</strong> from the node <code>0</code>, where a node is <strong>reachable</strong> if the distance is <code>maxMoves</code> or less.</p>\n\n<p>Given the original graph and <code>maxMoves</code>, return <em>the number of nodes that are <strong>reachable</strong> from node </em><code>0</code><em> in the new graph</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/01/origfinal.png\" style=\"width: 600px; height: 247px;\" />\n<pre>\n<strong>Input:</strong> edges = [[0,1,10],[0,2,1],[1,2,2]], maxMoves = 6, n = 3\n<strong>Output:</strong> 13\n<strong>Explanation:</strong> The edge subdivisions are shown in the image above.\nThe nodes that are reachable are highlighted in yellow.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], maxMoves = 10, n = 4\n<strong>Output:</strong> 23\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> edges = [[1,2,4],[1,4,5],[1,3,1],[2,3,4],[3,4,5]], maxMoves = 17, n = 5\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> Node 0 is disconnected from the rest of the graph, so only node 0 is reachable.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>0 &lt;= edges.length &lt;= min(n * (n - 1) / 2, 10<sup>4</sup>)</code></li>\n\t<li><code>edges[i].length == 3</code></li>\n\t<li><code>0 &lt;= u<sub>i</sub> &lt; v<sub>i</sub> &lt; n</code></li>\n\t<li>There are <strong>no multiple edges</strong> in the graph.</li>\n\t<li><code>0 &lt;= cnt<sub>i</sub> &lt;= 10<sup>4</sup></code></li>\n\t<li><code>0 &lt;= maxMoves &lt;= 10<sup>9</sup></code></li>\n\t<li><code>1 &lt;= n &lt;= 3000</code></li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "882", "questionId": "918", "questionTitle": "Reachable Nodes In Subdivided Graph", "questionTitleSlug": "reachable-nodes-in-subdivided-graph", "similarQuestions": "[{\"title\": \"Find All People With Secret\", \"titleSlug\": \"find-all-people-with-secret\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Paths in Maze That Lead to Same Room\", \"titleSlug\": \"paths-in-maze-that-lead-to-same-room\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"25.7K\", \"totalSubmission\": \"51.5K\", \"totalAcceptedRaw\": 25713, \"totalSubmissionRaw\": 51461, \"acRate\": \"50.0%\"}", "topicTags": [ { "name": "Graph", "slug": "graph" }, { "name": "Heap (Priority Queue)", "slug": "heap-priority-queue" }, { "name": "Shortest Path", "slug": "shortest-path" } ] } } }
882
Hard
[ "You are given an undirected graph (the \"original graph\") with n nodes labeled from 0 to n - 1. You decide to subdivide each edge in the graph into a chain of nodes, with the number of new nodes varying between each edge.\n\nThe graph is given as a 2D array of edges where edges[i] = [ui, vi, cnti] indicates that ...
[ { "hash": -8514548304626364000, "runtime": "442ms", "solution": "class Solution(object):\n def reachableNodes(self, edges, maxMoves, n):\n\n visited = defaultdict(bool)\n dist = defaultdict(lambda: sys.maxsize)\n adj_list = defaultdict(list)\n \n for u, v, d in edge...
class Solution(object): def reachableNodes(self, edges, maxMoves, n): """ :type edges: List[List[int]] :type maxMoves: int :type n: int :rtype: int """
None
None
None
None
None
None
50
Level 5
Hi
broken-calculator
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There is a broken calculator that has the integer <code>startValue</code> on its display initially. In one operation, you can:</p>\n\n<ul>\n\t<li>multiply the number on display by <code>2</code>, or</li>\n\t<li>subtract <code>1</code> from the number on display.</li>\n</ul>\n\n<p>Given two integers <code>startValue</code> and <code>target</code>, return <em>the minimum number of operations needed to display </em><code>target</code><em> on the calculator</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> startValue = 2, target = 3\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> Use double operation and then decrement operation {2 -&gt; 4 -&gt; 3}.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> startValue = 5, target = 8\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> Use decrement and then double {5 -&gt; 4 -&gt; 8}.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> startValue = 3, target = 10\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> Use double, decrement and double {3 -&gt; 6 -&gt; 5 -&gt; 10}.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= startValue, target &lt;= 10<sup>9</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "991", "questionId": "1033", "questionTitle": "Broken Calculator", "questionTitleSlug": "broken-calculator", "similarQuestions": "[{\"title\": \"2 Keys Keyboard\", \"titleSlug\": \"2-keys-keyboard\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Minimum Operations to Make the Integer Zero\", \"titleSlug\": \"minimum-operations-to-make-the-integer-zero\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"97.2K\", \"totalSubmission\": \"179.1K\", \"totalAcceptedRaw\": 97194, \"totalSubmissionRaw\": 179066, \"acRate\": \"54.3%\"}", "topicTags": [ { "name": "Math", "slug": "math" }, { "name": "Greedy", "slug": "greedy" } ] } } }
991
Medium
[ "There is a broken calculator that has the integer startValue on its display initially. In one operation, you can:\n\n\n\tmultiply the number on display by 2, or\n\tsubtract 1 from the number on display.\n\n\nGiven two integers startValue and target, return the minimum number of operations needed to display target ...
[ { "hash": -3128087309603036700, "runtime": "13ms", "solution": "class Solution(object):\n def brokenCalc(self, startValue, target):\n \"\"\"\n :type startValue: int\n :type target: int\n :rtype: int\n \"\"\"\n i=0\n while startValue<target:\n ...
class Solution(object): def brokenCalc(self, startValue, target): """ :type startValue: int :type target: int :rtype: int """
None
None
None
None
None
None
54.3
Level 3
Hi
count-subarrays-with-median-k
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an array <code>nums</code> of size <code>n</code> consisting of <strong>distinct </strong>integers from <code>1</code> to <code>n</code> and a positive integer <code>k</code>.</p>\n\n<p>Return <em>the number of non-empty subarrays in </em><code>nums</code><em> that have a <strong>median</strong> equal to </em><code>k</code>.</p>\n\n<p><strong>Note</strong>:</p>\n\n<ul>\n\t<li>The median of an array is the <strong>middle </strong>element after sorting the array in <strong>ascending </strong>order. If the array is of even length, the median is the <strong>left </strong>middle element.\n\n\t<ul>\n\t\t<li>For example, the median of <code>[2,3,1,4]</code> is <code>2</code>, and the median of <code>[8,4,3,5,1]</code> is <code>4</code>.</li>\n\t</ul>\n\t</li>\n\t<li>A subarray is a contiguous part of an array.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [3,2,1,4,5], k = 4\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> The subarrays that have a median equal to 4 are: [4], [4,5] and [1,4,5].\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [2,3,1], k = 3\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> [3] is the only subarray that has a median equal to 3.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>n == nums.length</code></li>\n\t<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= nums[i], k &lt;= n</code></li>\n\t<li>The integers in <code>nums</code> are distinct.</li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "2488", "questionId": "2574", "questionTitle": "Count Subarrays With Median K", "questionTitleSlug": "count-subarrays-with-median-k", "similarQuestions": "[{\"title\": \"Number of Subarrays with Bounded Maximum\", \"titleSlug\": \"number-of-subarrays-with-bounded-maximum\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold\", \"titleSlug\": \"number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Sum of Imbalance Numbers of All Subarrays\", \"titleSlug\": \"sum-of-imbalance-numbers-of-all-subarrays\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"10.8K\", \"totalSubmission\": \"24.5K\", \"totalAcceptedRaw\": 10770, \"totalSubmissionRaw\": 24495, \"acRate\": \"44.0%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" }, { "name": "Prefix Sum", "slug": "prefix-sum" } ] } } }
2488
Hard
[ "You are given an array nums of size n consisting of distinct integers from 1 to n and a positive integer k.\n\nReturn the number of non-empty subarrays in nums that have a median equal to k.\n\nNote:\n\n\n\tThe median of an array is the middle element after sorting the array in ascending order. If the array is of ...
[ { "hash": -7459937574891019000, "runtime": "420ms", "solution": "class Solution(object):\n def countSubarrays_Solution1(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n arr = []\n kIdx = 0\n n = len(nums)\...
class Solution(object): def countSubarrays(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """
None
None
None
None
None
None
44
Level 5
Hi
distribute-money-to-maximum-children
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an integer <code>money</code> denoting the amount of money (in dollars) that you have and another integer <code>children</code> denoting the number of children that you must distribute the money to.</p>\n\n<p>You have to distribute the money according to the following rules:</p>\n\n<ul>\n\t<li>All money must be distributed.</li>\n\t<li>Everyone must receive at least <code>1</code> dollar.</li>\n\t<li>Nobody receives <code>4</code> dollars.</li>\n</ul>\n\n<p>Return <em>the <strong>maximum</strong> number of children who may receive <strong>exactly</strong> </em><code>8</code> <em>dollars if you distribute the money according to the aforementioned rules</em>. If there is no way to distribute the money, return <code>-1</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> money = 20, children = 3\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> \nThe maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is:\n- 8 dollars to the first child.\n- 9 dollars to the second child. \n- 3 dollars to the third child.\nIt can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> money = 16, children = 2\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> Each child can be given 8 dollars.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= money &lt;= 200</code></li>\n\t<li><code>2 &lt;= children &lt;= 30</code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "2591", "questionId": "2663", "questionTitle": "Distribute Money to Maximum Children", "questionTitleSlug": "distribute-money-to-maximum-children", "similarQuestions": "[{\"title\": \"Distribute Candies to People\", \"titleSlug\": \"distribute-candies-to-people\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Fair Distribution of Cookies\", \"titleSlug\": \"fair-distribution-of-cookies\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Calculate Money in Leetcode Bank\", \"titleSlug\": \"calculate-money-in-leetcode-bank\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"23.9K\", \"totalSubmission\": \"125.9K\", \"totalAcceptedRaw\": 23930, \"totalSubmissionRaw\": 125911, \"acRate\": \"19.0%\"}", "topicTags": [ { "name": "Math", "slug": "math" }, { "name": "Greedy", "slug": "greedy" } ] } } }
2591
Easy
[ "You are given an integer money denoting the amount of money (in dollars) that you have and another integer children denoting the number of children that you must distribute the money to.\n\nYou have to distribute the money according to the following rules:\n\n\n\tAll money must be distributed.\n\tEveryone must rec...
[ { "hash": -2598234223712484000, "runtime": "30ms", "solution": "class Solution(object):\n def distMoney(self, money, children):\n \"\"\"\n :type money: int\n :type children: int\n :rtype: int\n \"\"\"\n if money / children < 1:\n return -1\n ...
class Solution(object): def distMoney(self, money, children): """ :type money: int :type children: int :rtype: int """
None
None
None
None
None
None
19
Level 1
Hi
unique-morse-code-words
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:</p>\n\n<ul>\n\t<li><code>&#39;a&#39;</code> maps to <code>&quot;.-&quot;</code>,</li>\n\t<li><code>&#39;b&#39;</code> maps to <code>&quot;-...&quot;</code>,</li>\n\t<li><code>&#39;c&#39;</code> maps to <code>&quot;-.-.&quot;</code>, and so on.</li>\n</ul>\n\n<p>For convenience, the full table for the <code>26</code> letters of the English alphabet is given below:</p>\n\n<pre>\n[&quot;.-&quot;,&quot;-...&quot;,&quot;-.-.&quot;,&quot;-..&quot;,&quot;.&quot;,&quot;..-.&quot;,&quot;--.&quot;,&quot;....&quot;,&quot;..&quot;,&quot;.---&quot;,&quot;-.-&quot;,&quot;.-..&quot;,&quot;--&quot;,&quot;-.&quot;,&quot;---&quot;,&quot;.--.&quot;,&quot;--.-&quot;,&quot;.-.&quot;,&quot;...&quot;,&quot;-&quot;,&quot;..-&quot;,&quot;...-&quot;,&quot;.--&quot;,&quot;-..-&quot;,&quot;-.--&quot;,&quot;--..&quot;]</pre>\n\n<p>Given an array of strings <code>words</code> where each word can be written as a concatenation of the Morse code of each letter.</p>\n\n<ul>\n\t<li>For example, <code>&quot;cab&quot;</code> can be written as <code>&quot;-.-..--...&quot;</code>, which is the concatenation of <code>&quot;-.-.&quot;</code>, <code>&quot;.-&quot;</code>, and <code>&quot;-...&quot;</code>. We will call such a concatenation the <strong>transformation</strong> of a word.</li>\n</ul>\n\n<p>Return <em>the number of different <strong>transformations</strong> among all words we have</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> words = [&quot;gin&quot;,&quot;zen&quot;,&quot;gig&quot;,&quot;msg&quot;]\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> The transformation of each word is:\n&quot;gin&quot; -&gt; &quot;--...-.&quot;\n&quot;zen&quot; -&gt; &quot;--...-.&quot;\n&quot;gig&quot; -&gt; &quot;--...--.&quot;\n&quot;msg&quot; -&gt; &quot;--...--.&quot;\nThere are 2 different transformations: &quot;--...-.&quot; and &quot;--...--.&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> words = [&quot;a&quot;]\n<strong>Output:</strong> 1\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= words.length &lt;= 100</code></li>\n\t<li><code>1 &lt;= words[i].length &lt;= 12</code></li>\n\t<li><code>words[i]</code> consists of lowercase English letters.</li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "804", "questionId": "822", "questionTitle": "Unique Morse Code Words", "questionTitleSlug": "unique-morse-code-words", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"335.5K\", \"totalSubmission\": \"405.8K\", \"totalAcceptedRaw\": 335540, \"totalSubmissionRaw\": 405777, \"acRate\": \"82.7%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" }, { "name": "String", "slug": "string" } ] } } }
804
Easy
[ "International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows:\n\n\n\t'a' maps to \".-\",\n\t'b' maps to \"-...\",\n\t'c' maps to \"-.-.\", and so on.\n\n\nFor convenience, the full table for the 26 letters of the English alphabet is given below:\n\n[\"...
[ { "hash": 8043857671943135000, "runtime": "41ms", "solution": "class Solution:\n def uniqueMorseRepresentations(self, words: List[str]) -> int:\n hashmap = {'a':\".-\",'b':\"-...\",'c':\"-.-.\",'d':\"-..\",'e':\".\",'f':\"..-.\",'g':\"--.\",'h':\"....\",'i':\"..\",'j':\".---\",'k':\"-.-\",'l':...
class Solution(object): def uniqueMorseRepresentations(self, words): """ :type words: List[str] :rtype: int """
None
None
None
None
None
None
82.7
Level 1
Hi
univalued-binary-tree
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A binary tree is <strong>uni-valued</strong> if every node in the tree has the same value.</p>\n\n<p>Given the <code>root</code> of a binary tree, return <code>true</code><em> if the given tree is <strong>uni-valued</strong>, or </em><code>false</code><em> otherwise.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2018/12/28/unival_bst_1.png\" style=\"width: 265px; height: 172px;\" />\n<pre>\n<strong>Input:</strong> root = [1,1,1,1,1,null,1]\n<strong>Output:</strong> true\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2018/12/28/unival_bst_2.png\" style=\"width: 198px; height: 169px;\" />\n<pre>\n<strong>Input:</strong> root = [2,2,2,5,2]\n<strong>Output:</strong> false\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li>The number of nodes in the tree is in the range <code>[1, 100]</code>.</li>\n\t<li><code>0 &lt;= Node.val &lt; 100</code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "965", "questionId": "1005", "questionTitle": "Univalued Binary Tree", "questionTitleSlug": "univalued-binary-tree", "similarQuestions": "[{\"title\": \"Find All The Lonely Nodes\", \"titleSlug\": \"find-all-the-lonely-nodes\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"206.8K\", \"totalSubmission\": \"293.9K\", \"totalAcceptedRaw\": 206819, \"totalSubmissionRaw\": 293886, \"acRate\": \"70.4%\"}", "topicTags": [ { "name": "Tree", "slug": "tree" }, { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Breadth-First Search", "slug": "breadth-first-search" }, { "name": "Binary Tree", "slug": "binary-tree" } ] } } }
965
Easy
[ "A binary tree is uni-valued if every node in the tree has the same value.\n\nGiven the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.\n\n \nExample 1:\n\nInput: root = [1,1,1,1,1,null,1]\nOutput: true\n\n\nExample 2:\n\nInput: root = [2,2,2,5,2]\nOutput: false\n\n\n \nConst...
[ { "hash": 839511969501426300, "runtime": "19ms", "solution": "# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution(object):\n def isU...
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution(object): def isUnivalTree(self, root): """ :type root: TreeNode :rtype: bool """
None
None
None
None
None
None
70.4
Level 1
Hi
all-divisions-with-the-highest-score-of-a-binary-array
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> binary array <code>nums</code> of length <code>n</code>. <code>nums</code> can be divided at index <code>i</code> (where <code>0 &lt;= i &lt;= n)</code> into two arrays (possibly empty) <code>nums<sub>left</sub></code> and <code>nums<sub>right</sub></code>:</p>\n\n<ul>\n\t<li><code>nums<sub>left</sub></code> has all the elements of <code>nums</code> between index <code>0</code> and <code>i - 1</code> <strong>(inclusive)</strong>, while <code>nums<sub>right</sub></code> has all the elements of nums between index <code>i</code> and <code>n - 1</code> <strong>(inclusive)</strong>.</li>\n\t<li>If <code>i == 0</code>, <code>nums<sub>left</sub></code> is <strong>empty</strong>, while <code>nums<sub>right</sub></code> has all the elements of <code>nums</code>.</li>\n\t<li>If <code>i == n</code>, <code>nums<sub>left</sub></code> has all the elements of nums, while <code>nums<sub>right</sub></code> is <strong>empty</strong>.</li>\n</ul>\n\n<p>The <strong>division score</strong> of an index <code>i</code> is the <strong>sum</strong> of the number of <code>0</code>&#39;s in <code>nums<sub>left</sub></code> and the number of <code>1</code>&#39;s in <code>nums<sub>right</sub></code>.</p>\n\n<p>Return <em><strong>all distinct indices</strong> that have the <strong>highest</strong> possible <strong>division score</strong></em>. You may return the answer in <strong>any order</strong>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [0,0,1,0]\n<strong>Output:</strong> [2,4]\n<strong>Explanation:</strong> Division at index\n- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [0,0,<u><strong>1</strong></u>,0]. The score is 0 + 1 = 1.\n- 1: nums<sub>left</sub> is [<u><strong>0</strong></u>]. nums<sub>right</sub> is [0,<u><strong>1</strong></u>,0]. The score is 1 + 1 = 2.\n- 2: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>]. nums<sub>right</sub> is [<u><strong>1</strong></u>,0]. The score is 2 + 1 = 3.\n- 3: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>,1]. nums<sub>right</sub> is [0]. The score is 2 + 0 = 2.\n- 4: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>,1,<u><strong>0</strong></u>]. nums<sub>right</sub> is []. The score is 3 + 0 = 3.\nIndices 2 and 4 both have the highest possible division score 3.\nNote the answer [4,2] would also be accepted.</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [0,0,0]\n<strong>Output:</strong> [3]\n<strong>Explanation:</strong> Division at index\n- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [0,0,0]. The score is 0 + 0 = 0.\n- 1: nums<sub>left</sub> is [<u><strong>0</strong></u>]. nums<sub>right</sub> is [0,0]. The score is 1 + 0 = 1.\n- 2: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>]. nums<sub>right</sub> is [0]. The score is 2 + 0 = 2.\n- 3: nums<sub>left</sub> is [<u><strong>0</strong></u>,<u><strong>0</strong></u>,<u><strong>0</strong></u>]. nums<sub>right</sub> is []. The score is 3 + 0 = 3.\nOnly index 3 has the highest possible division score 3.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,1]\n<strong>Output:</strong> [0]\n<strong>Explanation:</strong> Division at index\n- 0: nums<sub>left</sub> is []. nums<sub>right</sub> is [<u><strong>1</strong></u>,<u><strong>1</strong></u>]. The score is 0 + 2 = 2.\n- 1: nums<sub>left</sub> is [1]. nums<sub>right</sub> is [<u><strong>1</strong></u>]. The score is 0 + 1 = 1.\n- 2: nums<sub>left</sub> is [1,1]. nums<sub>right</sub> is []. The score is 0 + 0 = 0.\nOnly index 0 has the highest possible division score 2.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>n == nums.length</code></li>\n\t<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>\n\t<li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2155", "questionId": "2261", "questionTitle": "All Divisions With the Highest Score of a Binary Array", "questionTitleSlug": "all-divisions-with-the-highest-score-of-a-binary-array", "similarQuestions": "[{\"title\": \"Ones and Zeroes\", \"titleSlug\": \"ones-and-zeroes\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Max Consecutive Ones II\", \"titleSlug\": \"max-consecutive-ones-ii\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Count Subarrays With More Ones Than Zeros\", \"titleSlug\": \"count-subarrays-with-more-ones-than-zeros\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Array Partition\", \"titleSlug\": \"array-partition\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Divide Array in Sets of K Consecutive Numbers\", \"titleSlug\": \"divide-array-in-sets-of-k-consecutive-numbers\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"27.3K\", \"totalSubmission\": \"42.7K\", \"totalAcceptedRaw\": 27263, \"totalSubmissionRaw\": 42717, \"acRate\": \"63.8%\"}", "topicTags": [ { "name": "Array", "slug": "array" } ] } } }
2155
Medium
[ "You are given a 0-indexed binary array nums of length n. nums can be divided at index i (where 0 <= i <= n) into two arrays (possibly empty) numsleft and numsright:\n\n\n\tnumsleft has all the elements of nums between index 0 and i - 1 (inclusive), while numsright has all the elements of nums between index i and n...
[ { "hash": -2736015909119714300, "runtime": "2521ms", "solution": "class Solution(object):\n def maxScoreIndices(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n result=[]\n max = zeroes = 0\n total = sum(nums)\n for i...
class Solution(object): def maxScoreIndices(self, nums): """ :type nums: List[int] :rtype: List[int] """
None
None
None
None
None
None
63.8
Level 2
Hi
minimum-size-subarray-in-infinite-array
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> array <code>nums</code> and an integer <code>target</code>.</p>\n\n<p>A <strong>0-indexed</strong> array <code>infinite_nums</code> is generated by infinitely appending the elements of <code>nums</code> to itself.</p>\n\n<p>Return <em>the length of the <strong>shortest</strong> subarray of the array </em><code>infinite_nums</code><em> with a sum equal to </em><code>target</code><em>.</em> If there is no such subarray return <code>-1</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,2,3], target = 5\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> In this example infinite_nums = [1,2,3,1,2,3,1,2,...].\nThe subarray in the range [1,2], has the sum equal to target = 5 and length = 2.\nIt can be proven that 2 is the shortest length of a subarray with sum equal to target = 5.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,1,1,2,3], target = 4\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> In this example infinite_nums = [1,1,1,2,3,1,1,1,2,3,1,1,...].\nThe subarray in the range [4,5], has the sum equal to target = 4 and length = 2.\nIt can be proven that 2 is the shortest length of a subarray with sum equal to target = 4.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [2,4,6,8], target = 3\n<strong>Output:</strong> -1\n<strong>Explanation:</strong> In this example infinite_nums = [2,4,6,8,2,4,6,8,...].\nIt can be proven that there is no subarray with sum equal to target = 3.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= target &lt;= 10<sup>9</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2875", "questionId": "3141", "questionTitle": "Minimum Size Subarray in Infinite Array", "questionTitleSlug": "minimum-size-subarray-in-infinite-array", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"16K\", \"totalSubmission\": \"53K\", \"totalAcceptedRaw\": 15988, \"totalSubmissionRaw\": 53018, \"acRate\": \"30.2%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" }, { "name": "Sliding Window", "slug": "sliding-window" }, { "name": "Prefix Sum", "slug": "prefix-sum" } ] } } }
2875
Medium
[ "You are given a 0-indexed array nums and an integer target.\n\nA 0-indexed array infinite_nums is generated by infinitely appending the elements of nums to itself.\n\nReturn the length of the shortest subarray of the array infinite_nums with a sum equal to target. If there is no such subarray return -1.\n\n \nExam...
[ { "hash": -1207243946393822700, "runtime": "406ms", "solution": "class Solution(object):\n def minSizeSubarray(self, nums, target):\n \"\"\"\n :type nums: List[int]\n :type target: int\n :rtype: int\n \"\"\"\n\n\n \"\"\"\n 1 1 1 2 3 (1 1 ...
class Solution(object): def minSizeSubarray(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """
None
None
None
None
None
None
30.2
Level 4
Hi
smallest-string-with-swaps
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a string <code>s</code>, and an array of pairs of indices in the string&nbsp;<code>pairs</code>&nbsp;where&nbsp;<code>pairs[i] =&nbsp;[a, b]</code>&nbsp;indicates 2 indices(0-indexed) of the string.</p>\n\n<p>You can&nbsp;swap the characters at any pair of indices in the given&nbsp;<code>pairs</code>&nbsp;<strong>any number of times</strong>.</p>\n\n<p>Return the&nbsp;lexicographically smallest string that <code>s</code>&nbsp;can be changed to after using the swaps.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2]]\n<strong>Output:</strong> &quot;bacd&quot;\n<strong>Explaination:</strong> \nSwap s[0] and s[3], s = &quot;bcad&quot;\nSwap s[1] and s[2], s = &quot;bacd&quot;\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;dcab&quot;, pairs = [[0,3],[1,2],[0,2]]\n<strong>Output:</strong> &quot;abcd&quot;\n<strong>Explaination: </strong>\nSwap s[0] and s[3], s = &quot;bcad&quot;\nSwap s[0] and s[2], s = &quot;acbd&quot;\nSwap s[1] and s[2], s = &quot;abcd&quot;</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> s = &quot;cba&quot;, pairs = [[0,1],[1,2]]\n<strong>Output:</strong> &quot;abc&quot;\n<strong>Explaination: </strong>\nSwap s[0] and s[1], s = &quot;bca&quot;\nSwap s[1] and s[2], s = &quot;bac&quot;\nSwap s[0] and s[1], s = &quot;abc&quot;\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= s.length &lt;= 10^5</code></li>\n\t<li><code>0 &lt;= pairs.length &lt;= 10^5</code></li>\n\t<li><code>0 &lt;= pairs[i][0], pairs[i][1] &lt;&nbsp;s.length</code></li>\n\t<li><code>s</code>&nbsp;only contains lower case English letters.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1202", "questionId": "1308", "questionTitle": "Smallest String With Swaps", "questionTitleSlug": "smallest-string-with-swaps", "similarQuestions": "[{\"title\": \"Minimize Hamming Distance After Swap Operations\", \"titleSlug\": \"minimize-hamming-distance-after-swap-operations\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Process Restricted Friend Requests\", \"titleSlug\": \"process-restricted-friend-requests\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Largest Number After Digit Swaps by Parity\", \"titleSlug\": \"largest-number-after-digit-swaps-by-parity\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Lexicographically Smallest Beautiful String\", \"titleSlug\": \"lexicographically-smallest-beautiful-string\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Make Lexicographically Smallest Array by Swapping Elements\", \"titleSlug\": \"make-lexicographically-smallest-array-by-swapping-elements\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"108.4K\", \"totalSubmission\": \"186.8K\", \"totalAcceptedRaw\": 108445, \"totalSubmissionRaw\": 186797, \"acRate\": \"58.1%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" }, { "name": "String", "slug": "string" }, { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Breadth-First Search", "slug": "breadth-first-search" }, { "name": "Union Find", "slug": "union-find" }, { "name": "Sorting", "slug": "sorting" } ] } } }
1202
Medium
[ "You are given a string s, and an array of pairs of indices in the string pairs where pairs[i] = [a, b] indicates 2 indices(0-indexed) of the string.\n\nYou can swap the characters at any pair of indices in the given pairs any number of times.\n\nReturn the lexicographically smallest string that s can be changed to...
[ { "hash": 1342034885985452000, "runtime": "651ms", "solution": "class Solution:\n def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str:\n class UnionFind:\n def __init__(self):\n self.parents = {}\n self.rank = {}\n def fi...
class Solution(object): def smallestStringWithSwaps(self, s, pairs): """ :type s: str :type pairs: List[List[int]] :rtype: str """
None
None
None
None
None
None
58.1
Level 3
Hi
find-unique-binary-string
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an array of strings <code>nums</code> containing <code>n</code> <strong>unique</strong> binary strings each of length <code>n</code>, return <em>a binary string of length </em><code>n</code><em> that <strong>does not appear</strong> in </em><code>nums</code><em>. If there are multiple answers, you may return <strong>any</strong> of them</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [&quot;01&quot;,&quot;10&quot;]\n<strong>Output:</strong> &quot;11&quot;\n<strong>Explanation:</strong> &quot;11&quot; does not appear in nums. &quot;00&quot; would also be correct.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [&quot;00&quot;,&quot;01&quot;]\n<strong>Output:</strong> &quot;11&quot;\n<strong>Explanation:</strong> &quot;11&quot; does not appear in nums. &quot;10&quot; would also be correct.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [&quot;111&quot;,&quot;011&quot;,&quot;001&quot;]\n<strong>Output:</strong> &quot;101&quot;\n<strong>Explanation:</strong> &quot;101&quot; does not appear in nums. &quot;000&quot;, &quot;010&quot;, &quot;100&quot;, and &quot;110&quot; would also be correct.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>n == nums.length</code></li>\n\t<li><code>1 &lt;= n &lt;= 16</code></li>\n\t<li><code>nums[i].length == n</code></li>\n\t<li><code>nums[i] </code>is either <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>\n\t<li>All the strings of <code>nums</code> are <strong>unique</strong>.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1980", "questionId": "2107", "questionTitle": "Find Unique Binary String", "questionTitleSlug": "find-unique-binary-string", "similarQuestions": "[{\"title\": \"Missing Number\", \"titleSlug\": \"missing-number\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Find All Numbers Disappeared in an Array\", \"titleSlug\": \"find-all-numbers-disappeared-in-an-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Random Pick with Blacklist\", \"titleSlug\": \"random-pick-with-blacklist\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"123.7K\", \"totalSubmission\": \"166.5K\", \"totalAcceptedRaw\": 123708, \"totalSubmissionRaw\": 166463, \"acRate\": \"74.3%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "String", "slug": "string" }, { "name": "Backtracking", "slug": "backtracking" } ] } } }
1980
Medium
[ "Given an array of strings nums containing n unique binary strings each of length n, return a binary string of length n that does not appear in nums. If there are multiple answers, you may return any of them.\n\n \nExample 1:\n\nInput: nums = [\"01\",\"10\"]\nOutput: \"11\"\nExplanation: \"11\" does not appear in n...
[ { "hash": 3861746947075758600, "runtime": "46ms", "solution": "class Solution(object):\n def findDifferentBinaryString(self, nums):\n \"\"\"\n :type nums: List[str]\n :rtype: str\n \"\"\"\n seen=set(nums)\n n=len(nums)\n for i in range(2**n,2**(n+1)):\...
class Solution(object): def findDifferentBinaryString(self, nums): """ :type nums: List[str] :rtype: str """
None
None
None
None
None
None
74.3
Level 2
Hi
minimum-domino-rotations-for-equal-row
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>In a row of dominoes, <code>tops[i]</code> and <code>bottoms[i]</code> represent the top and bottom halves of the <code>i<sup>th</sup></code> domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)</p>\n\n<p>We may rotate the <code>i<sup>th</sup></code> domino, so that <code>tops[i]</code> and <code>bottoms[i]</code> swap values.</p>\n\n<p>Return the minimum number of rotations so that all the values in <code>tops</code> are the same, or all the values in <code>bottoms</code> are the same.</p>\n\n<p>If it cannot be done, return <code>-1</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/05/14/domino.png\" style=\"height: 300px; width: 421px;\" />\n<pre>\n<strong>Input:</strong> tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2]\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> \nThe first figure represents the dominoes as given by tops and bottoms: before we do any rotations.\nIf we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> tops = [3,5,1,2,3], bottoms = [3,6,3,3,4]\n<strong>Output:</strong> -1\n<strong>Explanation:</strong> \nIn this case, it is not possible to rotate the dominoes to make one row of values equal.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= tops.length &lt;= 2 * 10<sup>4</sup></code></li>\n\t<li><code>bottoms.length == tops.length</code></li>\n\t<li><code>1 &lt;= tops[i], bottoms[i] &lt;= 6</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1007", "questionId": "1049", "questionTitle": "Minimum Domino Rotations For Equal Row", "questionTitleSlug": "minimum-domino-rotations-for-equal-row", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"198.3K\", \"totalSubmission\": \"379.6K\", \"totalAcceptedRaw\": 198279, \"totalSubmissionRaw\": 379628, \"acRate\": \"52.2%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Greedy", "slug": "greedy" } ] } } }
1007
Medium
[ "In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the ith domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)\n\nWe may rotate the ith domino, so that tops[i] and bottoms[i] swap values.\n\nReturn the minimum number of rotations so that all th...
[ { "hash": 2968987213111126000, "runtime": "833ms", "solution": "class Solution(object):\n def minDominoRotations(self, tops, bottoms):\n \"\"\"\n :type tops: List[int]\n :type bottoms: List[int]\n :rtype: int\n \"\"\"\n self.steps = float('inf')\n \n ...
class Solution(object): def minDominoRotations(self, tops, bottoms): """ :type tops: List[int] :type bottoms: List[int] :rtype: int """
None
None
None
None
None
None
52.2
Level 3
Hi
number-of-valid-clock-times
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a string of length <code>5</code> called <code>time</code>, representing the current time on a digital clock in the format <code>&quot;hh:mm&quot;</code>. The <strong>earliest</strong> possible time is <code>&quot;00:00&quot;</code> and the <strong>latest</strong> possible time is <code>&quot;23:59&quot;</code>.</p>\n\n<p>In the string <code>time</code>, the digits represented by the <code>?</code>&nbsp;symbol are <strong>unknown</strong>, and must be <strong>replaced</strong> with a digit from <code>0</code> to <code>9</code>.</p>\n\n<p>Return<em> an integer </em><code>answer</code><em>, the number of valid clock times that can be created by replacing every </em><code>?</code><em>&nbsp;with a digit from </em><code>0</code><em> to </em><code>9</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> time = &quot;?5:00&quot;\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> We can replace the ? with either a 0 or 1, producing &quot;05:00&quot; or &quot;15:00&quot;. Note that we cannot replace it with a 2, since the time &quot;25:00&quot; is invalid. In total, we have two choices.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> time = &quot;0?:0?&quot;\n<strong>Output:</strong> 100\n<strong>Explanation:</strong> Each ? can be replaced by any digit from 0 to 9, so we have 100 total choices.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> time = &quot;??:??&quot;\n<strong>Output:</strong> 1440\n<strong>Explanation:</strong> There are 24 possible choices for the hours, and 60 possible choices for the minutes. In total, we have 24 * 60 = 1440 choices.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>time</code> is a valid string of length <code>5</code> in the format <code>&quot;hh:mm&quot;</code>.</li>\n\t<li><code>&quot;00&quot; &lt;= hh &lt;= &quot;23&quot;</code></li>\n\t<li><code>&quot;00&quot; &lt;= mm &lt;= &quot;59&quot;</code></li>\n\t<li>Some of the digits might be replaced with <code>&#39;?&#39;</code> and need to be replaced with digits from <code>0</code> to <code>9</code>.</li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "2437", "questionId": "2528", "questionTitle": "Number of Valid Clock Times", "questionTitleSlug": "number-of-valid-clock-times", "similarQuestions": "[{\"title\": \"Largest Time for Given Digits\", \"titleSlug\": \"largest-time-for-given-digits\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Latest Time by Replacing Hidden Digits\", \"titleSlug\": \"latest-time-by-replacing-hidden-digits\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"23.6K\", \"totalSubmission\": \"53.3K\", \"totalAcceptedRaw\": 23570, \"totalSubmissionRaw\": 53258, \"acRate\": \"44.3%\"}", "topicTags": [ { "name": "String", "slug": "string" }, { "name": "Enumeration", "slug": "enumeration" } ] } } }
2437
Easy
[ "You are given a string of length 5 called time, representing the current time on a digital clock in the format \"hh:mm\". The earliest possible time is \"00:00\" and the latest possible time is \"23:59\".\n\nIn the string time, the digits represented by the ? symbol are unknown, and must be replaced with a digit f...
[ { "hash": -2297881449530334700, "runtime": "41ms", "solution": "class Solution(object):\n def countTime(self, time):\n \"\"\"\n :type time: str\n :rtype: int\n \"\"\"\n count = 0\n\n for h in range(24):\n for m in range(60):\n if (ti...
class Solution(object): def countTime(self, time): """ :type time: str :rtype: int """
None
None
None
None
None
None
44.3
Level 1
Hi
prime-palindrome
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer n, return <em>the smallest <strong>prime palindrome</strong> greater than or equal to </em><code>n</code>.</p>\n\n<p>An integer is <strong>prime</strong> if it has exactly two divisors: <code>1</code> and itself. Note that <code>1</code> is not a prime number.</p>\n\n<ul>\n\t<li>For example, <code>2</code>, <code>3</code>, <code>5</code>, <code>7</code>, <code>11</code>, and <code>13</code> are all primes.</li>\n</ul>\n\n<p>An integer is a <strong>palindrome</strong> if it reads the same from left to right as it does from right to left.</p>\n\n<ul>\n\t<li>For example, <code>101</code> and <code>12321</code> are palindromes.</li>\n</ul>\n\n<p>The test cases are generated so that the answer always exists and is in the range <code>[2, 2 * 10<sup>8</sup>]</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<pre><strong>Input:</strong> n = 6\n<strong>Output:</strong> 7\n</pre><p><strong class=\"example\">Example 2:</strong></p>\n<pre><strong>Input:</strong> n = 8\n<strong>Output:</strong> 11\n</pre><p><strong class=\"example\">Example 3:</strong></p>\n<pre><strong>Input:</strong> n = 13\n<strong>Output:</strong> 101\n</pre>\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= n &lt;= 10<sup>8</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "866", "questionId": "897", "questionTitle": "Prime Palindrome", "questionTitleSlug": "prime-palindrome", "similarQuestions": "[{\"title\": \"Sum of k-Mirror Numbers\", \"titleSlug\": \"sum-of-k-mirror-numbers\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"30.8K\", \"totalSubmission\": \"118.7K\", \"totalAcceptedRaw\": 30781, \"totalSubmissionRaw\": 118682, \"acRate\": \"25.9%\"}", "topicTags": [ { "name": "Math", "slug": "math" } ] } } }
866
Medium
[ "Given an integer n, return the smallest prime palindrome greater than or equal to n.\n\nAn integer is prime if it has exactly two divisors: 1 and itself. Note that 1 is not a prime number.\n\n\n\tFor example, 2, 3, 5, 7, 11, and 13 are all primes.\n\n\nAn integer is a palindrome if it reads the same from left to r...
[ { "hash": -1277829170904851500, "runtime": "95ms", "solution": "class Solution(object):\n def primePalindrome(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n if 8 <= n <= 11:\n return 11\n for x in range(10**(len(str(n))//2), 10**5):\n ...
class Solution(object): def primePalindrome(self, n): """ :type n: int :rtype: int """
None
None
None
None
None
None
25.9
Level 4
Hi
maximum-ascending-subarray-sum
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an array of positive integers <code>nums</code>, return the <em>maximum possible sum of an <strong>ascending</strong> subarray in </em><code>nums</code>.</p>\n\n<p>A subarray is defined as a contiguous sequence of numbers in an array.</p>\n\n<p>A subarray <code>[nums<sub>l</sub>, nums<sub>l+1</sub>, ..., nums<sub>r-1</sub>, nums<sub>r</sub>]</code> is <strong>ascending</strong> if for all <code>i</code> where <code>l &lt;= i &lt; r</code>, <code>nums<sub>i </sub> &lt; nums<sub>i+1</sub></code>. Note that a subarray of size <code>1</code> is <strong>ascending</strong>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [10,20,30,5,10,50]\n<strong>Output:</strong> 65\n<strong>Explanation: </strong>[5,10,50] is the ascending subarray with the maximum sum of 65.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [10,20,30,40,50]\n<strong>Output:</strong> 150\n<strong>Explanation: </strong>[10,20,30,40,50] is the ascending subarray with the maximum sum of 150.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [12,17,15,13,10,11,12]\n<strong>Output:</strong> 33\n<strong>Explanation: </strong>[10,11,12] is the ascending subarray with the maximum sum of 33.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 100</code></li>\n\t<li><code>1 &lt;= nums[i] &lt;= 100</code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "1800", "questionId": "1927", "questionTitle": "Maximum Ascending Subarray Sum", "questionTitleSlug": "maximum-ascending-subarray-sum", "similarQuestions": "[{\"title\": \"Find Good Days to Rob the Bank\", \"titleSlug\": \"find-good-days-to-rob-the-bank\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Maximum Number of Books You Can Take\", \"titleSlug\": \"maximum-number-of-books-you-can-take\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Count Strictly Increasing Subarrays\", \"titleSlug\": \"count-strictly-increasing-subarrays\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"49.4K\", \"totalSubmission\": \"79.1K\", \"totalAcceptedRaw\": 49435, \"totalSubmissionRaw\": 79074, \"acRate\": \"62.5%\"}", "topicTags": [ { "name": "Array", "slug": "array" } ] } } }
1800
Easy
[ "Given an array of positive integers nums, return the maximum possible sum of an ascending subarray in nums.\n\nA subarray is defined as a contiguous sequence of numbers in an array.\n\nA subarray [numsl, numsl+1, ..., numsr-1, numsr] is ascending if for all i where l <= i < r, numsi < numsi+1. Note that a subarra...
[ { "hash": -5378647661516357000, "runtime": "24ms", "solution": "class Solution(object):\n def maxAscendingSum(self, nums):\n maxSum = 0\n i = 0\n N = len(nums)\n p1 = 0\n while(i < N):\n summer = nums[i]\n while(i+1 < N and nums[i+1] > nums[i])...
class Solution(object): def maxAscendingSum(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
62.5
Level 1
Hi
exclusive-time-of-functions
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>On a <strong>single-threaded</strong> CPU, we execute a program containing <code>n</code> functions. Each function has a unique ID between <code>0</code> and <code>n-1</code>.</p>\n\n<p>Function calls are <strong>stored in a <a href=\"https://en.wikipedia.org/wiki/Call_stack\">call stack</a></strong>: when a function call starts, its ID is pushed onto the stack, and when a function call ends, its ID is popped off the stack. The function whose ID is at the top of the stack is <strong>the current function being executed</strong>. Each time a function starts or ends, we write a log with the ID, whether it started or ended, and the timestamp.</p>\n\n<p>You are given a list <code>logs</code>, where <code>logs[i]</code> represents the <code>i<sup>th</sup></code> log message formatted as a string <code>&quot;{function_id}:{&quot;start&quot; | &quot;end&quot;}:{timestamp}&quot;</code>. For example, <code>&quot;0:start:3&quot;</code> means a function call with function ID <code>0</code> <strong>started at the beginning</strong> of timestamp <code>3</code>, and <code>&quot;1:end:2&quot;</code> means a function call with function ID <code>1</code> <strong>ended at the end</strong> of timestamp <code>2</code>. Note that a function can be called <b>multiple times, possibly recursively</b>.</p>\n\n<p>A function&#39;s <strong>exclusive time</strong> is the sum of execution times for all function calls in the program. For example, if a function is called twice, one call executing for <code>2</code> time units and another call executing for <code>1</code> time unit, the <strong>exclusive time</strong> is <code>2 + 1 = 3</code>.</p>\n\n<p>Return <em>the <strong>exclusive time</strong> of each function in an array, where the value at the </em><code>i<sup>th</sup></code><em> index represents the exclusive time for the function with ID </em><code>i</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2019/04/05/diag1b.png\" style=\"width: 550px; height: 239px;\" />\n<pre>\n<strong>Input:</strong> n = 2, logs = [&quot;0:start:0&quot;,&quot;1:start:2&quot;,&quot;1:end:5&quot;,&quot;0:end:6&quot;]\n<strong>Output:</strong> [3,4]\n<strong>Explanation:</strong>\nFunction 0 starts at the beginning of time 0, then it executes 2 for units of time and reaches the end of time 1.\nFunction 1 starts at the beginning of time 2, executes for 4 units of time, and ends at the end of time 5.\nFunction 0 resumes execution at the beginning of time 6 and executes for 1 unit of time.\nSo function 0 spends 2 + 1 = 3 units of total time executing, and function 1 spends 4 units of total time executing.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 1, logs = [&quot;0:start:0&quot;,&quot;0:start:2&quot;,&quot;0:end:5&quot;,&quot;0:start:6&quot;,&quot;0:end:6&quot;,&quot;0:end:7&quot;]\n<strong>Output:</strong> [8]\n<strong>Explanation:</strong>\nFunction 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.\nFunction 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.\nFunction 0 (initial call) resumes execution then immediately calls itself again.\nFunction 0 (2nd recursive call) starts at the beginning of time 6 and executes for 1 unit of time.\nFunction 0 (initial call) resumes execution at the beginning of time 7 and executes for 1 unit of time.\nSo function 0 spends 2 + 4 + 1 + 1 = 8 units of total time executing.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> n = 2, logs = [&quot;0:start:0&quot;,&quot;0:start:2&quot;,&quot;0:end:5&quot;,&quot;1:start:6&quot;,&quot;1:end:6&quot;,&quot;0:end:7&quot;]\n<strong>Output:</strong> [7,1]\n<strong>Explanation:</strong>\nFunction 0 starts at the beginning of time 0, executes for 2 units of time, and recursively calls itself.\nFunction 0 (recursive call) starts at the beginning of time 2 and executes for 4 units of time.\nFunction 0 (initial call) resumes execution then immediately calls function 1.\nFunction 1 starts at the beginning of time 6, executes 1 unit of time, and ends at the end of time 6.\nFunction 0 resumes execution at the beginning of time 6 and executes for 2 units of time.\nSo function 0 spends 2 + 4 + 1 = 7 units of total time executing, and function 1 spends 1 unit of total time executing.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= n &lt;= 100</code></li>\n\t<li><code>1 &lt;= logs.length &lt;= 500</code></li>\n\t<li><code>0 &lt;= function_id &lt; n</code></li>\n\t<li><code>0 &lt;= timestamp &lt;= 10<sup>9</sup></code></li>\n\t<li>No two start events will happen at the same timestamp.</li>\n\t<li>No two end events will happen at the same timestamp.</li>\n\t<li>Each function has an <code>&quot;end&quot;</code> log for each <code>&quot;start&quot;</code> log.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "636", "questionId": "636", "questionTitle": "Exclusive Time of Functions", "questionTitleSlug": "exclusive-time-of-functions", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"197.5K\", \"totalSubmission\": \"320.6K\", \"totalAcceptedRaw\": 197499, \"totalSubmissionRaw\": 320601, \"acRate\": \"61.6%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Stack", "slug": "stack" } ] } } }
636
Medium
[ "On a single-threaded CPU, we execute a program containing n functions. Each function has a unique ID between 0 and n-1.\n\nFunction calls are stored in a call stack: when a function call starts, its ID is pushed onto the stack, and when a function call ends, its ID is popped off the stack. The function whose ID is...
[ { "hash": -282324804671134460, "runtime": "69ms", "solution": "class Solution(object):\n def exclusiveTime(self, n, logs):\n \"\"\"\n :type n: int\n :type logs: List[str]\n :rtype: List[int]\n \"\"\"\n def __getnum(str):\n idx = 1\n whil...
class Solution(object): def exclusiveTime(self, n, logs): """ :type n: int :type logs: List[str] :rtype: List[int] """
None
None
None
None
None
None
61.6
Level 2
Hi
longest-well-performing-interval
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>We are given <code>hours</code>, a list of the number of hours worked per day for a given employee.</p>\n\n<p>A day is considered to be a <em>tiring day</em> if and only if the number of hours worked is (strictly) greater than <code>8</code>.</p>\n\n<p>A <em>well-performing interval</em> is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days.</p>\n\n<p>Return the length of the longest well-performing interval.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> hours = [9,9,6,0,6,6,9]\n<strong>Output:</strong> 3\n<strong>Explanation: </strong>The longest well-performing interval is [9,9,6].\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> hours = [6,6,6]\n<strong>Output:</strong> 0\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= hours.length &lt;= 10<sup>4</sup></code></li>\n\t<li><code>0 &lt;= hours[i] &lt;= 16</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1124", "questionId": "1219", "questionTitle": "Longest Well-Performing Interval", "questionTitleSlug": "longest-well-performing-interval", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"29.7K\", \"totalSubmission\": \"85.4K\", \"totalAcceptedRaw\": 29681, \"totalSubmissionRaw\": 85427, \"acRate\": \"34.7%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" }, { "name": "Stack", "slug": "stack" }, { "name": "Monotonic Stack", "slug": "monotonic-stack" }, { "name": "Prefix Sum", "slug": "prefix-sum" } ] } } }
1124
Medium
[ "We are given hours, a list of the number of hours worked per day for a given employee.\n\nA day is considered to be a tiring day if and only if the number of hours worked is (strictly) greater than 8.\n\nA well-performing interval is an interval of days for which the number of tiring days is strictly larger than t...
[ { "hash": -8395080351682476000, "runtime": "151ms", "solution": "class Solution(object):\n def longestWPI(self, hours):\n max_wpi, ntv, first_preimage = 0, 0, {}\n\n for day_idx, hrs_today in enumerate(hours):\n\n # check if today was a tiring day or not\n ntv += 1...
class Solution(object): def longestWPI(self, hours): """ :type hours: List[int] :rtype: int """
None
None
None
None
None
None
34.7
Level 4
Hi
sum-of-distances-in-tree
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There is an undirected connected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code> and <code>n - 1</code> edges.</p>\n\n<p>You are given the integer <code>n</code> and the array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>\n\n<p>Return an array <code>answer</code> of length <code>n</code> where <code>answer[i]</code> is the sum of the distances between the <code>i<sup>th</sup></code> node in the tree and all other nodes.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/07/23/lc-sumdist1.jpg\" style=\"width: 304px; height: 224px;\" />\n<pre>\n<strong>Input:</strong> n = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]]\n<strong>Output:</strong> [8,12,6,10,10,10]\n<strong>Explanation:</strong> The tree is shown above.\nWe can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5)\nequals 1 + 1 + 2 + 2 + 2 = 8.\nHence, answer[0] = 8, and so on.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/07/23/lc-sumdist2.jpg\" style=\"width: 64px; height: 65px;\" />\n<pre>\n<strong>Input:</strong> n = 1, edges = []\n<strong>Output:</strong> [0]\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/07/23/lc-sumdist3.jpg\" style=\"width: 144px; height: 145px;\" />\n<pre>\n<strong>Input:</strong> n = 2, edges = [[1,0]]\n<strong>Output:</strong> [1,1]\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li>\n\t<li><code>edges.length == n - 1</code></li>\n\t<li><code>edges[i].length == 2</code></li>\n\t<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>\n\t<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>\n\t<li>The given input represents a valid tree.</li>\n</ul>\n", "difficulty": "Hard", "questionFrontendId": "834", "questionId": "863", "questionTitle": "Sum of Distances in Tree", "questionTitleSlug": "sum-of-distances-in-tree", "similarQuestions": "[{\"title\": \"Distribute Coins in Binary Tree\", \"titleSlug\": \"distribute-coins-in-binary-tree\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Count Nodes With the Highest Score\", \"titleSlug\": \"count-nodes-with-the-highest-score\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Collect Coins in a Tree\", \"titleSlug\": \"collect-coins-in-a-tree\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Maximum Score After Applying Operations on a Tree\", \"titleSlug\": \"maximum-score-after-applying-operations-on-a-tree\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"88.5K\", \"totalSubmission\": \"149.4K\", \"totalAcceptedRaw\": 88537, \"totalSubmissionRaw\": 149383, \"acRate\": \"59.3%\"}", "topicTags": [ { "name": "Dynamic Programming", "slug": "dynamic-programming" }, { "name": "Tree", "slug": "tree" }, { "name": "Depth-First Search", "slug": "depth-first-search" }, { "name": "Graph", "slug": "graph" } ] } } }
834
Hard
[ "There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges.\n\nYou are given the integer n and the array edges where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.\n\nReturn an array answer of length n where answer[i] is the sum of the dista...
[ { "hash": 4076565103399564000, "runtime": "835ms", "solution": "class Solution(object):\n def sumOfDistancesInTree(self, n, edges):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :rtype: List[int]\n \"\"\"\n neighbor = collections.defaultdict(set)\n...
class Solution(object): def sumOfDistancesInTree(self, n, edges): """ :type n: int :type edges: List[List[int]] :rtype: List[int] """
None
None
None
None
None
None
59.3
Level 5
Hi
lucky-numbers-in-a-matrix
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an <code>m x n</code> matrix of <strong>distinct </strong>numbers, return <em>all <strong>lucky numbers</strong> in the matrix in <strong>any </strong>order</em>.</p>\n\n<p>A <strong>lucky number</strong> is an element of the matrix such that it is the minimum element in its row and maximum in its column.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> matrix = [[3,7,8],[9,11,13],[15,16,17]]\n<strong>Output:</strong> [15]\n<strong>Explanation:</strong> 15 is the only lucky number since it is the minimum in its row and the maximum in its column.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]\n<strong>Output:</strong> [12]\n<strong>Explanation:</strong> 12 is the only lucky number since it is the minimum in its row and the maximum in its column.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> matrix = [[7,8],[1,2]]\n<strong>Output:</strong> [7]\n<strong>Explanation:</strong> 7 is the only lucky number since it is the minimum in its row and the maximum in its column.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>m == mat.length</code></li>\n\t<li><code>n == mat[i].length</code></li>\n\t<li><code>1 &lt;= n, m &lt;= 50</code></li>\n\t<li><code>1 &lt;= matrix[i][j] &lt;= 10<sup>5</sup></code>.</li>\n\t<li>All elements in the matrix are distinct.</li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "1380", "questionId": "1496", "questionTitle": "Lucky Numbers in a Matrix", "questionTitleSlug": "lucky-numbers-in-a-matrix", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"97K\", \"totalSubmission\": \"136.5K\", \"totalAcceptedRaw\": 97009, \"totalSubmissionRaw\": 136507, \"acRate\": \"71.1%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Matrix", "slug": "matrix" } ] } } }
1380
Easy
[ "Given an m x n matrix of distinct numbers, return all lucky numbers in the matrix in any order.\n\nA lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.\n\n \nExample 1:\n\nInput: matrix = [[3,7,8],[9,11,13],[15,16,17]]\nOutput: [15]\nExplanation: 15 i...
[ { "hash": -5399601047316612000, "runtime": "100ms", "solution": "class Solution(object):\n def luckyNumbers (self, matrix):\n col = []\n for i in range(0, len(matrix[0])):\n temp = []\n for j in range(0, len(matrix)):\n temp.append( matrix[j][i] )\n ...
class Solution(object): def luckyNumbers (self, matrix): """ :type matrix: List[List[int]] :rtype: List[int] """
None
None
None
None
None
None
71.1
Level 1
Hi
design-browser-history
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You have a <strong>browser</strong> of one tab where you start on the <code>homepage</code> and you can visit another <code>url</code>, get back in the history number of <code>steps</code> or move forward in the history number of <code>steps</code>.</p>\n\n<p>Implement the <code>BrowserHistory</code> class:</p>\n\n<ul>\n\t<li><code>BrowserHistory(string homepage)</code> Initializes the object with the <code>homepage</code>&nbsp;of the browser.</li>\n\t<li><code>void visit(string url)</code>&nbsp;Visits&nbsp;<code>url</code> from the current page. It clears up all the forward history.</li>\n\t<li><code>string back(int steps)</code>&nbsp;Move <code>steps</code> back in history. If you can only return <code>x</code> steps in the history and <code>steps &gt; x</code>, you will&nbsp;return only <code>x</code> steps. Return the current <code>url</code>&nbsp;after moving back in history <strong>at most</strong> <code>steps</code>.</li>\n\t<li><code>string forward(int steps)</code>&nbsp;Move <code>steps</code> forward in history. If you can only forward <code>x</code> steps in the history and <code>steps &gt; x</code>, you will&nbsp;forward only&nbsp;<code>x</code> steps. Return the current <code>url</code>&nbsp;after forwarding in history <strong>at most</strong> <code>steps</code>.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example:</strong></p>\n\n<pre>\n<b>Input:</b>\n[&quot;BrowserHistory&quot;,&quot;visit&quot;,&quot;visit&quot;,&quot;visit&quot;,&quot;back&quot;,&quot;back&quot;,&quot;forward&quot;,&quot;visit&quot;,&quot;forward&quot;,&quot;back&quot;,&quot;back&quot;]\n[[&quot;leetcode.com&quot;],[&quot;google.com&quot;],[&quot;facebook.com&quot;],[&quot;youtube.com&quot;],[1],[1],[1],[&quot;linkedin.com&quot;],[2],[2],[7]]\n<b>Output:</b>\n[null,null,null,null,&quot;facebook.com&quot;,&quot;google.com&quot;,&quot;facebook.com&quot;,null,&quot;linkedin.com&quot;,&quot;google.com&quot;,&quot;leetcode.com&quot;]\n\n<b>Explanation:</b>\nBrowserHistory browserHistory = new BrowserHistory(&quot;leetcode.com&quot;);\nbrowserHistory.visit(&quot;google.com&quot;); // You are in &quot;leetcode.com&quot;. Visit &quot;google.com&quot;\nbrowserHistory.visit(&quot;facebook.com&quot;); // You are in &quot;google.com&quot;. Visit &quot;facebook.com&quot;\nbrowserHistory.visit(&quot;youtube.com&quot;); // You are in &quot;facebook.com&quot;. Visit &quot;youtube.com&quot;\nbrowserHistory.back(1); // You are in &quot;youtube.com&quot;, move back to &quot;facebook.com&quot; return &quot;facebook.com&quot;\nbrowserHistory.back(1); // You are in &quot;facebook.com&quot;, move back to &quot;google.com&quot; return &quot;google.com&quot;\nbrowserHistory.forward(1); // You are in &quot;google.com&quot;, move forward to &quot;facebook.com&quot; return &quot;facebook.com&quot;\nbrowserHistory.visit(&quot;linkedin.com&quot;); // You are in &quot;facebook.com&quot;. Visit &quot;linkedin.com&quot;\nbrowserHistory.forward(2); // You are in &quot;linkedin.com&quot;, you cannot move forward any steps.\nbrowserHistory.back(2); // You are in &quot;linkedin.com&quot;, move back two steps to &quot;facebook.com&quot; then to &quot;google.com&quot;. return &quot;google.com&quot;\nbrowserHistory.back(7); // You are in &quot;google.com&quot;, you can move back only one step to &quot;leetcode.com&quot;. return &quot;leetcode.com&quot;\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= homepage.length &lt;= 20</code></li>\n\t<li><code>1 &lt;= url.length &lt;= 20</code></li>\n\t<li><code>1 &lt;= steps &lt;= 100</code></li>\n\t<li><code>homepage</code> and <code>url</code> consist of&nbsp; &#39;.&#39; or lower case English letters.</li>\n\t<li>At most <code>5000</code>&nbsp;calls will be made to <code>visit</code>, <code>back</code>, and <code>forward</code>.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1472", "questionId": "1582", "questionTitle": "Design Browser History", "questionTitleSlug": "design-browser-history", "similarQuestions": "[{\"title\": \"Design Video Sharing Platform\", \"titleSlug\": \"design-video-sharing-platform\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"207.3K\", \"totalSubmission\": \"267.2K\", \"totalAcceptedRaw\": 207298, \"totalSubmissionRaw\": 267203, \"acRate\": \"77.6%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Linked List", "slug": "linked-list" }, { "name": "Stack", "slug": "stack" }, { "name": "Design", "slug": "design" }, { "name": "Doubly-Linked List", "slug": "doubly-linked-list" }, { "name": "Data Stream", "slug": "data-stream" } ] } } }
1472
Medium
[ "You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps.\n\nImplement the BrowserHistory class:\n\n\n\tBrowserHistory(string homepage) Initializes the object with the homepage of the browser....
[ { "hash": -5284126673219090000, "runtime": "414ms", "solution": "class Node:\n def __init__(self, url):\n self.url = url\n self.next = None\n self.prev = None\n\nclass BrowserHistory(object):\n\n def __init__(self, homepage):\n \"\"\"\n :type homepage: str\n ...
class BrowserHistory(object): def __init__(self, homepage): """ :type homepage: str """ def visit(self, url): """ :type url: str :rtype: None """ def back(self, steps): """ :type steps: int :rtype: str """ def forward(self, steps): """ :type steps: int :rtype: str """ # Your BrowserHistory object will be instantiated and called as such: # obj = BrowserHistory(homepage) # obj.visit(url) # param_2 = obj.back(steps) # param_3 = obj.forward(steps)
None
None
None
None
None
None
77.6
Level 2
Hi
target-sum
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an integer array <code>nums</code> and an integer <code>target</code>.</p>\n\n<p>You want to build an <strong>expression</strong> out of nums by adding one of the symbols <code>&#39;+&#39;</code> and <code>&#39;-&#39;</code> before each integer in nums and then concatenate all the integers.</p>\n\n<ul>\n\t<li>For example, if <code>nums = [2, 1]</code>, you can add a <code>&#39;+&#39;</code> before <code>2</code> and a <code>&#39;-&#39;</code> before <code>1</code> and concatenate them to build the expression <code>&quot;+2-1&quot;</code>.</li>\n</ul>\n\n<p>Return the number of different <strong>expressions</strong> that you can build, which evaluates to <code>target</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,1,1,1,1], target = 3\n<strong>Output:</strong> 5\n<strong>Explanation:</strong> There are 5 ways to assign symbols to make the sum of nums be target 3.\n-1 + 1 + 1 + 1 + 1 = 3\n+1 - 1 + 1 + 1 + 1 = 3\n+1 + 1 - 1 + 1 + 1 = 3\n+1 + 1 + 1 - 1 + 1 = 3\n+1 + 1 + 1 + 1 - 1 = 3\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1], target = 1\n<strong>Output:</strong> 1\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 20</code></li>\n\t<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>\n\t<li><code>0 &lt;= sum(nums[i]) &lt;= 1000</code></li>\n\t<li><code>-1000 &lt;= target &lt;= 1000</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "494", "questionId": "494", "questionTitle": "Target Sum", "questionTitleSlug": "target-sum", "similarQuestions": "[{\"title\": \"Expression Add Operators\", \"titleSlug\": \"expression-add-operators\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Ways to Express an Integer as Sum of Powers\", \"titleSlug\": \"ways-to-express-an-integer-as-sum-of-powers\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"533.5K\", \"totalSubmission\": \"1.2M\", \"totalAcceptedRaw\": 533452, \"totalSubmissionRaw\": 1152804, \"acRate\": \"46.3%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Dynamic Programming", "slug": "dynamic-programming" }, { "name": "Backtracking", "slug": "backtracking" } ] } } }
494
Medium
[ "You are given an integer array nums and an integer target.\n\nYou want to build an expression out of nums by adding one of the symbols '+' and '-' before each integer in nums and then concatenate all the integers.\n\n\n\tFor example, if nums = [2, 1], you can add a '+' before 2 and a '-' before 1 and concatenate t...
[ { "hash": -6733724374461577000, "runtime": "291ms", "solution": "class Solution:\n # TC: O(tot_sum * n)\n # SC: O(tot_sum * n)\n def findTargetSumWays(self, nums: List[int], target: int) -> int:\n\n cache = dict()\n\n def dfs(idx, curr_sum):\n\n if idx == len(nums):\n ...
class Solution(object): def findTargetSumWays(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """
None
None
None
None
None
None
46.3
Level 4
Hi
exam-room
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There is an exam room with <code>n</code> seats in a single row labeled from <code>0</code> to <code>n - 1</code>.</p>\n\n<p>When a student enters the room, they must sit in the seat that maximizes the distance to the closest person. If there are multiple such seats, they sit in the seat with the lowest number. If no one is in the room, then the student sits at seat number <code>0</code>.</p>\n\n<p>Design a class that simulates the mentioned exam room.</p>\n\n<p>Implement the <code>ExamRoom</code> class:</p>\n\n<ul>\n\t<li><code>ExamRoom(int n)</code> Initializes the object of the exam room with the number of the seats <code>n</code>.</li>\n\t<li><code>int seat()</code> Returns the label of the seat at which the next student will set.</li>\n\t<li><code>void leave(int p)</code> Indicates that the student sitting at seat <code>p</code> will leave the room. It is guaranteed that there will be a student sitting at seat <code>p</code>.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input</strong>\n[&quot;ExamRoom&quot;, &quot;seat&quot;, &quot;seat&quot;, &quot;seat&quot;, &quot;seat&quot;, &quot;leave&quot;, &quot;seat&quot;]\n[[10], [], [], [], [], [4], []]\n<strong>Output</strong>\n[null, 0, 9, 4, 2, null, 5]\n\n<strong>Explanation</strong>\nExamRoom examRoom = new ExamRoom(10);\nexamRoom.seat(); // return 0, no one is in the room, then the student sits at seat number 0.\nexamRoom.seat(); // return 9, the student sits at the last seat number 9.\nexamRoom.seat(); // return 4, the student sits at the last seat number 4.\nexamRoom.seat(); // return 2, the student sits at the last seat number 2.\nexamRoom.leave(4);\nexamRoom.seat(); // return 5, the student sits at the last seat number 5.\n\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>\n\t<li>It is guaranteed that there is a student sitting at seat <code>p</code>.</li>\n\t<li>At most <code>10<sup>4</sup></code> calls will be made to <code>seat</code> and <code>leave</code>.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "855", "questionId": "885", "questionTitle": "Exam Room", "questionTitleSlug": "exam-room", "similarQuestions": "[{\"title\": \"Maximize Distance to Closest Person\", \"titleSlug\": \"maximize-distance-to-closest-person\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"57.8K\", \"totalSubmission\": \"133.7K\", \"totalAcceptedRaw\": 57834, \"totalSubmissionRaw\": 133657, \"acRate\": \"43.3%\"}", "topicTags": [ { "name": "Design", "slug": "design" }, { "name": "Heap (Priority Queue)", "slug": "heap-priority-queue" }, { "name": "Ordered Set", "slug": "ordered-set" } ] } } }
855
Medium
[ "There is an exam room with n seats in a single row labeled from 0 to n - 1.\n\nWhen a student enters the room, they must sit in the seat that maximizes the distance to the closest person. If there are multiple such seats, they sit in the seat with the lowest number. If no one is in the room, then the student sits ...
[ { "hash": 711190324631169300, "runtime": "61ms", "solution": "class ExamRoom:\n\n def __init__(self, n):\n self.n = n\n self.heap = []\n\n self.heap.append((-self.dist(0, n - 1), 0, n - 1))\n\n def seat(self):\n res = 0\n\n curr_dist, l, r = heappop(self.heap)\n ...
class ExamRoom(object): def __init__(self, n): """ :type n: int """ def seat(self): """ :rtype: int """ def leave(self, p): """ :type p: int :rtype: None """ # Your ExamRoom object will be instantiated and called as such: # obj = ExamRoom(n) # param_1 = obj.seat() # obj.leave(p)
None
None
None
None
None
None
43.3
Level 4
Hi
snakes-and-ladders
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an <code>n x n</code> integer matrix <code>board</code> where the cells are labeled from <code>1</code> to <code>n<sup>2</sup></code> in a <a href=\"https://en.wikipedia.org/wiki/Boustrophedon\" target=\"_blank\"><strong>Boustrophedon style</strong></a> starting from the bottom left of the board (i.e. <code>board[n - 1][0]</code>) and alternating direction each row.</p>\n\n<p>You start on square <code>1</code> of the board. In each move, starting from square <code>curr</code>, do the following:</p>\n\n<ul>\n\t<li>Choose a destination square <code>next</code> with a label in the range <code>[curr + 1, min(curr + 6, n<sup>2</sup>)]</code>.\n\n\t<ul>\n\t\t<li>This choice simulates the result of a standard <strong>6-sided die roll</strong>: i.e., there are always at most 6 destinations, regardless of the size of the board.</li>\n\t</ul>\n\t</li>\n\t<li>If <code>next</code> has a snake or ladder, you <strong>must</strong> move to the destination of that snake or ladder. Otherwise, you move to <code>next</code>.</li>\n\t<li>The game ends when you reach the square <code>n<sup>2</sup></code>.</li>\n</ul>\n\n<p>A board square on row <code>r</code> and column <code>c</code> has a snake or ladder if <code>board[r][c] != -1</code>. The destination of that snake or ladder is <code>board[r][c]</code>. Squares <code>1</code> and <code>n<sup>2</sup></code> do not have a snake or ladder.</p>\n\n<p>Note that you only take a snake or ladder at most once per move. If the destination to a snake or ladder is the start of another snake or ladder, you do <strong>not</strong> follow the subsequent&nbsp;snake or ladder.</p>\n\n<ul>\n\t<li>For example, suppose the board is <code>[[-1,4],[-1,3]]</code>, and on the first move, your destination square is <code>2</code>. You follow the ladder to square <code>3</code>, but do <strong>not</strong> follow the subsequent ladder to <code>4</code>.</li>\n</ul>\n\n<p>Return <em>the least number of moves required to reach the square </em><code>n<sup>2</sup></code><em>. If it is not possible to reach the square, return </em><code>-1</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2018/09/23/snakes.png\" style=\"width: 500px; height: 394px;\" />\n<pre>\n<strong>Input:</strong> board = [[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,35,-1,-1,13,-1],[-1,-1,-1,-1,-1,-1],[-1,15,-1,-1,-1,-1]]\n<strong>Output:</strong> 4\n<strong>Explanation:</strong> \nIn the beginning, you start at square 1 (at row 5, column 0).\nYou decide to move to square 2 and must take the ladder to square 15.\nYou then decide to move to square 17 and must take the snake to square 13.\nYou then decide to move to square 14 and must take the ladder to square 35.\nYou then decide to move to square 36, ending the game.\nThis is the lowest possible number of moves to reach the last square, so return 4.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> board = [[-1,-1],[-1,3]]\n<strong>Output:</strong> 1\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>n == board.length == board[i].length</code></li>\n\t<li><code>2 &lt;= n &lt;= 20</code></li>\n\t<li><code>board[i][j]</code> is either <code>-1</code> or in the range <code>[1, n<sup>2</sup>]</code>.</li>\n\t<li>The squares labeled <code>1</code> and <code>n<sup>2</sup></code> do not have any ladders or snakes.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "909", "questionId": "945", "questionTitle": "Snakes and Ladders", "questionTitleSlug": "snakes-and-ladders", "similarQuestions": "[{\"title\": \"Most Profitable Path in a Tree\", \"titleSlug\": \"most-profitable-path-in-a-tree\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"162.1K\", \"totalSubmission\": \"367.4K\", \"totalAcceptedRaw\": 162100, \"totalSubmissionRaw\": 367440, \"acRate\": \"44.1%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Breadth-First Search", "slug": "breadth-first-search" }, { "name": "Matrix", "slug": "matrix" } ] } } }
909
Medium
[ "You are given an n x n integer matrix board where the cells are labeled from 1 to n² in a Boustrophedon style starting from the bottom left of the board (i.e. board[n - 1][0]) and alternating direction each row.\n\nYou start on square 1 of the board. In each move, starting from square curr, do the following:\n\n\n...
[ { "hash": -6982095660841863000, "runtime": "114ms", "solution": "class Solution(object):\n def get_position(self, k, n):\n # subtract 1 from k as k is 1-indexed\n row = (k - 1) // n\n col = (k - 1) - row * n\n \n x = n - 1 - row\n y = col if row % 2 == 0 else...
class Solution(object): def snakesAndLadders(self, board): """ :type board: List[List[int]] :rtype: int """
None
None
None
None
None
None
44.1
Level 4
Hi
largest-1-bordered-square
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a 2D <code>grid</code> of <code>0</code>s and <code>1</code>s, return the number of elements in&nbsp;the largest <strong>square</strong>&nbsp;subgrid that has all <code>1</code>s on its <strong>border</strong>, or <code>0</code> if such a subgrid&nbsp;doesn&#39;t exist in the <code>grid</code>.</p>\r\n\r\n<p>&nbsp;</p>\r\n<p><strong class=\"example\">Example 1:</strong></p>\r\n\r\n<pre>\r\n<strong>Input:</strong> grid = [[1,1,1],[1,0,1],[1,1,1]]\r\n<strong>Output:</strong> 9\r\n</pre>\r\n\r\n<p><strong class=\"example\">Example 2:</strong></p>\r\n\r\n<pre>\r\n<strong>Input:</strong> grid = [[1,1,0,0]]\r\n<strong>Output:</strong> 1\r\n</pre>\r\n\r\n<p>&nbsp;</p>\r\n<p><strong>Constraints:</strong></p>\r\n\r\n<ul>\r\n\t<li><code>1 &lt;= grid.length &lt;= 100</code></li>\r\n\t<li><code>1 &lt;= grid[0].length &lt;= 100</code></li>\r\n\t<li><code>grid[i][j]</code> is <code>0</code> or <code>1</code></li>\r\n</ul>", "difficulty": "Medium", "questionFrontendId": "1139", "questionId": "1239", "questionTitle": "Largest 1-Bordered Square", "questionTitleSlug": "largest-1-bordered-square", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"23.7K\", \"totalSubmission\": \"47.3K\", \"totalAcceptedRaw\": 23721, \"totalSubmissionRaw\": 47292, \"acRate\": \"50.2%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Dynamic Programming", "slug": "dynamic-programming" }, { "name": "Matrix", "slug": "matrix" } ] } } }
1139
Medium
[ "Given a 2D grid of 0s and 1s, return the number of elements in the largest square subgrid that has all 1s on its border, or 0 if such a subgrid doesn't exist in the grid.\n\n \nExample 1:\n\nInput: grid = [[1,1,1],[1,0,1],[1,1,1]]\nOutput: 9\n\n\nExample 2:\n\nInput: grid = [[1,1,0,0]]\nOutput: 1\n\n\n \nConstrain...
[ { "hash": -5986589111047740000, "runtime": "101ms", "solution": "class Solution(object):\n def largest1BorderedSquare(self, grid):\n \"\"\"\n :type grid: List[List[int]]\n :rtype: int\n \"\"\"\n \n ROWS = len(grid)\n COLS = len(grid[0])\n \n ...
class Solution(object): def largest1BorderedSquare(self, grid): """ :type grid: List[List[int]] :rtype: int """
None
None
None
None
None
None
50.2
Level 3
Hi
count-nice-pairs-in-an-array
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an array <code>nums</code> that consists of non-negative integers. Let us define <code>rev(x)</code> as the reverse of the non-negative integer <code>x</code>. For example, <code>rev(123) = 321</code>, and <code>rev(120) = 21</code>. A pair of indices <code>(i, j)</code> is <strong>nice</strong> if it satisfies all of the following conditions:</p>\n\n<ul>\n\t<li><code>0 &lt;= i &lt; j &lt; nums.length</code></li>\n\t<li><code>nums[i] + rev(nums[j]) == nums[j] + rev(nums[i])</code></li>\n</ul>\n\n<p>Return <em>the number of nice pairs of indices</em>. Since that number can be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [42,11,1,97]\n<strong>Output:</strong> 2\n<strong>Explanation:</strong> The two pairs are:\n - (0,3) : 42 + rev(97) = 42 + 79 = 121, 97 + rev(42) = 97 + 24 = 121.\n - (1,2) : 11 + rev(1) = 11 + 1 = 12, 1 + rev(11) = 1 + 11 = 12.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [13,10,35,24,76]\n<strong>Output:</strong> 4\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "1814", "questionId": "1925", "questionTitle": "Count Nice Pairs in an Array", "questionTitleSlug": "count-nice-pairs-in-an-array", "similarQuestions": "[{\"title\": \"Number of Pairs of Interchangeable Rectangles\", \"titleSlug\": \"number-of-pairs-of-interchangeable-rectangles\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Count Number of Bad Pairs\", \"titleSlug\": \"count-number-of-bad-pairs\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Number of Pairs Satisfying Inequality\", \"titleSlug\": \"number-of-pairs-satisfying-inequality\", \"difficulty\": \"Hard\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"100.3K\", \"totalSubmission\": \"202.7K\", \"totalAcceptedRaw\": 100269, \"totalSubmissionRaw\": 202721, \"acRate\": \"49.5%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" }, { "name": "Math", "slug": "math" }, { "name": "Counting", "slug": "counting" } ] } } }
1814
Medium
[ "You are given an array nums that consists of non-negative integers. Let us define rev(x) as the reverse of the non-negative integer x. For example, rev(123) = 321, and rev(120) = 21. A pair of indices (i, j) is nice if it satisfies all of the following conditions:\n\n\n\t0 <= i < j < nums.length\n\tnums[i] + rev(n...
[ { "hash": -184751089875370880, "runtime": "1088ms", "solution": "class Solution(object):\n def countNicePairs(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n def rev(w):\n z=str(w)\n n=len(z)\n res=0\n ...
class Solution(object): def countNicePairs(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
49.5
Level 3
Hi
count-lattice-points-inside-a-circle
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a 2D integer array <code>circles</code> where <code>circles[i] = [x<sub>i</sub>, y<sub>i</sub>, r<sub>i</sub>]</code> represents the center <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and radius <code>r<sub>i</sub></code> of the <code>i<sup>th</sup></code> circle drawn on a grid, return <em>the <strong>number of lattice points</strong> </em><em>that are present inside <strong>at least one</strong> circle</em>.</p>\n\n<p><strong>Note:</strong></p>\n\n<ul>\n\t<li>A <strong>lattice point</strong> is a point with integer coordinates.</li>\n\t<li>Points that lie <strong>on the circumference of a circle</strong> are also considered to be inside it.</li>\n</ul>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/03/02/exa-11.png\" style=\"width: 300px; height: 300px;\" />\n<pre>\n<strong>Input:</strong> circles = [[2,2,1]]\n<strong>Output:</strong> 5\n<strong>Explanation:</strong>\nThe figure above shows the given circle.\nThe lattice points present inside the circle are (1, 2), (2, 1), (2, 2), (2, 3), and (3, 2) and are shown in green.\nOther points such as (1, 1) and (1, 3), which are shown in red, are not considered inside the circle.\nHence, the number of lattice points present inside at least one circle is 5.</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/03/02/exa-22.png\" style=\"width: 300px; height: 300px;\" />\n<pre>\n<strong>Input:</strong> circles = [[2,2,2],[3,4,1]]\n<strong>Output:</strong> 16\n<strong>Explanation:</strong>\nThe figure above shows the given circles.\nThere are exactly 16 lattice points which are present inside at least one circle. \nSome of them are (0, 2), (2, 0), (2, 4), (3, 2), and (4, 4).\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= circles.length &lt;= 200</code></li>\n\t<li><code>circles[i].length == 3</code></li>\n\t<li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 100</code></li>\n\t<li><code>1 &lt;= r<sub>i</sub> &lt;= min(x<sub>i</sub>, y<sub>i</sub>)</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2249", "questionId": "2332", "questionTitle": "Count Lattice Points Inside a Circle", "questionTitleSlug": "count-lattice-points-inside-a-circle", "similarQuestions": "[{\"title\": \"Queries on Number of Points Inside a Circle\", \"titleSlug\": \"queries-on-number-of-points-inside-a-circle\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"21.5K\", \"totalSubmission\": \"41.7K\", \"totalAcceptedRaw\": 21519, \"totalSubmissionRaw\": 41701, \"acRate\": \"51.6%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" }, { "name": "Math", "slug": "math" }, { "name": "Geometry", "slug": "geometry" }, { "name": "Enumeration", "slug": "enumeration" } ] } } }
2249
Medium
[ "Given a 2D integer array circles where circles[i] = [xi, yi, ri] represents the center (xi, yi) and radius ri of the ith circle drawn on a grid, return the number of lattice points that are present inside at least one circle.\n\nNote:\n\n\n\tA lattice point is a point with integer coordinates.\n\tPoints that lie o...
[ { "hash": -3808114713120577500, "runtime": "1157ms", "solution": "class Solution(object):\n def countLatticePoints(self, circles):\n \"\"\"\n :type circles: List[List[int]]\n :rtype: int\n \"\"\"\n ans = set()\n max_r = max(r for _, _, r in circles)\n ...
class Solution(object): def countLatticePoints(self, circles): """ :type circles: List[List[int]] :rtype: int """
None
None
None
None
None
None
51.6
Level 3
Hi
partition-array-such-that-maximum-difference-is-k
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. You may partition <code>nums</code> into one or more <strong>subsequences</strong> such that each element in <code>nums</code> appears in <strong>exactly</strong> one of the subsequences.</p>\n\n<p>Return <em>the <strong>minimum </strong>number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is <strong>at most</strong> </em><code>k</code><em>.</em></p>\n\n<p>A <strong>subsequence</strong> is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [3,6,1,2,5], k = 2\n<strong>Output:</strong> 2\n<strong>Explanation:</strong>\nWe can partition nums into the two subsequences [3,1,2] and [6,5].\nThe difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2.\nThe difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1.\nSince two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,2,3], k = 1\n<strong>Output:</strong> 2\n<strong>Explanation:</strong>\nWe can partition nums into the two subsequences [1,2] and [3].\nThe difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1.\nThe difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0.\nSince two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3].\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [2,2,4,5], k = 0\n<strong>Output:</strong> 3\n<strong>Explanation:</strong>\nWe can partition nums into the three subsequences [2,2], [4], and [5].\nThe difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0.\nThe difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0.\nThe difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0.\nSince three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>\n\t<li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2294", "questionId": "2387", "questionTitle": "Partition Array Such That Maximum Difference Is K", "questionTitleSlug": "partition-array-such-that-maximum-difference-is-k", "similarQuestions": "[{\"title\": \"Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit\", \"titleSlug\": \"longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Maximum Beauty of an Array After Applying Operation\", \"titleSlug\": \"maximum-beauty-of-an-array-after-applying-operation\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"36.8K\", \"totalSubmission\": \"50.3K\", \"totalAcceptedRaw\": 36760, \"totalSubmissionRaw\": 50256, \"acRate\": \"73.1%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Greedy", "slug": "greedy" }, { "name": "Sorting", "slug": "sorting" } ] } } }
2294
Medium
[ "You are given an integer array nums and an integer k. You may partition nums into one or more subsequences such that each element in nums appears in exactly one of the subsequences.\n\nReturn the minimum number of subsequences needed such that the difference between the maximum and minimum values in each subsequen...
[ { "hash": 5335998153193593000, "runtime": "749ms", "solution": "class Solution(object):\n def partitionArray(self, nums, k):\n nums.sort()\n ans = 1\n x = nums[0]\n\n for i in range(1,len(nums)):\n if nums[i] - x > k:\n x = nums[i]\n ...
class Solution(object): def partitionArray(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """
None
None
None
None
None
None
73.1
Level 2
Hi
maximum-star-sum-of-a-graph
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There is an undirected graph consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node.</p>\n\n<p>You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i.</sub></code></p>\n\n<p>A <strong>star graph</strong> is a subgraph of the given graph having a center node containing <code>0</code> or more neighbors. In other words, it is a subset of edges of the given graph such that there exists a common node for all edges.</p>\n\n<p>The image below shows star graphs with <code>3</code> and <code>4</code> neighbors respectively, centered at the blue node.</p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/11/07/max-star-sum-descdrawio.png\" style=\"width: 400px; height: 179px;\" />\n<p>The <strong>star sum</strong> is the sum of the values of all the nodes present in the star graph.</p>\n\n<p>Given an integer <code>k</code>, return <em>the <strong>maximum star sum</strong> of a star graph containing <strong>at most</strong> </em><code>k</code><em> edges.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/11/07/max-star-sum-example1drawio.png\" style=\"width: 300px; height: 291px;\" />\n<pre>\n<strong>Input:</strong> vals = [1,2,3,4,10,-10,-20], edges = [[0,1],[1,2],[1,3],[3,4],[3,5],[3,6]], k = 2\n<strong>Output:</strong> 16\n<strong>Explanation:</strong> The above diagram represents the input graph.\nThe star graph with the maximum star sum is denoted by blue. It is centered at 3 and includes its neighbors 1 and 4.\nIt can be shown it is not possible to get a star graph with a sum greater than 16.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> vals = [-5], edges = [], k = 0\n<strong>Output:</strong> -5\n<strong>Explanation:</strong> There is only one possible star graph, which is node 0 itself.\nHence, we return -5.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>n == vals.length</code></li>\n\t<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>\n\t<li><code>-10<sup>4</sup> &lt;= vals[i] &lt;= 10<sup>4</sup></code></li>\n\t<li><code>0 &lt;= edges.length &lt;= min(n * (n - 1) / 2</code><code>, 10<sup>5</sup>)</code></li>\n\t<li><code>edges[i].length == 2</code></li>\n\t<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt;= n - 1</code></li>\n\t<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>\n\t<li><code>0 &lt;= k &lt;= n - 1</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2497", "questionId": "2590", "questionTitle": "Maximum Star Sum of a Graph", "questionTitleSlug": "maximum-star-sum-of-a-graph", "similarQuestions": "[{\"title\": \"Number Of Ways To Reconstruct A Tree\", \"titleSlug\": \"number-of-ways-to-reconstruct-a-tree\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Find Center of Star Graph\", \"titleSlug\": \"find-center-of-star-graph\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"16K\", \"totalSubmission\": \"41.4K\", \"totalAcceptedRaw\": 15974, \"totalSubmissionRaw\": 41350, \"acRate\": \"38.6%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Greedy", "slug": "greedy" }, { "name": "Graph", "slug": "graph" }, { "name": "Sorting", "slug": "sorting" }, { "name": "Heap (Priority Queue)", "slug": "heap-priority-queue" } ] } } }
2497
Medium
[ "There is an undirected graph consisting of n nodes numbered from 0 to n - 1. You are given a 0-indexed integer array vals of length n where vals[i] denotes the value of the ith node.\n\nYou are also given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nod...
[ { "hash": -1004396627712004700, "runtime": "1157ms", "solution": "class Solution(object):\n def maxStarSum(self, vals, edges, k):\n \"\"\"\n :type vals: List[int]\n :type edges: List[List[int]]\n :type k: int\n :rtype: int\n \"\"\"\n adjacencyList = de...
class Solution(object): def maxStarSum(self, vals, edges, k): """ :type vals: List[int] :type edges: List[List[int]] :type k: int :rtype: int """
None
None
None
None
None
None
38.6
Level 4
Hi
the-number-of-beautiful-subsets
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p>\n\n<p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p>\n\n<p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p>\n\n<p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [2,4,6], k = 2\n<strong>Output:</strong> 4\n<strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].\nIt can be proved that there are only 4 beautiful subsets in the array [2,4,6].\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1], k = 1\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> The beautiful subset of the array nums is [1].\nIt can be proved that there is only 1 beautiful subset in the array [1].\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 20</code></li>\n\t<li><code>1 &lt;= nums[i], k &lt;= 1000</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2597", "questionId": "2696", "questionTitle": "The Number of Beautiful Subsets", "questionTitleSlug": "the-number-of-beautiful-subsets", "similarQuestions": "[{\"title\": \"Construct the Lexicographically Largest Valid Sequence\", \"titleSlug\": \"construct-the-lexicographically-largest-valid-sequence\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"18K\", \"totalSubmission\": \"57.4K\", \"totalAcceptedRaw\": 17969, \"totalSubmissionRaw\": 57404, \"acRate\": \"31.3%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Dynamic Programming", "slug": "dynamic-programming" }, { "name": "Backtracking", "slug": "backtracking" } ] } } }
2597
Medium
[ "You are given an array nums of positive integers and a positive integer k.\n\nA subset of nums is beautiful if it does not contain two integers with an absolute difference equal to k.\n\nReturn the number of non-empty beautiful subsets of the array nums.\n\nA subset of nums is an array that can be obtained by dele...
[ { "hash": 6924934195564993000, "runtime": "3898ms", "solution": "class Solution(object):\n def beautifulSubsets(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n self.ans = 0\n def backtracking(nums, path):\n ...
class Solution(object): def beautifulSubsets(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """
None
None
None
None
None
None
31.3
Level 4
Hi
plates-between-candles
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There is a long table with a line of plates and candles arranged on top of it. You are given a <strong>0-indexed</strong> string <code>s</code> consisting of characters <code>&#39;*&#39;</code> and <code>&#39;|&#39;</code> only, where a <code>&#39;*&#39;</code> represents a <strong>plate</strong> and a <code>&#39;|&#39;</code> represents a <strong>candle</strong>.</p>\n\n<p>You are also given a <strong>0-indexed</strong> 2D integer array <code>queries</code> where <code>queries[i] = [left<sub>i</sub>, right<sub>i</sub>]</code> denotes the <strong>substring</strong> <code>s[left<sub>i</sub>...right<sub>i</sub>]</code> (<strong>inclusive</strong>). For each query, you need to find the <strong>number</strong> of plates <strong>between candles</strong> that are <strong>in the substring</strong>. A plate is considered <strong>between candles</strong> if there is at least one candle to its left <strong>and</strong> at least one candle to its right <strong>in the substring</strong>.</p>\n\n<ul>\n\t<li>For example, <code>s = &quot;||**||**|*&quot;</code>, and a query <code>[3, 8]</code> denotes the substring <code>&quot;*||<strong><u>**</u></strong>|&quot;</code>. The number of plates between candles in this substring is <code>2</code>, as each of the two plates has at least one candle <strong>in the substring</strong> to its left <strong>and</strong> right.</li>\n</ul>\n\n<p>Return <em>an integer array</em> <code>answer</code> <em>where</em> <code>answer[i]</code> <em>is the answer to the</em> <code>i<sup>th</sup></code> <em>query</em>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n<img alt=\"ex-1\" src=\"https://assets.leetcode.com/uploads/2021/10/04/ex-1.png\" style=\"width: 400px; height: 134px;\" />\n<pre>\n<strong>Input:</strong> s = &quot;**|**|***|&quot;, queries = [[2,5],[5,9]]\n<strong>Output:</strong> [2,3]\n<strong>Explanation:</strong>\n- queries[0] has two plates between candles.\n- queries[1] has three plates between candles.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n<img alt=\"ex-2\" src=\"https://assets.leetcode.com/uploads/2021/10/04/ex-2.png\" style=\"width: 600px; height: 193px;\" />\n<pre>\n<strong>Input:</strong> s = &quot;***|**|*****|**||**|*&quot;, queries = [[1,17],[4,5],[14,17],[5,11],[15,16]]\n<strong>Output:</strong> [9,0,0,0,0]\n<strong>Explanation:</strong>\n- queries[0] has nine plates between candles.\n- The other queries have zero plates between candles.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>3 &lt;= s.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>s</code> consists of <code>&#39;*&#39;</code> and <code>&#39;|&#39;</code> characters.</li>\n\t<li><code>1 &lt;= queries.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>queries[i].length == 2</code></li>\n\t<li><code>0 &lt;= left<sub>i</sub> &lt;= right<sub>i</sub> &lt; s.length</code></li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "2055", "questionId": "2165", "questionTitle": "Plates Between Candles", "questionTitleSlug": "plates-between-candles", "similarQuestions": "[{\"title\": \"Find First and Last Position of Element in Sorted Array\", \"titleSlug\": \"find-first-and-last-position-of-element-in-sorted-array\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Can Make Palindrome from Substring\", \"titleSlug\": \"can-make-palindrome-from-substring\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"37.1K\", \"totalSubmission\": \"83.5K\", \"totalAcceptedRaw\": 37098, \"totalSubmissionRaw\": 83516, \"acRate\": \"44.4%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "String", "slug": "string" }, { "name": "Binary Search", "slug": "binary-search" }, { "name": "Prefix Sum", "slug": "prefix-sum" } ] } } }
2055
Medium
[ "There is a long table with a line of plates and candles arranged on top of it. You are given a 0-indexed string s consisting of characters '*' and '|' only, where a '*' represents a plate and a '|' represents a candle.\n\nYou are also given a 0-indexed 2D integer array queries where queries[i] = [lefti, righti] de...
[ { "hash": -7393439655977928000, "runtime": "1509ms", "solution": "class Solution(object):\n def platesBetweenCandles(self, s, queries):\n \"\"\"\n :type s: str\n :type queries: List[List[int]]\n :rtype: List[int]\n \"\"\"\n n = len(s)\n psum,sum = [0]*...
class Solution(object): def platesBetweenCandles(self, s, queries): """ :type s: str :type queries: List[List[int]] :rtype: List[int] """
None
None
None
None
None
None
44.4
Level 4
Hi
buy-two-chocolates
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an integer array <code>prices</code> representing the prices of various chocolates in a store. You are also given a single integer <code>money</code>, which represents your initial amount of money.</p>\n\n<p>You must buy <strong>exactly</strong> two chocolates in such a way that you still have some <strong>non-negative</strong> leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.</p>\n\n<p>Return <em>the amount of money you will have leftover after buying the two chocolates</em>. If there is no way for you to buy two chocolates without ending up in debt, return <code>money</code>. Note that the leftover must be non-negative.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> prices = [1,2,2], money = 3\n<strong>Output:</strong> 0\n<strong>Explanation:</strong> Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> prices = [3,2,3], money = 3\n<strong>Output:</strong> 3\n<strong>Explanation:</strong> You cannot buy 2 chocolates without going in debt, so we return 3.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>2 &lt;= prices.length &lt;= 50</code></li>\n\t<li><code>1 &lt;= prices[i] &lt;= 100</code></li>\n\t<li><code>1 &lt;= money &lt;= 100</code></li>\n</ul>\n", "difficulty": "Easy", "questionFrontendId": "2706", "questionId": "2756", "questionTitle": "Buy Two Chocolates", "questionTitleSlug": "buy-two-chocolates", "similarQuestions": "[]", "stats": "{\"totalAccepted\": \"41.8K\", \"totalSubmission\": \"66.3K\", \"totalAcceptedRaw\": 41840, \"totalSubmissionRaw\": 66326, \"acRate\": \"63.1%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Sorting", "slug": "sorting" } ] } } }
2706
Easy
[ "You are given an integer array prices representing the prices of various chocolates in a store. You are also given a single integer money, which represents your initial amount of money.\n\nYou must buy exactly two chocolates in such a way that you still have some non-negative leftover money. You would like to mini...
[ { "hash": -3917673152941805000, "runtime": "43ms", "solution": "class Solution(object):\n def buyChoco(self, prices, money):\n prices.sort()\n prices.sort()\n for i in range(2):\n money = money - prices[i]\n if money < 0:\n return money + prices[0] + ...
class Solution(object): def buyChoco(self, prices, money): """ :type prices: List[int] :type money: int :rtype: int """
None
None
None
None
None
None
63.1
Level 1
Hi
open-the-lock
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: <code>&#39;0&#39;, &#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;, &#39;5&#39;, &#39;6&#39;, &#39;7&#39;, &#39;8&#39;, &#39;9&#39;</code>. The wheels can rotate freely and wrap around: for example we can turn <code>&#39;9&#39;</code> to be <code>&#39;0&#39;</code>, or <code>&#39;0&#39;</code> to be <code>&#39;9&#39;</code>. Each move consists of turning one wheel one slot.</p>\n\n<p>The lock initially starts at <code>&#39;0000&#39;</code>, a string representing the state of the 4 wheels.</p>\n\n<p>You are given a list of <code>deadends</code> dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.</p>\n\n<p>Given a <code>target</code> representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> deadends = [&quot;0201&quot;,&quot;0101&quot;,&quot;0102&quot;,&quot;1212&quot;,&quot;2002&quot;], target = &quot;0202&quot;\n<strong>Output:</strong> 6\n<strong>Explanation:</strong> \nA sequence of valid moves would be &quot;0000&quot; -&gt; &quot;1000&quot; -&gt; &quot;1100&quot; -&gt; &quot;1200&quot; -&gt; &quot;1201&quot; -&gt; &quot;1202&quot; -&gt; &quot;0202&quot;.\nNote that a sequence like &quot;0000&quot; -&gt; &quot;0001&quot; -&gt; &quot;0002&quot; -&gt; &quot;0102&quot; -&gt; &quot;0202&quot; would be invalid,\nbecause the wheels of the lock become stuck after the display becomes the dead end &quot;0102&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> deadends = [&quot;8888&quot;], target = &quot;0009&quot;\n<strong>Output:</strong> 1\n<strong>Explanation:</strong> We can turn the last wheel in reverse to move from &quot;0000&quot; -&gt; &quot;0009&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> deadends = [&quot;8887&quot;,&quot;8889&quot;,&quot;8878&quot;,&quot;8898&quot;,&quot;8788&quot;,&quot;8988&quot;,&quot;7888&quot;,&quot;9888&quot;], target = &quot;8888&quot;\n<strong>Output:</strong> -1\n<strong>Explanation:</strong> We cannot reach the target without getting stuck.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= deadends.length &lt;= 500</code></li>\n\t<li><code>deadends[i].length == 4</code></li>\n\t<li><code>target.length == 4</code></li>\n\t<li>target <strong>will not be</strong> in the list <code>deadends</code>.</li>\n\t<li><code>target</code> and <code>deadends[i]</code> consist of digits only.</li>\n</ul>\n", "difficulty": "Medium", "questionFrontendId": "752", "questionId": "753", "questionTitle": "Open the Lock", "questionTitleSlug": "open-the-lock", "similarQuestions": "[{\"title\": \"Reachable Nodes With Restrictions\", \"titleSlug\": \"reachable-nodes-with-restrictions\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "stats": "{\"totalAccepted\": \"217.6K\", \"totalSubmission\": \"389.5K\", \"totalAcceptedRaw\": 217635, \"totalSubmissionRaw\": 389546, \"acRate\": \"55.9%\"}", "topicTags": [ { "name": "Array", "slug": "array" }, { "name": "Hash Table", "slug": "hash-table" }, { "name": "String", "slug": "string" }, { "name": "Breadth-First Search", "slug": "breadth-first-search" } ] } } }
752
Medium
[ "You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn '9' to be '0', or '0' to be '9'. Each move consists of turning one wheel one slot.\n\nThe lock initially starts at...
[ { "hash": 1697446389680873500, "runtime": "1440ms", "solution": "class Solution:\n def openLock(self, deadends: List[str], target: str) -> int:\n if '0000' in deadends:\n return -1\n res = float('inf')\n \n visit = set(deadends)\n q = collections.deque()\...
class Solution(object): def openLock(self, deadends, target): """ :type deadends: List[str] :type target: str :rtype: int """
None
None
None
None
None
None
55.9
Level 3
Hi