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
flatten-binary-tree-to-linked-list
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given the <code>root</code> of a binary tree, flatten the tree into a &quot;linked list&quot;:</p>\n\n<ul>\n\t<li>The &quot;linked list&quot; should use the same <code>TreeNode</code> class where the <code>right</code> child point...
114
Medium
[ "Given the root of a binary tree, flatten the tree into a \"linked list\":\n\n\n\tThe \"linked list\" should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.\n\tThe \"linked list\" should be in the same order as a pre-order trav...
[ { "hash": -1458688896394254300, "runtime": "18ms", "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 flatten(self, roo...
# 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 flatten(self, root): """ :type root: TreeNode :rtype: None Do no...
None
None
None
None
None
None
63.8
Level 2
Hi
remove-duplicates-from-sorted-array-ii
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href=\"https://en.wikipedia.org/wiki/In-place_algorithm\" target=\"_blank\"><strong>in-place</strong></a> such tha...
80
Medium
[ "Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.\n\nSince it is impossible to change the length of the array in some languages, you must instead have the resul...
[ { "hash": -1508618244638063600, "runtime": "56ms", "solution": "class Solution:\n def removeDuplicates(self, nums: List[int]) -> int:\n flag, l = 1, 0\n for r in range(1, len(nums)):\n if nums[l] != nums[r]:\n l += 1\n flag = 1\n n...
class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
56
Level 3
Hi
string-to-integer-atoi
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer (similar to C/C++&#39;s <code>atoi</code> function).</p>\n\n<p>The algorithm for <code>myAtoi(string s)</code> is as follows:...
8
Medium
[ "Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++'s atoi function).\n\nThe algorithm for myAtoi(string s) is as follows:\n\n\n\tRead in and ignore any leading whitespace.\n\tCheck if the next character (if not already at the end of the string) is '-' or '...
[ { "hash": 6737426818825551000, "runtime": "42ms", "solution": "class Solution:\n def myAtoi(self, s: str) -> int:\n state, sign, num = 0, 1, 0\n\n for i, ch in enumerate(s):\n if state == 0:\n if ch == ' ':\n continue\n elif ch...
class Solution(object): def myAtoi(self, s): """ :type s: str :rtype: int """
None
None
None
None
None
None
16.9
Level 4
Hi
min-stack
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.</p>\n\n<p>Implement the <code>MinStack</code> class:</p>\n\n<ul>\n\t<li><code>MinStack()</code> initializes the stack object.</li>\n...
155
Medium
[ "Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.\n\nImplement the MinStack class:\n\n\n\tMinStack() initializes the stack object.\n\tvoid push(int val) pushes the element val onto the stack.\n\tvoid pop() removes the element on the top of the stack.\n\tint top() get...
[ { "hash": -6944780556494614000, "runtime": "38ms", "solution": "class MinStack:\n\n def __init__(self):\n self.stack = []\n self.minStack = []\n\n def push(self, val: int) -> None:\n self.stack.append(val)\n if self.minStack:\n val = min(self.minStack[-1],val...
class MinStack(object): def __init__(self): def push(self, val): """ :type val: int :rtype: None """ def pop(self): """ :rtype: None """ def top(self): """ :rtype: int """ def get...
None
None
None
None
None
None
53.4
Level 3
Hi
all-oone-data-structure
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Design a data structure to store the strings&#39; count with the ability to return the strings with minimum and maximum counts.</p>\n\n<p>Implement the <code>AllOne</code> class:</p>\n\n<ul>\n\t<li><code>AllOne()</code> Initialize...
432
Hard
[ "Design a data structure to store the strings' count with the ability to return the strings with minimum and maximum counts.\n\nImplement the AllOne class:\n\n\n\tAllOne() Initializes the object of the data structure.\n\tinc(String key) Increments the count of the string key by 1. If key does not exist in the data ...
[ { "hash": -2655373376809774600, "runtime": "215ms", "solution": "class Node(object):\n def __init__(self,key, val=None, next = None,prev=None):\n self.key = key\n self.val = val\n self.next = next\n self.prev = prev\nclass AllOne(object):\n def __init__(self):\n ...
class AllOne(object): def __init__(self): def inc(self, key): """ :type key: str :rtype: None """ def dec(self, key): """ :type key: str :rtype: None """ def getMaxKey(self): """ :rtype: str ...
None
None
None
None
None
None
36.5
Level 5
Hi
clone-graph
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a reference of a node in a <strong><a href=\"https://en.wikipedia.org/wiki/Connectivity_(graph_theory)#Connected_graph\" target=\"_blank\">connected</a></strong> undirected graph.</p>\n\n<p>Return a <a href=\"https://en.wiki...
133
Medium
[ "Given a reference of a node in a connected undirected graph.\n\nReturn a deep copy (clone) of the graph.\n\nEach node in the graph contains a value (int) and a list (List[Node]) of its neighbors.\n\nclass Node {\n public int val;\n public List<Node> neighbors;\n}\n\n\n \n\nTest case format:\n\nFor simplicity...
[ { "hash": 4614293601134416000, "runtime": "49ms", "solution": "\"\"\"\n# Definition for a Node.\nclass Node:\n def __init__(self, val = 0, neighbors = None):\n self.val = val\n self.neighbors = neighbors if neighbors is not None else []\n\"\"\"\n\nfrom typing import Optional\nclass Solu...
""" # Definition for a Node. class Node(object): def __init__(self, val = 0, neighbors = None): self.val = val self.neighbors = neighbors if neighbors is not None else [] """ class Solution(object): def cloneGraph(self, node): """ :type node: Node :rtype: Node ""...
None
None
None
None
None
None
55.6
Level 3
Hi
range-sum-query-immutable
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer array <code>nums</code>, handle multiple queries of the following type:</p>\n\n<ol>\n\t<li>Calculate the <strong>sum</strong> of the elements of <code>nums</code> between indices <code>left</code> and <code>right<...
303
Easy
[ "Given an integer array nums, handle multiple queries of the following type:\n\n\n\tCalculate the sum of the elements of nums between indices left and right inclusive where left <= right.\n\n\nImplement the NumArray class:\n\n\n\tNumArray(int[] nums) Initializes the object with the integer array nums.\n\tint sumRan...
[ { "hash": -7224419793767653000, "runtime": "1540ms", "solution": "class NumArray:\n\n def __init__(self, nums: List[int]):\n self.nums = nums\n\n def sumRange(self, left: int, right: int) -> int:\n sum = 0\n for num in self.nums[left:right + 1]:\n sum += num\n ...
class NumArray(object): def __init__(self, nums): """ :type nums: List[int] """ def sumRange(self, left, right): """ :type left: int :type right: int :rtype: int """ # Your NumArray object will be instantiated and called as su...
None
None
None
None
None
None
61.9
Level 1
Hi
serialize-and-deserialize-binary-tree
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the sa...
297
Hard
[ "Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.\n\nDesign an algorithm to serialize and deseriali...
[ { "hash": -383627231256933000, "runtime": "103ms", "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 Codec:\n\n def serialize(self, root):\n \"\"\"...
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Codec: def serialize(self, root): """Encodes a tree to a single string. :type root: TreeNode :rtype: str ...
None
None
None
None
None
None
56.2
Level 5
Hi
populating-next-right-pointers-in-each-node-ii
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a binary tree</p>\n\n<pre>\nstruct Node {\n int val;\n Node *left;\n Node *right;\n Node *next;\n}\n</pre>\n\n<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointe...
117
Medium
[ "Given a binary tree\n\nstruct Node {\n int val;\n Node *left;\n Node *right;\n Node *next;\n}\n\n\nPopulate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.\n\nInitially, all next pointers are set to NULL.\n\n \nExample 1:\n\nInput: root...
[ { "hash": -3263974094173182000, "runtime": "47ms", "solution": "\"\"\"\n# Definition for a Node.\nclass Node:\n def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):\n self.val = val\n self.left = left\n self.right = right\n self...
""" # Definition for a Node. class Node(object): def __init__(self, val=0, left=None, right=None, next=None): self.val = val self.left = left self.right = right self.next = next """ class Solution(object): def connect(self, root): """ :type root: Node :rt...
None
None
None
None
None
None
51.6
Level 3
Hi
remove-element
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer array <code>nums</code> and an integer <code>val</code>, remove all occurrences of <code>val</code> in <code>nums</code> <a href=\"https://en.wikipedia.org/wiki/In-place_algorithm\" target=\"_blank\"><strong>in-pl...
27
Easy
[ "Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.\n\nConsider the number of elements in nums which are not equal to val be k, to get accepted, you need to d...
[ { "hash": 3094047977479763500, "runtime": "35ms", "solution": "class Solution:\n def removeElement(self, nums: List[int], val: int) -> int:\n l = len(nums)\n i = 0\n\n while i < l:\n if nums[i] == val:\n while l > 0 and nums[l-1] == val:\n ...
class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """
None
None
None
None
None
None
55.2
Level 1
Hi
implement-queue-using-stacks
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (<code>push</code>, <code>peek</code>, <code>pop</code>, and <code>empty</code>).</p>\n\n<...
232
Easy
[ "Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty).\n\nImplement the MyQueue class:\n\n\n\tvoid push(int x) Pushes element x to the back of the queue.\n\tint pop() Removes the element from the fron...
[ { "hash": -6206733782731865000, "runtime": "41ms", "solution": "class MyQueue:\n\n def __init__(self):\n self.s1 = []\n self.s2 = [] \n\n def push(self, x: int) -> None:\n self.s1.append(x)\n \n\n def pop(self) -> int:\n self.peek()\n return self...
class MyQueue(object): def __init__(self): def push(self, x): """ :type x: int :rtype: None """ def pop(self): """ :rtype: int """ def peek(self): """ :rtype: int """ def empty(se...
None
None
None
None
None
None
64.2
Level 1
Hi
my-calendar-iii
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A <code>k</code>-booking happens when <code>k</code> events have some non-empty intersection (i.e., there is some time that is common to all <code>k</code> events.)</p>\n\n<p>You are given some events <code>[startTime, endTime)</c...
732
Hard
[ "A k-booking happens when k events have some non-empty intersection (i.e., there is some time that is common to all k events.)\n\nYou are given some events [startTime, endTime), after each given event, return an integer k representing the maximum k-booking between all the previous events.\n\nImplement the MyCalenda...
[ { "hash": 7801637067158258000, "runtime": "1118ms", "solution": "class MyCalendarThree(object):\n\n def __init__(self):\n self.dict0={}\n \n\n def book(self, startTime, endTime):\n \"\"\"\n :type startTime: int\n :type endTime: int\n :rtype: int\n \...
class MyCalendarThree(object): def __init__(self): def book(self, startTime, endTime): """ :type startTime: int :type endTime: int :rtype: int """ # Your MyCalendarThree object will be instantiated and called as such: # obj = MyCalendarThree() # para...
None
None
None
None
None
None
71.6
Level 5
Hi
prefix-and-suffix-search
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Design a special dictionary that searches the words in it by a prefix and a suffix.</p>\n\n<p>Implement the <code>WordFilter</code> class:</p>\n\n<ul>\n\t<li><code>WordFilter(string[] words)</code> Initializes the object with the ...
745
Hard
[ "Design a special dictionary that searches the words in it by a prefix and a suffix.\n\nImplement the WordFilter class:\n\n\n\tWordFilter(string[] words) Initializes the object with the words in the dictionary.\n\tf(string pref, string suff) Returns the index of the word in the dictionary, which has the prefix pref...
[ { "hash": 6530719457292058000, "runtime": "1328ms", "solution": "class WordFilter(object):\n\n def __init__(self, words):\n \n self.d = {}\n \n for w in range(len(words)):\n word = words[w]\n \n for j in range(len(word)-1 , -1 , -1): # for s...
class WordFilter(object): def __init__(self, words): """ :type words: List[str] """ def f(self, pref, suff): """ :type pref: str :type suff: str :rtype: int """ # Your WordFilter object will be instantiated and called as such:...
None
None
None
None
None
None
41
Level 5
Hi
binary-search-tree-iterator
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Implement the <code>BSTIterator</code> class that represents an iterator over the <strong><a href=\"https://en.wikipedia.org/wiki/Tree_traversal#In-order_(LNR)\" target=\"_blank\">in-order traversal</a></strong> of a binary search...
173
Medium
[ "Implement the BSTIterator class that represents an iterator over the in-order traversal of a binary search tree (BST):\n\n\n\tBSTIterator(TreeNode root) Initializes an object of the BSTIterator class. The root of the BST is given as part of the constructor. The pointer should be initialized to a non-existent numbe...
[ { "hash": 2612294384402682000, "runtime": "35ms", "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 BSTIterator(object):\n\n 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 BSTIterator(object): def __init__(self, root): """ :type root: TreeNode """ ...
None
None
None
None
None
None
71.1
Level 2
Hi
linked-list-cycle-ii
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given the <code>head</code> of a linked list, return <em>the node where the cycle begins. If there is no cycle, return </em><code>null</code>.</p>\n\n<p>There is a cycle in a linked list if there is some node in the list that can ...
142
Medium
[ "Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.\n\nThere is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail'...
[ { "hash": -3473160001557572600, "runtime": "42ms", "solution": "# Definition for singly-linked list.\n# class ListNode(object):\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\nclass Solution(object):\n def detectCycle(self, head):\n if head == None or hea...
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def detectCycle(self, head): """ :type head: ListNode :rtype: ListNode """
None
None
None
None
None
None
50.3
Level 3
Hi
powx-n
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Implement <a href=\"http://www.cplusplus.com/reference/valarray/pow/\" target=\"_blank\">pow(x, n)</a>, which calculates <code>x</code> raised to the power <code>n</code> (i.e., <code>x<sup>n</sup></code>).</p>\n\n<p>&nbsp;</p>\n<...
50
Medium
[ "Implement pow(x, n), which calculates x raised to the power n (i.e., xn).\n\n \nExample 1:\n\nInput: x = 2.00000, n = 10\nOutput: 1024.00000\n\n\nExample 2:\n\nInput: x = 2.10000, n = 3\nOutput: 9.26100\n\n\nExample 3:\n\nInput: x = 2.00000, n = -2\nOutput: 0.25000\nExplanation: 2-2 = 1/2² = 1/4 = 0.25\n\n\n \nCon...
[ { "hash": -1614412672151899100, "runtime": "36ms", "solution": "class Solution:\n def myPow(self, x: float, n: int) -> float:\n\n return x**n\n\n\n " }, { "hash": -3678993015712523000, "runtime": "26ms", "solution": "class Solution:\n def myPow(self, x: float, n: int)...
class Solution(object): def myPow(self, x, n): """ :type x: float :type n: int :rtype: float """
None
None
None
None
None
None
34.2
Level 4
Hi
move-zeroes
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer array <code>nums</code>, move all <code>0</code>&#39;s to the end of it while maintaining the relative order of the non-zero elements.</p>\n\n<p><strong>Note</strong> that you must do this in-place without making ...
283
Easy
[ "Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.\n\nNote that you must do this in-place without making a copy of the array.\n\n \nExample 1:\nInput: nums = [0,1,0,3,12]\nOutput: [1,3,12,0,0]\nExample 2:\nInput: nums = [0]\nOutput: [0]\n\n \nC...
[ { "hash": -8092627721611206000, "runtime": "1793ms", "solution": "class Solution:\n def moveZeroes(self, nums: List[int]) -> None:\n \"\"\"\n Do not return anything, modify nums in-place instead.\n \"\"\"\n for i in range(len(nums)):\n if nums[i]:\n ...
class Solution(object): def moveZeroes(self, nums): """ :type nums: List[int] :rtype: None Do not return anything, modify nums in-place instead. """
None
None
None
None
None
None
61.5
Level 1
Hi
intersection-of-two-linked-lists
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given the heads of two singly linked-lists <code>headA</code> and <code>headB</code>, return <em>the node at which the two lists intersect</em>. If the two linked lists have no intersection at all, return <code>null</code>.</p>\n\...
160
Easy
[ "Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.\n\nFor example, the following two linked lists begin to intersect at node c1:\n\nThe test cases are generated such that there are no cycles...
[ { "hash": -5423285930565351000, "runtime": "132ms", "solution": "# Definition for singly-linked list.\n# class ListNode(object):\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\nclass Solution(object):\n def getIntersectionNode(self, headA, headB):\n \"\"\"\...
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def getIntersectionNode(self, headA, headB): """ :type head1, head1: ListNode :rtype: ListNode """
None
None
None
None
None
None
56.3
Level 1
Hi
data-stream-as-disjoint-intervals
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given a data stream input of non-negative integers <code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code>, summarize the numbers seen so far as a list of disjoint intervals.</p>\n\n<p>Implement the <code>SummaryRanges</code...
352
Hard
[ "Given a data stream input of non-negative integers a₁, a₂, ..., an, summarize the numbers seen so far as a list of disjoint intervals.\n\nImplement the SummaryRanges class:\n\n\n\tSummaryRanges() Initializes the object with an empty stream.\n\tvoid addNum(int value) Adds the integer value to the stream.\n\tint[][]...
[ { "hash": -2532639113853905400, "runtime": "15ms", "solution": "class SummaryRanges:\n\n def __init__(self):\n \"\"\"\n Initialize your data structure here.\n \"\"\"\n self.intervals = [[float('-inf'), float('-inf')], [float('inf'), float('inf')]]\n \n\n def addN...
class SummaryRanges(object): def __init__(self): def addNum(self, value): """ :type value: int :rtype: None """ def getIntervals(self): """ :rtype: List[List[int]] """ # Your SummaryRanges object will be instantiated and...
None
None
None
None
None
None
59.8
Level 5
Hi
find-peak-element
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A peak element is an element that is strictly greater than its neighbors.</p>\n\n<p>Given a <strong>0-indexed</strong> integer array <code>nums</code>, find a peak element, and return its index. If the array contains multiple peak...
162
Medium
[ "A peak element is an element that is strictly greater than its neighbors.\n\nGiven a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.\n\nYou may imagine that nums[-1] = nums[n] = -∞. In other words, an element is al...
[ { "hash": -1658407933902814200, "runtime": "38ms", "solution": "class Solution:\n def findPeakElement(self, nums: List[int]) -> int:\n n = len(nums)\n l = 0 \n r = n - 1\n while l <= r:\n mid = l + (r-l)//2\n if mid > 0 and nums[mid-1] > nums[mid]:\n ...
class Solution(object): def findPeakElement(self, nums): """ :type nums: List[int] :rtype: int """
None
None
None
None
None
None
45.8
Level 4
Hi
reverse-bits
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Reverse bits of a given 32 bits unsigned integer.</p>\n\n<p><strong>Note:</strong></p>\n\n<ul>\n\t<li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given ...
190
Easy
[ "Reverse bits of a given 32 bits unsigned integer.\n\nNote:\n\n\n\tNote that in some languages, such as Java, there is no unsigned integer type. In this case, both input and output will be given as a signed integer type. They should not affect your implementation, as the integer's internal binary representation is ...
[ { "hash": -2187065473253083100, "runtime": "23ms", "solution": "class Solution:\n # @param n, an integer\n # @return an integer\n def reverseBits(self, n):\n # Convert the integer to a binary string, reverse it, and pad with zeros\n binary_string = bin(n)[2:].zfill(32)\n\n ...
class Solution: # @param n, an integer # @return an integer def reverseBits(self, n):
None
None
None
None
None
None
56.8
Level 1
Hi
populating-next-right-pointers-in-each-node
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>\n\n<pre>\nstruct Node {\n int val;\n Node *left;\n...
116
Medium
[ "You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:\n\nstruct Node {\n int val;\n Node *left;\n Node *right;\n Node *next;\n}\n\n\nPopulate each next pointer to point to its next right node. If there is no ...
[ { "hash": -7723236337863566000, "runtime": "79ms", "solution": "\"\"\"\n# Definition for a Node.\nclass Node:\n def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):\n self.val = val\n self.left = left\n self.right = right\n self...
""" # Definition for a Node. class Node(object): def __init__(self, val=0, left=None, right=None, next=None): self.val = val self.left = left self.right = right self.next = next """ class Solution(object): def connect(self, root): """ :type root: Node :rt...
None
None
None
None
None
None
61.8
Level 2
Hi
group-anagrams
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an array of strings <code>strs</code>, group <strong>the anagrams</strong> together. You can return the answer in <strong>any order</strong>.</p>\n\n<p>An <strong>Anagram</strong> is a word or phrase formed by rearranging th...
49
Medium
[ "Given an array of strings strs, group the anagrams together. You can return the answer in any order.\n\nAn Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.\n\n \nExample 1:\nInput: strs = [\"eat\",\"tea\",\"tan\",\"a...
[ { "hash": 8147282127164134000, "runtime": "46ms", "solution": "f = open('user.out', 'w')\n\n\nfor strs in map(loads, stdin):\n w = defaultdict(list)\n for s in strs:\n w[''.join(sorted(s))].append(s)\n \n print(str(w.values())[12:-1].replace(\"'\", '\"').replace(\" \", \"\"), file=f)\...
class Solution(object): def groupAnagrams(self, strs): """ :type strs: List[str] :rtype: List[List[str]] """
None
None
None
None
None
None
67.2
Level 2
Hi
implement-stack-using-queues
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (<code>push</code>, <code>top</code>, <code>pop</code>, and <code>empty</code>).</p>\n\n<p>...
225
Easy
[ "Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).\n\nImplement the MyStack class:\n\n\n\tvoid push(int x) Pushes element x to the top of the stack.\n\tint pop() Removes the element on the top of th...
[ { "hash": 7838225155284446000, "runtime": "36ms", "solution": "class MyStack:\n\n def __init__(self):\n self.q1, self.q2 = collections.deque(), collections.deque()\n\n def push(self, x: int) -> None:\n self.q1.append(x)\n \n\n def pop(self) -> int:\n while len(self.q...
class MyStack(object): def __init__(self): def push(self, x): """ :type x: int :rtype: None """ def pop(self): """ :rtype: int """ def top(self): """ :rtype: int """ def empty(sel...
None
None
None
None
None
None
62.6
Level 1
Hi
longest-common-prefix
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Write a function to find the longest common prefix string amongst an array of strings.</p>\n\n<p>If there is no common prefix, return an empty string <code>&quot;&quot;</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Ex...
14
Easy
[ "Write a function to find the longest common prefix string amongst an array of strings.\n\nIf there is no common prefix, return an empty string \"\".\n\n \nExample 1:\n\nInput: strs = [\"flower\",\"flow\",\"flight\"]\nOutput: \"fl\"\n\n\nExample 2:\n\nInput: strs = [\"dog\",\"racecar\",\"car\"]\nOutput: \"\"\nExpla...
[ { "hash": -8132176980939346000, "runtime": "42ms", "solution": "class Solution:\n def longestCommonPrefix(self, strs: List[str]) -> str:\n def compare2(s1, s2, l1, l2, mymax):\n if l2<l1:\n s1,s2= s2,s1\n l1,l2= l2,l1\n i=0\n while...
class Solution(object): def longestCommonPrefix(self, strs): """ :type strs: List[str] :rtype: str """
None
None
None
None
None
None
41.9
Level 1
Hi
erect-the-fence
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an array <code>trees</code> where <code>trees[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents the location of a tree in the garden.</p>\n\n<p>Fence the entire garden using the minimum length of rope, as it is e...
587
Hard
[ "You are given an array trees where trees[i] = [xi, yi] represents the location of a tree in the garden.\n\nFence the entire garden using the minimum length of rope, as it is expensive. The garden is well-fenced only if all the trees are enclosed.\n\nReturn the coordinates of trees that are exactly located on the f...
[ { "hash": -1531062222928317000, "runtime": "172ms", "solution": "import itertools\n\n# Monotone Chain Algorithm\nclass Solution(object):\n def outerTrees(self, points):\n\t\n def ccw(A, B, C):\n return (B[0]-A[0])*(C[1]-A[1]) - (B[1]-A[1])*(C[0]-A[0])\n\n if len(points) <= 1:...
class Solution(object): def outerTrees(self, trees): """ :type trees: List[List[int]] :rtype: List[List[int]] """
None
None
None
None
None
None
51.8
Level 5
Hi
linked-list-cycle
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p>\n\n<p>There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following ...
141
Easy
[ "Given head, the head of a linked list, determine if the linked list has a cycle in it.\n\nThere is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is con...
[ { "hash": -1637782096246903000, "runtime": "832ms", "solution": "# Definition for singly-linked list.\n# class ListNode(object):\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\nclass Solution(object):\n def hasCycle(self, head):\n \"\"\"\n :type head...
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def hasCycle(self, head): """ :type head: ListNode :rtype: bool """
None
None
None
None
None
None
49.1
Level 1
Hi
copy-list-with-random-pointer
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>A linked list of length <code>n</code> is given such that each node contains an additional random pointer, which could point to any node in the list, or <code>null</code>.</p>\n\n<p>Construct a <a href=\"https://en.wikipedia.org/w...
138
Medium
[ "A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.\n\nConstruct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding ...
[ { "hash": 906869585509512000, "runtime": "45ms", "solution": "\"\"\"\n# Definition for a Node.\nclass Node:\n def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):\n self.val = int(x)\n self.next = next\n self.random = random\n\"\"\"\n\nclass Solution:\n def ...
""" # Definition for a Node. class Node: def __init__(self, x, next=None, random=None): self.val = int(x) self.next = next self.random = random """ class Solution(object): def copyRandomList(self, head): """ :type head: Node :rtype: Node """
None
None
None
None
None
None
54.7
Level 3
Hi
random-pick-with-blacklist
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>You are given an integer <code>n</code> and an array of <strong>unique</strong> integers <code>blacklist</code>. Design an algorithm to pick a random integer in the range <code>[0, n - 1]</code> that is <strong>not</strong> in <co...
710
Hard
[ "You are given an integer n and an array of unique integers blacklist. Design an algorithm to pick a random integer in the range [0, n - 1] that is not in blacklist. Any integer that is in the mentioned range and not in blacklist should be equally likely to be returned.\n\nOptimize your algorithm such that it minim...
[ { "hash": -5062798063867445000, "runtime": "265ms", "solution": "class Solution(object):\n\n def __init__(self, n, blacklist):\n self.n = n\n if len(blacklist) > n // 3:\n self.whitelist = list(set(range(n)) - set(blacklist))\n else:\n self.blacklist = set(b...
class Solution(object): def __init__(self, n, blacklist): """ :type n: int :type blacklist: List[int] """ def pick(self): """ :rtype: int """ # Your Solution object will be instantiated and called as such: # obj = Solution(n, blacklis...
None
None
None
None
None
None
33.3
Level 5
Hi
max-points-on-a-line
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an array of <code>points</code> where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the <strong>X-Y</strong> plane, return <em>the maximum number of points that lie on the same straight line</...
149
Hard
[ "Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.\n\n \nExample 1:\n\nInput: points = [[1,1],[2,2],[3,3]]\nOutput: 3\n\n\nExample 2:\n\nInput: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]\nOutput: 4\n\n...
[ { "hash": 5815475300018747000, "runtime": "2080ms", "solution": "class Solution(object):\n def maxPoints(self, points):\n \"\"\"\n :type points: List[List[int]]\n :rtype: int\n \"\"\"\n ll = len(points) \n if ll == 1: return 1\n elif ll == 2: return 2\...
class Solution(object): def maxPoints(self, points): """ :type points: List[List[int]] :rtype: int """
None
None
None
None
None
None
26.1
Level 5
Hi
unique-binary-search-trees-ii
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Given an integer <code>n</code>, return <em>all the structurally unique <strong>BST&#39;</strong>s (binary search trees), which has exactly </em><code>n</code><em> nodes of unique values from</em> <code>1</code> <em>to</em> <code>...
95
Medium
[ "Given an integer n, return all the structurally unique BST's (binary search trees), which has exactly n nodes of unique values from 1 to n. Return the answer in any order.\n\n \nExample 1:\n\nInput: n = 3\nOutput: [[1,null,2,null,3],[1,null,3,2],[2,1,3],[3,1,null,null,2],[3,2,null,1]]\n\n\nExample 2:\n\nInput: n =...
[ { "hash": 1079829676123211900, "runtime": "59ms", "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 generateTrees(self...
# 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 generateTrees(self, n): """ :type n: int :rtype: List[TreeNode] ...
None
None
None
None
None
None
56.8
Level 3
Hi
insert-delete-getrandom-o1-duplicates-allowed
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p><code>RandomizedCollection</code> is a data structure that contains a collection of numbers, possibly duplicates (i.e., a multiset). It should support inserting and removing specific elements and also reporting a random element.</...
381
Hard
[ "RandomizedCollection is a data structure that contains a collection of numbers, possibly duplicates (i.e., a multiset). It should support inserting and removing specific elements and also reporting a random element.\n\nImplement the RandomizedCollection class:\n\n\n\tRandomizedCollection() Initializes the empty Ra...
[ { "hash": -8265696971294679000, "runtime": "325ms", "solution": "class RandomizedCollection(object):\n\n def __init__(self):\n self.lst = []\n self.idx = defaultdict(set) \n\n def insert(self, val):\n \"\"\"\n :type val: int\n :rtype: bool\n \"\"\"\n...
class RandomizedCollection(object): def __init__(self): def insert(self, val): """ :type val: int :rtype: bool """ def remove(self, val): """ :type val: int :rtype: bool """ def getRandom(self): """ ...
None
None
None
None
None
None
35.4
Level 5
Hi
number-of-1-bits
{ "data": { "question": { "categoryTitle": "Algorithms", "content": "<p>Write a function that takes&nbsp;the binary representation of an unsigned integer and returns the number of &#39;1&#39; bits it has (also known as the <a href=\"http://en.wikipedia.org/wiki/Hamming_weight\" target=\"_blank\">Hammi...
191
Easy
[ "Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).\n\nNote:\n\n\n\tNote that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It...
[ { "hash": -1772664895384409000, "runtime": "20ms", "solution": "class Solution(object):\n def hammingWeight(self, n):\n \"\"\"\n :type n: int\n :rtype: int\n \"\"\"\n res = 0\n for i in range(32):\n bit = (n >> i) & 1\n if bit:\n ...
class Solution(object): def hammingWeight(self, n): """ :type n: int :rtype: int """
None
None
None
None
None
None
69.7
Level 1
Hi