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
diameter-of-binary-tree
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given the <code>root</code> of a binary tree, return <em>the length of the <strong>diameter</strong> of the tree</em>.</p>\n\n<p>The <strong>diameter</strong> of a binary tree is the <strong>length</strong> of the longest path bet...
543
Easy
[ "Given the root of a binary tree, return the length of the diameter of the tree.\n\nThe diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.\n\nThe length of a path between two nodes is represented by the number of edges between...
[ { "hash": 3207859012760275500, "runtime": "319ms", "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 d...
# 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 diameterOfBinaryTree(self, root): """ :type root: TreeNode :rtyp...
None
None
None
None
None
None
58.4
Level 1
Hi
implement-rand10-using-rand7
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given the <strong>API</strong> <code>rand7()</code> that generates a uniform random integer in the range <code>[1, 7]</code>, write a function <code>rand10()</code> that generates a uniform random integer in the range <code>[1, 10...
470
Medium
[ "" ]
[ { "hash": 8427271188024453000, "runtime": "277ms", "solution": "# The rand7() API is already defined for you.\n# def rand7():\n# @return a random integer in the range 1 to 7\n\nclass Solution(object):\n def rand10(self):\n\n # [1, 49]\n\n while True:\n num = (rand7() - 1) * 7...
[470] Implement Rand10() Using Rand7() is on the run... json from response parse failed, please open a new issue at: https://github.com/clearloop/leetcode-cli/.
None
None
None
None
None
None
45.9
Level 4
Hi
minimum-additions-to-make-valid-string
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a string <code>word</code> to which you can insert letters &quot;a&quot;, &quot;b&quot; or &quot;c&quot; anywhere and any number of times, return <em>the minimum number of letters that must be inserted so that <code>word</co...
2645
Medium
[ "Given a string word to which you can insert letters \"a\", \"b\" or \"c\" anywhere and any number of times, return the minimum number of letters that must be inserted so that word becomes valid.\n\nA string is called valid if it can be formed by concatenating the string \"abc\" several times.\n\n \nExample 1:\n\nI...
[ { "hash": 7393005780563854000, "runtime": "19ms", "solution": "#use a array to store the valid pattern\n#use a pointer i to indicate the current position of valid pattern\n#use a pointer j to indicate the current position of word\n#check if [i] match [j], if no, increase i and increase the result by 1, ...
class Solution(object): def addMinimum(self, word): """ :type word: str :rtype: int """
None
None
None
None
None
None
49.5
Level 3
Hi
matrix-similarity-after-cyclic-shifts
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>mat</code> and an integer <code>k</code>. You have to cyclically <strong>right</strong> shift <strong>odd</strong> indexed rows <code>k</code> time...
2946
Easy
[ "You are given a 0-indexed m x n integer matrix mat and an integer k. You have to cyclically right shift odd indexed rows k times and cyclically left shift even indexed rows k times.\n\nReturn true if the initial and final matrix are exactly the same and false otherwise.\n\n \nExample 1:\n\nInput: mat = [[1,2,1,2],...
[ { "hash": 2072218976159894300, "runtime": "108ms", "solution": "class Solution(object):\n def areSimilar(self, mat, k):\n \"\"\"\n :type mat: List[List[int]]\n :type k: int\n :rtype: bool\n \"\"\"\n realTemp=[row[:] for row in mat]\n def leftShift(arra...
class Solution(object): def areSimilar(self, mat, k): """ :type mat: List[List[int]] :type k: int :rtype: bool """
None
None
None
None
None
None
56.6
Level 1
Hi
sum-of-numbers-with-units-digit-k
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given two integers <code>num</code> and <code>k</code>, consider a set of positive integers with the following properties:</p>\n\n<ul>\n\t<li>The units digit of each integer is <code>k</code>.</li>\n\t<li>The sum of the integers i...
2310
Medium
[ "Given two integers num and k, consider a set of positive integers with the following properties:\n\n\n\tThe units digit of each integer is k.\n\tThe sum of the integers is num.\n\n\nReturn the minimum possible size of such a set, or -1 if no such set exists.\n\nNote:\n\n\n\tThe set can contain multiple instances o...
[ { "hash": 2757575175410871300, "runtime": "22ms", "solution": "class Solution(object):\n def minimumNumbers(self, num, k):\n if num==0:\n return 0\n if k==0:\n if num%10==0:\n return 1\n else:\n return -1\n i=1\n ...
class Solution(object): def minimumNumbers(self, num, k): """ :type num: int :type k: int :rtype: int """
None
None
None
None
None
None
26.4
Level 4
Hi
find-three-consecutive-integers-that-sum-to-a-given-number
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer <code>num</code>, return <em>three consecutive integers (as a sorted array)</em><em> that <strong>sum</strong> to </em><code>num</code>. If <code>num</code> cannot be expressed as the sum of three consecutive inte...
2177
Medium
[ "Given an integer num, return three consecutive integers (as a sorted array) that sum to num. If num cannot be expressed as the sum of three consecutive integers, return an empty array.\n\n \nExample 1:\n\nInput: num = 33\nOutput: [10,11,12]\nExplanation: 33 can be expressed as 10 + 11 + 12 = 33.\n10, 11, 12 are 3 ...
[ { "hash": -8082068775060322000, "runtime": "18ms", "solution": "class Solution(object):\n def sumOfThree(self, num):\n \"\"\"\n :type num: int\n :rtype: List[int]\n \"\"\"\n L =[]\n if num%3==0:\n n=num//3\n L.append(n-1)\n L....
class Solution(object): def sumOfThree(self, num): """ :type num: int :rtype: List[int] """
None
None
None
None
None
None
64.4
Level 2
Hi
count-k-subsequences-of-a-string-with-maximum-beauty
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a string <code>s</code> and an integer <code>k</code>.</p>\n\n<p>A <strong>k-subsequence</strong> is a <strong>subsequence</strong> of <code>s</code>, having length <code>k</code>, and all its characters are <strong>...
2842
Hard
[ "You are given a string s and an integer k.\n\nA k-subsequence is a subsequence of s, having length k, and all its characters are unique, i.e., every character occurs once.\n\nLet f(c) denote the number of times the character c occurs in s.\n\nThe beauty of a k-subsequence is the sum of f(c) for every character c i...
[ { "hash": 6246337986593854000, "runtime": "84ms", "solution": "class Solution(object):\n def countKSubsequencesWithMaxBeauty(self, s, k):\n \"\"\"\n :type s: str\n :type k: int\n :rtype: int\n \"\"\"\n if k > 26:\n return 0\n cnt = [0] * 26\...
class Solution(object): def countKSubsequencesWithMaxBeauty(self, s, k): """ :type s: str :type k: int :rtype: int """
None
None
None
None
None
None
28.1
Level 5
Hi
snapshot-array
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Implement a SnapshotArray that supports the following interface:</p>\n\n<ul>\n\t<li><code>SnapshotArray(int length)</code> initializes an array-like data structure with the given length. <strong>Initially, each element equals 0</s...
1146
Medium
[ "Implement a SnapshotArray that supports the following interface:\n\n\n\tSnapshotArray(int length) initializes an array-like data structure with the given length. Initially, each element equals 0.\n\tvoid set(index, val) sets the element at the given index to be equal to val.\n\tint snap() takes a snapshot of the a...
[ { "hash": -7342427708513537000, "runtime": "801ms", "solution": "\"\"\"\nex: [0, 0]\nfirst index = snap_id\nsecond index = value at snap_id\n[[[0,0]], [[0,0]], [[0, 0]]]\n\"\"\"\nclass SnapshotArray(object):\n\n def __init__(self, length):\n \"\"\"\n :type length: int\n \"\"\"\n ...
class SnapshotArray(object): def __init__(self, length): """ :type length: int """ def set(self, index, val): """ :type index: int :type val: int :rtype: None """ def snap(self): """ :rtype: int """ ...
None
None
None
None
None
None
37.2
Level 4
Hi
all-possible-full-binary-trees
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer <code>n</code>, return <em>a list of all possible <strong>full binary trees</strong> with</em> <code>n</code> <em>nodes</em>. Each node of each tree in the answer must have <code>Node.val == 0</code>.</p>\n\n<p>Ea...
894
Medium
[ "Given an integer n, return a list of all possible full binary trees with n nodes. Each node of each tree in the answer must have Node.val == 0.\n\nEach element of the answer is the root node of one possible tree. You may return the final list of trees in any order.\n\nA full binary tree is a binary tree where each...
[ { "hash": 7749415820072024000, "runtime": "350ms", "solution": "# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n ALLOWED_VALUES_N = [1...
# 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 allPossibleFBT(self, n): """ :type n: int :rtype: List[TreeNode]...
None
None
None
None
None
None
82.8
Level 2
Hi
rearrange-array-elements-by-sign
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length consisting of an <strong>equal</strong> number of positive and negative integers.</p>\n\n<p>You should <strong>rearrange</s...
2149
Medium
[ "You are given a 0-indexed integer array nums of even length consisting of an equal number of positive and negative integers.\n\nYou should rearrange the elements of nums such that the modified array follows the given conditions:\n\n\n\tEvery consecutive pair of integers have opposite signs.\n\tFor all integers wit...
[ { "hash": 244854689079622460, "runtime": "1166ms", "solution": "class Solution(object):\n def rearrangeArray(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: List[int]\n \"\"\"\n pos=[]\n neg=[]\n for i in range (len(nums)):\n if (num...
class Solution(object): def rearrangeArray(self, nums): """ :type nums: List[int] :rtype: List[int] """
None
None
None
None
None
None
82
Level 2
Hi
occurrences-after-bigram
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given two strings <code>first</code> and <code>second</code>, consider occurrences in some text of the form <code>&quot;first second third&quot;</code>, where <code>second</code> comes immediately after <code>first</code>, and <co...
1078
Easy
[ "Given two strings first and second, consider occurrences in some text of the form \"first second third\", where second comes immediately after first, and third comes immediately after second.\n\nReturn an array of all the words third for each occurrence of \"first second third\".\n\n \nExample 1:\nInput: text = \"...
[ { "hash": -1908787807607066400, "runtime": "34ms", "solution": "class Solution:\n def findOcurrences(self, text: str, first: str, second: str) -> List[str]:\n words = text.split(\" \")\n \n res = []\n \n for i in range(len(words) - 2):\n if words[i] == fi...
class Solution(object): def findOcurrences(self, text, first, second): """ :type text: str :type first: str :type second: str :rtype: List[str] """
None
None
None
None
None
None
63.4
Level 1
Hi
wiggle-subsequence
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A <strong>wiggle sequence</strong> is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequenc...
376
Medium
[ "A wiggle sequence is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequences.\n\n\n\tF...
[ { "hash": -8866189836585281000, "runtime": "10ms", "solution": "class Solution(object):\n def wiggleMaxLength(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n # consider the case 1, 4, 9, 8, 9, 8, 9, 8\n wiggle_up_before = [1 for num in ...
class Solution(object): def wiggleMaxLength(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
48.4
Level 3
Hi
second-minimum-node-in-a-binary-tree
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly <code>two</code> or <code>zero</code> sub-node. If the node has two sub-nodes, then this node&#39;s va...
671
Easy
[ "Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. More formally, the property root.val = min(root.left.val, root.ri...
[ { "hash": -9136869973805939000, "runtime": "8ms", "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 fi...
# 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 findSecondMinimumValue(self, root): """ :type root: TreeNode :rt...
None
None
None
None
None
None
44.3
Level 1
Hi
maximum-number-of-weeks-for-which-you-can-work
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There are <code>n</code> projects numbered from <code>0</code> to <code>n - 1</code>. You are given an integer array <code>milestones</code> where each <code>milestones[i]</code> denotes the number of milestones the <code>i<sup>th...
1953
Medium
[ "There are n projects numbered from 0 to n - 1. You are given an integer array milestones where each milestones[i] denotes the number of milestones the ith project has.\n\nYou can work on the projects following these two rules:\n\n\n\tEvery week, you will finish exactly one milestone of one project. You must work e...
[ { "hash": -7995577254731603000, "runtime": "555ms", "solution": "class Solution(object):\n def numberOfWeeks(self, milestones):\n mx = max(milestones)\n sm = sum(milestones) - mx\n if sm >= mx:\n return sm+mx\n else: \n return 2*sm+1" }, { "ha...
class Solution(object): def numberOfWeeks(self, milestones): """ :type milestones: List[int] :rtype: int """
None
None
None
None
None
None
40
Level 4
Hi
rotated-digits
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>An integer <code>x</code> is a <strong>good</strong> if after rotating each digit individually by 180 degrees, we get a valid number that is different from <code>x</code>. Each digit must be rotated - we cannot choose to leave it ...
788
Medium
[ "An integer x is a good if after rotating each digit individually by 180 degrees, we get a valid number that is different from x. Each digit must be rotated - we cannot choose to leave it alone.\n\nA number is valid if each digit remains a digit after rotation. For example:\n\n\n\t0, 1, and 8 rotate to themselves,\...
[ { "hash": -7804618773792240000, "runtime": "77ms", "solution": "class Solution(object):\n def rotatedDigits(self, n):\n def is_good(n):\n good = False\n while n > 0:\n d = n%10\n good = good or self.valid(n%10)\n if (d == 3 or ...
class Solution(object): def rotatedDigits(self, n): """ :type n: int :rtype: int """
None
None
None
None
None
None
56.4
Level 3
Hi
find-valid-matrix-given-row-and-column-sums
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given two arrays <code>rowSum</code> and <code>colSum</code> of non-negative integers where <code>rowSum[i]</code> is the sum of the elements in the <code>i<sup>th</sup></code> row and <code>colSum[j]</code> is the sum of ...
1605
Medium
[ "You are given two arrays rowSum and colSum of non-negative integers where rowSum[i] is the sum of the elements in the ith row and colSum[j] is the sum of the elements of the jth column of a 2D matrix. In other words, you do not know the elements of the matrix, but you do know the sums of each row and column.\n\nFi...
[ { "hash": 1615325031092348000, "runtime": "679ms", "solution": "class Solution(object):\n def restoreMatrix(self, rowSum, colSum):\n \"\"\"\n :type rowSum: List[int]\n :type colSum: List[int]\n :rtype: List[List[int]]\n \"\"\"\n \n out = [[0]*len(colSu...
class Solution(object): def restoreMatrix(self, rowSum, colSum): """ :type rowSum: List[int] :type colSum: List[int] :rtype: List[List[int]] """
None
None
None
None
None
None
77.5
Level 2
Hi
short-encoding-of-words
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A <strong>valid encoding</strong> of an array of <code>words</code> is any reference string <code>s</code> and array of indices <code>indices</code> such that:</p>\n\n<ul>\n\t<li><code>words.length == indices.length</code></li>\n\...
820
Medium
[ "A valid encoding of an array of words is any reference string s and array of indices indices such that:\n\n\n\twords.length == indices.length\n\tThe reference string s ends with the '#' character.\n\tFor each index indices[i], the substring of s starting from indices[i] and up to (but not including) the next '#' c...
[ { "hash": -1833032432883583200, "runtime": "3505ms", "solution": "class Solution(object):\n\n # def postfix\n def is_postfix(self, words, w):\n return any((word[-len(w):] == w for word in words))\n\n\n def minimumLengthEncoding(self, words):\n \"\"\"\n :type words: List[str...
class Solution(object): def minimumLengthEncoding(self, words): """ :type words: List[str] :rtype: int """
None
None
None
None
None
None
60.5
Level 2
Hi
bulb-switcher
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There are <code>n</code> bulbs that are initially off. You first turn on all the bulbs, then&nbsp;you turn off every second bulb.</p>\n\n<p>On the third round, you toggle every third bulb (turning on if it&#39;s off or turning off...
319
Medium
[ "There are n bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.\n\nOn the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb.\n\nRet...
[ { "hash": 5555519762057672000, "runtime": "17ms", "solution": "class Solution(object):\n def bulbSwitch(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n return int(n**0.5)" }, { "hash": -8836380008660326000, "runtime": "8ms", "solution": "c...
class Solution(object): def bulbSwitch(self, n): """ :type n: int :rtype: int """
None
None
None
None
None
None
52.5
Level 3
Hi
minimum-bit-flips-to-convert-number
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A <strong>bit flip</strong> of a number <code>x</code> is choosing a bit in the binary representation of <code>x</code> and <strong>flipping</strong> it from either <code>0</code> to <code>1</code> or <code>1</code> to <code>0</co...
2220
Easy
[ "A bit flip of a number x is choosing a bit in the binary representation of x and flipping it from either 0 to 1 or 1 to 0.\n\n\n\tFor example, for x = 7, the binary representation is 111 and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get 1...
[ { "hash": 6860867407259183000, "runtime": "11ms", "solution": "class Solution(object):\n def minBitFlips(self, start, goal):\n \"\"\"\n :type start: int\n :type goal: int\n :rtype: int\n \"\"\"\n binstart = bin(start).replace(\"0b\", \"\")\n binend = b...
class Solution(object): def minBitFlips(self, start, goal): """ :type start: int :type goal: int :rtype: int """
None
None
None
None
None
None
83
Level 1
Hi
number-of-ways-to-paint-n-3-grid
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You have a <code>grid</code> of size <code>n x 3</code> and you want to paint each cell of the grid with exactly one of the three colors: <strong>Red</strong>, <strong>Yellow,</strong> or <strong>Green</strong> while making sure t...
1411
Hard
[ "You have a grid of size n x 3 and you want to paint each cell of the grid with exactly one of the three colors: Red, Yellow, or Green while making sure that no two adjacent cells have the same color (i.e., no two cells that share vertical or horizontal sides have the same color).\n\nGiven n the number of rows of t...
[ { "hash": -7134644599678250000, "runtime": "19ms", "solution": "class Solution(object):\n def numOfWays(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n a = 6\n b = 6\n M = [3, 2, 2, 2]\n f = lambda M, N: [M[0]*N[0]+M[1]*N[2], M[0]*N[1...
class Solution(object): def numOfWays(self, n): """ :type n: int :rtype: int """
None
None
None
None
None
None
63.3
Level 5
Hi
partition-string-into-minimum-beautiful-substrings
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a binary string <code>s</code>, partition the string into one or more <strong>substrings</strong> such that each substring is <strong>beautiful</strong>.</p>\n\n<p>A string is <strong>beautiful</strong> if:</p>\n\n<ul>\n\t<l...
2767
Medium
[ "Given a binary string s, partition the string into one or more substrings such that each substring is beautiful.\n\nA string is beautiful if:\n\n\n\tIt doesn't contain leading zeros.\n\tIt's the binary representation of a number that is a power of 5.\n\n\nReturn the minimum number of substrings in such partition. ...
[ { "hash": 2023998947173552400, "runtime": "98ms", "solution": "class Solution(object):\n def minimumBeautifulSubstrings(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n if s[0] == '0':\n return -1\n powers = self.powersFive()\n ans...
class Solution(object): def minimumBeautifulSubstrings(self, s): """ :type s: str :rtype: int """
None
None
None
None
None
None
51.9
Level 3
Hi
projection-area-of-3d-shapes
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an <code>n x n</code> <code>grid</code> where we place some <code>1 x 1 x 1</code> cubes that are axis-aligned with the <code>x</code>, <code>y</code>, and <code>z</code> axes.</p>\n\n<p>Each value <code>v = grid[i][...
883
Easy
[ "You are given an n x n grid where we place some 1 x 1 x 1 cubes that are axis-aligned with the x, y, and z axes.\n\nEach value v = grid[i][j] represents a tower of v cubes placed on top of the cell (i, j).\n\nWe view the projection of these cubes onto the xy, yz, and zx planes.\n\nA projection is like a shadow, th...
[ { "hash": -6237394370291933000, "runtime": "48ms", "solution": "class Solution(object):\n def projectionArea(self, grid):\n n = len(grid)\n xy_plane = sum(1 for i in range(n) for j in range(n) if grid[i][j] > 0)\n yz_plane = sum(max(grid[i][j] for i in range(n)) for j in range(n)...
class Solution(object): def projectionArea(self, grid): """ :type grid: List[List[int]] :rtype: int """
None
None
None
None
None
None
72
Level 1
Hi
flip-columns-for-maximum-number-of-equal-rows
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an <code>m x n</code> binary matrix <code>matrix</code>.</p>\n\n<p>You can choose any number of columns in the matrix and flip every cell in that column (i.e., Change the value of the cell from <code>0</code> to <cod...
1072
Medium
[ "You are given an m x n binary matrix matrix.\n\nYou can choose any number of columns in the matrix and flip every cell in that column (i.e., Change the value of the cell from 0 to 1 or vice versa).\n\nReturn the maximum number of rows that have all values equal after some number of flips.\n\n \nExample 1:\n\nInput...
[ { "hash": 4327129020706765300, "runtime": "1867ms", "solution": "class Solution(object):\n def maxEqualRowsAfterFlips(self, matrix):\n def toint(lis):\n i = 0\n n = len(lis)\n output = 0\n while i < n:\n output += lis[i]*2**i\n ...
class Solution(object): def maxEqualRowsAfterFlips(self, matrix): """ :type matrix: List[List[int]] :rtype: int """
None
None
None
None
None
None
63.9
Level 2
Hi
find-words-containing-character
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> array of strings <code>words</code> and a character <code>x</code>.</p>\n\n<p>Return <em>an <strong>array of indices</strong> representing the words that contain the character </em><code>...
2942
Easy
[ "You are given a 0-indexed array of strings words and a character x.\n\nReturn an array of indices representing the words that contain the character x.\n\nNote that the returned array may be in any order.\n\n \nExample 1:\n\nInput: words = [\"leet\",\"code\"], x = \"e\"\nOutput: [0,1]\nExplanation: \"e\" occurs in ...
[ { "hash": 5637193760910606000, "runtime": "33ms", "solution": "class Solution(object):\n def findWordsContaining(self, words, x):\n \"\"\"\n :type words: List[str]\n :type x: str\n :rtype: List[int]\n \"\"\"\n result = []\n for i in range(len(words)):\...
class Solution(object): def findWordsContaining(self, words, x): """ :type words: List[str] :type x: str :rtype: List[int] """
None
None
None
None
None
None
88.5
Level 1
Hi
height-of-binary-tree-after-subtree-removal-queries
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given the <code>root</code> of a <strong>binary tree</strong> with <code>n</code> nodes. Each node is assigned a unique value from <code>1</code> to <code>n</code>. You are also given an array <code>queries</code> of size ...
2458
Hard
[ "You are given the root of a binary tree with n nodes. Each node is assigned a unique value from 1 to n. You are also given an array queries of size m.\n\nYou have to perform m independent queries on the tree where in the ith query you do the following:\n\n\n\tRemove the subtree rooted at the node with the value qu...
[ { "hash": 4336412991961038000, "runtime": "1577ms", "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 ...
# 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 treeQueries(self, root, queries): """ :type root: Optional[TreeNode] ...
None
None
None
None
None
None
39.1
Level 5
Hi
sum-of-all-subset-xor-totals
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>The <strong>XOR total</strong> of an array is defined as the bitwise <code>XOR</code> of<strong> all its elements</strong>, or <code>0</code> if the array is<strong> empty</strong>.</p>\n\n<ul>\n\t<li>For example, the <strong>XOR ...
1863
Easy
[ "The XOR total of an array is defined as the bitwise XOR of all its elements, or 0 if the array is empty.\n\n\n\tFor example, the XOR total of the array [2,5,6] is 2 XOR 5 XOR 6 = 1.\n\n\nGiven an array nums, return the sum of all XOR totals for every subset of nums. \n\nNote: Subsets with the same elements should ...
[ { "hash": -141250481151104290, "runtime": "46ms", "solution": "from itertools import chain, combinations\n\nclass Solution(object):\n def subsetXORSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n\n subsets = list(powerset(nums))\n to...
class Solution(object): def subsetXORSum(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
80.9
Level 1
Hi
determine-if-two-strings-are-close
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p>\n\n<ul>\n\t<li>Operation 1: Swap any two <strong>existing</strong> characters.\n\n\t<ul>\n\t\t<li>For examp...
1657
Medium
[ "Two strings are considered close if you can attain one from the other using the following operations:\n\n\n\tOperation 1: Swap any two existing characters.\n\n\t\n\t\tFor example, abcde -> aecdb\n\t\n\t\n\tOperation 2: Transform every occurrence of one existing character into another existing character, and do the...
[ { "hash": 6849965946489971000, "runtime": "342ms", "solution": "class Solution(object):\n def closeStrings(self, word1, word2):\n \"\"\"\n :type word1: str\n :type word2: str\n :rtype: bool\n \"\"\"\n count1 = Counter(word1)\n count2 = Counter(word2)\n...
class Solution(object): def closeStrings(self, word1, word2): """ :type word1: str :type word2: str :rtype: bool """
None
None
None
None
None
None
53.6
Level 3
Hi
minimum-remove-to-make-valid-parentheses
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a string <font face=\"monospace\">s</font> of <code>&#39;(&#39;</code> , <code>&#39;)&#39;</code> and lowercase English characters.</p>\n\n<p>Your task is to remove the minimum number of parentheses ( <code>&#39;(&#39;</code...
1249
Medium
[ "Given a string s of '(' , ')' and lowercase English characters.\n\nYour task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.\n\nFormally, a parentheses string is valid if and only if:\n\n\n\tIt is the emp...
[ { "hash": 5842353224149602000, "runtime": "2256ms", "solution": "class Solution(object):\n def minRemoveToMakeValid(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n num_unclosed = 0\n new_s = ''\n for char in s:\n if char == '(':\n...
class Solution(object): def minRemoveToMakeValid(self, s): """ :type s: str :rtype: str """
None
None
None
None
None
None
66.6
Level 2
Hi
super-egg-drop
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given <code>k</code> identical eggs and you have access to a building with <code>n</code> floors labeled from <code>1</code> to <code>n</code>.</p>\n\n<p>You know that there exists a floor <code>f</code> where <code>0 &lt;...
887
Hard
[ "You are given k identical eggs and you have access to a building with n floors labeled from 1 to n.\n\nYou know that there exists a floor f where 0 <= f <= n such that any egg dropped at a floor higher than f will break, and any egg dropped at or below floor f will not break.\n\nEach move, you may take an unbroken...
[ { "hash": 1481824020007452400, "runtime": "94ms", "solution": "class Solution:\n def superEggDrop(self, K, N):\n dp = [[0] * (K + 1) for _ in range(N + 1)]\n\n for i in range(1, N + 1):\n for j in range(1, K + 1):\n dp[i][j] = 1 + dp[i - 1][j - 1] + dp[i - 1][j...
class Solution(object): def superEggDrop(self, k, n): """ :type k: int :type n: int :rtype: int """
None
None
None
None
None
None
27.4
Level 5
Hi
xor-operation-in-an-array
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an integer <code>n</code> and an integer <code>start</code>.</p>\n\n<p>Define an array <code>nums</code> where <code>nums[i] = start + 2 * i</code> (<strong>0-indexed</strong>) and <code>n == nums.length</code>.</p>\...
1486
Easy
[ "You are given an integer n and an integer start.\n\nDefine an array nums where nums[i] = start + 2 * i (0-indexed) and n == nums.length.\n\nReturn the bitwise XOR of all elements of nums.\n\n \nExample 1:\n\nInput: n = 5, start = 0\nOutput: 8\nExplanation: Array nums is equal to [0, 2, 4, 6, 8] where (0 ^ 2 ^ 4 ^ ...
[ { "hash": 329316334103427700, "runtime": "19ms", "solution": "class Solution(object):\n def xorOperation(self, n, start):\n \"\"\"\n :type n: int\n :type start: int\n :rtype: int\n \"\"\"\n res = 0\n for i in range(n):\n res ^= start + (i*2)\n...
class Solution(object): def xorOperation(self, n, start): """ :type n: int :type start: int :rtype: int """
None
None
None
None
None
None
85.3
Level 1
Hi
maximum-genetic-difference-query
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There is a rooted tree consisting of <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. Each node&#39;s number denotes its <strong>unique genetic value</strong> (i.e. the genetic value of node <code>x</code> is <c...
1938
Hard
[ "There is a rooted tree consisting of n nodes numbered 0 to n - 1. Each node's number denotes its unique genetic value (i.e. the genetic value of node x is x). The genetic difference between two genetic values is defined as the bitwise-XOR of their values. You are given the integer array parents, where parents[i] i...
[ { "hash": 1129383621335689900, "runtime": "5147ms", "solution": "class Trie(object):\n LEN = 18\n def __init__(self):\n self.root = {}\n \n def insert(self,num):\n curr = self.root\n for i in range(Trie.LEN-1,-1,-1):\n bit = num>>i&1\n if(bit not in...
class Solution(object): def maxGeneticDifference(self, parents, queries): """ :type parents: List[int] :type queries: List[List[int]] :rtype: List[int] """
None
None
None
None
None
None
41.8
Level 5
Hi
stream-of-characters
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Design an algorithm that accepts a stream of characters and checks if a suffix of these characters is a string of a given array of strings <code>words</code>.</p>\n\n<p>For example, if <code>words = [&quot;abc&quot;, &quot;xyz&quo...
1032
Hard
[ "Design an algorithm that accepts a stream of characters and checks if a suffix of these characters is a string of a given array of strings words.\n\nFor example, if words = [\"abc\", \"xyz\"] and the stream added the four characters (one by one) 'a', 'x', 'y', and 'z', your algorithm should detect that the suffix ...
[ { "hash": 5841509960143767000, "runtime": "343ms", "solution": "class StreamChecker(object):\n\n def __init__(self, words):\n \"\"\"\n :type words: List[str]\n \"\"\"\n self.trie = {}\n self.stream = deque([])\n for word in words:\n node = self.tri...
class StreamChecker(object): def __init__(self, words): """ :type words: List[str] """ def query(self, letter): """ :type letter: str :rtype: bool """ # Your StreamChecker object will be instantiated and called as such: # obj = Stream...
None
None
None
None
None
None
51.8
Level 5
Hi
merge-triplets-to-form-target-triplet
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A <strong>triplet</strong> is an array of three integers. You are given a 2D integer array <code>triplets</code>, where <code>triplets[i] = [a<sub>i</sub>, b<sub>i</sub>, c<sub>i</sub>]</code> describes the <code>i<sup>th</sup></c...
1899
Medium
[ "A triplet is an array of three integers. You are given a 2D integer array triplets, where triplets[i] = [ai, bi, ci] describes the ith triplet. You are also given an integer array target = [x, y, z] that describes the triplet you want to obtain.\n\nTo obtain target, you may apply the following operation on triplet...
[ { "hash": 2521456049632668000, "runtime": "1610ms", "solution": "class Solution(object):\n def mergeTriplets(self, triplets, target):\n \"\"\"\n :type triplets: List[List[int]]\n :type target: List[int]\n :rtype: bool\n \"\"\"\n # track index that can be cove...
class Solution(object): def mergeTriplets(self, triplets, target): """ :type triplets: List[List[int]] :type target: List[int] :rtype: bool """
None
None
None
None
None
None
65.5
Level 2
Hi
max-sum-of-a-pair-with-equal-sum-of-digits
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <strong>positive</strong> integers. You can choose two indices <code>i</code> and <code>j</code>, such that <code>i != j</code>, and the sum of digit...
2342
Medium
[ "You are given a 0-indexed array nums consisting of positive integers. You can choose two indices i and j, such that i != j, and the sum of digits of the number nums[i] is equal to that of nums[j].\n\nReturn the maximum value of nums[i] + nums[j] that you can obtain over all possible indices i and j that satisfy th...
[ { "hash": -4775489775952247000, "runtime": "1497ms", "solution": "class Solution(object):\n def maximumSum(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n max_sum = -1 # Initialize the maximum sum to a value smaller than any possible sum\n ...
class Solution(object): def maximumSum(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
54.2
Level 3
Hi
longest-duplicate-substring
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a string <code>s</code>, consider all <em>duplicated substrings</em>: (contiguous) substrings of s that occur 2 or more times.&nbsp;The occurrences&nbsp;may overlap.</p>\n\n<p>Return <strong>any</strong> duplicated&nbsp;subs...
1044
Hard
[ "Given a string s, consider all duplicated substrings: (contiguous) substrings of s that occur 2 or more times. The occurrences may overlap.\n\nReturn any duplicated substring that has the longest possible length. If s does not have a duplicated substring, the answer is \"\".\n\n \nExample 1:\nInput: s = \"banana\"...
[ { "hash": 7071225273933777000, "runtime": "1388ms", "solution": "class Solution(object):\n def search(self, s, length):\n MOD = 2**63 - 1 \n base = 26 \n\n \n def rolling_hash(s, length):\n hash_val = 0\n for i in range(length):\n hash...
class Solution(object): def longestDupSubstring(self, s): """ :type s: str :rtype: str """
None
None
None
None
None
None
30.4
Level 5
Hi
double-a-number-represented-as-a-linked-list
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p>\n\n<p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubl...
2816
Medium
[ "You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes.\n\nReturn the head of the linked list after doubling it.\n\n \nExample 1:\n\nInput: head = [1,8,9]\nOutput: [3,7,8]\nExplanation: The figure above corresponds to the given linked list which represents the ...
[ { "hash": -3503138738794324500, "runtime": "717ms", "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 doubleIt(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 doubleIt(self, head): """ :type head: Optional[ListNode] :rtype: Optional[ListNode] """
None
None
None
None
None
None
48.4
Level 3
Hi
rotating-the-box
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an <code>m x n</code> matrix of characters <code>box</code> representing a side-view of a box. Each cell of the box is one of the following:</p>\r\n\r\n<ul>\r\n\t<li>A stone <code>&#39;#&#39;</code></li>\r\n\t<li>A s...
1861
Medium
[ "You are given an m x n matrix of characters box representing a side-view of a box. Each cell of the box is one of the following:\n\n\n\tA stone '#'\n\tA stationary obstacle '*'\n\tEmpty '.'\n\n\nThe box is rotated 90 degrees clockwise, causing some of the stones to fall due to gravity. Each stone falls down until ...
[ { "hash": 8362970827209107000, "runtime": "1824ms", "solution": "class Solution(object):\n def rotateTheBox(self, box):\n \"\"\"\n :type box: List[List[str]]\n :rtype: List[List[str]]\n \"\"\"\n for i in range(len(box)):\n n_stone = 0\n for j i...
class Solution(object): def rotateTheBox(self, box): """ :type box: List[List[str]] :rtype: List[List[str]] """
None
None
None
None
None
None
66.8
Level 2
Hi
best-team-with-no-conflicts
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the <strong>sum</strong> of scores of all the players in the team.</p>\n\n...
1626
Medium
[ "You are the manager of a basketball team. For the upcoming tournament, you want to choose the team with the highest overall score. The score of the team is the sum of scores of all the players in the team.\n\nHowever, the basketball team is not allowed to have conflicts. A conflict exists if a younger player has a...
[ { "hash": -5620445345262298000, "runtime": "1158ms", "solution": "class Solution(object):\n def bestTeamScore(self, scores, ages):\n \"\"\"\n :type scores: List[int]\n :type ages: List[int]\n :rtype: int\n \"\"\"\n \n pairs = [[scores[i], ages[i]] for ...
class Solution(object): def bestTeamScore(self, scores, ages): """ :type scores: List[int] :type ages: List[int] :rtype: int """
None
None
None
None
None
None
50.5
Level 3
Hi
apply-operations-to-make-all-array-elements-equal-to-zero
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and a positive integer <code>k</code>.</p>\n\n<p>You can apply the following operation on the array <strong>any</strong> number of times:</p>\n\n<ul>\n\t<l...
2772
Medium
[ "You are given a 0-indexed integer array nums and a positive integer k.\n\nYou can apply the following operation on the array any number of times:\n\n\n\tChoose any subarray of size k from the array and decrease all its elements by 1.\n\n\nReturn true if you can make all the array elements equal to 0, or false othe...
[ { "hash": -6497626751501585000, "runtime": "647ms", "solution": "class Solution(object):\n def checkArray(self, nums, k):\n n = len(nums)\n nums += [0] * k\n nums[k] += nums[0]\n for i in range(1, n + k):\n if nums[i] < nums[i - 1]:\n return False...
class Solution(object): def checkArray(self, nums, k): """ :type nums: List[int] :type k: int :rtype: bool """
None
None
None
None
None
None
32
Level 4
Hi
maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a <code>m x n</code> matrix <code>mat</code> and an integer <code>threshold</code>, return <em>the maximum side-length of a square with a sum less than or equal to </em><code>threshold</code><em> or return </em><code>0</code...
1292
Medium
[ "Given a m x n matrix mat and an integer threshold, return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square.\n\n \nExample 1:\n\nInput: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4\nOutput: 2\nExplanation: The maximum sid...
[ { "hash": -8504461011187858000, "runtime": "814ms", "solution": "class Solution(object):\n def maxSideLength(self, mat, threshold):\n \"\"\"\n :type mat: List[List[int]]\n :type threshold: int\n :rtype: int\n \"\"\"\n smallest = mat[0][0]\n for cur_lis...
class Solution(object): def maxSideLength(self, mat, threshold): """ :type mat: List[List[int]] :type threshold: int :rtype: int """
None
None
None
None
None
None
53.4
Level 3
Hi
sum-of-two-integers
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given two integers <code>a</code> and <code>b</code>, return <em>the sum of the two integers without using the operators</em> <code>+</code> <em>and</em> <code>-</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1...
371
Medium
[ "Given two integers a and b, return the sum of the two integers without using the operators + and -.\n\n \nExample 1:\nInput: a = 1, b = 2\nOutput: 3\nExample 2:\nInput: a = 2, b = 3\nOutput: 5\n\n \nConstraints:\n\n\n\t-1000 <= a, b <= 1000\n\n\n" ]
[ { "hash": 8035420118843379000, "runtime": "41ms", "solution": "class Solution:\n def getSum(self, a: int, b: int) -> int:\n mask = 0xffffffff\n while b!=0:\n a, b = (a^b) & mask, ((a & b) << 1) & mask \n if a > (mask >> 1):\n return ~(a ^ mask)\n else...
class Solution(object): def getSum(self, a, b): """ :type a: int :type b: int :rtype: int """
None
None
None
None
None
None
51.3
Level 3
Hi
all-nodes-distance-k-in-binary-tree
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given the <code>root</code> of a binary tree, the value of a target node <code>target</code>, and an integer <code>k</code>, return <em>an array of the values of all nodes that have a distance </em><code>k</code><em> from the targ...
863
Medium
[ "Given the root of a binary tree, the value of a target node target, and an integer k, return an array of the values of all nodes that have a distance k from the target node.\n\nYou can return the answer in any order.\n\n \nExample 1:\n\nInput: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2\nOutput: [7,4,1...
[ { "hash": 127123585986710780, "runtime": "26ms", "solution": "# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\nclass Solution(object):\n def findParents(self,root,map):\n ...
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def distanceK(self, root, target, k): """ :type root: TreeNode :type target: TreeNode :ty...
None
None
None
None
None
None
64.1
Level 2
Hi
apply-bitwise-operations-to-make-strings-equal
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given two <strong>0-indexed binary</strong> strings <code>s</code> and <code>target</code> of the same length <code>n</code>. You can do the following operation on <code>s</code> <strong>any</strong> number of times:</p>\n...
2546
Medium
[ "You are given two 0-indexed binary strings s and target of the same length n. You can do the following operation on s any number of times:\n\n\n\tChoose two different indices i and j where 0 <= i, j < n.\n\tSimultaneously, replace s[i] with (s[i] OR s[j]) and s[j] with (s[i] XOR s[j]).\n\n\nFor example, if s = \"0...
[ { "hash": 3865282343787609600, "runtime": "30ms", "solution": "class Solution(object):\n def makeStringsEqual(self, s, target):\n \"\"\"\n :type s: str\n :type target: str\n :rtype: bool\n \"\"\"\n\n count_zeros_s = s.count('0')\n count_zeros_target = ...
class Solution(object): def makeStringsEqual(self, s, target): """ :type s: str :type target: str :rtype: bool """
None
None
None
None
None
None
40.6
Level 4
Hi
check-if-n-and-its-double-exist
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an array <code>arr</code> of integers, check if there exist two indices <code>i</code> and <code>j</code> such that :</p>\n\n<ul>\n\t<li><code>i != j</code></li>\n\t<li><code>0 &lt;= i, j &lt; arr.length</code></li>\n\t<li><...
1346
Easy
[ "Given an array arr of integers, check if there exist two indices i and j such that :\n\n\n\ti != j\n\t0 <= i, j < arr.length\n\tarr[i] == 2 * arr[j]\n\n\n \nExample 1:\n\nInput: arr = [10,2,5,3]\nOutput: true\nExplanation: For i = 0 and j = 2, arr[i] == 10 == 2 * 5 == 2 * arr[j]\n\n\nExample 2:\n\nInput: arr = [3,...
[ { "hash": -3788181499126365700, "runtime": "93ms", "solution": "class Solution(object):\n def checkIfExist(self, arr):\n \"\"\"\n :type arr: List[int]\n :rtype: bool\n \"\"\"\n for i in range(len(arr)-1):\n for j in range(i+1,len(arr)):\n i...
class Solution(object): def checkIfExist(self, arr): """ :type arr: List[int] :rtype: bool """
None
None
None
None
None
None
36.9
Level 1
Hi
numbers-at-most-n-given-digit-set
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an array of <code>digits</code> which is sorted in <strong>non-decreasing</strong> order. You can write numbers using each <code>digits[i]</code> as many times as we want. For example, if <code>digits = [&#39;1&#39;,&#39;3&#...
902
Hard
[ "Given an array of digits which is sorted in non-decreasing order. You can write numbers using each digits[i] as many times as we want. For example, if digits = ['1','3','5'], we may write numbers such as '13', '551', and '1351315'.\n\nReturn the number of positive integers that can be generated that are less than ...
[ { "hash": -8278191957518614000, "runtime": "15ms", "solution": "import math\nclass Solution(object):\n def atMostNGivenDigitSet(self, digits, num):\n \"\"\"\n :type digits: List[str]\n :type n: int\n :rtype: int\n \"\"\"\n numstr=str(num)\n nl=len(nums...
class Solution(object): def atMostNGivenDigitSet(self, digits, n): """ :type digits: List[str] :type n: int :rtype: int """
None
None
None
None
None
None
42.2
Level 5
Hi
design-parking-system
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.</p>\n\n<p>Implement the <code>ParkingSystem</code> class:</p>\n\n<ul...
1603
Easy
[ "Design a parking system for a parking lot. The parking lot has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.\n\nImplement the ParkingSystem class:\n\n\n\tParkingSystem(int big, int medium, int small) Initializes object of the ParkingSystem class. The number of s...
[ { "hash": -3444915209028385300, "runtime": "109ms", "solution": "class ParkingSystem(object):\n\n def __init__(self, big, medium, small):\n \"\"\"\n :type big: int\n :type medium: int\n :type small: int\n \"\"\"\n self.space={}\n self.space[1]=big\n ...
class ParkingSystem(object): def __init__(self, big, medium, small): """ :type big: int :type medium: int :type small: int """ def addCar(self, carType): """ :type carType: int :rtype: bool """ # Your ParkingSystem obj...
None
None
None
None
None
None
88.1
Level 1
Hi
sorting-the-sentence
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A <strong>sentence</strong> is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.</p>\r\n\r\n<p>A sentence can be <strong>shuffle...
1859
Easy
[ "A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each word consists of lowercase and uppercase English letters.\n\nA sentence can be shuffled by appending the 1-indexed word position to each word then rearranging the words in the sentence.\n\n\n\tFor example, t...
[ { "hash": -5587973483235592000, "runtime": "17ms", "solution": "class Solution(object):\n def sortSentence(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n x=s.split()\n print(x)\n dic = {}\n for i in x:\n dic[i[-1]] = i[:-1...
class Solution(object): def sortSentence(self, s): """ :type s: str :rtype: str """
None
None
None
None
None
None
83
Level 1
Hi
count-integers-in-intervals
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an <strong>empty</strong> set of intervals, implement a data structure that can:</p>\n\n<ul>\n\t<li><strong>Add</strong> an interval to the set of intervals.</li>\n\t<li><strong>Count</strong> the number of integers that are...
2276
Hard
[ "Given an empty set of intervals, implement a data structure that can:\n\n\n\tAdd an interval to the set of intervals.\n\tCount the number of integers that are present in at least one interval.\n\n\nImplement the CountIntervals class:\n\n\n\tCountIntervals() Initializes the object with an empty set of intervals.\n\...
[ { "hash": -8290206602874744000, "runtime": "3226ms", "solution": "#NOTE: Look at previous submissions for helpful hints and comments\nfrom sortedcontainers import SortedList\nclass CountIntervals(object):\n\n def __init__(self):\n self.ivals = SortedList([])\n self.ccount = 0\n\n # d...
class CountIntervals(object): def __init__(self): def add(self, left, right): """ :type left: int :type right: int :rtype: None """ def count(self): """ :rtype: int """ # Your CountIntervals object will be instan...
None
None
None
None
None
None
34.5
Level 5
Hi
node-with-highest-edge-score
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a directed graph with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, where each node has <strong>exactly one</strong> outgoing edge.</p>\n\n<p>The graph is represented by a given <strong>0-in...
2374
Medium
[ "You are given a directed graph with n nodes labeled from 0 to n - 1, where each node has exactly one outgoing edge.\n\nThe graph is represented by a given 0-indexed integer array edges of length n, where edges[i] indicates that there is a directed edge from node i to node edges[i].\n\nThe edge score of a node i is...
[ { "hash": 511346773668581600, "runtime": "908ms", "solution": "class Solution(object):\n def edgeScore(self, edges):\n \"\"\"\n :type edges: List[int]\n :rtype: int\n \"\"\"\n scorList = [0] * len(edges)\n for i in range(len(edges)):\n scorList[edg...
class Solution(object): def edgeScore(self, edges): """ :type edges: List[int] :rtype: int """
None
None
None
None
None
None
46.9
Level 4
Hi
cat-and-mouse
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A game on an <strong>undirected</strong> graph is played by two players, Mouse and Cat, who alternate turns.</p>\n\n<p>The graph is given as follows: <code>graph[a]</code> is a list of all nodes <code>b</code> such that <code>ab</...
913
Hard
[ "A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns.\n\nThe graph is given as follows: graph[a] is a list of all nodes b such that ab is an edge of the graph.\n\nThe mouse starts at node 1 and goes first, the cat starts at node 2 and goes second, and there is a hole at node 0...
[ { "hash": 7163389831099780000, "runtime": "443ms", "solution": "class Solution(object):\n def catMouseGame(self, graph):\n n = len(graph)\n\n degree = [[[0 for k in range(3)] for j in range(n)] for i in range(n)]\n for i in range(n):\n for j in range(n):\n ...
class Solution(object): def catMouseGame(self, graph): """ :type graph: List[List[int]] :rtype: int """
None
None
None
None
None
None
34.1
Level 5
Hi
minimum-difference-between-highest-and-lowest-of-k-scores
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, where <code>nums[i]</code> represents the score of the <code>i<sup>th</sup></code> student. You are also given an integer <code>k</code>.</p>\n\n<p>Pick t...
1984
Easy
[ "You are given a 0-indexed integer array nums, where nums[i] represents the score of the ith student. You are also given an integer k.\n\nPick the scores of any k students from the array so that the difference between the highest and the lowest of the k scores is minimized.\n\nReturn the minimum possible difference...
[ { "hash": -7536184941000260000, "runtime": "55ms", "solution": "class Solution(object):\n def minimumDifference(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n nums.sort()\n l, r = 0, k - 1\n res = float(...
class Solution(object): def minimumDifference(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """
None
None
None
None
None
None
55.8
Level 1
Hi
maximum-split-of-positive-even-integers
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an integer <code>finalSum</code>. Split it into a sum of a <strong>maximum</strong> number of <strong>unique</strong> positive even integers.</p>\n\n<ul>\n\t<li>For example, given <code>finalSum = 12</code>, the foll...
2178
Medium
[ "You are given an integer finalSum. Split it into a sum of a maximum number of unique positive even integers.\n\n\n\tFor example, given finalSum = 12, the following splits are valid (unique positive even integers summing up to finalSum): (12), (2 + 10), (2 + 4 + 6), and (4 + 8). Among them, (2 + 4 + 6) contains the...
[ { "hash": -1920199753794909000, "runtime": "381ms", "solution": "from itertools import combinations\nclass Solution(object):\n def maximumEvenSplit(self, finalSum):\n if finalSum%2!=0:\n return []\n else:\n l=[]\n i=1\n while i*(i+1)<finalSum:...
class Solution(object): def maximumEvenSplit(self, finalSum): """ :type finalSum: int :rtype: List[int] """
None
None
None
None
None
None
59.4
Level 2
Hi
evaluate-boolean-binary-tree
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given the <code>root</code> of a <strong>full binary tree</strong> with the following properties:</p>\n\n<ul>\n\t<li><strong>Leaf nodes</strong> have either the value <code>0</code> or <code>1</code>, where <code>0</code> ...
2331
Easy
[ "You are given the root of a full binary tree with the following properties:\n\n\n\tLeaf nodes have either the value 0 or 1, where 0 represents False and 1 represents True.\n\tNon-leaf nodes have either the value 2 or 3, where 2 represents the boolean OR and 3 represents the boolean AND.\n\n\nThe evaluation of a no...
[ { "hash": -8729426629916400000, "runtime": "28ms", "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 e...
# 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 evaluateTree(self, root): """ :type root: Optional[TreeNode] :rt...
None
None
None
None
None
None
77.9
Level 1
Hi
remove-palindromic-subsequences
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a string <code>s</code> consisting <strong>only</strong> of letters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>. In a single step you can remove one <strong>palindromic subsequence</strong> from <code>s</co...
1332
Easy
[ "You are given a string s consisting only of letters 'a' and 'b'. In a single step you can remove one palindromic subsequence from s.\n\nReturn the minimum number of steps to make the given string empty.\n\nA string is a subsequence of a given string if it is generated by deleting some characters of a given string ...
[ { "hash": -6645957447515952000, "runtime": "14ms", "solution": "class Solution(object):\n def removePalindromeSub(self, s):\n return int(s==s[::-1]) or 2\n " }, { "hash": -1090706656762995500, "runtime": "15ms", "solution": "class Solution(object):\n def removePalindr...
class Solution(object): def removePalindromeSub(self, s): """ :type s: str :rtype: int """
None
None
None
None
None
None
76.3
Level 1
Hi
divide-players-into-teams-of-equal-skill
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a positive integer array <code>skill</code> of <strong>even</strong> length <code>n</code> where <code>skill[i]</code> denotes the skill of the <code>i<sup>th</sup></code> player. Divide the players into <code>n / 2<...
2491
Medium
[ "You are given a positive integer array skill of even length n where skill[i] denotes the skill of the ith player. Divide the players into n / 2 teams of size 2 such that the total skill of each team is equal.\n\nThe chemistry of a team is equal to the product of the skills of the players on that team.\n\nReturn th...
[ { "hash": 6939383169896396000, "runtime": "416ms", "solution": "class Solution(object):\n def dividePlayers(self, skill):\n l,r= 0, len(skill)-1\n res=0\n skill.sort()\n eachPlayer= sum(skill)/ (len(skill)/2)\n while l<r:\n if s...
class Solution(object): def dividePlayers(self, skill): """ :type skill: List[int] :rtype: int """
None
None
None
None
None
None
59
Level 3
Hi
baseball-game
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.</p>\n\n<p>You are given a list of strings <code>operations</code>, where <code>operations[i]</code> i...
682
Easy
[ "You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record.\n\nYou are given a list of strings operations, where operations[i] is the ith operation you must apply to the record and is one of the following:\n\n\n\tAn integer x.\n\n\t\n\t\tRecord a...
[ { "hash": 3845253481299417600, "runtime": "16ms", "solution": "class Solution(object):\n def calPoints(self, operations):\n \"\"\"\n :type operations: List[str]\n :rtype: int\n \"\"\"\n ans = []\n for i in operations:\n if i == 'C':\n ...
class Solution(object): def calPoints(self, operations): """ :type operations: List[str] :rtype: int """
None
None
None
None
None
None
75.4
Level 1
Hi
minimum-number-of-refueling-stops
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A car travels from a starting position to a destination which is <code>target</code> miles east of the starting position.</p>\n\n<p>There are gas stations along the way. The gas stations are represented as an array <code>stations<...
871
Hard
[ "A car travels from a starting position to a destination which is target miles east of the starting position.\n\nThere are gas stations along the way. The gas stations are represented as an array stations where stations[i] = [positioni, fueli] indicates that the ith gas station is positioni miles east of the starti...
[ { "hash": 5117513874152362000, "runtime": "98ms", "solution": "class Solution(object):\n def minRefuelStops(self, target, startFuel, stations):\n \"\"\"\n :type target: int\n :type startFuel: int\n :type stations: List[List[int]]\n :rtype: int\n \"\"\"\n ...
class Solution(object): def minRefuelStops(self, target, startFuel, stations): """ :type target: int :type startFuel: int :type stations: List[List[int]] :rtype: int """
None
None
None
None
None
None
39.8
Level 5
Hi
rings-and-rods
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There are <code>n</code> rings and each ring is either red, green, or blue. The rings are distributed <strong>across ten rods</strong> labeled from <code>0</code> to <code>9</code>.</p>\n\n<p>You are given a string <code>rings</co...
2103
Easy
[ "There are n rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from 0 to 9.\n\nYou are given a string rings of length 2n that describes the n rings that are placed onto the rods. Every two characters in rings forms a color-position pair that is used to describe eac...
[ { "hash": -4339186926154170000, "runtime": "18ms", "solution": "class Solution(object):\n def countPoints(self, rings):\n \"\"\"\n :type rings: str\n :rtype: int\n \"\"\"\n ringDict ={str(i):'' for i in range(10)}\n\n for i in range(0,len(rings) -1,2):\n ...
class Solution(object): def countPoints(self, rings): """ :type rings: str :rtype: int """
None
None
None
None
None
None
80.8
Level 1
Hi
n-ary-tree-preorder-traversal
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given the <code>root</code> of an n-ary tree, return <em>the preorder 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 separa...
589
Easy
[ "Given the root of an n-ary tree, return the preorder 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\n\n\nInput: root = [1,null,3,2,4,null,5,6]\nOutput: [1,3,5,6,2...
[ { "hash": -3609456374048558000, "runtime": "43ms", "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 preorder(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 preorder(self, root): """ :type root: Node :rtype: List[int] """
None
None
None
None
None
None
75.3
Level 1
Hi
make-the-string-great
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a string <code>s</code> of lower and upper case English letters.</p>\n\n<p>A good string is a string which doesn&#39;t have <strong>two adjacent characters</strong> <code>s[i]</code> and <code>s[i + 1]</code> where:</p>\n\n<...
1544
Easy
[ "Given a string s of lower and upper case English letters.\n\nA good string is a string which doesn't have two adjacent characters s[i] and s[i + 1] where:\n\n\n\t0 <= i <= s.length - 2\n\ts[i] is a lower-case letter and s[i + 1] is the same letter but in upper-case or vice-versa.\n\n\nTo make the string good, you ...
[ { "hash": 574157142142391550, "runtime": "10ms", "solution": "class Solution(object):\n def makeGood(self, s):\n \"\"\"\n :type s: str\n :rtype: str\n \"\"\"\n \n # Use stack to store the visited characters.\n stack = []\n \n # It...
class Solution(object): def makeGood(self, s): """ :type s: str :rtype: str """
None
None
None
None
None
None
63.1
Level 1
Hi
find-the-winner-of-an-array-game
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer array <code>arr</code> of <strong>distinct</strong> integers and an integer <code>k</code>.</p>\n\n<p>A game will be played between the first two elements of the array (i.e. <code>arr[0]</code> and <code>arr[1]</c...
1535
Medium
[ "Given an integer array arr of distinct integers and an integer k.\n\nA game will be played between the first two elements of the array (i.e. arr[0] and arr[1]). In each round of the game, we compare arr[0] with arr[1], the larger integer wins and remains at position 0, and the smaller integer moves to the end of t...
[ { "hash": -2922617111854436400, "runtime": "2404ms", "solution": "class Solution(object):\n def getWinner(self, arr, k):\n \"\"\"\n :type arr: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n\n n = len(arr)\n win_count = 0\n stop = False\n ...
class Solution(object): def getWinner(self, arr, k): """ :type arr: List[int] :type k: int :rtype: int """
None
None
None
None
None
None
57.4
Level 3
Hi
first-completely-painted-row-or-column
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> integer array <code>arr</code>, and an <code>m x n</code> integer <strong>matrix</strong> <code>mat</code>. <code>arr</code> and <code>mat</code> both contain <strong>all</strong> the int...
2661
Medium
[ "You are given a 0-indexed integer array arr, and an m x n integer matrix mat. arr and mat both contain all the integers in the range [1, m * n].\n\nGo through each index i in arr starting from index 0 and paint the cell in mat containing the integer arr[i].\n\nReturn the smallest index i at which either a row or a...
[ { "hash": 2788075583571156500, "runtime": "1055ms", "solution": "class Solution(object):\n def firstCompleteIndex(self, arr, mat):\n all_sets = [] \n r, c = len(mat), len(mat[0])\n\n d = {} #l : [x,y]\n for y in range(r):\n for x in range(c):\n ...
class Solution(object): def firstCompleteIndex(self, arr, mat): """ :type arr: List[int] :type mat: List[List[int]] :rtype: int """
None
None
None
None
None
None
49.8
Level 3
Hi
shift-2d-grid
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a 2D <code>grid</code> of size <code>m x n</code>&nbsp;and an integer <code>k</code>. You need to shift the <code>grid</code>&nbsp;<code>k</code> times.</p>\n\n<p>In one shift operation:</p>\n\n<ul>\n\t<li>Element at <code>g...
1260
Easy
[ "Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.\n\nIn one shift operation:\n\n\n\tElement at grid[i][j] moves to grid[i][j + 1].\n\tElement at grid[i][n - 1] moves to grid[i + 1][0].\n\tElement at grid[m - 1][n - 1] moves to grid[0][0].\n\n\nReturn the 2D grid after applying shi...
[ { "hash": -6341854766361882000, "runtime": "132ms", "solution": "import copy\n\nclass Solution(object):\n def shiftGrid(self, grid, k):\n \"\"\"\n :type grid: List[List[int]]\n :type k: int\n :rtype: List[List[int]]\n \"\"\"\n for _ in range(k):\n ...
class Solution(object): def shiftGrid(self, grid, k): """ :type grid: List[List[int]] :type k: int :rtype: List[List[int]] """
None
None
None
None
None
None
67.4
Level 1
Hi
maximum-running-time-of-n-computers
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You have <code>n</code> computers. You are given the integer <code>n</code> and a <strong>0-indexed</strong> integer array <code>batteries</code> where the <code>i<sup>th</sup></code> battery can <strong>run</strong> a computer fo...
2141
Hard
[ "You have n computers. You are given the integer n and a 0-indexed integer array batteries where the ith battery can run a computer for batteries[i] minutes. You are interested in running all n computers simultaneously using the given batteries.\n\nInitially, you can insert at most one battery into each computer. A...
[ { "hash": -4604894421243581000, "runtime": "464ms", "solution": "class Solution(object):\n def maxRunTime(self, n, arr):\n \"\"\"\n tricky question\n 先Sort, max(arr) > avg(n), 可以移除最大算左邊n-1\n 重複以上步驟\n 直到max(arr) <= avg(n) 就代表說所有電池可以平均分配\n \"\"\"\n arr.s...
class Solution(object): def maxRunTime(self, n, batteries): """ :type n: int :type batteries: List[int] :rtype: int """
None
None
None
None
None
None
50.2
Level 5
Hi
time-needed-to-inform-all-employees
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A company has <code>n</code> employees with a unique ID for each employee from <code>0</code> to <code>n - 1</code>. The head of the company is the one with <code>headID</code>.</p>\n\n<p>Each employee has one direct manager given...
1376
Medium
[ "A company has n employees with a unique ID for each employee from 0 to n - 1. The head of the company is the one with headID.\n\nEach employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also, it is guaranteed that the subordi...
[ { "hash": 7975943125240588000, "runtime": "975ms", "solution": "class Solution(object):\n def __init__(self):\n self.time = 0\n def numOfMinutes(self, n, headID, manager, informTime):\n \"\"\"\n :type n: int\n :type headID: int\n :type manager: List[int]\n ...
class Solution(object): def numOfMinutes(self, n, headID, manager, informTime): """ :type n: int :type headID: int :type manager: List[int] :type informTime: List[int] :rtype: int """
None
None
None
None
None
None
60.1
Level 2
Hi
basic-calculator-ii
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>.&nbsp;</p>\n\n<p>The integer division should truncate toward zero.</p>\n\n<p>You may assume that the given expres...
227
Medium
[ "Given a string s which represents an expression, evaluate this expression and return its value. \n\nThe integer division should truncate toward zero.\n\nYou may assume that the given expression is always valid. All intermediate results will be in the range of [-2³¹, 2³¹ - 1].\n\nNote: You are not allowed to use an...
[ { "hash": -3544289378378034000, "runtime": "249ms", "solution": "class Solution(object):\n def calculate(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n stack = []\n current = 0\n operation = '+'\n res = 0\n for i in range(len(...
class Solution(object): def calculate(self, s): """ :type s: str :rtype: int """
None
None
None
None
None
None
43
Level 4
Hi
peeking-iterator
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Design an iterator that supports the <code>peek</code> operation on an existing iterator in addition to the <code>hasNext</code> and the <code>next</code> operations.</p>\n\n<p>Implement the <code>PeekingIterator</code> class:</p>...
284
Medium
[ "Design an iterator that supports the peek operation on an existing iterator in addition to the hasNext and the next operations.\n\nImplement the PeekingIterator class:\n\n\n\tPeekingIterator(Iterator<int> nums) Initializes the object with the given integer iterator iterator.\n\tint next() Returns the next element ...
[ { "hash": 6502787846287123000, "runtime": "11ms", "solution": "# Below is the interface for Iterator, which is already defined for you.\n#\n# class Iterator(object):\n# def __init__(self, nums):\n# \"\"\"\n# Initializes an iterator object to the beginning of a list.\n# :type ...
# Below is the interface for Iterator, which is already defined for you. # # class Iterator(object): # def __init__(self, nums): # """ # Initializes an iterator object to the beginning of a list. # :type nums: List[int] # """ # # def hasNext(self): # """ # Returns...
None
None
None
None
None
None
59.1
Level 3
Hi
smallest-string-starting-from-leaf
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given the <code>root</code> of a binary tree where each node has a value in the range <code>[0, 25]</code> representing the letters <code>&#39;a&#39;</code> to <code>&#39;z&#39;</code>.</p>\n\n<p>Return <em>the <strong>lex...
988
Medium
[ "You are given the root of a binary tree where each node has a value in the range [0, 25] representing the letters 'a' to 'z'.\n\nReturn the lexicographically smallest string that starts at a leaf of this tree and ends at the root.\n\nAs a reminder, any shorter prefix of a string is lexicographically smaller.\n\n\n...
[ { "hash": 8265674737586463000, "runtime": "26ms", "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 sm...
# 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 smallestFromLeaf(self, root): """ :type root: TreeNode :rtype: s...
None
None
None
None
None
None
50.9
Level 3
Hi
meeting-rooms-iii
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>\n\n<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</s...
2402
Hard
[ "You are given an integer n. There are n rooms numbered from 0 to n - 1.\n\nYou are given a 2D integer array meetings where meetings[i] = [starti, endi] means that a meeting will be held during the half-closed time interval [starti, endi). All the values of starti are unique.\n\nMeetings are allocated to rooms in t...
[ { "hash": -4383203927502628400, "runtime": "1212ms", "solution": "class Solution(object):\n def mostBooked(self, n, meetings):\n \"\"\"\n :type n: int\n :type meetings: List[List[int]]\n :rtype: int\n [0,10],[1,5],[2,7],[3,4]]\n room0:[0,10][3,4]\n roo...
class Solution(object): def mostBooked(self, n, meetings): """ :type n: int :type meetings: List[List[int]] :rtype: int """
None
None
None
None
None
None
33.6
Level 5
Hi
remove-covered-intervals
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an array <code>intervals</code> where <code>intervals[i] = [l<sub>i</sub>, r<sub>i</sub>]</code> represent the interval <code>[l<sub>i</sub>, r<sub>i</sub>)</code>, remove all intervals that are covered by another interval i...
1288
Medium
[ "Given an array intervals where intervals[i] = [li, ri] represent the interval [li, ri), remove all intervals that are covered by another interval in the list.\n\nThe interval [a, b) is covered by the interval [c, d) if and only if c <= a and b <= d.\n\nReturn the number of remaining intervals.\n\n \nExample 1:\n\n...
[ { "hash": -4238024045866862600, "runtime": "64ms", "solution": "class Solution(object):\n def removeCoveredIntervals(self, intervals):\n \"\"\"\n :type intervals: List[List[int]]\n :rtype: int\n \"\"\"\n intervals.sort(key = lambda i: (i[0], -i[1]))\n\n res =...
class Solution(object): def removeCoveredIntervals(self, intervals): """ :type intervals: List[List[int]] :rtype: int """
None
None
None
None
None
None
56.6
Level 3
Hi
minimum-height-trees
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A tree is an undirected graph in which any two vertices are connected by&nbsp;<i>exactly</i>&nbsp;one path. In other words, any connected graph without simple cycles is a tree.</p>\n\n<p>Given a tree of <code>n</code> nodes&nbsp;l...
310
Medium
[ "A tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.\n\nGiven a tree of n nodes labelled from 0 to n - 1, and an array of n - 1 edges where edges[i] = [ai, bi] indicates that there is an undirected edge betwe...
[ { "hash": -6466062405988275000, "runtime": "4265ms", "solution": "class Solution:\n def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:\n \n neighbor = {i:set() for i in range(n)}\n\n for i in range(len(edges)):\n [n1, n2] = edges[i]\n ...
class Solution(object): def findMinHeightTrees(self, n, edges): """ :type n: int :type edges: List[List[int]] :rtype: List[int] """
None
None
None
None
None
None
38.7
Level 4
Hi
count-number-of-texts
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Alice is texting Bob using her phone. The <strong>mapping</strong> of digits to letters is shown in the figure below.</p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2022/03/15/1200px-telephone-keypad2svg.png\" style=...
2266
Medium
[ "Alice is texting Bob using her phone. The mapping of digits to letters is shown in the figure below.\n\nIn order to add a letter, Alice has to press the key of the corresponding digit i times, where i is the position of the letter in the key.\n\n\n\tFor example, to add the letter 's', Alice has to press '7' four t...
[ { "hash": 2718963033324089000, "runtime": "1171ms", "solution": "class Solution(object):\n def countTexts(self, pressedKeys):\n \"\"\"\n :type pressedKeys: str\n :rtype: int\n \"\"\"\n dic={}\n def recur(end):\n if end in dic: return dic[end]\n ...
class Solution(object): def countTexts(self, pressedKeys): """ :type pressedKeys: str :rtype: int """
None
None
None
None
None
None
47.4
Level 3
Hi
check-if-number-is-a-sum-of-powers-of-three
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer <code>n</code>, return <code>true</code> <em>if it is possible to represent </em><code>n</code><em> as the sum of distinct powers of three.</em> Otherwise, return <code>false</code>.</p>\n\n<p>An integer <code>y</...
1780
Medium
[ "Given an integer n, return true if it is possible to represent n as the sum of distinct powers of three. Otherwise, return false.\n\nAn integer y is a power of three if there exists an integer x such that y == 3x.\n\n \nExample 1:\n\nInput: n = 12\nOutput: true\nExplanation: 12 = 3¹ + 3²\n\n\nExample 2:\n\nInput: ...
[ { "hash": 8332251672422390000, "runtime": "12ms", "solution": "class Solution(object):\n def checkPowersOfThree(self, n):\n \"\"\"\n :type n: int\n :rtype: bool\n \"\"\"\n ternary = []\n while n > 0:# to store the ternary values\n ternary.append(n ...
class Solution(object): def checkPowersOfThree(self, n): """ :type n: int :rtype: bool """
None
None
None
None
None
None
66.9
Level 2
Hi
minimum-swaps-to-make-sequences-increasing
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given two integer arrays of the same length <code>nums1</code> and <code>nums2</code>. In one operation, you are allowed to swap <code>nums1[i]</code> with <code>nums2[i]</code>.</p>\n\n<ul>\n\t<li>For example, if <code>nu...
801
Hard
[ "You are given two integer arrays of the same length nums1 and nums2. In one operation, you are allowed to swap nums1[i] with nums2[i].\n\n\n\tFor example, if nums1 = [1,2,3,8], and nums2 = [5,6,7,4], you can swap the element at i = 3 to obtain nums1 = [1,2,3,4] and nums2 = [5,6,7,8].\n\n\nReturn the minimum number...
[ { "hash": -5463000043089508000, "runtime": "904ms", "solution": "class Solution(object):\n def minSwap(self, nums1, nums2):\n \"\"\"\n :type nums1: List[int]\n :type nums2: List[int]\n :rtype: int\n \"\"\"\n n = len(nums1)\n dp = [[float('inf')]*2 for ...
class Solution(object): def minSwap(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: int """
None
None
None
None
None
None
39.6
Level 5
Hi
destination-city
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given the array <code>paths</code>, where <code>paths[i] = [cityA<sub>i</sub>, cityB<sub>i</sub>]</code> means there exists a direct path going from <code>cityA<sub>i</sub></code> to <code>cityB<sub>i</sub></code>. <em>Ret...
1436
Easy
[ "You are given the array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going from cityAi to cityBi. Return the destination city, that is, the city without any path outgoing to another city.\n\nIt is guaranteed that the graph of paths forms a line without any loop, therefore, there will b...
[ { "hash": -300826718623196500, "runtime": "32ms", "solution": "class Solution(object):\n def destCity(self, paths):\n return list({path[1] for path in paths}.difference({path[0] for path in paths}))[0]\n " }, { "hash": 4294631849590317600, "runtime": "22ms", "solution": ...
class Solution(object): def destCity(self, paths): """ :type paths: List[List[str]] :rtype: str """
None
None
None
None
None
None
77
Level 1
Hi
count-good-meals
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A <strong>good meal</strong> is a meal that contains <strong>exactly two different food items</strong> with a sum of deliciousness equal to a power of two.</p>\n\n<p>You can pick <strong>any</strong> two different foods to make a ...
1711
Medium
[ "A good meal is a meal that contains exactly two different food items with a sum of deliciousness equal to a power of two.\n\nYou can pick any two different foods to make a good meal.\n\nGiven an array of integers deliciousness where deliciousness[i] is the deliciousness of the i​​​​​​th​​​​​​​​ item of food, retur...
[ { "hash": -5893700590560400000, "runtime": "774ms", "solution": "class Solution(object):\n def countPairs(self, deliciousness):\n count = 0\n counter = Counter(deliciousness)\n powersOfTwo = [2 ** i for i in range(0,22)]\n\n for key, value in counter.items():\n ...
class Solution(object): def countPairs(self, deliciousness): """ :type deliciousness: List[int] :rtype: int """
None
None
None
None
None
None
29.9
Level 4
Hi
minimum-number-of-operations-to-convert-time
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given two strings <code>current</code> and <code>correct</code> representing two <strong>24-hour times</strong>.</p>\n\n<p>24-hour times are formatted as <code>&quot;HH:MM&quot;</code>, where <code>HH</code> is between <co...
2224
Easy
[ "You are given two strings current and correct representing two 24-hour times.\n\n24-hour times are formatted as \"HH:MM\", where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.\n\nIn one operation you can increase the time current by 1, 5, 15, or 6...
[ { "hash": 1021914311576173000, "runtime": "20ms", "solution": "class Solution(object):\n def convertTime(self, current, correct):\n # Get the current and correct time in minutes\n currMinutes = (int(current[0:2]) * 60) + int(current[3:5])\n corrMinutes = (int(correct[0:2]) * 60) ...
class Solution(object): def convertTime(self, current, correct): """ :type current: str :type correct: str :rtype: int """
None
None
None
None
None
None
65.1
Level 1
Hi
maximum-candies-allocated-to-k-children
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> integer array <code>candies</code>. Each element in the array denotes a pile of candies of size <code>candies[i]</code>. You can divide each pile into any number of <strong>sub piles</str...
2226
Medium
[ "You are given a 0-indexed integer array candies. Each element in the array denotes a pile of candies of size candies[i]. You can divide each pile into any number of sub piles, but you cannot merge two piles together.\n\nYou are also given an integer k. You should allocate piles of candies to k children such that e...
[ { "hash": 5876855952414302000, "runtime": "1000ms", "solution": "class Solution(object):\n def maximumCandies(self, candies, k):\n \"\"\"\n :type candies: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n l, r = 1, max(candies)\n while l <= r:\n ...
class Solution(object): def maximumCandies(self, candies, k): """ :type candies: List[int] :type k: int :rtype: int """
None
None
None
None
None
None
37.4
Level 4
Hi
design-twitter
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the <code>10</code> most recent tweets in the user&#39;s news feed.</p>\n\n<p>Implement the <code>Twitter</code> ...
355
Medium
[ "Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the 10 most recent tweets in the user's news feed.\n\nImplement the Twitter class:\n\n\n\tTwitter() Initializes your twitter object.\n\tvoid postTweet(int userId, int tweetId) Composes a new tweet w...
[ { "hash": 5144157847042651000, "runtime": "17ms", "solution": "class Twitter(object):\n\n def __init__(self):\n self.hm = {}\n self.tweets = []\n\n def postTweet(self, userId, tweetId):\n tweet = [userId, tweetId]\n self.tweets.append(tweet)\n\n def getNewsFeed(self,...
class Twitter(object): def __init__(self): def postTweet(self, userId, tweetId): """ :type userId: int :type tweetId: int :rtype: None """ def getNewsFeed(self, userId): """ :type userId: int :rtype: List[int] """ ...
None
None
None
None
None
None
38.7
Level 4
Hi
valid-arrangement-of-pairs
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> 2D integer array <code>pairs</code> where <code>pairs[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>. An arrangement of <code>pairs</code> is <strong>valid</strong> if for every index <...
2097
Hard
[ "You are given a 0-indexed 2D integer array pairs where pairs[i] = [starti, endi]. An arrangement of pairs is valid if for every index i where 1 <= i < pairs.length, we have endi-1 == starti.\n\nReturn any valid arrangement of pairs.\n\nNote: The inputs will be generated such that there exists a valid arrangement o...
[ { "hash": -6929827935215835000, "runtime": "1961ms", "solution": "\"\"\"\"\"\"\n\"\"\"\nhttps://en.wikipedia.org/wiki/Eulerian_path#Hierholzer's_algorithm\nhttps://en.wikipedia.org/wiki/Eulerian_path\n\"\"\"\nclass Solution:\n def validArrangement1(self, pairs):\n graph = defaultdict(list)\n ...
class Solution(object): def validArrangement(self, pairs): """ :type pairs: List[List[int]] :rtype: List[List[int]] """
None
None
None
None
None
None
42.3
Level 5
Hi
count-all-valid-pickup-and-delivery-options
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given <code>n</code> orders, each order consists of a pickup and a delivery service.</p>\n\n<p>Count all valid pickup/delivery possible sequences such that delivery(i) is always after of&nbsp;pickup(i).&nbsp;</p>\n\n<p>Since the a...
1359
Hard
[ "Given n orders, each order consists of a pickup and a delivery service.\n\nCount all valid pickup/delivery possible sequences such that delivery(i) is always after of pickup(i). \n\nSince the answer may be too large, return it modulo 10^9 + 7.\n\n \nExample 1:\n\nInput: n = 1\nOutput: 1\nExplanation: Unique order ...
[ { "hash": -4790129033919011000, "runtime": "12ms", "solution": "class Solution(object):\n def countOrders(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n\n if n == 1: return 1\n if n == 2: return 6\n \n return (n * (2 * n - 1) * self....
class Solution(object): def countOrders(self, n): """ :type n: int :rtype: int """
None
None
None
None
None
None
65.4
Level 5
Hi
minimum-degree-of-a-connected-trio-in-a-graph
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an undirected graph. You are given an integer <code>n</code> which is the number of nodes in the graph and an array <code>edges</code>, where each <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that...
1761
Hard
[ "You are given an undirected graph. You are given an integer n which is the number of nodes in the graph and an array edges, where each edges[i] = [ui, vi] indicates that there is an undirected edge between ui and vi.\n\nA connected trio is a set of three nodes where there is an edge between every pair of them.\n\n...
[ { "hash": -3906262861858552300, "runtime": "2847ms", "solution": "class Solution(object):\n def minTrioDegree(self, n, edges):\n \"\"\"\n :type n: int\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"\n\n # set of three form a cycle\n # degree is t...
class Solution(object): def minTrioDegree(self, n, edges): """ :type n: int :type edges: List[List[int]] :rtype: int """
None
None
None
None
None
None
42.1
Level 5
Hi
shortest-subarray-with-sum-at-least-k
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the length of the shortest non-empty <strong>subarray</strong> of </em><code>nums</code><em> with a sum of at least </em><code>k</code>. If there i...
862
Hard
[ "Given an integer array nums and an integer k, return the length of the shortest non-empty subarray of nums with a sum of at least k. If there is no such subarray, return -1.\n\nA subarray is a contiguous part of an array.\n\n \nExample 1:\nInput: nums = [1], k = 1\nOutput: 1\nExample 2:\nInput: nums = [1,2], k = 4...
[ { "hash": -9045279729622837000, "runtime": "1296ms", "solution": "class Solution(object):\n def shortestSubarray(self, nums, k):\n \"\"\"\n :type nums: List[int]\n :type k: int\n :rtype: int\n \"\"\"\n res = float('inf')\n n = len(nums)\n queue ...
class Solution(object): def shortestSubarray(self, nums, k): """ :type nums: List[int] :type k: int :rtype: int """
None
None
None
None
None
None
25.9
Level 5
Hi
delete-operation-for-two-strings
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of <strong>steps</strong> required to make</em> <code>word1</code> <em>and</em> <code>word2</code> <em>the same</em>.</p>\n\n<p>In one <str...
583
Medium
[ "Given two strings word1 and word2, return the minimum number of steps required to make word1 and word2 the same.\n\nIn one step, you can delete exactly one character in either string.\n\n \nExample 1:\n\nInput: word1 = \"sea\", word2 = \"eat\"\nOutput: 2\nExplanation: You need one step to make \"sea\" to \"ea\" an...
[ { "hash": -6136796668165714000, "runtime": "248ms", "solution": "class Solution(object):\n def minDistance(self, word1, word2):\n \"\"\"\n :type word1: str\n :type word2: str\n :rtype: int\n \"\"\"\n\n def dfs(i, j, memo):\n\n if i == len(word1):\n...
class Solution(object): def minDistance(self, word1, word2): """ :type word1: str :type word2: str :rtype: int """
None
None
None
None
None
None
61
Level 2
Hi
repeated-dna-sequences
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>The <strong>DNA sequence</strong> is composed of a series of nucleotides abbreviated as <code>&#39;A&#39;</code>, <code>&#39;C&#39;</code>, <code>&#39;G&#39;</code>, and <code>&#39;T&#39;</code>.</p>\n\n<ul>\n\t<li>For example, <c...
187
Medium
[ "The DNA sequence is composed of a series of nucleotides abbreviated as 'A', 'C', 'G', and 'T'.\n\n\n\tFor example, \"ACGAATTCCG\" is a DNA sequence.\n\n\nWhen studying DNA, it is useful to identify repeated sequences within the DNA.\n\nGiven a string s that represents a DNA sequence, return all the 10-letter-long ...
[ { "hash": 1511514762205599000, "runtime": "108ms", "solution": "class Solution(object):\n def findRepeatedDnaSequences(self, s):\n \"\"\"\n :type s: str\n :rtype: List[str]\n \"\"\"\n oneSet = {}\n ans = []\n for index in range(len(s) - 9):\n ...
class Solution(object): def findRepeatedDnaSequences(self, s): """ :type s: str :rtype: List[str] """
None
None
None
None
None
None
48.1
Level 3
Hi
check-if-every-row-and-column-contains-all-numbers
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>An <code>n x n</code> matrix is <strong>valid</strong> if every row and every column contains <strong>all</strong> the integers from <code>1</code> to <code>n</code> (<strong>inclusive</strong>).</p>\n\n<p>Given an <code>n x n</co...
2133
Easy
[ "An n x n matrix is valid if every row and every column contains all the integers from 1 to n (inclusive).\n\nGiven an n x n integer matrix matrix, return true if the matrix is valid. Otherwise, return false.\n\n \nExample 1:\n\nInput: matrix = [[1,2,3],[3,1,2],[2,3,1]]\nOutput: true\nExplanation: In this case, n =...
[ { "hash": -5089169251865357000, "runtime": "731ms", "solution": "class Solution(object):\n def checkValid(self, matrix):\n n = len(matrix)\n \n # on vérifie la condition sur chaque ligne\n for ligne in matrix:\n total = []\n for case in ligne:\n ...
class Solution(object): def checkValid(self, matrix): """ :type matrix: List[List[int]] :rtype: bool """
None
None
None
None
None
None
51.6
Level 1
Hi
largest-color-value-in-a-directed-graph
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p>\r\n\r\n<p>You are given a string <code>colors</code> where ...
1857
Hard
[ "There is a directed graph of n colored nodes and m edges. The nodes are numbered from 0 to n - 1.\n\nYou are given a string colors where colors[i] is a lowercase English letter representing the color of the ith node in this graph (0-indexed). You are also given a 2D array edges where edges[j] = [aj, bj] indicates ...
[ { "hash": -5997990508831441000, "runtime": "1609ms", "solution": "class Solution(object):\n def largestPathValue(self, colors, edges):\n \"\"\"\n :type colors: str\n :type edges: List[List[int]]\n :rtype: int\n \"\"\"\n \n n = len(colors)\n colo...
class Solution(object): def largestPathValue(self, colors, edges): """ :type colors: str :type edges: List[List[int]] :rtype: int """
None
None
None
None
None
None
50.8
Level 5
Hi
count-pairs-of-points-with-distance-k
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>2D</strong> integer array <code>coordinates</code> and an integer <code>k</code>, where <code>coordinates[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> are the coordinates of the <code>i<sup>th</sup></code> po...
2857
Medium
[ "You are given a 2D integer array coordinates and an integer k, where coordinates[i] = [xi, yi] are the coordinates of the ith point in a 2D plane.\n\nWe define the distance between two points (x₁, y₁) and (x₂, y₂) as (x1 XOR x2) + (y1 XOR y2) where XOR is the bitwise XOR operation.\n\nReturn the number of pairs (i...
[ { "hash": 5788878507206710000, "runtime": "3481ms", "solution": "class Solution(object):\n def countPairs(self, coordinates, k):\n \"\"\"\n :type coordinates: List[List[int]]\n :type k: int\n :rtype: int\n \"\"\"\n coordinates,ans=Counter(map(tuple,coordinate...
class Solution(object): def countPairs(self, coordinates, k): """ :type coordinates: List[List[int]] :type k: int :rtype: int """
None
None
None
None
None
None
31.6
Level 4
Hi
design-linked-list
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Design your implementation of the linked list. You can choose to use a singly or doubly linked list.<br />\nA node in a singly linked list should have two attributes: <code>val</code> and <code>next</code>. <code>val</code> is the...
707
Medium
[ "Design your implementation of the linked list. You can choose to use a singly or doubly linked list.\nA node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.\nIf you want to use the doubly linked list, you will...
[ { "hash": 3333765859882256000, "runtime": "77ms", "solution": "class MyLinkedList:\n\n def __init__(self):\n self.l = []\n \n\n def get(self, index: int) -> int:\n if index<0 or index>=len(self.l): return -1\n return self.l[index]\n\n def addAtHead(self, val: int) ->...
class MyLinkedList(object): def __init__(self): def get(self, index): """ :type index: int :rtype: int """ def addAtHead(self, val): """ :type val: int :rtype: None """ def addAtTail(self, val): """ ...
None
None
None
None
None
None
28
Level 4
Hi
k-th-symbol-in-grammar
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>We build a table of <code>n</code> rows (<strong>1-indexed</strong>). We start by writing <code>0</code> in the <code>1<sup>st</sup></code> row. Now in every subsequent row, we look at the previous row and replace each occurrence ...
779
Medium
[ "We build a table of n rows (1-indexed). We start by writing 0 in the 1st row. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.\n\n\n\tFor example, for n = 3, the 1st row is 0, the 2nd row is 01, and the 3rd row is 0110.\n\n\nGiven ...
[ { "hash": -4535787640440895000, "runtime": "19ms", "solution": "class Solution(object):\n def kthGrammar(self, level, pos):\n if pos == 1:\n return 0\n Grammer = self.kthGrammar(level-1, (pos+1)//2)\n \n if Grammer == 0:\n if pos % 2 == 0:\n ...
class Solution(object): def kthGrammar(self, n, k): """ :type n: int :type k: int :rtype: int """
None
None
None
None
None
None
46.2
Level 4
Hi
course-schedule-ii
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>There are a total of <code>numCourses</code> courses you have to take, labeled from <code>0</code> to <code>numCourses - 1</code>. You are given an array <code>prerequisites</code> where <code>prerequisites[i] = [a<sub>i</sub>, b<...
210
Medium
[ "There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.\n\n\n\tFor example, the pair [0, 1], indicates that to take course 0 you hav...
[ { "hash": -1468179241892916200, "runtime": "75ms", "solution": "class Solution:\n def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:\n adj=[[] for i in range(numCourses)]\n res=[]\n for u,v in prerequisites:\n adj[v].append(u)\n v...
class Solution(object): def findOrder(self, numCourses, prerequisites): """ :type numCourses: int :type prerequisites: List[List[int]] :rtype: List[int] """
None
None
None
None
None
None
49.6
Level 3
Hi
find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given two binary trees <code>original</code> and <code>cloned</code> and given a reference to a node <code>target</code> in the original tree.</p>\n\n<p>The <code>cloned</code> tree is a <strong>copy of</strong> the <code>original...
1379
Easy
[ "Given two binary trees original and cloned and given a reference to a node target in the original tree.\n\nThe cloned tree is a copy of the original tree.\n\nReturn a reference to the same node in the cloned tree.\n\nNote that you are not allowed to change any of the two trees or the target node and the answer mus...
[ { "hash": -9118669115666503000, "runtime": "509ms", "solution": "# Definition for a binary tree node.\n# class TreeNode(object):\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\nclass Solution(object):\n def getTargetCopy(self, original...
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def getTargetCopy(self, original, cloned, target): """ :type original: TreeNode :type cloned: Tre...
None
None
None
None
None
None
85.9
Level 1
Hi
valid-parenthesis-string
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a string <code>s</code> containing only three types of characters: <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code> and <code>&#39;*&#39;</code>, return <code>true</code> <em>if</em> <code>s</code> <em>is <strong>valid</st...
678
Medium
[ "Given a string s containing only three types of characters: '(', ')' and '*', return true if s is valid.\n\nThe following rules define a valid string:\n\n\n\tAny left parenthesis '(' must have a corresponding right parenthesis ')'.\n\tAny right parenthesis ')' must have a corresponding left parenthesis '('.\n\tLef...
[ { "hash": -2865892876218404400, "runtime": "14ms", "solution": "class Solution(object):\n def checkValidString(self, s):\n \"\"\"\n :type s: str\n :rtype: bool\n \"\"\"\n leftMin, leftMax = 0, 0\n\n for c in s:\n if c == \"(\":\n lef...
class Solution(object): def checkValidString(self, s): """ :type s: str :rtype: bool """
None
None
None
None
None
None
34.6
Level 4
Hi
search-in-a-binary-search-tree
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given the <code>root</code> of a binary search tree (BST) and an integer <code>val</code>.</p>\n\n<p>Find the node in the BST that the node&#39;s value equals <code>val</code> and return the subtree rooted with that node. ...
700
Easy
[ "You are given the root of a binary search tree (BST) and an integer val.\n\nFind the node in the BST that the node's value equals val and return the subtree rooted with that node. If such a node does not exist, return null.\n\n \nExample 1:\n\nInput: root = [4,2,7,1,3], val = 2\nOutput: [2,1,3]\n\n\nExample 2:\n\n...
[ { "hash": -2576799646301725000, "runtime": "73ms", "solution": "# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def searchBST(self, r...
# 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 searchBST(self, root, val): """ :type root: TreeNode :type val: ...
None
None
None
None
None
None
78.9
Level 1
Hi
number-of-steps-to-reduce-a-number-in-binary-representation-to-one
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given the binary representation of an integer as a string <code>s</code>, return <em>the number of steps to reduce it to </em><code>1</code><em> under the following rules</em>:</p>\n\n<ul>\n\t<li>\n\t<p>If the current number is ev...
1404
Medium
[ "Given the binary representation of an integer as a string s, return the number of steps to reduce it to 1 under the following rules:\n\n\n\t\n\tIf the current number is even, you have to divide it by 2.\n\t\n\t\n\tIf the current number is odd, you have to add 1 to it.\n\t\n\n\nIt is guaranteed that you can always ...
[ { "hash": 2724714421396122000, "runtime": "8ms", "solution": "class Solution(object):\n def numSteps(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n \"\"\"\n decimal = convertToBinary(s)\n steps =0\n while decimal!=1:\n if decimal%2==0:\n ...
class Solution(object): def numSteps(self, s): """ :type s: str :rtype: int """
None
None
None
None
None
None
52.7
Level 3
Hi
k-highest-ranked-items-within-a-price-range
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>0-indexed</strong> 2D integer array <code>grid</code> of size <code>m x n</code> that represents a map of the items in a shop. The integers in the grid represent the following:</p>\n\n<ul>\n\t<li><code>0</c...
2146
Medium
[ "You are given a 0-indexed 2D integer array grid of size m x n that represents a map of the items in a shop. The integers in the grid represent the following:\n\n\n\t0 represents a wall that you cannot pass through.\n\t1 represents an empty cell that you can freely move to and from.\n\tAll other positive integers r...
[ { "hash": -8720607347355120000, "runtime": "2409ms", "solution": "class Solution(object):\n from collections import deque\n def highestRankedKItems(self, grid, pricing, start, k):\n \"\"\"\n :type grid: List[List[int]]\n :type pricing: List[int]\n :type start: List[int]...
class Solution(object): def highestRankedKItems(self, grid, pricing, start, k): """ :type grid: List[List[int]] :type pricing: List[int] :type start: List[int] :type k: int :rtype: List[List[int]] """
None
None
None
None
None
None
42.1
Level 4
Hi
repeated-string-match
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given two strings <code>a</code> and <code>b</code>, return <em>the minimum number of times you should repeat string </em><code>a</code><em> so that string</em> <code>b</code> <em>is a substring of it</em>. If it is impossible for...
686
Medium
[ "Given two strings a and b, return the minimum number of times you should repeat string a so that string b is a substring of it. If it is impossible for b​​​​​​ to be a substring of a after repeating it, return -1.\n\nNotice: string \"abc\" repeated 0 times is \"\", repeated 1 time is \"abc\" and repeated 2 times i...
[ { "hash": -8424804256067555000, "runtime": "106ms", "solution": "class Solution(object):\n def repeatedStringMatch(self, a, b):\n\n counter = 1 \n temp = a\n\n while len(a) < len(b):\n a = a + temp\n counter += 1\n\n if b in a:\n return cou...
class Solution(object): def repeatedStringMatch(self, a, b): """ :type a: str :type b: str :rtype: int """
None
None
None
None
None
None
34.5
Level 4
Hi
minimum-moves-to-equal-array-elements
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer array <code>nums</code> of size <code>n</code>, return <em>the minimum number of moves required to make all array elements equal</em>.</p>\n\n<p>In one move, you can increment <code>n - 1</code> elements of the ar...
453
Medium
[ "Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.\n\nIn one move, you can increment n - 1 elements of the array by 1.\n\n \nExample 1:\n\nInput: nums = [1,2,3]\nOutput: 3\nExplanation: Only three moves are needed (remember each move increments two ...
[ { "hash": -2977889015275073500, "runtime": "204ms", "solution": "class Solution(object):\n def minMoves(self, nums):\n \"\"\"\n :type nums: List[int]\n :rtype: int\n \"\"\"\n min_value = nums[0]\n moves = 0\n\n for i in range(1, len(nums)):\n ...
class Solution(object): def minMoves(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
56.5
Level 3
Hi
check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a <code>sentence</code> that consists of some words separated by a <strong>single space</strong>, and a <code>searchWord</code>, check if <code>searchWord</code> is a prefix of any word in <code>sentence</code>.</p>\n\n<p>Re...
1455
Easy
[ "Given a sentence that consists of some words separated by a single space, and a searchWord, check if searchWord is a prefix of any word in sentence.\n\nReturn the index of the word in sentence (1-indexed) where searchWord is a prefix of this word. If searchWord is a prefix of more than one word, return the index o...
[ { "hash": -8826136118669222000, "runtime": "10ms", "solution": "class Solution(object):\n def isPrefixOfWord(self, sentence, searchWord):\n \"\"\"\n :type sentence: str\n :type searchWord: str\n :rtype: int\n \"\"\"\n\n word_list=sentence.split()\n for...
class Solution(object): def isPrefixOfWord(self, sentence, searchWord): """ :type sentence: str :type searchWord: str :rtype: int """
None
None
None
None
None
None
64.1
Level 1
Hi
total-appeal-of-a-string
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>The <b>appeal</b> of a string is the number of <strong>distinct</strong> characters found in the string.</p>\n\n<ul>\n\t<li>For example, the appeal of <code>&quot;abbca&quot;</code> is <code>3</code> because it has <code>3</code> ...
2262
Hard
[ "The appeal of a string is the number of distinct characters found in the string.\n\n\n\tFor example, the appeal of \"abbca\" is 3 because it has 3 distinct characters: 'a', 'b', and 'c'.\n\n\nGiven a string s, return the total appeal of all of its substrings.\n\nA substring is a contiguous sequence of characters w...
[ { "hash": 8981706439573810000, "runtime": "119ms", "solution": "class Solution(object):\n def appealSum(self, s):\n \"\"\"\n :type s: str\n :rtype: int\n\n sol1: n^3 solution\n sol2: n^2 solution calculate freq duing add up\n sol3: utilize formual\n\n ...
class Solution(object): def appealSum(self, s): """ :type s: str :rtype: int """
None
None
None
None
None
None
55.5
Level 5
Hi