question_id
int64
1
3.58k
name
stringlengths
3
77
content
stringlengths
516
21.8k
acRate
float64
9.94
93
difficulty
stringclasses
3 values
topics
listlengths
0
10
runtimeDistribution
stringlengths
53
2.38k
memoryDistribution
stringlengths
54
2.14k
rt_list
listlengths
1
156
rt_solution_count
int64
1
156
mm_list
listlengths
1
154
mm_solution_count
int64
1
154
131
palindrome-partitioning
<p>Given a string <code>s</code>, partition <code>s</code> such that every <span data-keyword="substring-nonempty">substring</span> of the partition is a <span data-keyword="palindrome-string"><strong>palindrome</strong></span>. Return <em>all possible palindrome partitioning of </em><code>s</code>.</p> <p>&nbsp;</p> ...
70.275043
Medium
[ "string", "dynamic-programming", "backtracking" ]
{"lang": "python3", "distribution": [["102", 0.0178], ["108", 0.0089], ["114", 0.0089], ["119", 0.0089], ["125", 0.0089], ["131", 0.0178], ["136", 0.0089], ["142", 0.0089], ["147", 0.0355], ["153", 0.0711], ["159", 0.0355], ["164", 0.0444], ["170", 0.0444], ["176", 0.0444], ["181", 0.0444], ["187", 0.0622], ["193", 0.0...
{"lang": "python3", "distribution": [["22901", 0.0534], ["23105", 0.0089], ["23309", 0.0178], ["23513", 0.0089], ["23716", 0.0089], ["23920", 0.0089], ["24124", 0.0711], ["24328", 0.8441], ["24531", 4.1141], ["24735", 0.9419], ["24939", 0.08], ["25143", 0.0089], ["25346", 0.0089], ["25550", 0.0889], ["25754", 0.0533], ...
[ { "code": "# class Solution:\ndef partition( s: str) -> List[List[str]]:\n \n def find_palind(s):\n palins_end = defaultdict(list)\n for i in range(len(s)):\n if 0 < i < len(s)-1 and s[i-1] == s[i+1]:\n start,end = i-1,i+1\n while 0 <= start and end <...
121
[ { "code": "from collections.abc import Generator\nfrom functools import lru_cache\n\nclass Solution:\n def partition(self, string: str) -> Generator[list[str], None, None]:\n @lru_cache()\n def is_palindrome(left: int, right: int) -> bool:\n while left < right:\n if st...
112
132
palindrome-partitioning-ii
<p>Given a string <code>s</code>, partition <code>s</code> such that every <span data-keyword="substring-nonempty">substring</span> of the partition is a <span data-keyword="palindrome-string">palindrome</span>.</p> <p>Return <em>the <strong>minimum</strong> cuts needed for a palindrome partitioning of</em> <code>s</c...
34.332739
Hard
[ "string", "dynamic-programming" ]
{"lang": "python3", "distribution": [["71", 0.2292], ["128", 0.0764], ["185", 0.0764], ["243", 0.0764], ["300", 0.0764], ["357", 0.3056], ["414", 1.9864000000000002], ["471", 3.9728000000000008], ["529", 3.0560000000000005], ["586", 9.0916], ["643", 11.154399999999995], ["700", 5.500799999999996], ["757", 3.82000000000...
{"lang": "python3", "distribution": [["19711", 34.30139999999998], ["26535", 0.9168000000000002], ["33359", 0.1528], ["40183", 0.1528], ["47006", 41.864299999999986], ["53830", 0.1528], ["60654", 0.2292], ["67478", 0.0764], ["74301", 3.2851000000000004], ["81125", 4.736699999999999], ["87949", 0.0764], ["94773", 1.6808...
[ { "code": "class Solution:\n def minCut(self, s: str) -> int:\n if s == s[::-1]:\n return 0\n for index in range(1, len(s)):\n if s[:index] == s[:index][::-1] and s[index:] == s[index:][::-1]:\n return 1\n min_cuts = [cuts for cuts in range(-1, len(s)...
139
[ { "code": "class Solution:\n def minCut(self, s: str) -> int:\n N = len(s)\n dp = [i for i in range(-1,N)]\n for i in range(N):\n j = 0\n while i-j>=0 and i+j<N and s[i-j]==s[i+j]:\n dp[i+j+1] = min(dp[i+j+1], dp[i-j]+1)\n j+=1\n ...
84
133
clone-graph
<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> <p>Return a <a href="https://en.wikipedia.org/wiki/Object_copying#Deep_copy" target="_blank"><strong>deep copy</strong></a> (clone...
59.051185
Medium
[ "hash-table", "depth-first-search", "breadth-first-search", "graph" ]
{"lang": "python3", "distribution": [["19", 0.0228], ["20", 0.0228], ["21", 0.0137], ["22", 0.0365], ["23", 0.2007], ["24", 0.1004], ["25", 0.1642], ["26", 0.2372], ["27", 0.8485], ["28", 0.4014], ["29", 0.6067], ["30", 1.2636], ["31", 2.5454], ["32", 1.3867], ["33", 1.7517], ["34", 3.5946], ["35", 5.8252], ["36", 3.70...
{"lang": "python3", "distribution": [["16200", 0.0046], ["16500", 0.0091], ["16600", 0.1779], ["16700", 2.6594], ["16800", 20.6368], ["16900", 35.9228], ["17000", 35.0561]]}
[ { "code": "class Solution:\n def cloneGraph(self, node: \"Node\") -> \"Node\":\n oldToNew = {}\n\n def dfs(node):\n if node in oldToNew:\n return oldToNew[node]\n\n copy = Node(node.val)\n oldToNew[node] = copy\n for nei in node.neighbo...
53
[ { "code": "\"\"\"\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 Solution:\n def cloneGraph(self, node: Optional['Node']) -> O...
11
134
gas-station
<p>There are <code>n</code> gas stations along a circular route, where the amount of gas at the <code>i<sup>th</sup></code> station is <code>gas[i]</code>.</p> <p>You have a car with an unlimited gas tank and it costs <code>cost[i]</code> of gas to travel from the <code>i<sup>th</sup></code> station to its next <code>...
45.608174
Medium
[ "array", "greedy" ]
{"lang": "python3", "distribution": [["90", 0.066], ["102", 0.059300000000000005], ["113", 0.019799999999999998], ["125", 0.0066], ["136", 0.0197], ["147", 0.0066], ["159", 0.0066], ["170", 0.0066], ["182", 0.0066], ["193", 0.0197], ["205", 0.0197], ["216", 0.0197], ["227", 0.0329], ["239", 0.0132], ["250", 0.0789], ["...
{"lang": "python3", "distribution": [["19200", 0.0066], ["19900", 0.0066], ["20000", 0.0395], ["20100", 0.0526], ["20200", 0.0132], ["20400", 0.0066], ["20500", 0.0197], ["20600", 0.0132], ["20700", 0.0263], ["20800", 0.0132], ["20900", 0.0066], ["21000", 0.0132], ["21100", 0.0329], ["21200", 0.0395], ["21300", 0.046],...
[ { "code": "def canCompleteCircuit(gas: List[int], cost: List[int]) -> int:\n if sum(gas) < sum(cost):\n return -1\n total = 0\n start = 0\n for i in range(len(gas)):\n total += (gas[i] - cost[i])\n if total < 0:\n total = 0\n start = i+1\n\n return start...
135
[ { "code": "class Solution:\n def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:\n sum_cost = sum(cost)\n sum_gas = sum(gas)\n\n if sum_cost > sum_gas:\n return -1\n\n cur_gas = 0\n start_ind = 0\n\n for i in range(len(gas)):\n ...
52
135
candy
<p>There are <code>n</code> children standing in a line. Each child is assigned a rating value given in the integer array <code>ratings</code>.</p> <p>You are giving candies to these children subjected to the following requirements:</p> <ul> <li>Each child must have at least one candy.</li> <li>Children with a high...
43.844561
Hard
[ "array", "greedy" ]
{"lang": "python3", "distribution": [["42", 0.0197], ["44", 0.0098], ["50", 0.0098], ["51", 0.0197], ["52", 0.0098], ["53", 0.0098], ["54", 0.0098], ["56", 0.0098], ["58", 0.0098], ["61", 0.0098], ["62", 0.0098], ["63", 0.0098], ["88", 0.0098], ["94", 0.0098], ["96", 0.0098], ["100", 0.0197], ["101", 0.0295], ["102", 0...
{"lang": "python3", "distribution": [["17700", 0.0098], ["17900", 0.0197], ["18000", 0.0295], ["18100", 0.0591], ["18200", 0.0197], ["18300", 0.0098], ["18400", 0.0197], ["18500", 0.0098], ["18700", 0.0197], ["18800", 0.128], ["18900", 1.2993], ["19000", 15.8972], ["19100", 24.3134], ["19200", 7.3236], ["19300", 10.581...
[ { "code": "class Solution:\n\n def candy(self, ratings: List[int]) -> int:\n up = 0\n down = 0\n peak = 0\n total = 1\n\n for i in range(len(ratings)-1):\n total+=1\n if ratings[i] < ratings[i+1]:\n down = 0\n up += 1\n ...
109
[ { "code": "class Solution:\n def candy(self, ratings: List[int]) -> int:\n n = len(ratings)\n res = [1]*(n)\n for i in range(1, n):\n if (ratings[i-1]<ratings[i]) and not(res[i-1]<res[i]):\n res[i] = res[i-1]+1\n # print(res)\n # for i in range(1, ...
46
136
single-number
<p>Given a <strong>non-empty</strong>&nbsp;array of integers <code>nums</code>, every element appears <em>twice</em> except for one. Find that single one.</p> <p>You must&nbsp;implement a solution with a linear runtime complexity and use&nbsp;only constant&nbsp;extra space.</p> <p>&nbsp;</p> <p><strong class="example...
74.091617
Easy
[ "array", "bit-manipulation" ]
{"lang": "python3", "distribution": [["53", 0.16499999999999995], ["104", 84.90050000000001], ["155", 0.7623000000000005], ["206", 0.12539999999999998], ["257", 0.10229999999999996], ["308", 0.029700000000000004], ["359", 0.0132], ["410", 0.0033], ["461", 0.023100000000000002], ["512", 0.0066], ["563", 0.0066], ["614",...
{"lang": "python3", "distribution": [["17700", 0.0033], ["17900", 0.0132], ["18000", 0.0594], ["18100", 0.109], ["18200", 0.0495], ["18300", 0.0627], ["18400", 0.066], ["18500", 0.1222], ["18600", 0.2047], ["18700", 0.2608], ["18800", 0.4193], ["18900", 4.3155], ["19000", 41.1411], ["19100", 35.6402], ["19200", 8.0004]...
[ { "code": "with open('user.out', 'a') as f:\n for n in map(loads, stdin):\n f.write(str(reduce(xor,n))+'\\n')\nexit(0)\n ", "runtime": "53" }, { "code": "with open('user.out', 'a') as f:\n for n in map(loads, stdin):\n f.write(str(reduce(xor,n))+'\\n')\nexit(0)\n \n ...
154
[ { "code": "class Solution:\n def singleNumber(self, nums: List[int]) -> int:\n \n for i in nums:\n \n if nums.count(i)==1:\n return i\n \n\n", "memory": "17700" }, { "code": "with open('user.out', 'a') as f:\n for n in map(load...
27
137
single-number-ii
<p>Given an integer array <code>nums</code> where&nbsp;every element appears <strong>three times</strong> except for one, which appears <strong>exactly once</strong>. <em>Find the single element and return it</em>.</p> <p>You must&nbsp;implement a solution with a linear runtime complexity and use&nbsp;only constant&nb...
63.768279
Medium
[ "array", "bit-manipulation" ]
{"lang": "python3", "distribution": [["31", 0.20650000000000002], ["41", 4.2597000000000005], ["50", 33.646100000000004], ["59", 26.5342], ["69", 3.2464999999999997], ["78", 6.1737], ["88", 5.704700000000001], ["97", 2.9274], ["107", 0.9946000000000002], ["116", 0.563], ["125", 0.2814], ["135", 0.2253], ["144", 0.1691]...
{"lang": "python3", "distribution": [["17400", 0.0188], ["17700", 0.0188], ["17900", 0.0188], ["18100", 0.0563], ["18200", 0.1314], ["18300", 0.2627], ["18400", 2.1768], ["18500", 22.1993], ["18600", 29.2363], ["18700", 11.3342], ["18800", 21.8803], ["18900", 4.6538], ["19000", 2.0266]]}
[ { "code": "class Solution:\n def singleNumber(self, nums: List[int]) -> int:\n cnt = defaultdict(int)\n \n for x in nums:\n cnt[x] += 1\n\n for x, freq in cnt.items():\n if freq == 1:\n return x\n \n return -1", "runtime": "31...
124
[ { "code": "f = open('user.out', \"w\")\ninp = map(loads, stdin)\nfor i in inp:\n ones = 0\n twos = 0\n for _ in i:\n ones ^= (_& ~twos)\n twos ^= (_& ~ones)\n print(ones, file=f)\nexit(0)", "memory": "17400" }, { "code": "sys.stdout = open('user.out', 'w')\nfor nums in map(...
20
138
copy-list-with-random-pointer
<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> <p>Construct a <a href="https://en.wikipedia.org/wiki/Object_copying#Deep_copy" target="_blank"><strong>deep copy</strong></a> of the list. ...
57.619023
Medium
[ "hash-table", "linked-list" ]
{"lang": "python3", "distribution": [["12", 0.0059], ["17", 0.0059], ["18", 0.0236], ["19", 0.0177], ["20", 0.0825], ["21", 0.0413], ["22", 0.171], ["23", 0.2063], ["24", 0.4598], ["25", 0.3655], ["26", 0.7545], ["27", 0.7133], ["28", 1.7626], ["29", 1.568], ["30", 2.3108], ["31", 2.1045], ["32", 4.7159], ["33", 4.598]...
{"lang": "python3", "distribution": [["16500", 0.0059], ["16600", 0.0118], ["17000", 0.0354], ["17100", 0.112], ["17200", 2.3815], ["17300", 18.4449], ["17400", 34.2136], ["17500", 37.8684]]}
[ { "code": "\"\"\"\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 copyRandomList(self, head: 'Optional[Node]') -> 'Optional[N...
51
[ { "code": "\"\"\"\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\nwith open(\"user.out\",'w') as f:\n for i in stdin: f.write(i)\n f.write('\\n'...
12
139
word-break
<p>Given a string <code>s</code> and a dictionary of strings <code>wordDict</code>, return <code>true</code> if <code>s</code> can be segmented into a space-separated sequence of one or more dictionary words.</p> <p><strong>Note</strong> that the same word in the dictionary may be reused multiple times in the segmenta...
47.228119
Medium
[ "array", "hash-table", "string", "dynamic-programming", "trie", "memoization" ]
{"lang": "python3", "distribution": [["15", 0.0045], ["16", 0.0045], ["17", 0.0045], ["18", 0.0135], ["19", 0.0269], ["20", 0.0359], ["21", 0.0135], ["22", 0.0898], ["23", 0.0943], ["24", 0.2425], ["25", 0.1572], ["26", 0.4042], ["27", 0.4536], ["28", 0.8354], ["29", 0.8443], ["30", 1.3923], ["31", 1.7021], ["32", 2.76...
{"lang": "python3", "distribution": [["16100", 0.009], ["16200", 0.0135], ["16300", 0.1123], ["16400", 2.8429], ["16500", 16.8822], ["16600", 30.3288], ["16700", 28.0742], ["16800", 9.629], ["16900", 3.7681], ["17000", 1.1767], ["17100", 0.3279], ["17200", 0.0674], ["17300", 0.1617], ["17400", 0.2695], ["17500", 0.3683...
[ { "code": "class Solution:\n def wordBreak(self, s: str, wordDict: List[str]) -> bool:\n\n dp = [True] + [False] * len(s)\n\n for i in range(1,len(s)+1):\n for word in wordDict:\n start = i - len(word)\n\n if start >= 0 and dp[start] and s[start:i] == wo...
69
[ { "code": "results = 0\n \ndef wordBreak(s: str, d):\n n = len(s)\n dp = [False] * (n + 1)\n dp[0] = True\n\n for i in range(1, n + 1):\n for j in range(i):\n if dp[j] and s[j:i] in d:\n dp[i] = True\n break\n global results\n results = (resul...
26
140
word-break-ii
<p>Given a string <code>s</code> and a dictionary of strings <code>wordDict</code>, add spaces in <code>s</code> to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in <strong>any order</strong>.</p> <p><strong>Note</strong> that the same word in the dictionary may be...
51.718036
Hard
[ "array", "hash-table", "string", "dynamic-programming", "backtracking", "trie", "memoization" ]
{"lang": "python3", "distribution": [["15", 0.0406], ["16", 0.0609], ["17", 0.0203], ["18", 0.0812], ["19", 0.1421], ["20", 0.2436], ["21", 0.2436], ["22", 0.5277], ["23", 0.5074], ["24", 0.5886], ["25", 0.9336], ["26", 2.8821], ["27", 2.395], ["28", 2.0499], ["29", 3.3895], ["30", 5.7845], ["31", 4.6885], ["32", 4.160...
{"lang": "python3", "distribution": [["16200", 0.1015], ["16300", 1.9484], ["16400", 14.9584], ["16500", 33.7325], ["16600", 38.1977]]}
[ { "code": "class Solution:\n def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:\n trie = {}\n for word in wordDict:\n node = trie\n for char in word:\n node = node.setdefault(char, {})\n node[\"$\"] = word\n \n n = le...
45
[ { "code": "class Solution:\n def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:\n self.memory = {}\n self.wordDict = set(wordDict)\n return self.backTracking(s)\n\n def backTracking(self, s):\n if s == '':\n return ['']\n if s in self.memory:\n ...
8
227
basic-calculator-ii
<p>Given a string <code>s</code> which represents an expression, <em>evaluate this expression and return its value</em>.&nbsp;</p> <p>The integer division should truncate toward zero.</p> <p>You may assume that the given expression is always valid. All intermediate results will be in the range of <code>[-2<sup>31</su...
44.203317
Medium
[ "math", "string", "stack" ]
{"lang": "python3", "distribution": [["44", 0.022], ["46", 0.011], ["48", 0.011], ["50", 0.011], ["53", 0.011], ["55", 0.0221], ["57", 0.0993], ["59", 0.011], ["62", 0.0441], ["64", 0.1987], ["66", 0.3752], ["68", 0.5408], ["70", 1.4237], ["73", 1.4678], ["75", 1.9645000000000001], ["77", 2.13], ["79", 3.95099999999999...
{"lang": "python3", "distribution": [["18400", 0.011], ["18500", 0.011], ["18700", 0.0221], ["18800", 0.0773], ["18900", 0.3863], ["19000", 4.3152], ["19100", 15.4508], ["19200", 6.1362], ["19300", 1.0043], ["19400", 0.3863], ["19500", 0.2649], ["19600", 0.4194], ["19700", 1.2361], ["19800", 7.1074], ["19900", 23.5625]...
[ { "code": "class Solution:\n def calculate(self, s: str) -> int:\n def stol(s):\n b = []\n st = \"\"\n for i in range(0,len(s)):\n if s[i].isnumeric():\n st+=s[i]\n else:\n if s[i] == \" \": continue\n...
146
[ { "code": "class Solution:\n def calculate(self, s: str) -> int:\n if not s or s.isspace():\n return 0\n\n length = len(s)\n current_number = 0\n last_number = 0\n result = 0\n operation = '+'\n\n for i in range(length):\n current_char = ...
90
228
summary-ranges
<p>You are given a <strong>sorted unique</strong> integer array <code>nums</code>.</p> <p>A <strong>range</strong> <code>[a,b]</code> is the set of all integers from <code>a</code> to <code>b</code> (inclusive).</p> <p>Return <em>the <strong>smallest sorted</strong> list of ranges that <strong>cover all the numbers i...
51.585798
Easy
[ "array" ]
{"lang": "python3", "distribution": [["11", 0.0066], ["12", 0.0066], ["14", 0.0198], ["15", 0.0264], ["16", 0.0331], ["17", 0.0198], ["18", 0.1058], ["19", 0.2049], ["20", 0.2446], ["21", 0.2182], ["22", 0.8065], ["23", 0.5421], ["24", 1.1767], ["25", 1.0775], ["26", 2.6906], ["27", 1.8312], ["28", 3.1665], ["29", 3.89...
{"lang": "python3", "distribution": [["16000", 0.0132], ["16100", 0.0132], ["16200", 0.2182], ["16300", 5.6059], ["16400", 25.3917], ["16500", 43.4653]]}
[ { "code": "class Solution:\n def summaryRanges(self, nums: List[int]) -> List[str]:\n out = []\n start = 0\n end = 0\n if len(nums) == 0:\n return out\n while end < len(nums) - 1:\n if nums[end+1] == nums[end] + 1:\n end += 1\n ...
51
[ { "code": "class Solution:\n def summaryRanges(self, nums: List[int]) -> List[str]:\n if len(nums) == 0:\n return []\n ans = []\n start = nums[0]\n end = nums[0]\n for i in range(1, len(nums)):\n if nums[i] == end + 1:\n end = nums[i]\n ...
9
229
majority-element-ii
<p>Given an integer array of size <code>n</code>, find all elements that appear more than <code>&lfloor; n/3 &rfloor;</code> times.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,3] <strong>Output:</strong> [3] </pre> <p><strong class="example">Example ...
52.630748
Medium
[ "array", "hash-table", "sorting", "counting" ]
{"lang": "python3", "distribution": [["32", 0.0148], ["38", 0.0148], ["72", 0.0148], ["74", 0.0148], ["78", 0.0594], ["79", 0.0445], ["80", 0.0594], ["81", 0.089], ["82", 0.282], ["83", 0.3117], ["84", 0.371], ["85", 0.653], ["86", 1.1725], ["87", 1.2467], ["88", 1.2467], ["89", 1.9887], ["90", 2.2113], ["91", 2.6863],...
{"lang": "python3", "distribution": [["17100", 0.0148], ["17200", 0.0297], ["17500", 0.0445], ["17600", 0.2226], ["17700", 2.3894], ["17800", 22.4844], ["17900", 55.2389]]}
[ { "code": "def majorityElement(nums: List[int]) -> List[int]:\n return [n for n, c in Counter(nums).items() if c > len(nums) / 3]\nwith open('user.out', 'w') as f:\n for case in map(loads, stdin):\n f.write(f\"{majorityElement(case)}\\n\")\nexit(0)", "runtime": "32" }, { "code": "def ma...
69
[ { "code": "def majorityElement(nums: List[int]) -> List[int]:\n treshold = len(nums) / 3\n return [n for n, c in Counter(nums).items() if c > treshold]\nwith open('user.out', 'w') as f:\n for case in map(loads, stdin):\n f.write(f\"{majorityElement(case)}\\n\")\nexit(0)", "memory": "17100" ...
10
230
kth-smallest-element-in-a-bst
<p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value (<strong>1-indexed</strong>) of all the values of the nodes in the tree</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="h...
73.655408
Medium
[ "tree", "depth-first-search", "binary-search-tree", "binary-tree" ]
{"lang": "python3", "distribution": [["18", 0.0039], ["19", 0.0077], ["20", 0.0077], ["22", 0.0039], ["23", 0.0309], ["24", 0.0116], ["25", 0.0193], ["26", 0.0309], ["27", 0.147], ["28", 0.1161], ["29", 0.1315], ["30", 0.2979], ["31", 0.7079], ["32", 0.5532], ["33", 0.5571], ["34", 1.2418], ["35", 1.8104], ["36", 1.736...
{"lang": "python3", "distribution": [["18000", 0.0116], ["18100", 0.0619], ["18200", 0.0426], ["18300", 0.0426], ["18400", 0.0542], ["18500", 0.0193], ["18600", 0.0039], ["19000", 0.0193], ["19100", 0.0232], ["19200", 0.1199], ["19300", 1.9149], ["19400", 22.4139], ["19500", 58.1199], ["19600", 11.4352]]}
[ { "code": "# 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 kthSmallest(self, root: Optional[TreeNode], k: int) -> int:\n count = 0...
60
[ { "code": "# 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 kthSmallest(self, root: Optional[TreeNode], k: int) -> int:\n arr = []\...
20
231
power-of-two
<p>Given an integer <code>n</code>, return <em><code>true</code> if it is a power of two. Otherwise, return <code>false</code></em>.</p> <p>An integer <code>n</code> is a power of two, if there exists an integer <code>x</code> such that <code>n == 2<sup>x</sup></code>.</p> <p>&nbsp;</p> <p><strong class="example">Exa...
47.966962
Easy
[ "math", "bit-manipulation", "recursion" ]
{"lang": "python3", "distribution": [["14", 0.0104], ["15", 0.0415], ["16", 0.0622], ["17", 0.0207], ["18", 0.0726], ["19", 0.1451], ["20", 0.1762], ["21", 0.114], ["22", 0.3939], ["23", 0.9743], ["24", 0.5701], ["25", 0.6737], ["26", 1.0779], ["27", 3.0265], ["28", 2.3528], ["29", 1.8968], ["30", 2.5394], ["31", 6.270...
{"lang": "python3", "distribution": [["16100", 0.0726], ["16200", 0.3835], ["16300", 6.2293], ["16400", 25.2799], ["16500", 43.2939]]}
[ { "code": "class Solution:\n def isPowerOfTwo(self, n: int) -> bool:\n while n > 1:\n n /= 2\n \n return n == 1\n ", "runtime": "14" }, { "code": "import math\nclass Solution:\n def isPowerOfTwo(self, n: int) -> bool:\n return n>0 and n&(n-1)==0", ...
53
[ { "code": "class Solution:\n def isPowerOfTwo(self, n: int) -> bool:\n pow = 0\n while pow < 32:\n if 2**pow == n:\n return True\n pow += 1\n return False", "memory": "16100" }, { "code": "class Solution:\n def isPowerOfTwo(self, n: int...
8
232
implement-queue-using-stacks
<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> <p>Implement the <code>MyQueue</code> class:</p> <ul> <li><code>void push(int x)</code> ...
66.979966
Easy
[ "stack", "design", "queue" ]
{"lang": "python3", "distribution": [["12", 0.0098], ["14", 0.0098], ["15", 0.0196], ["16", 0.0393], ["17", 0.0589], ["18", 0.1473], ["19", 0.1963], ["20", 0.2847], ["21", 0.1669], ["22", 0.697], ["23", 0.7069], ["24", 1.021], ["25", 0.9719], ["26", 2.6212], ["27", 1.8849], ["28", 2.6212], ["29", 3.4459], ["30", 6.3224...
{"lang": "python3", "distribution": [["16200", 0.0295], ["16300", 0.2258], ["16400", 5.3701], ["16500", 24.4159], ["16600", 42.2933]]}
[ { "code": "class MyQueue:\n\n def __init__(self):\n self.ans = deque()\n\n def push(self, x: int) -> None:\n self.ans.append(x)\n\n def pop(self) -> int:\n return self.ans.popleft()\n\n def peek(self) -> int:\n return self.ans[0]\n\n def empty(self) -> bool:\n r...
47
[ { "code": "class MyQueue:\n\n def __init__(self):\n self.s1 = []\n self.s2 = []\n\n def push(self, x: int) -> None:\n for i in range(len(self.s1)):\n self.s2.append(self.s1.pop())\n self.s2.append(x)\n for i in range(len(self.s2)):\n self.s1.append(...
8
233
number-of-digit-one
<p>Given an integer <code>n</code>, count <em>the total number of digit </em><code>1</code><em> appearing in all non-negative integers less than or equal to</em> <code>n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 13 <strong>Output:</strong> 6 </pre> ...
34.908403
Hard
[ "math", "dynamic-programming", "recursion" ]
{"lang": "python3", "distribution": [["15", 0.134], ["18", 0.134], ["19", 0.134], ["20", 0.4021], ["21", 0.134], ["22", 0.8043], ["23", 0.5362], ["24", 0.8043], ["25", 1.0724], ["26", 2.2788], ["27", 1.4745], ["28", 3.0831], ["29", 3.2172], ["30", 4.6917], ["31", 5.0938], ["32", 7.5067], ["33", 7.3727], ["34", 8.7131],...
{"lang": "python3", "distribution": [["16300", 2.681], ["16400", 15.5496], ["16500", 33.378], ["16600", 38.2038]]}
[ { "code": "class Solution:\n def countDigitOne(self, n: int) -> int:\n if n == 0:\n return 0\n if n <= 9:\n return 1\n first_digit = int(str(n)[0])\n remaining = int(str(n)[1::])\n lowest_10th_power = int(\"9\"*(len(str(n))-1))\n # print(first_d...
36
[ { "code": "class Solution:\n def countDigitOne(self, n: int) -> int:\n \n s = str(n)\n ln = len(s)\n dpa = [0]*ln\n dpc = [0]*ln\n dpa[-1] = int(s[-1] != '0')\n dpc[-1] = 1\n\n tens = 1\n for i in range(ln - 2, -1, -1):\n dig = int(s[i...
7
234
palindrome-linked-list
<p>Given the <code>head</code> of a singly linked list, return <code>true</code><em> if it is a </em><span data-keyword="palindrome-sequence"><em>palindrome</em></span><em> or </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://asset...
54.130864
Easy
[ "linked-list", "two-pointers", "stack", "recursion" ]
{"lang": "python3", "distribution": [["33", 0.07629999999999999], ["37", 0.0277], ["41", 0.0069], ["45", 0.0138], ["50", 0.0208], ["54", 0.0278], ["58", 0.0069], ["63", 0.0069], ["67", 0.0069], ["71", 0.0069], ["75", 0.0139], ["80", 0.0208], ["84", 0.0278], ["88", 0.0208], ["92", 0.0555], ["97", 0.0416], ["101", 0.0555...
{"lang": "python3", "distribution": [["17235", 0.1249], ["17506", 0.0486], ["17778", 0.2637], ["18049", 0.8119], ["18320", 1.485], ["18591", 1.2282], ["18863", 0.5413], ["19134", 0.2151], ["19405", 0.0902], ["19676", 0.0278], ["19948", 0.0555], ["20219", 0.0555], ["20490", 0.0347], ["20761", 0.0833], ["21033", 0.0902],...
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nimport sys\n\nwith open('user.out', 'w') as f:\n while True:\n line = sys.stdin.readline().strip()[1:-1]\n if not line:\n ...
124
[ { "code": "import sys\n\nwith open('user.out', 'w') as f:\n while True:\n line = sys.stdin.readline().strip()[1:-1]\n print(f\"in loop : {line}\")\n if not line:\n break\n f.write('true\\n' if line == line[::-1] else 'false\\n')\n\nexit(0)\n", "memory": "17235" },...
109
235
lowest-common-ancestor-of-a-binary-search-tree
<p>Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: &ldquo;The lowest common ancestor is defined between two nodes <code>...
66.056456
Medium
[ "tree", "depth-first-search", "binary-search-tree", "binary-tree" ]
{"lang": "python3", "distribution": [["32", 0.0092], ["33", 0.0092], ["34", 0.0553], ["35", 0.0507], ["36", 0.0921], ["37", 0.1014], ["38", 0.2304], ["39", 0.2165], ["40", 0.3501], ["41", 0.41], ["42", 0.9721], ["43", 0.8799], ["44", 1.0089], ["45", 1.4282], ["46", 2.5891], ["47", 2.0962], ["48", 2.3312], ["49", 3.4092...
{"lang": "python3", "distribution": [["18100", 0.0046], ["18200", 0.023], ["18300", 0.0046], ["19500", 0.0046], ["19600", 0.0046], ["19700", 0.0092], ["19800", 0.0092], ["19900", 0.0092], ["20000", 0.0829], ["20100", 1.1518], ["20200", 17.9443], ["20300", 48.4566]]}
[ { "code": "# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\nclass Solution:\n def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':\n cur = r...
57
[ { "code": "# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\nclass Solution:\n def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':\n \n ...
14
236
lowest-common-ancestor-of-a-binary-tree
<p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p> <p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: &ldquo;The lowest common ancestor is defined between two nodes <code>p</code> and <cod...
63.835774
Medium
[ "tree", "depth-first-search", "binary-tree" ]
{"lang": "python3", "distribution": [["24", 0.018699999999999998], ["26", 0.0744], ["29", 0.2233], ["32", 0.5630000000000001], ["34", 1.3491], ["37", 2.5076], ["39", 5.712899999999999], ["42", 5.936299999999999], ["45", 14.8825], ["47", 17.1435], ["50", 11.4259], ["53", 15.4594], ["55", 6.9458], ["58", 7.0063], ["60", ...
{"lang": "python3", "distribution": [["18300", 0.0047], ["18400", 0.0093], ["19100", 0.0047], ["19200", 0.0047], ["19300", 0.0047], ["19400", 0.0279], ["19500", 0.2652], ["19600", 1.0561], ["19700", 0.7304], ["19800", 0.6374], ["19900", 0.307], ["20000", 0.3908], ["20100", 1.9819], ["20200", 1.5585], ["20300", 0.2326],...
[ { "code": "# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\nclass Solution:\n def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':\n \n ...
104
[ { "code": "# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\n# class Solution:\n# def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':\n# i...
48
121
best-time-to-buy-and-sell-stock
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that st...
54.139827
Easy
[ "array", "dynamic-programming" ]
{"lang": "python3", "distribution": [["73", 0.018500000000000003], ["83", 0.0638], ["92", 0.038200000000000005], ["102", 0.012199999999999999], ["112", 0.0060999999999999995], ["121", 0.005999999999999999], ["131", 0.006200000000000001], ["140", 0.0049], ["150", 0.0149], ["159", 0.0048], ["169", 0.0037], ["178", 0.0024...
{"lang": "python3", "distribution": [["23200", 0.0012], ["23300", 0.0123], ["23400", 0.0859], ["23500", 0.103], ["23600", 0.0037], ["23700", 0.0012], ["24200", 0.0025], ["24400", 0.0037], ["24500", 0.0012], ["24600", 0.0049], ["24700", 0.0086], ["24800", 0.0074], ["24900", 0.0025], ["25000", 0.0049], ["25100", 0.011], ...
[ { "code": "class Solution:\n def maxProfit(prices: List[int]) -> int:\n curr_buy = float(\"inf\")\n curr_sell = -float(\"inf\")\n diff = -float(\"inf\")\n\n for price in prices:\n if curr_buy > price:\n curr_buy = price\n curr_sell = -float...
129
[ { "code": "class Solution:\n def maxProfit( prices: List[int]) -> int:\n \n\n min_element=float(\"inf\")\n max_element=-float(\"inf\")\n ans=0\n for num in prices:\n\n min_element=min(min_element,num)\n \n\n ans=max(ans,num-min_element)\n ...
50
122
best-time-to-buy-and-sell-stock-ii
<p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy i...
67.643976
Medium
[ "array", "dynamic-programming", "greedy" ]
{"lang": "python3", "distribution": [["26", 0.0036], ["28", 0.0073], ["29", 0.0109], ["30", 0.0036], ["31", 0.0219], ["32", 0.0146], ["33", 0.0036], ["34", 0.0146], ["35", 0.0292], ["36", 0.0219], ["37", 0.0438], ["38", 0.0511], ["39", 0.0875], ["40", 0.1167], ["41", 0.1422], ["42", 0.2225], ["43", 0.5325], ["44", 0.41...
{"lang": "python3", "distribution": [["16700", 0.0036], ["16900", 0.0656], ["17000", 0.0948], ["17100", 0.0109], ["17200", 0.0255], ["17400", 0.0292], ["17500", 0.0292], ["17600", 0.2152], ["17700", 3.3735], ["17800", 39.0007], ["17900", 44.7484], ["18000", 4.4311], ["18100", 0.3246], ["18200", 0.0365], ["18300", 0.014...
[ { "code": "class Solution:\n def maxProfit(prices: List[int]) -> int:\n # if dont sell on low day and dont buy on high day\n prev = 10000\n profit = 0\n for price in prices:\n if(prev<price):\n profit += price - prev\n prev = price\n ret...
82
[ { "code": "def maxProfit(prices: List[int]) -> int:\n prev = prices[0]\n out = 0\n for price in prices:\n if price > prev:\n out += price - prev\n prev = price\n return out\n\nf = open('user.out', 'w')\nfor i in map(loads, stdin):\n f.write(f'{maxProfit(i)}\\n')\nf.flush(...
41
123
best-time-to-buy-and-sell-stock-iii
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete <strong>at most two transactions</strong>.</p> <p><strong>Note:</strong> You may not engage in multiple tran...
49.180373
Hard
[ "array", "dynamic-programming" ]
{"lang": "python3", "distribution": [["159", 0.0519], ["187", 0.0346], ["216", 0.0346], ["244", 0.0346], ["273", 0.0173], ["302", 0.0173], ["330", 0.0173], ["359", 0.0173], ["387", 0.0346], ["416", 0.0173], ["444", 0.0173], ["473", 0.0346], ["501", 0.0346], ["530", 0.0173], ["558", 0.0173], ["587", 0.12109999999999999]...
{"lang": "python3", "distribution": [["28898", 63.597199999999994], ["35496", 4.327200000000002], ["42093", 3.5826000000000016], ["48691", 0.9169999999999999], ["55288", 3.9635000000000002], ["61886", 3.046300000000001], ["68483", 1.2115], ["75081", 1.2287000000000001], ["81678", 2.5442000000000005], ["88276", 1.488200...
[ { "code": "def maxProfit(prices: List[int]) -> int:\n # Compute max run of prices[:i+1]\n left = []\n lo, hi, ct = 100_001, 0, 0\n for x in prices:\n if x < lo:\n lo = x\n hi = 0\n elif x > hi:\n hi = x\n ct = max(ct, hi - lo)\n left.a...
141
[ { "code": "\ndef maxProfit(prices: List[int]) -> int:\n \n buy1, buy2, sell1, sell2 = math.inf, math.inf, 0, 0\n\n for price in prices:\n buy1 = min(buy1, price)\n sell1 = max(sell1, price - buy1)\n\n buy2 = min(buy2, price - sell1)\n sell2 = max(sell2, price - buy2)\n\n ...
106
124
binary-tree-maximum-path-sum
<p>A <strong>path</strong> in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence <strong>at most once</strong>. Note that the path does not need to pass through the root.</p> <p>The <strong>path sum</strong> of a pa...
40.404334
Hard
[ "dynamic-programming", "tree", "depth-first-search", "binary-tree" ]
{"lang": "python3", "distribution": [["38", 0.0316], ["39", 0.0189], ["40", 0.0189], ["41", 0.0316], ["42", 0.0379], ["43", 0.0821], ["44", 0.101], ["45", 0.1579], ["46", 0.1831], ["47", 0.3536], ["48", 0.3599], ["49", 0.5494], ["50", 0.6504], ["51", 0.9283], ["52", 1.2503], ["53", 1.484], ["54", 1.9134], ["55", 2.1975...
{"lang": "python3", "distribution": [["20100", 0.0063], ["20200", 0.0189], ["20300", 0.0126], ["20400", 0.0189], ["20500", 0.0189], ["20600", 0.0316], ["20700", 0.1516], ["20800", 2.046], ["20900", 20.3839], ["21000", 50.1768], ["21100", 18.7484]]}
[ { "code": "# 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 maxPathSum(self, root: Optional[TreeNode]) -> int:\n #no need lf to lf\...
69
[ { "code": "# 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 maxPathSum(self, root: Optional[TreeNode]) -> int:\n res=[root.val]\n ...
14
125
valid-palindrome
<p>A phrase is a <strong>palindrome</strong> if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.</p> <p>Given a string <code>s</code>, return <code>true</code><em> if...
48.718699
Easy
[ "two-pointers", "string" ]
{"lang": "python3", "distribution": [["16", 0.0016], ["18", 0.0016], ["19", 0.0063], ["20", 0.0063], ["22", 0.0158], ["23", 0.0348], ["24", 0.0237], ["25", 0.0316], ["26", 0.0979], ["27", 0.1674], ["28", 0.1785], ["29", 0.1974], ["30", 0.4123], ["31", 0.6334], ["32", 0.7519], ["33", 0.9003], ["34", 1.3758], ["35", 1.82...
{"lang": "python3", "distribution": [["16100", 0.0016], ["16400", 0.0016], ["16500", 0.0063], ["16600", 0.0047], ["16700", 0.0695], ["16800", 1.052], ["16900", 16.53], ["17000", 29.8931], ["17100", 8.4854], ["17200", 4.8066], ["17300", 5.4526], ["17400", 1.3805], ["17500", 0.5181], ["17600", 0.4202], ["17700", 0.8087],...
[ { "code": "class Solution:\n def isPalindrome(self, s: str) -> bool:\n l, r = 0, len(s)-1\n while l < r:\n while l < r and not s[l].isalnum():\n l += 1\n while l <r and not s[r].isalnum():\n r -= 1\n if s[l].lower() != s[r].lower():...
91
[ { "code": "class Solution:\n def isPalindrome(self, s: str) -> bool:\n start = 0 \n end = len(s) - 1\n while start < end:\n while start < end and not s[start].isalnum():\n start += 1\n \n while end > start and not s[end].isalnum():\n ...
72
126
word-ladder-ii
<p>A <strong>transformation sequence</strong> from word <code>beginWord</code> to word <code>endWord</code> using a dictionary <code>wordList</code> is a sequence of words <code>beginWord -&gt; s<sub>1</sub> -&gt; s<sub>2</sub> -&gt; ... -&gt; s<sub>k</sub></code> such that:</p> <ul> <li>Every adjacent pair of words ...
27.158029
Hard
[ "hash-table", "string", "backtracking", "breadth-first-search" ]
{"lang": "python3", "distribution": [["27", 0.20400000000000001], ["30", 0.068], ["33", 0.136], ["36", 1.3595], ["39", 2.4473000000000003], ["42", 4.8946], ["45", 6.866], ["48", 6.866099999999999], ["51", 6.5941], ["54", 2.9912], ["57", 3.6029999999999998], ["60", 3.7390000000000003], ["63", 4.8267], ["66", 5.8464], ["...
{"lang": "python3", "distribution": [["16500", 0.068], ["16600", 0.4759], ["16700", 2.6513], ["16800", 7.07], ["16900", 9.7213], ["17000", 13.2563], ["17100", 16.5874], ["17200", 22.0258], ["17300", 8.0897], ["17400", 3.9429], ["17500", 2.7192], ["17600", 2.7192], ["17700", 1.0197], ["17800", 1.1557], ["17900", 0.7478]...
[ { "code": "class Solution:\n def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:\n if endWord not in wordList: return []\n wordmap = defaultdict(list)\n for word in wordList:\n for i in range(len(word)):\n wordmap[word[0:...
122
[ { "code": "'''\nclass Solution:\n def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]:\n \n def hasConn(s1, s2): #guaranteed same length\n cnt = 0\n for i in range(len(s1)):\n if s1[i] != s2[i]:\n cn...
27
127
word-ladder
<p>A <strong>transformation sequence</strong> from word <code>beginWord</code> to word <code>endWord</code> using a dictionary <code>wordList</code> is a sequence of words <code>beginWord -&gt; s<sub>1</sub> -&gt; s<sub>2</sub> -&gt; ... -&gt; s<sub>k</sub></code> such that:</p> <ul> <li>Every adjacent pair of words ...
40.375657
Hard
[ "hash-table", "string", "breadth-first-search" ]
{"lang": "python3", "distribution": [["119", 40.232600000000026], ["221", 4.5832999999999995], ["323", 13.648800000000003], ["425", 14.183200000000005], ["527", 2.6223000000000005], ["629", 2.3352000000000013], ["730", 1.6300000000000017], ["832", 0.9481], ["934", 0.4511999999999997], ["1036", 0.2723], ["1138", 0.35839...
{"lang": "python3", "distribution": [["16862", 0.031100000000000003], ["16987", 0.0233], ["17112", 0.031], ["17237", 0.0388], ["17362", 2.1714], ["17487", 4.1024], ["17612", 3.9938], ["17737", 2.5126], ["17862", 18.2939], ["17987", 9.2439], ["18112", 2.2489], ["18237", 1.1322], ["18362", 0.7755], ["18487", 0.2559], ["1...
[ { "code": "class Solution:\n def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:\n wordSet = set(wordList)\n\n if endWord not in wordSet:\n return 0\n\n beginSet = {beginWord}\n endSet = {endWord}\n distance = 1\n\n while begi...
151
[ { "code": "# BFS approach \n# hit -> cog\n# ait, bit, cit, dit, ..., etc -> none of them are in the wordList\n# hat, hbt, hct, ..., etc -> only \"hot\" goes to the queue\n# hia, hib, hic, ..., etc -> none of them are in the wordList\n# aot, bot, cot, dot, ..., etc -> only \"dot\" goes to the queue\n# hat, hbt,...
121
128
longest-consecutive-sequence
<p>Given an unsorted array of integers <code>nums</code>, return <em>the length of the longest consecutive elements sequence.</em></p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums ...
47.447254
Medium
[ "array", "hash-table", "union-find" ]
{"lang": "python3", "distribution": [["108", 0.08929999999999996], ["177", 0.0069], ["245", 0.0069], ["314", 35.90619999999999], ["383", 21.00329999999998], ["452", 2.917500000000002], ["520", 1.383], ["589", 0.7709000000000003], ["658", 0.39689999999999986], ["727", 0.18999999999999984], ["796", 0.11029999999999993], ...
{"lang": "python3", "distribution": [["26890", 0.2465], ["27070", 0.0776], ["27250", 0.0136], ["27430", 0.0046], ["27610", 0.0023], ["27790", 0.0046], ["27970", 0.0023], ["28150", 0.0046], ["28330", 0.0046], ["28510", 0.0205], ["28690", 0.0685], ["28870", 0.016], ["29050", 0.0091], ["29230", 0.0068], ["29410", 0.0023],...
[ { "code": "\"\"\"not mine obv\"\"\"\ndef longestConsecutive(t):\n s = set(t)\n q = 0\n while s:\n n = s.pop()\n l = n - 1\n while l in s:\n s.remove(l)\n l-=1\n h = n + 1\n while h in s:\n s.remove(h)\n h+=1\n q = max...
142
[ { "code": "class Solution:\n def longestConsecutive(self, nums: List[int]) -> int:\n if not nums:\n return 0\n max_count = 1\n count = 1\n heapq.heapify(nums)\n prev = heapq.heappop(nums)\n while nums:\n small = heapq.heappop(nums)\n ...
135
129
sum-root-to-leaf-numbers
<p>You are given the <code>root</code> of a binary tree containing digits from <code>0</code> to <code>9</code> only.</p> <p>Each root-to-leaf path in the tree represents a number.</p> <ul> <li>For example, the root-to-leaf path <code>1 -&gt; 2 -&gt; 3</code> represents the number <code>123</code>.</li> </ul> <p>Re...
66.682765
Medium
[ "tree", "depth-first-search", "binary-tree" ]
{"lang": "python3", "distribution": [["14", 0.0101], ["15", 0.0504], ["16", 0.0202], ["17", 0.0101], ["18", 0.0404], ["19", 0.1816], ["20", 0.111], ["21", 0.1513], ["22", 0.5044], ["23", 0.7667], ["24", 0.4641], ["25", 0.9483], ["26", 2.3204], ["27", 2.3103], ["28", 1.6646], ["29", 2.5626], ["30", 5.7708], ["31", 5.256...
{"lang": "python3", "distribution": [["16100", 0.0404], ["16200", 0.2522], ["16300", 3.3697], ["16400", 22.2659], ["16500", 46.6102]]}
[ { "code": "# 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 sumNumbers(self, root: Optional[TreeNode]) -> int:\n\n return self.help...
46
[ { "code": "# 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\n def sumNumbers(self, root: Optional[TreeNode]) -> int:\n\n\n #O(N) since ...
8
130
surrounded-regions
<p>You are given an <code>m x n</code> matrix <code>board</code> containing <strong>letters</strong> <code>&#39;X&#39;</code> and <code>&#39;O&#39;</code>, <strong>capture regions</strong> that are <strong>surrounded</strong>:</p> <ul> <li><strong>Connect</strong>: A cell is connected to adjacent cells horizontally o...
40.750059
Medium
[ "array", "depth-first-search", "breadth-first-search", "union-find", "matrix" ]
{"lang": "python3", "distribution": [["42", 0.0225], ["44", 0.0075], ["46", 0.0075], ["48", 0.0075], ["51", 0.0075], ["53", 0.0075], ["55", 0.0151], ["57", 0.0151], ["59", 0.0075], ["62", 0.0075], ["64", 0.0377], ["66", 0.0528], ["68", 0.0453], ["71", 0.1283], ["73", 0.1887], ["75", 0.3245], ["77", 0.3396], ["79", 0.47...
{"lang": "python3", "distribution": [["17390", 0.0679], ["17570", 3.962], ["17750", 39.7404], ["17930", 27.1602], ["18110", 4.528], ["18290", 2.4073], ["18470", 0.2793], ["18650", 0.0377], ["18830", 0.0679], ["19010", 0.0604], ["19190", 0.19619999999999999], ["19370", 0.0905], ["19550", 0.0452], ["19730", 0.1811], ["19...
[ { "code": "def solve(board: List[List[str]]) -> None:\n # check if O island ever touches border\n HEIGHT = len(board)\n WIDTH = len(board[0])\n\n def dfs(i, j):\n board[i][j] = \"o\"\n if i > 0 and board[i - 1][j] == \"O\":\n dfs(i - 1, j)\n if i < HEIGHT - 1 and boar...
139
[ { "code": "class Solution:\n def solve(self, grid: List[List[int]]) -> None:\n\n # idea\n # do not do anything if the position is on boundary or if it is X (captured)\n # only modify if the position is \"O\" inside\n\n\n # --- the idea same as number of closed regions would not wo...
140
289
game-of-life
<p>According to&nbsp;<a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life" target="_blank">Wikipedia&#39;s article</a>: &quot;The <b>Game of Life</b>, also known simply as <b>Life</b>, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.&quot;</p> <p>The board is made up o...
70.066157
Medium
[ "array", "matrix", "simulation" ]
{"lang": "python3", "distribution": [["11", 0.0178], ["15", 0.0178], ["18", 0.0535], ["19", 0.1961], ["20", 0.0535], ["21", 0.0713], ["22", 0.1961], ["23", 0.6953], ["24", 0.4635], ["25", 0.4457], ["26", 0.517], ["27", 2.2642], ["28", 1.6046], ["29", 1.6224], ["30", 2.2464], ["31", 5.509], ["32", 4.2075], ["33", 3.8688...
{"lang": "python3", "distribution": [["16100", 0.0178], ["16200", 0.0891], ["16300", 1.1054], ["16400", 11.8738], ["16500", 27.6163], ["16600", 41.2195]]}
[ { "code": "class Solution:\n def gameOfLife(self, board: List[List[int]]) -> None:\n \"\"\"\n Do not return anything, modify board in-place instead.\n \"\"\"\n if not board or not board[0]:\n return\n m = len(board)\n n = len(board[0])\n # 0 dead ->...
48
[ { "code": "\"\"\"\n0,1,0\n0,0,1\n1,1,1\n0,0,0\n\n0,1,1\n0,0,1\n1,1,1\n0,0,0\n\"\"\"\n\nclass Solution:\n def gameOfLife(self, board: List[List[int]]) -> None:\n \"\"\"\n Do not return anything, modify board in-place instead.\n \"\"\"\n m = len(board)\n n = len(board[0])\n ...
10
290
word-pattern
<p>Given a <code>pattern</code> and a string <code>s</code>, find if <code>s</code>&nbsp;follows the same pattern.</p> <p>Here <b>follow</b> means a full match, such that there is a bijection between a letter in <code>pattern</code> and a <b>non-empty</b> word in <code>s</code>. Specifically:</p> <ul> <li>Each lette...
42.421763
Easy
[ "hash-table", "string" ]
{"lang": "python3", "distribution": [["14", 0.0147], ["15", 0.0147], ["16", 0.044], ["17", 0.0147], ["18", 0.1174], ["19", 0.1541], ["20", 0.3376], ["21", 0.2422], ["22", 0.5945], ["23", 0.5064], ["24", 1.0862], ["25", 1.0715], ["26", 2.6934], ["27", 1.82], ["28", 3.1117], ["29", 3.7282], ["30", 5.5702], ["31", 4.0584]...
{"lang": "python3", "distribution": [["15900", 0.0073], ["16100", 0.022], ["16200", 0.0954], ["16300", 2.8181], ["16400", 21.525], ["16500", 38.5366]]}
[ { "code": "class Solution:\n def wordPattern(self, pattern: str, s: str) -> bool:\n wordList = s.split(\" \")\n if len(pattern) != len(wordList):\n return False\n data1, data2 = {}, {}\n for i in range(len(pattern)):\n if wordList[i] not in data1:\n ...
50
[ { "code": "class Solution:\n def wordPattern(self, pattern: str, s: str) -> bool:\n \n s = s.split()\n\n return (len(set(pattern)) ==\n len(set(s)) ==\n len(set(zip_longest(pattern,s))))\n\n", "memory": "15900" }, { "code": "class Solution:\n ...
9
292
nim-game
<p>You are playing the following Nim Game with your friend:</p> <ul> <li>Initially, there is a heap of stones on the table.</li> <li>You and your friend will alternate taking turns, and <strong>you go first</strong>.</li> <li>On each turn, the person whose turn it is will remove 1 to 3 stones from the heap.</li> <...
57.263834
Easy
[ "math", "brainteaser", "game-theory" ]
{"lang": "python3", "distribution": [["15", 0.0344], ["16", 0.1031], ["17", 0.0344], ["18", 0.1031], ["19", 0.1719], ["20", 0.3438], ["21", 0.0344], ["22", 0.7563], ["23", 0.7219], ["24", 1.4782], ["25", 1.2719], ["26", 2.4751], ["27", 1.6501], ["28", 3.6439], ["29", 4.2283], ["30", 5.122], ["31", 3.472], ["32", 7.5284...
{"lang": "python3", "distribution": [["16200", 0.1719], ["16300", 3.1626], ["16400", 23.2039], ["16500", 43.2451]]}
[ { "code": "class Solution:\n def canWinNim(self, n: int) -> bool:\n return n % 4", "runtime": "15" }, { "code": "class Solution:\n def canWinNim(self, n: int) -> bool:\n return n%4 != 0", "runtime": "16" }, { "code": "class Solution:\n def canWinNim(self, n: int) -...
40
[ { "code": "class Solution:\n def canWinNim(self, n: int) -> bool:\n return False if n%4==0 else True\n ", "memory": "16200" }, { "code": "class Solution:\n def canWinNim(self, n: int) -> bool:\n return n % 4\n ", "memory": "16300" }, { "code": "class Sol...
6
295
find-median-from-data-stream
<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value, and the median is the mean of the two middle values.</p> <ul> <li>For example, for <code>arr = [2,3,4]</code>, the median is <code>3</code>.</li> <li>For example, for <code>arr = ...
52.336317
Hard
[ "two-pointers", "design", "sorting", "heap-priority-queue", "data-stream" ]
{"lang": "python3", "distribution": [["328", 3.7508999999999997], ["352", 29.630999999999997], ["377", 28.1562], ["402", 15.591700000000001], ["426", 2.742199999999999], ["451", 0.9915000000000002], ["476", 1.8457000000000001], ["500", 1.0691000000000002], ["525", 0.12899999999999998], ["550", 0.14629999999999999], ["5...
{"lang": "python3", "distribution": [["36200", 0.0086], ["36300", 0.0172], ["36500", 0.0086], ["36600", 0.0086], ["36700", 0.0086], ["36800", 0.0086], ["36900", 0.0172], ["37000", 0.0172], ["37100", 0.0345], ["37200", 0.069], ["37300", 0.0431], ["37400", 0.1035], ["37500", 0.0604], ["37600", 0.1552], ["37700", 0.138], ...
[ { "code": "class MedianFinder:\n\n def __init__(self):\n self.left, self.right = [math.inf], [math.inf]\n heapq.heapify(self.left)\n heapq.heapify(self.right)\n self.middle = []\n\n def addNum(self, num: int) -> None:\n if self.middle:\n if self.middle[0] < nu...
125
[ { "code": "class MedianFinder:\n\n def __init__(self):\n self.ans=[]\n\n def addNum(self, num: int) -> None:\n self.ans.append(num)\n\n def findMedian(self) -> float:\n self.ans.sort()\n n=len(self.ans)\n if n%2==0:\n x,y=self.ans[n//2],self.ans[(n//2)-1]\n...
37
297
serialize-and-deserialize-binary-tree
<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 same or another computer environment.</p> <p>Design an algorithm to serialize and deseri...
57.513703
Hard
[ "string", "tree", "depth-first-search", "breadth-first-search", "design", "binary-tree" ]
{"lang": "python3", "distribution": [["55", 0.0228], ["56", 0.0343], ["58", 0.0457], ["59", 0.0228], ["60", 0.1713], ["61", 0.1827], ["62", 0.2398], ["64", 0.2741], ["65", 0.9478], ["66", 0.6509], ["67", 0.6623], ["69", 1.0392], ["70", 1.4845], ["71", 3.7683999999999997], ["72", 2.5009], ["73", 3.0376], ["75", 3.3002],...
{"lang": "python3", "distribution": [["18700", 0.0114], ["18800", 0.0571], ["18900", 0.137], ["19000", 0.0114], ["19100", 0.0114], ["19200", 0.0457], ["19300", 0.0457], ["19400", 0.0343], ["19800", 0.0114], ["19900", 0.0228], ["20000", 0.0114], ["20100", 0.1827], ["20200", 1.633], ["20300", 8.9643], ["20400", 16.7295],...
[ { "code": "# 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 \"\"\"Encodes a tree to a single string.\n :type root: TreeN...
123
[ { "code": "# 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 global r\n r = root\n return \"\"\n\n def deserial...
91
81
search-in-rotated-sorted-array-ii
<p>There is an integer array <code>nums</code> sorted in non-decreasing order (not necessarily with <strong>distinct</strong> values).</p> <p>Before being passed to your function, <code>nums</code> is <strong>rotated</strong> at an unknown pivot index <code>k</code> (<code>0 &lt;= k &lt; nums.length</code>) such that ...
38.148418
Medium
[ "array", "binary-search" ]
{"lang": "python3", "distribution": [["32", 0.0166], ["33", 0.0499], ["34", 0.0997], ["35", 0.1164], ["36", 0.0997], ["37", 0.1828], ["38", 0.2826], ["39", 0.5818], ["40", 0.3657], ["41", 0.5818], ["42", 1.4129], ["43", 2.4767], ["44", 1.5625], ["45", 2.1941], ["46", 3.607], ["47", 5.0366], ["48", 3.873], ["49", 4.0559...
{"lang": "python3", "distribution": [["16800", 0.3989], ["16900", 5.4023], ["17000", 23.9694], ["17100", 41.9714]]}
[ { "code": "class Solution:\n def search(self, nums: List[int], target: int) -> bool:\n return True if target in nums else False", "runtime": "32" }, { "code": "class Solution:\n def search(self, nums: List[int], target: int) -> bool:\n l, r = 0, len(nums)-1\n\n while l<=r:...
47
[ { "code": "class Solution:\n def search(self, nums: List[int], target: int) -> bool:\n for num in nums:\n if num == target:\n return True\n return False\n ", "memory": "16800" }, { "code": "class Solution:\n def search(self, nums: List[int], targe...
7
82
remove-duplicates-from-sorted-list-ii
<p>Given the <code>head</code> of a sorted linked list, <em>delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list</em>. Return <em>the linked list <strong>sorted</strong> as well</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="htt...
48.302718
Medium
[ "linked-list", "two-pointers" ]
{"lang": "python3", "distribution": [["19", 0.0127], ["20", 0.0254], ["22", 0.1269], ["23", 0.1523], ["24", 0.0761], ["25", 0.2284], ["26", 0.4441], ["27", 0.7613], ["28", 0.3934], ["29", 0.8501], ["30", 2.284], ["31", 2.0556], ["32", 1.6242], ["33", 2.855], ["34", 5.5577], ["35", 4.606], ["36", 3.6163], ["37", 6.1667]...
{"lang": "python3", "distribution": [["16000", 0.0127], ["16100", 0.0127], ["16200", 0.0508], ["16300", 0.8121], ["16400", 14.7697], ["16500", 47.494]]}
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:\n h=head\n p=ListNode(-1)\n ...
50
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:\n left_node = ListNode(-1)\n new_h...
9
83
remove-duplicates-from-sorted-list
<p>Given the <code>head</code> of a sorted linked list, <em>delete all duplicates such that each element appears only once</em>. Return <em>the linked list <strong>sorted</strong> as well</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/202...
53.403702
Easy
[ "linked-list" ]
{"lang": "python3", "distribution": [["15", 0.006], ["18", 0.006], ["19", 0.024], ["20", 0.036], ["21", 0.03], ["22", 0.042], ["23", 0.2521], ["24", 0.054], ["25", 0.138], ["26", 0.4561], ["27", 0.8102], ["28", 0.3961], ["29", 0.7562], ["30", 1.6984], ["31", 2.2745], ["32", 1.6204], ["33", 2.1545], ["34", 4.729], ["35"...
{"lang": "python3", "distribution": [["15600", 0.006], ["16000", 0.018], ["16100", 0.018], ["16200", 0.072], ["16300", 0.9722], ["16400", 18.232], ["16500", 50.3211]]}
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:\n pointer = head\n while pointer:\...
52
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:\n current = head\n while current a...
8
84
largest-rectangle-in-histogram
<p>Given an array of integers <code>heights</code> representing the histogram&#39;s bar height where the width of each bar is <code>1</code>, return <em>the area of the largest rectangle in the histogram</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode...
45.483207
Hard
[ "array", "stack", "monotonic-stack" ]
{"lang": "python3", "distribution": [["158", 0.0492], ["166", 0.0082], ["175", 0.0082], ["183", 0.0082], ["191", 0.0164], ["200", 0.024600000000000004], ["208", 0.0082], ["217", 0.0082], ["225", 0.0082], ["234", 0.0082], ["242", 0.0082], ["251", 0.0082], ["259", 0.0082], ["267", 0.0164], ["276", 0.0082], ["284", 0.0082...
{"lang": "python3", "distribution": [["25400", 0.0164], ["25500", 0.0082], ["25700", 0.0164], ["25800", 0.0164], ["25900", 0.0164], ["26000", 0.0164], ["27900", 0.0082], ["28100", 0.0082], ["28400", 0.0082], ["28700", 0.0082], ["28800", 0.0082], ["29000", 0.0164], ["29100", 0.0082], ["29200", 0.0246], ["29300", 0.0328]...
[ { "code": "# class Solution:\ndef largestRectangleArea(heights: List[int]) -> int:\n n = len(heights)\n max_area = 0\n stack = []\n for j in range(n):\n h = heights[j]\n while stack and h < heights[stack[-1]]:\n i = stack.pop()\n left = 0 if not stack else stack[-...
109
[ { "code": "class Solution:\n def largestRectangleArea(heights: List[int]) -> int:\n n = len(heights)\n max_area = 0\n stack = []\n for j in range(n):\n h = heights[j]\n while stack and h < heights[stack[-1]]:\n i = stack.pop()\n ...
99
85
maximal-rectangle
<p>Given a <code>rows x cols</code>&nbsp;binary <code>matrix</code> filled with <code>0</code>&#39;s and <code>1</code>&#39;s, find the largest rectangle containing only <code>1</code>&#39;s and return <em>its area</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://asse...
51.67384
Hard
[ "array", "dynamic-programming", "stack", "matrix", "monotonic-stack" ]
{"lang": "python3", "distribution": [["82", 0.1332], ["107", 0.0333], ["133", 0.0666], ["158", 0.0998], ["183", 0.0333], ["208", 45.82359999999999], ["234", 27.12130000000001], ["259", 5.3575], ["284", 1.1983000000000001], ["309", 0.6658], ["335", 0.1998], ["360", 0.09990000000000002], ["385", 0.9653999999999999], ["41...
{"lang": "python3", "distribution": [["17200", 0.1331], ["17300", 0.0333], ["17400", 0.0333], ["17500", 0.1997], ["17600", 1.3977], ["17700", 5.6572], ["17800", 20.2329], ["17900", 39.1015], ["18000", 22.1631], ["18100", 3.7937], ["18200", 0.6988], ["18300", 0.3328], ["18400", 0.2329], ["18500", 0.1331], ["18600", 0.06...
[ { "code": "def largestRectangleArea(heights: List[int]) -> int:\n stack = []\n max_area = 0\n heights.append(0) \n\n for i in range(len(heights)):\n while stack and heights[i] < heights[stack[-1]]:\n h = heights[stack.pop()]\n w = i if not stack else i - stack[-1] - 1\n...
130
[ { "code": "def largestRectangleArea(heights: List[int]) -> int:\n stack = []\n max_area = 0\n heights.append(0) \n\n for i in range(len(heights)):\n while stack and heights[i] < heights[stack[-1]]:\n h = heights[stack.pop()]\n w = i if not stack else i - stack[-1] - 1\n...
25
86
partition-list
<p>Given the <code>head</code> of a linked list and a value <code>x</code>, partition it such that all nodes <strong>less than</strong> <code>x</code> come before nodes <strong>greater than or equal</strong> to <code>x</code>.</p> <p>You should <strong>preserve</strong> the original relative order of the nodes in each...
57.210937
Medium
[ "linked-list", "two-pointers" ]
{"lang": "python3", "distribution": [["16", 0.0488], ["17", 0.0163], ["18", 0.0163], ["19", 0.0814], ["20", 0.0488], ["21", 0.0326], ["22", 0.2117], ["23", 0.4884], ["24", 0.4396], ["25", 0.4559], ["26", 0.7489], ["27", 1.9212], ["28", 1.6119], ["29", 1.7584], ["30", 2.1654], ["31", 4.9007], ["32", 4.7216], ["33", 4.41...
{"lang": "python3", "distribution": [["16200", 0.0814], ["16300", 2.0352], ["16400", 18.3654], ["16500", 43.5689]]}
[ { "code": "\"\"\"\nWhat if we use a queue to store all nodes in order that are greater than x\nAll smaller nodes than x will be appended in list and move ahead in list to find more nodes.\n\nPut the partition node in the queue as well\n\"\"\"\n\n# Definition for singly-linked list.\n# class ListNode:\n# def...
48
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:\n less = ListNode(0)\n ...
6
87
scramble-string
<p>We can scramble a string s to get a string t using the following algorithm:</p> <ol> <li>If the length of the string is 1, stop.</li> <li>If the length of the string is &gt; 1, do the following: <ul> <li>Split the string into two non-empty substrings at a random index, i.e., if the string is <code>s</code>, di...
40.737301
Hard
[ "string", "dynamic-programming" ]
{"lang": "python3", "distribution": [["28", 0.5468], ["32", 1.3281], ["36", 2.8125], ["40", 6.4844], ["44", 9.609300000000001], ["48", 9.2969], ["52", 6.6406], ["56", 3.75], ["60", 3.2812], ["64", 2.7343], ["68", 2.6562], ["72", 1.7188], ["76", 1.3281], ["80", 0.8593000000000001], ["84", 1.5625], ["88", 1.3281], ["92",...
{"lang": "python3", "distribution": [["16400", 0.2344], ["16500", 1.7188], ["16600", 7.4219], ["16700", 11.3281], ["16800", 14.0625], ["16900", 9.1406], ["17000", 3.6719], ["17100", 3.0469], ["17200", 3.0469], ["17300", 5.0781], ["17400", 4.4531], ["17500", 3.4375], ["17600", 3.6719], ["17700", 0.7031], ["17800", 1.406...
[ { "code": "class Solution:\n # dictionary to store previously computed substrings\n map = {}\n\n def isScramble(self, s1: str, s2: str) -> bool:\n n = len(s1)\n # check if the two strings are equal\n if s1 == s2:\n return True\n # initialize frequency lists for s1...
132
[ { "code": "class Solution:\n def __init__(self):\n self.memo = {}\n\n def isScramble(self, s1: str, s2: str) -> bool:\n # Early pruning by checking character frequencies\n if sorted(s1) != sorted(s2):\n return False\n \n # If the strings are already the same, ...
62
88
merge-sorted-array
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in <strong>non-decreasing order</strong>, and two integers <code>m</code> and <code>n</code>, representing the number of elements in <code>nums1</code> and <code>nums2</code> respectively.</p> <p><strong>Merge</strong> <code>nums1</c...
51.006602
Easy
[ "array", "two-pointers", "sorting" ]
{"lang": "python3", "distribution": [["11", 0.0016], ["14", 0.0016], ["15", 0.0016], ["17", 0.0049], ["18", 0.0065], ["19", 0.0277], ["20", 0.0212], ["21", 0.0245], ["22", 0.1094], ["23", 0.1518], ["24", 0.129], ["25", 0.1828], ["26", 0.5517], ["27", 0.5925], ["28", 0.5485], ["29", 0.8423], ["30", 2.1514], ["31", 1.975...
{"lang": "python3", "distribution": [["15800", 0.0016], ["15900", 0.0016], ["16000", 0.0033], ["16100", 0.018], ["16200", 0.0441], ["16300", 0.7035], ["16400", 10.602], ["16500", 36.7063], ["16600", 43.2323]]}
[ { "code": "class Solution:\n def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:\n \"\"\"\n Do not return anything, modify nums1 in-place instead.\n \"\"\"\n n1, n2, i = m-1, n-1, m+n-1\n while n2 >= 0:\n if n1 >=0 and nums1[n1] > nums2[n...
60
[ { "code": "class Solution:\n def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None:\n \"\"\"\n Do not return anything, modify nums1 in-place instead.\n \"\"\"\n ptr1 = m - 1\n ptr2 = n - 1\n\n for moving_ptr in range(m + n - 1, -1, -1):\n ...
13
89
gray-code
<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p> <ul> <li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li> <li>The first integer is <code>0</code>,</li> <li>An integer appears <strong>no more than once</...
60.071494
Medium
[ "math", "backtracking", "bit-manipulation" ]
{"lang": "python3", "distribution": [["48", 0.11], ["50", 0.32989999999999997], ["53", 0.3849], ["56", 1.3195000000000001], ["58", 3.5183999999999997], ["61", 3.2436], ["64", 5.7175], ["66", 10.8301], ["69", 8.6861], ["72", 11.9296], ["74", 11.819700000000001], ["77", 5.0577], ["80", 6.5969999999999995], ["82", 3.51839...
{"lang": "python3", "distribution": [["22400", 0.055], ["23400", 0.055], ["23500", 0.2199], ["23600", 1.4294], ["23700", 6.2672], ["23800", 8.5212], ["23900", 12.8092], ["24000", 25.1787], ["24100", 19.0764], ["24200", 1.9241], ["24300", 0.6597], ["24400", 0.6047], ["24500", 0.9346], ["24600", 0.11], ["24700", 0.4398],...
[ { "code": "class Solution:\n def grayCode(self, n: int) -> List[int]:\n res = []\n for i in range(2**n):\n res.append(i ^ i >> 1)\n return res", "runtime": "48" }, { "code": "class Solution:\n def grayCode(self, n: int) -> List[int]:\n ans = [0, 1]\n ...
107
[ { "code": "class Solution:\n def grayCode(self, n: int) -> List[int]:\n \n def gen(k):\n if k == 1:\n yield from [0, 1]\n return\n \n xs = list(gen(k-1))\n yield from xs\n y = 1 << (k-1)\n for x in r...
73
90
subsets-ii
<p>Given an integer array <code>nums</code> that may contain duplicates, return <em>all possible</em> <span data-keyword="subset"><em>subsets</em></span><em> (the power set)</em>.</p> <p>The solution set <strong>must not</strong> contain duplicate subsets. Return the solution in <strong>any order</strong>.</p> <p>&nb...
58.086
Medium
[ "array", "backtracking", "bit-manipulation" ]
{"lang": "python3", "distribution": [["16", 0.0139], ["17", 0.0069], ["18", 0.0139], ["19", 0.0277], ["20", 0.0277], ["21", 0.0208], ["22", 0.1247], ["23", 0.1317], ["24", 0.3327], ["25", 0.1733], ["26", 0.5891], ["27", 0.5267], ["28", 1.0673], ["29", 1.0257], ["30", 1.9683], ["31", 1.8296], ["32", 3.2019], ["33", 3.25...
{"lang": "python3", "distribution": [["16100", 0.0139], ["16300", 0.0139], ["16400", 0.0832], ["16500", 2.4742], ["16600", 18.5668], ["16700", 32.5178], ["16800", 34.6663]]}
[ { "code": "class Solution:\n def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:\n res = [[]]\n nums.sort()\n memory = {i: 0 for i in set(nums)}\n \n for num in nums:\n end = len(res)\n for s in res[memory[num] : end]: \n res.a...
55
[ { "code": "class Solution:\n def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:\n ans = set()\n nums.sort()\n def backtracking(path, idx):\n \n if len(path) <= len(nums):\n ans.add(tuple(path))\n \n\n for i in range(idx, len(nums...
11
141
linked-list-cycle
<p>Given <code>head</code>, the head of a linked list, determine if the linked list has a cycle in it.</p> <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 the&nbsp;<code>next</code>&nbsp;pointer. Internally, <code>pos</code>&nbsp;is used to den...
51.109695
Easy
[ "hash-table", "linked-list", "two-pointers" ]
{"lang": "python3", "distribution": [["18", 0.0074], ["19", 0.0025], ["20", 0.0025], ["22", 0.0074], ["23", 0.0098], ["24", 0.0172], ["25", 0.0172], ["26", 0.0613], ["27", 0.1128], ["28", 0.1422], ["29", 0.1005], ["30", 0.2624], ["31", 0.4732], ["32", 0.5247], ["33", 0.5811], ["34", 0.8533], ["35", 1.6429], ["36", 1.86...
{"lang": "python3", "distribution": [["16600", 0.0147], ["16700", 0.0098], ["16800", 0.0049], ["17800", 0.0466], ["17900", 0.0907], ["18000", 0.1643], ["18100", 0.0147], ["18200", 0.0123], ["18300", 0.0245], ["18400", 0.1471], ["18500", 0.8214], ["18600", 0.4708], ["18700", 0.0638], ["18800", 0.2501], ["18900", 3.6536]...
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\nclass Solution:\n def hasCycle(self, head: Optional[ListNode]) -> bool:\n fast = head\n slow = head\n \n while fast and fast.next...
69
[ { "code": "# # Definition for singly-linked list.\n# # class ListNode:\n# # def __init__(self, x):\n# # self.val = x\n# # self.next = None\n\n# class Solution:\n# def hasCycle(self, head: Optional[ListNode]) -> bool:\n# if not head.next:\n# return False\n# po...
30
142
linked-list-cycle-ii
<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> <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 the <code>next</code> pointer. Internally, <co...
52.607955
Medium
[ "hash-table", "linked-list", "two-pointers" ]
{"lang": "python3", "distribution": [["18", 0.0109], ["22", 0.0109], ["23", 0.0326], ["24", 0.0218], ["25", 0.0544], ["26", 0.0979], ["27", 0.1958], ["28", 0.1305], ["29", 0.1632], ["30", 0.5548], ["31", 0.9465], ["32", 0.5875], ["33", 0.9574], ["34", 2.2302], ["35", 2.4369], ["36", 1.6536], ["37", 2.4151], ["38", 5.26...
{"lang": "python3", "distribution": [["18100", 0.0218], ["18200", 0.2067], ["18300", 0.3155], ["18400", 0.0544], ["18500", 0.0218], ["18600", 0.087], ["18700", 1.6971], ["18800", 11.1619], ["18900", 10.6832], ["19000", 15.9051], ["19100", 35.7158], ["19200", 3.1005], ["19300", 0.4896], ["19400", 3.8512], ["19500", 10.0...
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\nclass Solution:\n def detectCycle(self, head: ListNode) -> ListNode:\n if not head or not head.next:\n return None\n \n # 步驟 ...
53
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\nclass Solution:\n def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:\n slow = head\n while slow:\n if slow.val =...
24
143
reorder-list
<p>You are given the head of a singly linked-list. The list can be represented as:</p> <pre> L<sub>0</sub> &rarr; L<sub>1</sub> &rarr; &hellip; &rarr; L<sub>n - 1</sub> &rarr; L<sub>n</sub> </pre> <p><em>Reorder the list to be on the following form:</em></p> <pre> L<sub>0</sub> &rarr; L<sub>n</sub> &rarr; L<sub>1</s...
60.092021
Medium
[ "linked-list", "two-pointers", "stack", "recursion" ]
{"lang": "python3", "distribution": [["28", 0.0128], ["31", 0.0191], ["32", 0.0319], ["33", 0.0255], ["34", 0.0447], ["35", 0.0447], ["36", 0.0893], ["37", 0.0574], ["38", 0.1212], ["39", 0.3382], ["40", 0.2808], ["41", 0.3318], ["42", 0.6827], ["43", 0.9252], ["44", 1.0209], ["45", 1.2698], ["46", 1.7164], ["47", 2.64...
{"lang": "python3", "distribution": [["22000", 0.0064], ["22200", 0.0064], ["22300", 0.0128], ["22600", 0.0128], ["22900", 0.0064], ["23000", 0.0064], ["23100", 0.0064], ["23600", 0.0191], ["23700", 0.0064], ["23800", 0.1212], ["23900", 0.2552], ["24000", 0.1149], ["24100", 0.1212], ["24200", 0.1021], ["24300", 0.1276]...
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reorderList(self, head: Optional[ListNode]) -> None:\n \"\"\"\n Do not return anything, modify head in-pla...
89
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reorderList(self, head: Optional[ListNode]) -> None:\n \"\"\"\n Do not return anything, modify head in-pla...
24
144
binary-tree-preorder-traversal
<p>Given the <code>root</code> of a binary tree, return <em>the preorder traversal of its nodes&#39; values</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p> <p><strong>Output:</str...
70.821843
Easy
[ "stack", "tree", "depth-first-search", "binary-tree" ]
{"lang": "python3", "distribution": [["11", 0.0071], ["12", 0.0071], ["15", 0.0214], ["16", 0.0286], ["17", 0.0214], ["18", 0.0715], ["19", 0.1429], ["20", 0.1287], ["21", 0.2573], ["22", 0.6361], ["23", 0.6933], ["24", 0.7505], ["25", 0.922], ["26", 2.3158], ["27", 2.0728], ["28", 2.0513], ["29", 3.3236], ["30", 6.189...
{"lang": "python3", "distribution": [["15800", 0.0071], ["15900", 0.0071], ["16100", 0.0143], ["16200", 0.2573], ["16300", 4.8388], ["16400", 26.2169], ["16500", 49.296]]}
[ { "code": "# 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 preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n if no...
51
[ { "code": "# 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 preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n \n ...
10
145
binary-tree-postorder-traversal
<p>Given the <code>root</code> of a&nbsp;binary tree, return <em>the postorder traversal of its nodes&#39; values</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p> <p><strong>Output...
73.438014
Easy
[ "stack", "tree", "depth-first-search", "binary-tree" ]
{"lang": "python3", "distribution": [["7", 0.003], ["12", 0.0089], ["14", 0.0089], ["15", 0.0238], ["16", 0.0356], ["17", 0.0208], ["18", 0.0831], ["19", 0.2138], ["20", 0.1573], ["21", 0.1484], ["22", 0.6502], ["23", 0.6056], ["24", 0.8461], ["25", 0.9322], ["26", 2.5413], ["27", 1.8228], ["28", 2.3691], ["29", 3.3221...
{"lang": "python3", "distribution": [["15900", 0.003], ["16000", 0.003], ["16100", 0.0327], ["16200", 0.2108], ["16300", 4.2038], ["16400", 25.5077], ["16500", 49.4181]]}
[ { "code": "# 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 postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n if n...
51
[ { "code": "# 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 postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:\n resu...
10
146
lru-cache
<p>Design a data structure that follows the constraints of a <strong><a href="https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU" target="_blank">Least Recently Used (LRU) cache</a></strong>.</p> <p>Implement the <code>LRUCache</code> class:</p> <ul> <li><code>LRUCache(int capacity)</code> Initialize the L...
43.256685
Medium
[ "hash-table", "linked-list", "design", "doubly-linked-list" ]
{"lang": "python3", "distribution": [["157", 0.0164], ["181", 0.0041], ["204", 0.0041], ["228", 0.0041], ["252", 0.0164], ["275", 0.0041], ["299", 0.0164], ["322", 0.0123], ["346", 0.0164], ["370", 0.0041], ["393", 0.0082], ["417", 0.0287], ["440", 0.0123], ["464", 0.0287], ["488", 0.15200000000000002], ["511", 7.1828]...
{"lang": "python3", "distribution": [["68800", 0.0082], ["68900", 0.0041], ["73600", 0.0041], ["74100", 0.0041], ["74800", 0.0041], ["74900", 0.0041], ["75100", 0.0041], ["75300", 0.0123], ["75400", 0.0082], ["75500", 0.0082], ["75600", 0.0041], ["75700", 0.0164], ["75800", 0.0123], ["76000", 0.0534], ["76100", 0.0945]...
[ { "code": "def lc(operations, params):\n cache = OrderedDict()\n result = []\n capacity = None\n for i, op in enumerate(operations):\n if op == \"LRUCache\":\n capacity = params[i][0]\n result.append(None)\n elif op == \"put\":\n key, value = params[i]\...
121
[ { "code": "def lc(operations, params):\n cache = OrderedDict()\n result = []\n capacity = None\n for i, op in enumerate(operations):\n if op == \"LRUCache\":\n capacity = params[i][0]\n result.append(None)\n elif op == \"put\":\n key, value = params[i]\...
71
147
insertion-sort-list
<p>Given the <code>head</code> of a singly linked list, sort the list using <strong>insertion sort</strong>, and return <em>the sorted list&#39;s head</em>.</p> <p>The steps of the <strong>insertion sort</strong> algorithm:</p> <ol> <li>Insertion sort iterates, consuming one input element each repetition and growing...
54.566277
Medium
[ "linked-list", "sorting" ]
{"lang": "python3", "distribution": [["30", 3.3314999999999997], ["44", 11.3875], ["57", 3.8289999999999997], ["71", 19.4431], ["84", 5.320699999999999], ["98", 0.9948], ["112", 0.1492], ["125", 0.0497], ["139", 0.0497], ["152", 0.0497], ["166", 0.0497], ["179", 0.0497], ["193", 0.0497], ["206", 0.0497], ["220", 0.0497...
{"lang": "python3", "distribution": [["18100", 0.0995], ["18200", 0.2486], ["18300", 3.7295], ["18400", 21.4321], ["18500", 36.7976], ["18600", 19.7911], ["18700", 7.6579]]}
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:\n if head==None:\n return\n ...
124
[ { "code": "class ListNode:\n def __init__(self, val=0, next=None):\n self.val = val\n self.next = next\n\nclass Solution:\n def insertionSortList(self, head: ListNode) -> ListNode:\n dummy = ListNode(0)\n curr = head\n \n while curr:\n prev = dummy\n ...
11
148
sort-list
<p>Given the <code>head</code> of a linked list, return <em>the list after sorting it in <strong>ascending order</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg" style="width: 450px; height: 194px;" /> <...
59.491966
Medium
[ "linked-list", "two-pointers", "divide-and-conquer", "sorting", "merge-sort" ]
{"lang": "python3", "distribution": [["51", 0.0791], ["57", 0.0527], ["63", 0.0132], ["69", 0.0132], ["75", 0.0263], ["81", 0.0132], ["87", 0.0132], ["93", 0.0263], ["99", 0.1579], ["105", 0.7103], ["111", 1.5259], ["117", 3.0518], ["123", 2.6965999999999997], ["128", 1.5259], ["134", 0.9208999999999998], ["140", 0.670...
{"lang": "python3", "distribution": [["20900", 0.0263], ["21000", 0.0658], ["21100", 0.0132], ["21200", 0.0132], ["26200", 0.0132], ["30600", 0.0132], ["31200", 0.0132], ["31500", 0.0263], ["31600", 0.0132], ["31700", 0.0263], ["31800", 0.0263], ["31900", 0.0921], ["32000", 0.1052], ["32100", 0.2368], ["32200", 1.6838]...
[ { "code": "with open(\"user.out\", \"w\") as Solution:\n for head in map(loads, stdin):\n Solution.write(f\"{sorted(head)}\\n\".replace(\" \",\"\"))\nexit()", "runtime": "51" }, { "code": "f = open('user.out','w')\nfor arr in map(loads, stdin):\n print(str(sorted(arr)).replace(' ',''), ...
148
[ { "code": "f = open('user.out','w')\nfor arr in map(loads, stdin):\n print(str(sorted(arr)).replace(' ',''), file=f)\nexit(0)", "memory": "20900" }, { "code": "f = open('user.out','w')\nfor arr in map(loads, stdin):\n print(str(sorted(arr)).replace(' ',''), file=f)\nexit(0)", "memory": "21...
69
149
max-points-on-a-line
<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</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" ...
27.52081
Hard
[ "array", "hash-table", "math", "geometry" ]
{"lang": "python3", "distribution": [["48", 13.637699999999999], ["70", 37.489999999999995], ["92", 18.772100000000002], ["114", 7.198999999999999], ["136", 3.3143], ["158", 1.6299], ["179", 1.3582999999999996], ["201", 1.1137000000000001], ["223", 0.8424], ["245", 0.7609], ["267", 0.3807], ["289", 0.2718], ["310", 0.3...
{"lang": "python3", "distribution": [["16354", 25.8898], ["16663", 45.096500000000006], ["16971", 0.9508], ["17280", 0.0544], ["17589", 0.1358], ["17898", 0.1087], ["18206", 0.0272], ["18515", 0.0815], ["18824", 0.0544], ["19133", 0.0815], ["19441", 0.2717], ["19750", 0.2988], ["20059", 0.1358], ["20368", 0.0815], ["20...
[ { "code": "class Solution:\n def maxPoints(self, points: List[List[int]]) -> int:\n n = len(points) #amount of point\n maxP = 0 # current max points - initalize variable\n\n for i in range(n):\n X = points[i][0]\n Y = points[i][1]\n map = {} # create temp...
140
[ { "code": "class Solution:\n def maxPoints(self, points: List[List[int]]) -> int:\n n = len(points)\n if n < 3:\n return n\n\n max_pts = 2\n for i in range(n):\n [x1, y1] = points[i]\n for j in range(i+1,n):\n [x2, y2] = points[j]\n ...
141
150
evaluate-reverse-polish-notation
<p>You are given an array of strings <code>tokens</code> that represents an arithmetic expression in a <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a>.</p> <p>Evaluate the expression. Return <em>an integer that represents the value of the expression</em>.</p> ...
52.703603
Medium
[ "array", "math", "stack" ]
{"lang": "python3", "distribution": [["26", 0.0042], ["28", 0.0042], ["30", 0.0042], ["31", 0.0125], ["32", 0.0042], ["33", 0.0083], ["34", 0.0125], ["36", 0.0083], ["39", 0.0042], ["40", 0.0083], ["41", 0.0208], ["42", 0.0125], ["43", 0.0167], ["44", 0.0167], ["45", 0.0125], ["46", 0.0333], ["47", 0.0834], ["48", 0.06...
{"lang": "python3", "distribution": [["16400", 0.0083], ["16500", 0.0167], ["16600", 0.0292], ["16700", 0.0417], ["16800", 0.2626], ["16900", 3.5555], ["17000", 36.6804], ["17100", 47.4428]]}
[ { "code": "def evalRPN(tokens: List[str]) -> int:\n stack = []\n operators = set([\"+\", \"-\", \"*\", \"/\"])\n for token in tokens:\n if token in operators:\n num1, num2 = stack.pop(), stack.pop()\n if token == \"+\":\n stack.append(num1 + num2)\n ...
108
[ { "code": "# class Solution:\n# def evalRPN(self, tokens: List[str]) -> int:\n# numbers = []\n# operations = {\n# \"+\" : lambda x, y: x + y,\n# \"-\" : lambda x, y: x - y,\n# \"*\" : lambda x, y: x * y,\n# \"/\" : lambda x, y: int(x / y)\n# ...
11
151
reverse-words-in-a-string
<p>Given an input string <code>s</code>, reverse the order of the <strong>words</strong>.</p> <p>A <strong>word</strong> is defined as a sequence of non-space characters. The <strong>words</strong> in <code>s</code> will be separated by at least one space.</p> <p>Return <em>a string of the words in reverse order conc...
46.225654
Medium
[ "two-pointers", "string" ]
{"lang": "python3", "distribution": [["12", 0.0027], ["14", 0.0108], ["15", 0.0269], ["16", 0.0134], ["17", 0.0188], ["18", 0.0296], ["19", 0.1371], ["20", 0.086], ["21", 0.1102], ["22", 0.2823], ["23", 0.6156], ["24", 0.4167], ["25", 0.6344], ["26", 1.5323], ["27", 1.828], ["28", 1.4328], ["29", 1.9892], ["30", 4.164]...
{"lang": "python3", "distribution": [["15800", 0.0027], ["15900", 0.0027], ["16100", 0.0054], ["16200", 0.0108], ["16300", 0.0538], ["16400", 1.2124], ["16500", 12.328], ["16600", 28.5188], ["16700", 41.5027]]}
[ { "code": "class Solution:\n def reverseWords(self, s: str) -> str:\n s = s.split()[::-1]\n print(s)\n s = \" \".join(s)\n return s", "runtime": "12" }, { "code": "class Solution:\n def reverseWords(self, s: str) -> str:\n s = s.strip()\n words = s.spl...
60
[ { "code": "class Solution:\n def reverseWords(self, s: str) -> str:\n a = s.split()\n #print(a) # This will print the list of words\n\n# Reverse the list of words\n a.reverse()\n\n# Join the reversed list of words with a single space\n r = ' '.join(a)\n return r\n \...
13
152
maximum-product-subarray
<p>Given an integer array <code>nums</code>, find a <span data-keyword="subarray-nonempty">subarray</span> that has the largest product, and return <em>the product</em>.</p> <p>The test cases are generated so that the answer will fit in a <strong>32-bit</strong> integer.</p> <p>&nbsp;</p> <p><strong class="example">E...
33.912273
Medium
[ "array", "dynamic-programming" ]
{"lang": "python3", "distribution": [["37", 0.0183], ["39", 0.0061], ["41", 0.0061], ["42", 0.0061], ["43", 0.0122], ["44", 0.0122], ["45", 0.0244], ["46", 0.0183], ["47", 0.0366], ["48", 0.0183], ["49", 0.0061], ["50", 0.0183], ["51", 0.0366], ["52", 0.0427], ["53", 0.1159], ["54", 0.2256], ["55", 0.2134], ["56", 0.28...
{"lang": "python3", "distribution": [["16200", 0.0061], ["16300", 0.0061], ["16500", 0.0061], ["16600", 0.0305], ["16700", 0.0854], ["16800", 0.7013], ["16900", 12.9406], ["17000", 56.8972], ["17100", 15.0811], ["17200", 1.2075], ["17300", 1.6953], ["17400", 0.3537], ["17500", 0.2195], ["17600", 0.1586], ["17700", 0.08...
[ { "code": "def maxProduct(a: List[int]) -> int:\n b = a[::-1]\n for i in range(1, len(a)):\n a[i] *= a[i - 1] or 1\n b[i] *= b[i - 1] or 1\n return max(a + b)\nf = open('user.out', 'w')\nfor i in stdin:\n nums = list(map(int,i.rstrip()[1:-1].split(r',')))\n print...
76
[ { "code": "class Solution:\n def maxProduct(self, nums: List[int]) -> int:\n res = float(\"-inf\")\n cur = 1\n for i in nums:\n cur *= i\n res = max(res, cur, i)\n if cur == 0:\n cur = 1\n \n rcur = 1\n for j in range(l...
30
153
find-minimum-in-rotated-sorted-array
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,2,4,5,6,7]</code> might become:</p> <ul> <li><code>[4,5,6,7,0,1,2]</code> if it was rotated <code>4</code> times.</li> <li><code...
51.319378
Medium
[ "array", "binary-search" ]
{"lang": "python3", "distribution": [["14", 0.003], ["20", 0.003], ["22", 0.0302], ["23", 0.0362], ["24", 0.0543], ["25", 0.0392], ["26", 0.1539], ["27", 0.1569], ["28", 0.2142], ["29", 0.2414], ["30", 0.7815], ["31", 0.691], ["32", 0.9203], ["33", 1.2039], ["34", 2.6191], ["35", 1.8919], ["36", 2.8032], ["37", 3.7869]...
{"lang": "python3", "distribution": [["16300", 0.006], ["16400", 0.0332], ["16500", 0.1418], ["16600", 2.1967], ["16700", 18.8226], ["16800", 34.3985], ["16900", 38.0435]]}
[ { "code": "class Solution:\n def findMin(self, nums: List[int]) -> int:\n left, right = 0, len(nums) - 1\n res = nums[0]\n while left <= right:\n mid = (left + right) // 2\n res = min(res, nums[mid])\n if nums[mid] >= nums[left]:\n if nums[...
53
[ { "code": "class Solution:\n\n def findMin(self, nums: List[int]) -> int:\n left, right = 0, len(nums) - 1\n \n while left <right:\n mid = (left + right) // 2\n \n if nums[mid] > nums[right]:\n # Minimum is in the right part\n ...
11
154
find-minimum-in-rotated-sorted-array-ii
<p>Suppose an array of length <code>n</code> sorted in ascending order is <strong>rotated</strong> between <code>1</code> and <code>n</code> times. For example, the array <code>nums = [0,1,4,4,5,6,7]</code> might become:</p> <ul> <li><code>[4,5,6,7,0,1,4]</code> if it was rotated <code>4</code> times.</li> <li><code...
43.755276
Hard
[ "array", "binary-search" ]
{"lang": "python3", "distribution": [["27", 0.0376], ["29", 0.0376], ["30", 0.0376], ["32", 0.0751], ["33", 0.0751], ["34", 0.1502], ["35", 0.338], ["36", 0.1127], ["37", 0.1878], ["38", 0.6384], ["39", 0.7886], ["40", 0.4882], ["41", 1.1265], ["42", 2.8539], ["43", 1.9151], ["44", 1.427], ["45", 2.6286], ["46", 5.4074...
{"lang": "python3", "distribution": [["16700", 0.1127], ["16800", 0.5257], ["16900", 8.4116], ["17000", 25.6478], ["17100", 44.6489]]}
[ { "code": "class Solution:\n def findMin(self, a: List[int]) -> int:\n beg=0\n last=len(a)-1\n while beg<last:\n mid=(last+beg)//2\n if a[mid]>a[last]:\n beg=mid+1\n elif a[mid]<a[last]:\n last=mid\n else:\n ...
47
[ { "code": "class Solution:\n def findMin(self, nums: List[int]) -> int:\n if len(nums) == 1: return nums[0]\n lowest = nums[0]\n # Find the pivot.\n i = 1\n # while i < len(nums) and nums[i-1] < nums[i]:\n # i += 1\n for i in range(1, len(nums)):\n ...
9
155
min-stack
<p>Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.</p> <p>Implement the <code>MinStack</code> class:</p> <ul> <li><code>MinStack()</code> initializes the stack object.</li> <li><code>void push(int val)</code> pushes the element <code>val</code> onto the stack.</li> ...
54.984805
Medium
[ "stack", "design" ]
{"lang": "python3", "distribution": [["28", 0.022], ["34", 0.6137], ["40", 3.0391000000000004], ["45", 15.2104], ["51", 32.0317], ["56", 24.8173], ["62", 10.5238], ["68", 0.9022000000000001], ["73", 0.1205], ["79", 0.054900000000000004], ["85", 0.0476], ["90", 0.0805], ["96", 0.0219], ["101", 0.0147], ["107", 0.011], [...
{"lang": "python3", "distribution": [["19400", 0.0037], ["19600", 0.011], ["19700", 0.0073], ["19800", 0.0146], ["19900", 0.011], ["20000", 0.0365], ["20100", 0.2046], ["20200", 3.0611], ["20300", 5.9066], ["20400", 5.4573], ["20500", 36.2836], ["20600", 23.2795], ["20700", 1.673], ["20800", 6.9696], ["20900", 5.8336],...
[ { "code": "class MinStack:\n\n def __init__(self):\n self.stack = []\n self.min_stack = []\n\n def push(self, val: int) -> None:\n self.stack.append(val)\n self.min_stack.append(val if not self.min_stack or self.min_stack[-1] > val else self.min_stack[-1])\n\n def pop(self) ...
139
[ { "code": "class MinStack:\n\n def __init__(self):\n self.stack=[]\n self.min_stack=[]\n \n def push(self, val: int) -> None:\n self.stack.append(val)\n if not self.min_stack or val<=self.min_stack[-1]:\n self.min_stack.append(val)\n \n def pop(self) -> Non...
24
160
intersection-of-two-linked-lists
<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> <p>For example, the following two linked lists begin to intersect at node <code>c1</code...
58.841172
Easy
[ "hash-table", "linked-list", "two-pointers" ]
{"lang": "python3", "distribution": [["38", 0.0083], ["56", 0.0331], ["57", 0.0331], ["58", 0.0248], ["59", 0.0248], ["60", 0.0662], ["61", 0.0496], ["62", 0.1324], ["63", 0.1986], ["64", 0.2648], ["65", 0.3889], ["66", 0.5378], ["67", 0.6702], ["68", 0.9019], ["69", 1.299], ["70", 1.299], ["71", 1.8037], ["72", 2.2174...
{"lang": "python3", "distribution": [["17900", 0.0083], ["25500", 0.0083], ["26000", 0.0083], ["26400", 0.0083], ["26500", 0.0083], ["26600", 0.0331], ["26700", 0.0496], ["26800", 0.0414], ["26900", 0.6123], ["27000", 8.3237], ["27100", 38.8714], ["27200", 25.0952], ["27300", 3.169], ["27400", 2.4822], ["27500", 2.7635...
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\n# class Solution:\n# def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:\n# tempA = headA\n# tempB = head...
62
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\n# class Solution:\n# def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:\n# tempA = headA\n# tempB = head...
29
237
delete-node-in-a-linked-list
<p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p> <p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p> <p>All the values of the linked list are <strong>unique</str...
80.767738
Medium
[ "linked-list" ]
{"lang": "python3", "distribution": [["16", 0.0136], ["18", 0.0136], ["19", 0.0136], ["20", 0.0408], ["21", 0.1089], ["22", 0.1769], ["23", 0.1905], ["24", 0.2858], ["25", 0.1225], ["26", 0.7756], ["27", 0.6668], ["28", 1.2655], ["29", 1.4288], ["30", 2.531], ["31", 1.905], ["32", 3.7828], ["33", 4.3679], ["34", 5.0075...
{"lang": "python3", "distribution": [["16400", 0.0136], ["16500", 0.0953], ["16600", 3.1297], ["16700", 31.011], ["16800", 52.0343]]}
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\nclass Solution:\n def deleteNode(self, node:ListNode) -> None:\n node.val = node.next.val\n node.next = node.next.next\n\n\n \n "...
48
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\nclass Solution:\n def deleteNode(self, node):\n \"\"\"\n :type node: ListNode\n :rtype: void Do not return anything, modify node in-plac...
8
238
product-of-array-except-self
<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p> <p>The product of any prefix or suffix of <code>nums</code> is <strong>...
66.710518
Medium
[ "array", "prefix-sum" ]
{"lang": "python3", "distribution": [["63", 0.0050999999999999995], ["66", 0.0069], ["69", 0.0069], ["73", 0.0139], ["76", 0.0087], ["79", 0.031400000000000004], ["82", 0.019200000000000002], ["85", 0.0226], ["88", 0.0157], ["92", 0.0034], ["95", 0.0052], ["98", 0.0034], ["101", 0.0035], ["104", 0.0017], ["108", 0.0035...
{"lang": "python3", "distribution": [["19800", 0.0052], ["20100", 0.0017], ["22600", 0.0017], ["22700", 0.0017], ["23300", 0.0017], ["23700", 0.0017], ["23800", 0.0017], ["23900", 0.0035], ["24100", 0.0052], ["24200", 0.0017], ["24300", 0.007], ["24400", 0.007], ["24500", 0.0087], ["24600", 0.0594], ["24700", 0.0577], ...
[ { "code": "class Solution:\n def productExceptSelf( nums: List[int]) -> List[int]:\n n = len(nums)\n\n \n left = [0]*n\n right = [0]*n\n left_mul,right_mul = 1,1\n\n\n for i in range(n):\n j = -i-1\n\n left[i] = left_mul\n right[j] =...
135
[ { "code": "def productExceptSelf(nums: List[int]) -> List[int]:\n totalWithZero = 1\n totalWithOutZero = 1\n cnt = 0\n for i in nums:\n if i == 0:\n cnt += 1 \n if cnt > 1:\n return [0] * len(nums)\n totalWithZero *= i\n if i != 0:\n to...
55
239
sliding-window-maximum
<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p> <p>Return <em>the ma...
46.834897
Hard
[ "array", "queue", "sliding-window", "heap-priority-queue", "monotonic-queue" ]
{"lang": "python3", "distribution": [["249", 0.08500000000000002], ["266", 0.0425], ["283", 0.0085], ["301", 0.0085], ["318", 0.0085], ["335", 0.0085], ["352", 0.0085], ["369", 0.0085], ["386", 0.017], ["403", 0.017], ["421", 0.0085], ["438", 0.017], ["455", 0.017], ["472", 0.0425], ["489", 0.0425], ["506", 0.0425], ["...
{"lang": "python3", "distribution": [["25698", 0.0425], ["25896", 0.0425], ["26093", 0.0596], ["26291", 0.017], ["26488", 0.0085], ["26686", 0.0085], ["26883", 0.0596], ["27081", 0.0255], ["27278", 0.017], ["27476", 0.0085], ["27673", 0.0681], ["27871", 0.034], ["28068", 0.0596], ["28266", 0.1787], ["28463", 0.6126], [...
[ { "code": "class Solution:\n def maxSlidingWindow(nums: List[int], k: int) -> List[int]:\n q = deque()\n # only keep track the biggest\n for i in range(k-1):\n while q and nums[q[-1]] < nums[i]:\n q.pop()\n q.append(i)\n ans = []\n for i...
116
[ { "code": "class Solution:\n def maxSlidingWindow(nums: list[int], k: int) -> list[int]:\n \n result = []\n max_stack = []\n \n for i in range(k-1):\n while max_stack and nums[max_stack[-1]] < nums[i]:\n max_stack.pop()\n max_stack.appen...
127
240
search-a-2d-matrix-ii
<p>Write an efficient algorithm that searches for a value <code>target</code> in an <code>m x n</code> integer matrix <code>matrix</code>. This matrix has the following properties:</p> <ul> <li>Integers in each row are sorted in ascending from left to right.</li> <li>Integers in each column are sorted in ascending f...
53.515693
Medium
[ "array", "binary-search", "divide-and-conquer", "matrix" ]
{"lang": "python3", "distribution": [["41", 0.0196], ["42", 0.0196], ["47", 0.0391], ["50", 0.0196], ["51", 0.0196], ["113", 0.0196], ["115", 0.0196], ["116", 0.0391], ["117", 0.0391], ["118", 0.0782], ["119", 0.1369], ["120", 0.0978], ["121", 0.2346], ["122", 0.2151], ["123", 0.3129], ["124", 0.4497], ["125", 0.6648],...
{"lang": "python3", "distribution": [["20500", 0.0196], ["21400", 0.0196], ["21500", 0.0782], ["21600", 0.0196], ["22400", 0.0196], ["22500", 0.1369], ["22600", 0.176], ["22700", 0.7039], ["22800", 8.7994], ["22900", 49.2178], ["23000", 31.2671], ["23100", 2.9722]]}
[ { "code": "\ndef searchMatrix(matrix: List[List[int]], target: int) -> bool:\n i = j = 0\n m = len(matrix)\n n = len(matrix[0])\n\n while i < m and matrix[i][j] <= target:\n if matrix[i][j] == target:\n return 'true'\n i += 1\n \n i -= 1\n while i >= 0:\n j =...
57
[ { "code": "class Solution:\n def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:\n m, n = len(matrix), len(matrix[0])\n \n for row in matrix:\n if row[0] <= target <= row[-1]:\n left, right = 0, n - 1\n\n while left <= right:\n ...
17
241
different-ways-to-add-parentheses
<p>Given a string <code>expression</code> of numbers and operators, return <em>all possible results from computing all the different possible ways to group numbers and operators</em>. You may return the answer in <strong>any order</strong>.</p> <p>The test cases are generated such that the output values fit in a 32-bi...
66.284502
Medium
[ "math", "string", "dynamic-programming", "recursion", "memoization" ]
{"lang": "python3", "distribution": [["18", 0.1313], ["21", 0.3283], ["22", 0.394], ["23", 0.3283], ["24", 0.4596], ["25", 0.2626], ["26", 0.9192], ["27", 1.1819], ["28", 1.1819], ["29", 1.0506], ["30", 3.283], ["31", 3.7426], ["32", 2.8234], ["33", 3.8083], ["34", 4.9245], ["35", 5.5811], ["36", 6.6973], ["37", 4.9902...
{"lang": "python3", "distribution": [["16300", 0.1313], ["16400", 0.9849], ["16500", 8.2075], ["16600", 22.981], ["16700", 40.1182], ["16800", 19.501]]}
[ { "code": "class Solution:\n def diffWaysToCompute(self, expression: str) -> List[int]:\n\n @cache\n def dfs(expr):\n if len(expr) == 0:\n return []\n \n if expr.isnumeric():\n return [int(expr)]\n\n result = []\n ...
64
[ { "code": "class Solution:\n def recurse(self, expr) -> list[int]:\n try:\n return [int(expr)]\n except:\n results = []\n for op in range(len(expr)):\n if expr[op] in '+-*':\n left_side = self.recurse(expr[:op])\n ...
10
242
valid-anagram
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code> if <code>t</code> is an <span data-keyword="anagram">anagram</span> of <code>s</code>, and <code>false</code> otherwise.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</...
65.311587
Easy
[ "hash-table", "string", "sorting" ]
{"lang": "python3", "distribution": [["20", 0.0016], ["22", 0.0078], ["23", 0.011], ["24", 0.0078], ["25", 0.0188], ["26", 0.0627], ["27", 0.0627], ["28", 0.0909], ["29", 0.094], ["30", 0.1928], ["31", 0.2335], ["32", 0.4185], ["33", 0.4357], ["34", 0.5329], ["35", 0.7994], ["36", 1.1395], ["37", 1.1505], ["38", 1.4953...
{"lang": "python3", "distribution": [["15900", 0.0016], ["16100", 0.0016], ["16300", 0.0031], ["16400", 0.0031], ["16500", 0.0282], ["16600", 0.0376], ["16700", 0.4154], ["16800", 8.1804], ["16900", 44.9741], ["17000", 18.5646], ["17100", 0.7633], ["17200", 1.8699], ["17300", 6.3857], ["17400", 13.3059]]}
[ { "code": "from collections import Counter\n\nclass Solution:\n def isAnagram(self, s: str, t: str) -> bool:\n if len(s) != len(t):\n return False\n s_count = Counter(s)\n t_count = Counter(t)\n\n for i,j in s_count.items():\n if t_count.get(i, -1) != j:\n ...
82
[ { "code": "class Solution:\n def isAnagram(self, s: str, t: str) -> bool:\n \n count = defaultdict(int)\n\n for x in s:\n count[x] += 1\n\n for x in t:\n count[x] -= 1\n\n for val in count.values():\n if val != 0:\n return Fal...
22
31
next-permutation
<p>A <strong>permutation</strong> of an array of integers is an arrangement of its members into a sequence or linear order.</p> <ul> <li>For example, for <code>arr = [1,2,3]</code>, the following are all the permutations of <code>arr</code>: <code>[1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1]</code>.</li> ...
41.075688
Medium
[ "array", "two-pointers" ]
{"lang": "python3", "distribution": [["22", 0.0073], ["23", 0.0073], ["24", 0.0219], ["25", 0.0439], ["26", 0.1756], ["27", 0.1756], ["28", 0.1609], ["29", 0.2268], ["30", 0.7388], ["31", 0.5999], ["32", 0.929], ["33", 1.3021], ["34", 2.6408], ["35", 2.0263], ["36", 2.2531], ["37", 3.6138], ["38", 5.5743], ["39", 4.608...
{"lang": "python3", "distribution": [["16000", 0.0073], ["16100", 0.0219], ["16200", 0.117], ["16300", 3.6138], ["16400", 22.2677], ["16500", 40.3731]]}
[ { "code": "class Solution:\n def nextPermutation(self, nums: List[int]) -> None:\n length = len(nums) \n pivot = -1\n for i in range(length - 2, -1, -1):\n if nums[i] < nums[i + 1]:\n pivot = i\n break\n if pivot != -1:\n for j i...
48
[ { "code": "class Solution:\n def nextPermutation(self, nums: List[int]) -> None:\n \"\"\"\n Do not return anything, modify nums in-place instead.\n \"\"\"\n elem=-1\n for i in range(len(nums)-2,-1,-1):\n if nums[i]<nums[i+1]:\n elem=i\n ...
9
32
longest-valid-parentheses
<p>Given a string containing just the characters <code>&#39;(&#39;</code> and <code>&#39;)&#39;</code>, return <em>the length of the longest valid (well-formed) parentheses </em><span data-keyword="substring-nonempty"><em>substring</em></span>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre...
34.762127
Hard
[ "string", "dynamic-programming", "stack" ]
{"lang": "python3", "distribution": [["22", 0.0183], ["24", 0.0915], ["25", 0.0549], ["26", 0.1464], ["27", 0.2562], ["28", 0.366], ["29", 0.4392], ["30", 0.6953], ["31", 0.9332], ["32", 1.2626], ["33", 1.2077], ["34", 1.5737], ["35", 3.5865], ["36", 3.7328], ["37", 3.5499], ["38", 4.2086], ["39", 5.9103], ["40", 5.764...
{"lang": "python3", "distribution": [["16300", 0.0915], ["16400", 1.3358], ["16500", 6.6972], ["16600", 9.1308], ["16700", 6.9716], ["16800", 2.6532], ["16900", 1.5188], ["17000", 2.1958], ["17100", 10.6496], ["17200", 20.9149], ["17300", 21.9396], ["17400", 6.1482], ["17500", 0.9881], ["17600", 0.4941], ["17700", 0.23...
[ { "code": "class Solution:\n def longestValidParentheses(self, s: str) -> int:\n stack = [-1]\n max_len = 0\n \n for i, char in enumerate(s):\n if char == '(':\n stack.append(i)\n else:\n stack.pop()\n if not stack...
77
[ { "code": "class Solution:\n def longestValidParentheses(self, s: str) -> int:\n\n \n left = right = best = 0\n for i in range(len(s)):\n if s[i] == \"(\":\n left += 1\n else:\n right += 1\n if right > left:\n ...
33
33
search-in-rotated-sorted-array
<p>There is an integer array <code>nums</code> sorted in ascending order (with <strong>distinct</strong> values).</p> <p>Prior to being passed to your function, <code>nums</code> is <strong>possibly rotated</strong> at an unknown pivot index <code>k</code> (<code>1 &lt;= k &lt; nums.length</code>) such that the result...
41.508113
Medium
[ "array", "binary-search" ]
{"lang": "python3", "distribution": [["19", 0.0027], ["22", 0.0109], ["23", 0.0462], ["24", 0.0082], ["25", 0.0272], ["26", 0.125], ["27", 0.182], ["28", 0.1331], ["29", 0.2146], ["30", 0.6982], ["31", 0.6439], ["32", 0.6031], ["33", 0.9807], ["34", 2.5564], ["35", 2.1408], ["36", 1.6626], ["37", 2.8118], ["38", 6.0692...
{"lang": "python3", "distribution": [["16200", 0.0054], ["16400", 0.0082], ["16500", 0.0299], ["16600", 0.3505], ["16700", 5.1862], ["16800", 22.9889], ["16900", 41.1557], ["17000", 25.2438]]}
[ { "code": "class Solution:\n def condition(self, nums: List[int], target: int, mid: int, left:int , right:int ):\n if nums[left]<= nums[mid]:\n if nums[left] <= target < nums[mid]:\n return True\n else:\n return False\n else:\n if n...
55
[ { "code": "class Solution:\n def search(self, nums: List[int], target: int) -> int:\n left, right = 0, len(nums) - 1\n while left <= right:\n middle = (left + right) // 2\n if nums[middle] == target:\n return middle\n if nums[middle] >= nums[left]...
10
34
find-first-and-last-position-of-element-in-sorted-array
<p>Given an array of integers <code>nums</code> sorted in non-decreasing order, find the starting and ending position of a given <code>target</code> value.</p> <p>If <code>target</code> is not found in the array, return <code>[-1, -1]</code>.</p> <p>You must&nbsp;write an algorithm with&nbsp;<code>O(log n)</code> run...
45.186553
Medium
[ "array", "binary-search" ]
{"lang": "python3", "distribution": [["22", 0.0047], ["26", 0.0047], ["27", 0.0094], ["30", 0.0094], ["31", 0.0047], ["33", 0.0094], ["34", 0.0094], ["35", 0.0047], ["38", 0.0094], ["40", 0.0047], ["42", 0.0094], ["46", 0.0047], ["50", 0.0047], ["51", 0.0047], ["52", 0.0047], ["53", 0.0142], ["54", 0.0378], ["55", 0.03...
{"lang": "python3", "distribution": [["17100", 0.0189], ["17200", 0.0047], ["17300", 0.0236], ["17400", 0.0236], ["17500", 0.0992], ["17600", 0.411], ["17700", 5.7206], ["17800", 50.0402]]}
[ { "code": "f = open(\"user.out\", 'w')\nfor nums, tar in zip(stdin, stdin):\n if nums[1] == ']':\n print(\"[-1,-1]\", file=f)\n continue\n a = nums[1:-2].split(',')\n tar = tar.rstrip()\n t = int(tar)\n i = bisect_left(a, True, key=lambda x: int(x) >= t)\n if i == len(a) or a[i] ...
71
[ { "code": "class Solution:\n def searchRange(self, nums: List[int], target: int) -> List[int]:\n try:\n s = nums.index(target)\n except:\n return [-1,-1]\n res = [s,s]\n while s < len(nums)-1:\n s+=1\n if nums[s]==target:\n ...
11
35
search-insert-position
<p>Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.</p> <p>You must&nbsp;write an algorithm with&nbsp;<code>O(log n)</code> runtime complexity.</p> <p>&nbsp;</p> <p><strong class="example">Ex...
47.13847
Easy
[ "array", "binary-search" ]
{"lang": "python3", "distribution": [["22", 0.0028], ["25", 0.0056], ["26", 0.0028], ["27", 0.0085], ["28", 0.0113], ["29", 0.0085], ["30", 0.0254], ["31", 0.0847], ["32", 0.0875], ["33", 0.1016], ["34", 0.1779], ["35", 0.4235], ["36", 0.4433], ["37", 0.5308], ["38", 0.6607], ["39", 1.4823], ["40", 1.6432], ["41", 1.82...
{"lang": "python3", "distribution": [["16700", 0.0028], ["16800", 0.0254], ["16900", 0.0593], ["17000", 0.4376], ["17100", 7.3548], ["17200", 55.4985], ["17300", 24.3485]]}
[ { "code": "class Solution:\n def searchInsert(self, nums: List[int], target: int) -> int:\n for i in range(len(nums)):\n if nums[i]==target or nums[i]>target:\n return i\n return len(nums)\n ", "runtime": "22" }, { "code": "class Solution:\n def s...
54
[ { "code": "class Solution:\n def searchInsert(self, nums: List[int], target: int) -> int:\n left = 0\n right = len(nums) - 1\n\n while left <= right:\n mid = (left + right) // 2\n\n if target == nums[mid]:\n return mid\n elif target < nums[...
11
36
valid-sudoku
<p>Determine if a&nbsp;<code>9 x 9</code> Sudoku board&nbsp;is valid.&nbsp;Only the filled cells need to be validated&nbsp;<strong>according to the following rules</strong>:</p> <ol> <li>Each row&nbsp;must contain the&nbsp;digits&nbsp;<code>1-9</code> without repetition.</li> <li>Each column must contain the digits&...
60.586957
Medium
[ "array", "hash-table", "matrix" ]
{"lang": "python3", "distribution": [["31", 0.0035], ["32", 0.0069], ["33", 0.0104], ["34", 0.0139], ["35", 0.0104], ["36", 0.0243], ["38", 0.0278], ["39", 0.0208], ["40", 0.0069], ["41", 0.0104], ["42", 0.0347], ["43", 0.0312], ["44", 0.0069], ["45", 0.0173], ["46", 0.0243], ["47", 0.0139], ["48", 0.0035], ["49", 0.01...
{"lang": "python3", "distribution": [["16000", 0.0173], ["16100", 0.0278], ["16200", 0.0867], ["16300", 1.0026], ["16400", 12.9085], ["16500", 34.0665], ["16600", 39.3464]]}
[ { "code": "def is_valid(cells):\n seen = set()\n for cell in cells:\n if cell == '.':\n continue\n if cell in seen:\n return False\n seen.add(cell)\n return True\nclass Solution:\n def isValidSudoku(board: List[List[str]]) -> bool:\n board_T = zip(*b...
86
[ { "code": "class Solution:\n def isValidSudoku(self, board: List[List[str]]) -> bool:\n #create 3 hashmaps\n #2 of which are for col and row where key is the col or row index and val is a set of values\n #1 of which is for the squares where key is the (r//3, c//3) and value is the set of...
11
37
sudoku-solver
<p>Write a program to solve a Sudoku puzzle by filling the empty cells.</p> <p>A sudoku solution must satisfy <strong>all of the following rules</strong>:</p> <ol> <li>Each of the digits <code>1-9</code> must occur exactly once in each row.</li> <li>Each of the digits <code>1-9</code> must occur exactly once in eac...
62.818348
Hard
[ "array", "hash-table", "backtracking", "matrix" ]
{"lang": "python3", "distribution": [["29", 0.184], ["35", 1.1541000000000001], ["40", 1.3715], ["46", 1.7896], ["51", 1.0203], ["57", 0.853], ["63", 0.6356], ["68", 1.4049], ["74", 1.405], ["79", 2.0239], ["85", 2.5257], ["90", 1.6726999999999999], ["96", 2.6593], ["101", 1.9904], ["107", 2.0740000000000003], ["113", ...
{"lang": "python3", "distribution": [["16000", 0.0167], ["16100", 0.0167], ["16200", 0.0502], ["16300", 0.4516], ["16400", 5.5862], ["16500", 17.4109], ["16600", 31.7946], ["16700", 22.4787], ["16800", 10.8379], ["16900", 5.3855]]}
[ { "code": "class Solution:\n# def solveSudoku(self, board: List[List[str]]) -> None:\n def solveSudoku(self, board):\n \"\"\"\n Do not return anything, modify board in-place instead.\n \"\"\"\n # store possible values\n sets = [set(range(1,10)) for _ in range(81)]\n\n ...
156
[ { "code": "class Solution:\n def solveSudoku(self, board: List[List[str]]) -> None:\n \"\"\"\n Do not return anything, modify board in-place instead.\n \"\"\"\n\n # Ideas...\n\n # 3 lists of sets, representing 3x3, row, col\n\n # first pass to populate board\n\n ...
12
38
count-and-say
<p>The <strong>count-and-say</strong> sequence is a sequence of digit strings defined by the recursive formula:</p> <ul> <li><code>countAndSay(1) = &quot;1&quot;</code></li> <li><code>countAndSay(n)</code> is the run-length encoding of <code>countAndSay(n - 1)</code>.</li> </ul> <p><a href="http://en.wikipedia.org/...
56.405608
Medium
[ "string" ]
{"lang": "python3", "distribution": [["18", 0.0124], ["22", 0.0372], ["23", 0.0868], ["24", 0.0372], ["25", 0.0868], ["26", 0.2108], ["27", 0.2604], ["28", 0.3349], ["29", 0.3225], ["30", 0.7441], ["31", 1.017], ["32", 1.017], ["33", 1.141], ["34", 2.0216], ["35", 2.9145], ["36", 2.8277], ["37", 3.1874], ["38", 4.316],...
{"lang": "python3", "distribution": [["16200", 0.0124], ["16300", 0.3969], ["16400", 7.6274], ["16500", 21.1832], ["16600", 38.3728], ["16700", 15.9618], ["16800", 7.9871], ["16900", 3.0386]]}
[ { "code": "import gc\ngc.disable()\n\nfrom os import _exit\nfrom sys import stdin \nimport pickle, base64, zlib\n\nd = pickle.loads(zlib.decompress(base64.a85decode( b'GQI294c^j[&`\\'!>(3BTa0X.cN(g(I`k<;$cA)^nc,;=,Q6c)7Tbeh35`9%;5nO%OC&Xeb]=k2?7Z_uijBX^&R1ZHB#0-Y1TDoH(tp\\\\-oNk*>r?kN/%;^HO5F+5NRWQ`jbgQ`#o5:Kfr...
53
[ { "code": "class Solution:\n def countAndSay(self, n: int) -> str: \n if n == 1:\n return \"1\"\n cur_sequence = \"1\"\n for _ in range(2, n + 1):\n next_sequence = \"\"\n i = 0\n while i < len(cur_sequence):\n count = 1\n ...
10
39
combination-sum
<p>Given an array of <strong>distinct</strong> integers <code>candidates</code> and a target integer <code>target</code>, return <em>a list of all <strong>unique combinations</strong> of </em><code>candidates</code><em> where the chosen numbers sum to </em><code>target</code><em>.</em> You may return the combinations i...
72.714127
Medium
[ "array", "backtracking" ]
{"lang": "python3", "distribution": [["25", 0.0395], ["29", 0.14889999999999998], ["32", 0.7054], ["35", 1.5293999999999999], ["39", 3.1196], ["42", 9.0764], ["45", 11.3263], ["49", 14.1814], ["52", 19.4722], ["55", 11.8888], ["59", 7.857000000000001], ["62", 5.324199999999999], ["65", 1.8092], ["69", 1.1251], ["72", 0...
{"lang": "python3", "distribution": [["16100", 0.0091], ["16200", 0.0213], ["16300", 0.1764], ["16400", 2.5602], ["16500", 19.9678], ["16600", 33.7357], ["16700", 33.085]]}
[ { "code": "class Solution:\n def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:\n ans, path = [], []\n def backtrack(total, start_index):\n if total > target:\n return\n \n if total == target:\n ans.app...
153
[ { "code": "class Solution:\n def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:\n result = []\n subset = []\n def _combinationSum(i):\n if sum(subset) > target or i >= len(candidates):\n return\n if sum(subset) == target:...
12
40
combination-sum-ii
<p>Given a collection of candidate numbers (<code>candidates</code>) and a target number (<code>target</code>), find all unique combinations in <code>candidates</code>&nbsp;where the candidate numbers sum to <code>target</code>.</p> <p>Each number in <code>candidates</code>&nbsp;may only be used <strong>once</strong> ...
56.430949
Medium
[ "array", "backtracking" ]
{"lang": "python3", "distribution": [["21", 0.013400000000000002], ["22", 0.0053], ["23", 0.0027], ["24", 0.0321], ["25", 0.0267], ["26", 0.0534], ["27", 0.0641], ["28", 0.1363], ["29", 0.1683], ["30", 0.1576], ["31", 0.2351], ["32", 0.4355], ["33", 0.4729], ["34", 0.5664], ["35", 0.7855], ["36", 1.1996], ["37", 1.0954...
{"lang": "python3", "distribution": [["15900", 0.0053], ["16100", 0.0027], ["16200", 0.0347], ["16300", 0.5263], ["16400", 5.8429], ["16500", 22.8881], ["16600", 36.42], ["16700", 25.9898]]}
[ { "code": "class Solution:\n def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:\n candidates.sort()\n results = []\n\n def backtracking(index, path, remainingSum):\n if remainingSum == 0:\n results.append(list(path))\n ...
122
[ { "code": "class Solution(object):\n def combinationSum2(self, candidates, target):\n ans = []\n ds = []\n candidates.sort()\n\n\n def findCombination(ind, target):\n if target == 0:\n ans.append(ds[:])\n return\n for i in range(...
11
191
number-of-1-bits
<p>Write a function that takes the binary representation of a positive integer and returns the number of <span data-keyword="set-bit">set bits</span> it has (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p> <p>&nbsp;</p> <p><strong class="example">Example...
72.423365
Easy
[ "divide-and-conquer", "bit-manipulation" ]
{"lang": "python3", "distribution": [["12", 0.0062], ["14", 0.0062], ["15", 0.0437], ["16", 0.0062], ["17", 0.0062], ["18", 0.0375], ["19", 0.2187], ["20", 0.1437], ["21", 0.1187], ["22", 0.2374], ["23", 1.0622], ["24", 0.4936], ["25", 0.6186], ["26", 1.2246], ["27", 3.3677], ["28", 1.8807], ["29", 1.8994], ["30", 3.96...
{"lang": "python3", "distribution": [["15900", 0.0062], ["16000", 0.0187], ["16100", 0.0062], ["16200", 0.4873], ["16300", 6.1981], ["16400", 25.5545], ["16500", 43.349]]}
[ { "code": "class Solution:\n def hammingWeight(self, n: int) -> int:\n n_bin = str(bin(n))\n count = 0\n for i in n_bin:\n if i == \"1\":\n count += 1\n return count\n", "runtime": "12" }, { "code": "class Solution:\n def hammingWeight(self...
51
[ { "code": "class Solution:\n def hammingWeight(self, n: int) -> int:\n binary_rep = bin(n)[2:]\n ones_count = binary_rep.count('1')\n return ones_count\n ", "memory": "15900" }, { "code": "class Solution:\n def hammingWeight(self, n: int) -> int:\n ans = []\n...
11
198
house-robber
<p>You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and <b>it will automatically contact the police if two adjacent houses were broken ...
51.487546
Medium
[ "array", "dynamic-programming" ]
{"lang": "python3", "distribution": [["12", 0.0028], ["14", 0.0113], ["15", 0.0169], ["16", 0.0225], ["17", 0.031], ["18", 0.0281], ["19", 0.1717], ["20", 0.0535], ["21", 0.197], ["22", 0.3096], ["23", 0.774], ["24", 0.3828], ["25", 0.667], ["26", 1.6859], ["27", 2.4007], ["28", 1.4579], ["29", 2.1868], ["30", 4.8662],...
{"lang": "python3", "distribution": [["15900", 0.0056], ["16000", 0.0028], ["16100", 0.0394], ["16200", 0.4278], ["16300", 6.6618], ["16400", 24.1367], ["16500", 42.864], ["16600", 20.3006]]}
[ { "code": "class Solution:\n def rob(self, nums: List[int]) -> int:\n\n def house(i,nums, dp):\n if i == 0:\n return nums[0]\n if i == 1:\n return max(nums[0], nums[1])\n if dp[i] != -1:\n return dp[i] \n\n ...
52
[ { "code": "class Solution:\n def rob(self, nums: List[int]) -> int:\n if len(nums) <=2 :\n return max(nums)\n dp = [0] * len(nums)\n dp[0] = nums[0]\n dp[1] = max(nums[0], nums[1])\n \n\n for i in range(2, len(nums)):\n dp[i] = max(dp[i-1], nums...
13
199
binary-tree-right-side-view
<p>Given the <code>root</code> of a binary tree, imagine yourself standing on the <strong>right side</strong> of it, return <em>the values of the nodes you can see ordered from top to bottom</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/...
63.845867
Medium
[ "tree", "depth-first-search", "breadth-first-search", "binary-tree" ]
{"lang": "python3", "distribution": [["15", 0.0148], ["16", 0.0037], ["17", 0.0111], ["18", 0.0296], ["19", 0.148], ["20", 0.0814], ["21", 0.1073], ["22", 0.1812], ["23", 0.7805], ["24", 0.4254], ["25", 0.4513], ["26", 0.8581], ["27", 2.7187], ["28", 1.5609], ["29", 1.6312], ["30", 2.6891], ["31", 6.732], ["32", 4.2463...
{"lang": "python3", "distribution": [["15900", 0.0037], ["16000", 0.0148], ["16100", 0.0148], ["16200", 0.2293], ["16300", 4.287], ["16400", 24.1724], ["16500", 43.292]]}
[ { "code": "# 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 rightSideView(self, root: Optional[TreeNode]) -> List[int]:\n d=default...
48
[ { "code": "# 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 rightSideView(self, root: Optional[TreeNode]) -> List[int]:\n if not ro...
11
200
number-of-islands
<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p> <p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or ve...
60.394216
Medium
[ "array", "depth-first-search", "breadth-first-search", "union-find", "matrix" ]
{"lang": "python3", "distribution": [["49", 0.013600000000000001], ["53", 0.0226], ["56", 0.0182], ["60", 0.022699999999999998], ["63", 0.0113], ["67", 0.0045], ["70", 0.0045], ["74", 0.0023], ["78", 0.0023], ["81", 0.0046], ["85", 0.0045], ["88", 0.0046], ["92", 0.0046], ["95", 0.0023], ["99", 0.0046], ["102", 0.0023]...
{"lang": "python3", "distribution": [["17366", 0.07250000000000001], ["17498", 0.0068], ["17631", 0.0023], ["17763", 0.0114], ["17896", 0.0295], ["18028", 0.0091], ["18161", 0.027200000000000002], ["18293", 0.0113], ["18426", 0.0476], ["18558", 4.0600000000000005], ["18691", 12.7944], ["18823", 22.0887], ["18956", 14.0...
[ { "code": "def numIslands(grid: List[List[str]]) -> int:\n\n\n row, col = len(grid), len(grid[0])\n res = 0\n def dfs(r,c,grid): \n grid[r][c] = \"0\"\n if r < row -1 and grid[r+1][c] == \"1\":\n dfs(r+1,c,grid)\n if r > 0 and grid[r-1][c] == \"1\":\n ...
128
[ { "code": "def numIslands(grid: List[List[str]]) -> int:\n\n\n row, col = len(grid), len(grid[0])\n res = 0\n def dfs(r,c,grid): \n grid[r][c] = \"0\"\n if r < row -1 and grid[r+1][c] == \"1\":\n dfs(r+1,c,grid)\n if r > 0 and grid[r-1][c] == \"1\":\n ...
121
201
bitwise-and-of-numbers-range
<p>Given two integers <code>left</code> and <code>right</code> that represent the range <code>[left, right]</code>, return <em>the bitwise AND of all numbers in this range, inclusive</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> left = 5, right = 7 <strong>Ou...
47.22214
Medium
[ "bit-manipulation" ]
{"lang": "python3", "distribution": [["27", 0.0794], ["28", 0.0794], ["30", 0.2383], ["31", 0.2383], ["32", 0.2383], ["33", 0.2383], ["34", 0.1986], ["35", 0.5957], ["36", 0.6751], ["37", 0.556], ["38", 1.4297], ["39", 1.4297], ["40", 1.5488], ["41", 1.5091], ["42", 3.058], ["43", 2.5814], ["44", 2.8991], ["45", 3.614]...
{"lang": "python3", "distribution": [["16200", 0.1986], ["16300", 0.556], ["16400", 9.6902], ["16500", 39.5949], ["16600", 41.2629]]}
[ { "code": "class Solution:\n def rangeBitwiseAnd(self, left: int, right: int) -> int:\n shift = 0\n while left < right:\n left >>= 1\n right >>= 1\n shift += 1\n \n return left << shift", "runtime": "27" }, { "code": "class Solution:\n ...
56
[ { "code": "class Solution:\n def rangeBitwiseAnd(self, left: int, right: int) -> int:\n cnt = 0\n while left != right:\n left >>= 1\n right >>= 1\n cnt += 1\n return left << cnt", "memory": "16200" }, { "code": "class Solution:\n def rangeB...
8
202
happy-number
<p>Write an algorithm to determine if a number <code>n</code> is happy.</p> <p>A <strong>happy number</strong> is a number defined by the following process:</p> <ul> <li>Starting with any positive integer, replace the number by the sum of the squares of its digits.</li> <li>Repeat the process until the number equal...
56.800603
Easy
[ "hash-table", "math", "two-pointers" ]
{"lang": "python3", "distribution": [["14", 0.0047], ["15", 0.0047], ["16", 0.0093], ["18", 0.028], ["19", 0.0981], ["20", 0.0981], ["21", 0.0701], ["22", 0.1775], ["23", 0.6306], ["24", 0.5465], ["25", 0.4251], ["26", 0.9623], ["27", 1.8311], ["28", 1.8825], ["29", 1.747], ["30", 2.6439], ["31", 4.7038], ["32", 4.3442...
{"lang": "python3", "distribution": [["15300", 0.0047], ["16000", 0.0093], ["16100", 0.028], ["16200", 0.3317], ["16300", 4.9748], ["16400", 22.1973], ["16500", 40.8772], ["16600", 25.7754]]}
[ { "code": "class Solution:\n def getSquare(self, n):\n currentSum = 0\n while (n != 0):\n remain = n % 10\n currentSum += remain * remain\n n = n // 10\n return currentSum\n\n def isHappy(self, n: int) -> bool:\n slow = self.getSquare(n)\n ...
53
[ { "code": "class Solution:\n def isHappy(self, n: int) -> bool:\n hset = set()\n while n != 1:\n if n in hset: return False\n hset.add(n)\n n = sum([int(i) ** 2 for i in str(n)])\n else:\n return True", "memory": "15300" }, { "code"...
8
203
remove-linked-list-elements
<p>Given the <code>head</code> of a linked list and an integer <code>val</code>, remove all the nodes of the linked list that has <code>Node.val == val</code>, and return <em>the new head</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/202...
49.903257
Easy
[ "linked-list", "recursion" ]
{"lang": "python3", "distribution": [["19", 0.0113], ["20", 0.0113], ["22", 0.0113], ["24", 0.0113], ["26", 0.0226], ["27", 0.0113], ["29", 0.0226], ["30", 0.0792], ["31", 0.1697], ["32", 0.2036], ["33", 0.2262], ["34", 0.2828], ["35", 0.6108], ["36", 0.6221], ["37", 0.7805], ["38", 0.8483], ["39", 1.7645], ["40", 2.23...
{"lang": "python3", "distribution": [["16700", 0.0113], ["16800", 0.0339], ["18300", 0.0113], ["18400", 0.2036], ["18500", 1.2555], ["18600", 3.6195], ["18700", 0.6334], ["18800", 0.0679], ["18900", 0.0679], ["19000", 0.0679], ["19100", 0.1018], ["19200", 0.362], ["19300", 7.6688], ["19400", 47.3476], ["19500", 29.3406...
[ { "code": "from os import _exit\nfrom sys import stdin\n\nwith open('user.out', 'w') as f:\n for arr, val in zip(sys.stdin, sys.stdin):\n val = val[:-1] if val[-1] == '\\n' else val\n if arr == f'[{val}]\\n':\n f.write('[]\\n')\n continue\n start = arr.startswith(f\...
57
[ { "code": "from os import _exit\nfrom sys import stdin\n\nwith open('user.out', 'w') as f:\n for arr, val in zip(sys.stdin, sys.stdin):\n val = val[:-1] if val[-1] == '\\n' else val\n if arr == f'[{val}]\\n':\n f.write('[]\\n')\n continue\n start = arr.startswith(f\...
32
204
count-primes
<p>Given an integer <code>n</code>, return <em>the number of prime numbers that are strictly less than</em> <code>n</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 10 <strong>Output:</strong> 4 <strong>Explanation:</strong> There are 4 prime numbers less ...
33.980419
Medium
[ "array", "math", "enumeration", "number-theory" ]
{"lang": "python3", "distribution": [["63", 0.1602], ["134", 0.0178], ["204", 0.35600000000000004], ["275", 0.17800000000000002], ["346", 0.0534], ["416", 0.8363], ["487", 0.44499999999999995], ["558", 1.1387], ["628", 0.6229000000000002], ["699", 0.10679999999999999], ["770", 0.0534], ["841", 0.1602], ["911", 0.0534],...
{"lang": "python3", "distribution": [["17243", 0.17790000000000003], ["18531", 0.0356], ["19818", 0.0178], ["21106", 0.0178], ["22393", 0.0711], ["23681", 0.0534], ["24968", 0.0356], ["26256", 0.0178], ["27543", 0.0178], ["28831", 0.0178], ["30118", 0.0356], ["31406", 0.0711], ["32693", 0.0534], ["33981", 0.1246], ["35...
[ { "code": "class Solution:\n def countPrimes(self, n: int) -> int:\n if (n < 3):\n return 0\n elif (n == 3):\n return 1\n elif (n <= 5):\n return 2\n elif (n <= 7):\n return 3\n elif (n <= 11):\n return 4\n elif ...
149
[ { "code": "class Solution:\n def countPrimes(self, n: int) -> int:\n if (n < 3):\n return 0\n elif (n == 3):\n return 1\n elif (n <= 5):\n return 2\n elif (n <= 7):\n return 3\n elif (n <= 11):\n return 4\n elif ...
109
205
isomorphic-strings
<p>Given two strings <code>s</code> and <code>t</code>, <em>determine if they are isomorphic</em>.</p> <p>Two strings <code>s</code> and <code>t</code> are isomorphic if the characters in <code>s</code> can be replaced to get <code>t</code>.</p> <p>All occurrences of a character must be replaced with another characte...
45.71934
Easy
[ "hash-table", "string" ]
{"lang": "python3", "distribution": [["18", 0.0054], ["19", 0.0161], ["20", 0.0215], ["21", 0.0107], ["22", 0.0322], ["23", 0.0644], ["24", 0.0751], ["25", 0.0805], ["26", 0.1771], ["27", 0.2952], ["28", 0.3864], ["29", 0.5045], ["30", 0.9124], ["31", 1.2666], ["32", 1.229], ["33", 1.5403], ["34", 2.2648], ["35", 3.064...
{"lang": "python3", "distribution": [["16200", 0.0054], ["16300", 0.0215], ["16400", 0.0215], ["16500", 0.4937], ["16600", 9.6066], ["16700", 55.3641], ["16800", 13.0145], ["16900", 2.2755], ["17000", 1.2236], ["17100", 1.8033], ["17200", 7.4975], ["17300", 2.5653], ["17400", 0.3059], ["17500", 0.1932], ["17600", 0.112...
[ { "code": "class Solution:\n def isIsomorphic(self, s: str, t: str) -> bool:\n \n if len(s) != len(t):\n return False\n\n s_to_t = {}\n t_to_s = {}\n\n for s_ch, t_ch in zip(s, t):\n if s_ch in s_to_t:\n if s_to_t[s_ch] != t_ch:\n ...
70
[ { "code": "class Solution:\n def isIsomorphic(self, s: str, t: str) -> bool:\n if len(s) != len(t):\n return False\n \n map_s_to_t = {}\n map_t_to_s = {}\n \n for char_s, char_t in zip(s, t):\n if char_s in map_s_to_t:\n if map_s_...
29
206
reverse-linked-list
<p>Given the <code>head</code> of a singly linked list, reverse the list, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg" style="width: 542px; height: 222px;" /> <pre> <strong>Input:...
77.628634
Easy
[ "linked-list", "recursion" ]
{"lang": "python3", "distribution": [["13", 0.002], ["14", 0.0039], ["15", 0.0039], ["16", 0.0313], ["17", 0.0059], ["18", 0.0293], ["19", 0.0782], ["20", 0.1075], ["21", 0.0547], ["22", 0.1895], ["23", 0.3127], ["24", 0.5843], ["25", 0.4436], ["26", 0.9145], ["27", 1.3444], ["28", 2.1026], ["29", 1.7001], ["30", 2.339...
{"lang": "python3", "distribution": [["16900", 0.0039], ["17000", 0.002], ["17100", 0.002], ["17200", 0.0039], ["17300", 0.0234], ["17400", 0.0899], ["17500", 1.0924], ["17600", 14.1107], ["17700", 58.3656], ["17800", 20.7547], ["17900", 0.5276]]}
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:\n if head == None or head.next == None:\n ...
54
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:\n # prev = None //iteratively\n # while...
16
207
course-schedule
<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<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i<...
47.492156
Medium
[ "depth-first-search", "breadth-first-search", "graph", "topological-sort" ]
{"lang": "python3", "distribution": [["32", 0.0068], ["34", 0.0034], ["37", 0.0068], ["39", 0.0034], ["41", 0.0034], ["44", 0.0068], ["46", 0.0068], ["49", 0.0068], ["51", 0.0102], ["54", 0.0239], ["56", 0.0239], ["58", 0.0512], ["61", 0.041], ["63", 0.0786], ["66", 0.1572], ["68", 0.2665], ["71", 0.3314], ["73", 0.451...
{"lang": "python3", "distribution": [["17300", 0.0034], ["17500", 0.0034], ["17600", 0.0102], ["17700", 0.0034], ["17800", 0.0239], ["17900", 0.123], ["18000", 0.5022], ["18100", 4.5403], ["18200", 13.5937], ["18300", 27.9389], ["18400", 20.8466], ["18500", 12.497], ["18600", 7.5672], ["18700", 4.1884], ["18800", 2.524...
[ { "code": "class Solution:\n def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:\n track = [[] for _ in range(numCourses)]\n for a, b in prerequisites:\n track[a].append(b)\n \n visited = set()\n def dfs(i):\n if i in visited:...
137
[ { "code": "from collections import defaultdict, deque\nclass Solution:\n def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:\n adj_list = defaultdict(list)\n in_degree = [0] * numCourses\n \n for dest, src in prerequisites:\n adj_list[src].appe...
25
208
implement-trie-prefix-tree
<p>A <a href="https://en.wikipedia.org/wiki/Trie" target="_blank"><strong>trie</strong></a> (pronounced as &quot;try&quot;) or <strong>prefix tree</strong> is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocom...
66.279971
Medium
[ "hash-table", "string", "design", "trie" ]
{"lang": "python3", "distribution": [["83", 0.07669999999999999], ["85", 0.0768], ["87", 0.2127], ["88", 0.1182], ["90", 0.24810000000000001], ["92", 0.5257000000000001], ["94", 0.3781], ["95", 1.0161], ["97", 1.1579], ["99", 0.8153], ["100", 1.5065], ["102", 1.9496000000000002], ["104", 2.0499], ["105", 0.9925], ["107...
{"lang": "python3", "distribution": [["22772", 1.3586999999999998], ["22917", 0.3663], ["23062", 0.3722], ["23207", 0.4372], ["23352", 1.2465], ["23497", 0.1713], ["23642", 0.1595], ["23787", 0.0059], ["23932", 0.0059], ["24077", 0.0118], ["24222", 0.0118], ["24367", 0.0354], ["24512", 0.0295], ["24657", 0.0354], ["248...
[ { "code": "class Trie:\n\n def __init__(self):\n self.trie = {}\n\n def insert(self, word: str) -> None:\n d = self.trie\n for c in word:\n if c not in d:\n d[c] = {}\n d=d[c]\n d['.'] = '.'\n\n def search(self, word: str) -> bool:\n ...
139
[ { "code": "class Trie:\n\n def __init__(self):\n self.trie = []\n\n def insert(self, word: str) -> None:\n self.trie.append(word)\n\n def search(self, word: str) -> bool:\n if(word in self.trie):\n return True\n \n return False\n\n def startsWith(self, p...
118
209
minimum-size-subarray-sum
<p>Given an array of positive integers <code>nums</code> and a positive integer <code>target</code>, return <em>the <strong>minimal length</strong> of a </em><span data-keyword="subarray-nonempty"><em>subarray</em></span><em> whose sum is greater than or equal to</em> <code>target</code>. If there is no such subarray, ...
47.848605
Medium
[ "array", "binary-search", "sliding-window", "prefix-sum" ]
{"lang": "python3", "distribution": [["40", 0.018], ["42", 0.0045], ["45", 0.009], ["47", 0.018], ["49", 0.0045], ["52", 0.0181], ["54", 0.0045], ["57", 0.009], ["59", 0.0136], ["61", 0.0045], ["64", 0.0045], ["66", 0.0045], ["69", 0.0045], ["71", 0.009], ["73", 0.0045], ["76", 0.0181], ["78", 0.0316], ["81", 0.0181], ...
{"lang": "python3", "distribution": [["21200", 0.0045], ["21300", 0.0542], ["21400", 0.009], ["25100", 0.0045], ["25200", 0.0271], ["25300", 0.009], ["27300", 0.0045], ["28200", 0.0045], ["28800", 0.009], ["29000", 0.009], ["29100", 0.0136], ["29200", 0.0045], ["29300", 0.0136], ["29400", 0.0271], ["29500", 0.1039], ["...
[ { "code": "def minSubArrayLen(target: int, nums: List[int]) -> int:\n i = 0\n total = 0\n min_length = float('inf')\n \n for j in range(len(nums)):\n total += nums[j]\n \n while total >= target:\n min_length = min(min_length, j - i + 1)\n total -...
122
[ { "code": "class Solution:\n def minSubArrayLen(self, target: int, nums: List[int]) -> int:\n left = 0\n ans, curr_sum = float('inf'), 0\n for right in range(len(nums)):\n curr_sum += nums[right]\n while curr_sum >= target:\n ans = min(ans, right - le...
30
210
course-schedule-ii
<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<sub>i</sub>]</code> indicates that you <strong>must</strong> take course <code>b<sub>i<...
51.479268
Medium
[ "depth-first-search", "breadth-first-search", "graph", "topological-sort" ]
{"lang": "python3", "distribution": [["27", 0.01], ["29", 0.005], ["30", 0.005], ["31", 0.005], ["33", 0.005], ["34", 0.005], ["36", 0.0101], ["37", 0.005], ["39", 0.0101], ["40", 0.0101], ["41", 0.005], ["43", 0.0101], ["44", 0.005], ["46", 0.005], ["47", 0.005], ["49", 0.0101], ["50", 0.0101], ["51", 0.0101], ["53", ...
{"lang": "python3", "distribution": [["17100", 0.005], ["17200", 0.0151], ["17300", 0.0201], ["17400", 0.005], ["17500", 0.0252], ["17600", 0.0151], ["17700", 0.0302], ["17800", 0.151], ["17900", 1.208], ["18000", 2.4612], ["18100", 5.5365], ["18200", 10.0866], ["18300", 18.2102], ["18400", 18.2354], ["18500", 15.6684]...
[ { "code": "class Solution:\n def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:\n from collections import deque\n q = deque()\n indegree = [0]*numCourses\n G = [[] for i in range(numCourses)]\n order = []\n for v,u in prerequisites:\n ...
121
[ { "code": "class Solution:\n def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:\n # b -> a\n\n # construct graph as adjacency list\n adjList = {}\n for i in range(numCourses):\n adjList[i] = []\n\n # find indegree of all nodes\n ...
25
211
design-add-and-search-words-data-structure
<p>Design a data structure that supports adding new words and finding if a string matches any previously added string.</p> <p>Implement the <code>WordDictionary</code> class:</p> <ul> <li><code>WordDictionary()</code>&nbsp;Initializes the object.</li> <li><code>void addWord(word)</code> Adds <code>word</code> to th...
45.889436
Medium
[ "string", "depth-first-search", "design", "trie" ]
{"lang": "python3", "distribution": [["774", 0.042], ["809", 0.0105], ["843", 0.0315], ["878", 0.126], ["913", 0.33620000000000005], ["947", 0.5042000000000002], ["982", 1.1887999999999999], ["1017", 1.4204999999999999], ["1052", 1.1152999999999995], ["1086", 1.3994000000000002], ["1121", 2.1470000000000007], ["1156", ...
{"lang": "python3", "distribution": [["22541", 0.1051], ["23225", 0.7786], ["23909", 1.3468], ["24593", 1.0101], ["25276", 0.19990000000000002], ["25960", 0.0315], ["26644", 0.021], ["27328", 0.0105], ["28011", 0.0105], ["28695", 0.0105], ["29379", 0.021], ["30063", 0.0105], ["30746", 0.0105], ["31430", 0.021], ["32114...
[ { "code": "class WordDictionary:\n\n def __init__(self):\n self.dict = set()\n self.len_dict = {}\n self.search_cache = {}\n \n def addWord(self, word: str) -> None:\n # print(f\"{self.dict=}\")\n # print(f\"{self.len_dict=}\")\n # print(f\"{self.search_cache=}...
147
[ { "code": "class WordDictionary:\n\n def __init__(self):\n self.word_dict = collections.defaultdict(set)\n\n def addWord(self, word: str) -> None:\n self.word_dict[len(word)].add(word) \n\n def search(self, search_word: str) -> bool:\n if \".\" not in search_word:\n ...
125
212
word-search-ii
<p>Given an <code>m x n</code> <code>board</code>&nbsp;of characters and a list of strings <code>words</code>, return <em>all words on the board</em>.</p> <p>Each word must be constructed from letters of sequentially adjacent cells, where <strong>adjacent cells</strong> are horizontally or vertically neighboring. The ...
36.584051
Hard
[ "array", "string", "backtracking", "trie", "matrix" ]
{"lang": "python3", "distribution": [["94", 0.47020000000000006], ["206", 0.0655], ["317", 0.6118000000000001], ["428", 6.589400000000001], ["540", 3.6769], ["651", 3.501199999999999], ["762", 2.318899999999999], ["874", 1.6394999999999988], ["985", 1.015900000000001], ["1096", 0.7541000000000003], ["1208", 5.702299999...
{"lang": "python3", "distribution": [["16300", 0.0109], ["16400", 0.0109], ["16500", 0.0547], ["16600", 0.0438], ["16700", 0.0876], ["16800", 0.208], ["16900", 0.2627], ["17000", 0.3503], ["17100", 0.2846], ["17200", 0.2299], ["17300", 0.1642], ["17400", 0.0985], ["17500", 0.2737], ["17600", 0.416], ["17700", 2.6929], ...
[ { "code": "class Trie:\n def __init__(self, words=[]):\n self.root = {\"length\": 0}\n for word in words:\n self.insert(word)\n \n def insert(self, word):\n current = self.root\n for c in word:\n if c not in current:\n current[c] ...
155
[ { "code": "class TrieNode:\n\n def __init__(self, char):\n self.char = char\n self.children = dict() \n self.word = ''\n\nclass Trie:\n\n def __init__(self):\n self.root = TrieNode('')\n \n def add_word(self, word):\n curr = self.root\n for c in word:\n ...
61
213
house-robber-ii
<p>You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are <strong>arranged in a circle.</strong> That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and&nbsp;...
42.537251
Medium
[ "array", "dynamic-programming" ]
{"lang": "python3", "distribution": [["11", 0.0079], ["12", 0.0079], ["13", 0.0157], ["15", 0.0157], ["16", 0.0079], ["17", 0.0157], ["18", 0.0393], ["19", 0.1572], ["20", 0.1179], ["21", 0.1022], ["22", 0.228], ["23", 0.6683], ["24", 0.3381], ["25", 0.5739], ["26", 1.2658], ["27", 2.7439], ["28", 1.3759], ["29", 1.997...
{"lang": "python3", "distribution": [["15200", 0.0079], ["15900", 0.0079], ["16000", 0.0157], ["16100", 0.0236], ["16200", 0.283], ["16300", 4.395], ["16400", 22.714], ["16500", 40.294], ["16600", 25.4658]]}
[ { "code": "class Solution:\n def rob(self, nums: List[int]) -> int:\n \n n = len(nums)\n\n def dp(ns: List[int]) -> int:\n h1 = h2 = 0\n for n in ns:\n current = h1\n h1 = max(h1, n + h2)\n h2 = current\n \...
52
[ { "code": "class Solution:\n def rob(self, nums: List[int]) -> int:\n n = len(nums)\n if n == 1:\n return nums[0]\n dp = [-1] * (n)\n\n def recur(i, n):\n if i >= n:\n return 0\n elif dp[i] != -1:\n return dp[i]\n ...
14
214
shortest-palindrome
<p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p> <p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p> <p>&nbsp;</p> <p><strong class="example">Exampl...
34.064427
Hard
[ "string", "rolling-hash", "string-matching", "hash-function" ]
{"lang": "python3", "distribution": [["51", 48.5302], ["89", 9.616700000000003], ["127", 1.6023999999999998], ["166", 1.1574], ["204", 0.44509999999999994], ["242", 0.9791999999999998], ["280", 0.089], ["319", 0.2671], ["357", 0.089], ["395", 7.1235], ["434", 4.184800000000001], ["472", 0.6229999999999999], ["510", 1.7...
{"lang": "python3", "distribution": [["16500", 0.3562], ["16600", 1.1576], ["16700", 6.2333], ["16800", 6.5895], ["16900", 21.3713], ["17000", 23.4194], ["17100", 5.7881], ["17200", 2.2262], ["17300", 1.1576], ["17400", 0.3562], ["17500", 0.1781], ["17600", 0.5343], ["17800", 0.8905], ["17900", 0.5343], ["18100", 0.089...
[ { "code": "class Solution:\n def shortestPalindrome(self, s: str) -> str:\n i = 0\n n = len(s)\n for j in range(n):\n if s[i] == s[n-j-1]:\n i += 1\n if i==n:\n return s\n p = s[i:n][::-1]\n return p + self.shortestPalindrome(s[:i...
93
[ { "code": "class Solution:\n def shortestPalindrome(self, s: str) -> str:\n # if not s: return s\n # pre = ''\n # l, r = 0, len(s) - 1\n # while l <= r:\n # if s[l] != s[r]:\n # pre += s[r]\n # else:\n # l += 1\n # r -...
43
215
kth-largest-element-in-an-array
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>largest element in the array</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> largest element in the sorted order, not the <code>k<sup>th</sup></code> distinct element.</p> <p>Can...
67.114292
Medium
[ "array", "divide-and-conquer", "sorting", "heap-priority-queue", "quickselect" ]
{"lang": "python3", "distribution": [["87", 0.0232], ["95", 0.014499999999999999], ["103", 0.0058], ["111", 0.0029], ["119", 0.0087], ["127", 0.0029], ["135", 0.0029], ["143", 0.0029], ["151", 0.0058], ["159", 0.0029], ["167", 0.0029], ["175", 0.0029], ["183", 0.0029], ["191", 0.0029], ["199", 0.0029], ["207", 0.0029],...
{"lang": "python3", "distribution": [["23562", 0.0087], ["23687", 0.0116], ["23812", 0.0116], ["23937", 0.0116], ["24062", 0.0058], ["24187", 0.0058], ["24312", 0.0029], ["24437", 0.0145], ["24562", 0.0087], ["24687", 0.0029], ["24812", 0.0087], ["24937", 0.0204], ["25062", 0.0611], ["25187", 0.0262], ["25312", 0.0175]...
[ { "code": "def findKthLargest(nums: List[int], k: int) -> int:\n def findWhere(nums, k) -> Tuple:\n great = []\n e = 0\n less = []\n n = len(nums)\n if n < 3:\n pivot = random.choice(nums)\n else:\n mid = n // 2\n medianlist = sorted(...
124
[ { "code": "from heapq import _heapify_max, _heappop_max\ndef findKthLargest(nums: List[int], k: int) -> int:\n _heapify_max(nums)\n for _ in range(k-1):\n _heappop_max(nums)\n return _heappop_max(nums)\n\nwith open('user.out', 'w') as f:\n inputs = map(loads, stdin)\n for nums in inputs:\n...
113
216
combination-sum-iii
<p>Find all valid combinations of <code>k</code> numbers that sum up to <code>n</code> such that the following conditions are true:</p> <ul> <li>Only numbers <code>1</code> through <code>9</code> are used.</li> <li>Each number is used <strong>at most once</strong>.</li> </ul> <p>Return <em>a list of all possible va...
70.54601
Medium
[ "array", "backtracking" ]
{"lang": "python3", "distribution": [["10", 0.0139], ["15", 0.0277], ["18", 0.0555], ["19", 0.2497], ["20", 0.0555], ["21", 0.1387], ["22", 0.444], ["23", 0.6521], ["24", 0.4578], ["25", 0.9295], ["26", 1.8452], ["27", 2.2059], ["28", 1.54], ["29", 2.2336], ["30", 5.5633], ["31", 5.1193], ["32", 3.3158], ["33", 4.4811]...
{"lang": "python3", "distribution": [["15900", 0.0139], ["16100", 0.0416], ["16200", 0.1387], ["16300", 3.1909], ["16400", 22.0172], ["16500", 41.343]]}
[ { "code": "class Solution:\n def pick(self, number: int, k: int, n: int, used: Set[int], results: List[int], sum: int):\n used.add(number)\n sum += number\n if sum == n and len(used) == k:\n results.append(list(used))\n elif len(used) < k:\n for next_num in ...
51
[ { "code": "class Solution:\n def combinationSum3(self, k: int, n: int) -> List[List[int]]:\n # def backtrack(start, combination, total):\n # if len(combination) == k and total == n:\n # result.append(combination[:])\n # return\n # for i in range(star...
10
91
decode-ways
<p>You have intercepted a secret message encoded as a string of numbers. The message is <strong>decoded</strong> via the following mapping:</p> <p><code>&quot;1&quot; -&gt; &#39;A&#39;<br /> &quot;2&quot; -&gt; &#39;B&#39;<br /> ...<br /> &quot;25&quot; -&gt; &#39;Y&#39;<br /> &quot;26&quot; -&gt; &#39;Z&#39;</code></...
35.452494
Medium
[ "string", "dynamic-programming" ]
{"lang": "python3", "distribution": [["7", 0.0073], ["11", 0.0073], ["12", 0.0073], ["16", 0.0073], ["17", 0.0147], ["18", 0.0147], ["19", 0.1616], ["20", 0.1249], ["21", 0.1396], ["22", 0.1984], ["23", 0.9403], ["24", 0.5069], ["25", 0.6024], ["26", 1.1681], ["27", 2.5272], ["28", 1.7411], ["29", 1.9027], ["30", 3.496...
{"lang": "python3", "distribution": [["15900", 0.0073], ["16000", 0.0147], ["16100", 0.0147], ["16200", 0.1616], ["16300", 3.0414], ["16400", 16.0814], ["16500", 29.1434], ["16600", 22.9944], ["16700", 10.4761], ["16800", 10.4907]]}
[ { "code": "class Solution:\n def numDecodings(self, s: str) -> int:\n dp = [0]*len(s)\n dp[0] = 1 if s[0] != \"0\" else 0\n if dp[0]==0:\n return 0\n idx = 1\n while idx < len(s):\n one_digit = s[idx]\n if one_digit != \"0\":\n ...
52
[ { "code": "# Given an encoding map and a string s \n# - 1<=s.length <= 100\n# Find the number of ways to decode the string s\n# - We can backtrack\n# - After drawing a tree there are some repeated instances, this can be represented as dp\n# - A string 1106 can be decoded 106 + 06 number of ways if those decodin...
17
92
reverse-linked-list-ii
<p>Given the <code>head</code> of a singly linked list and two integers <code>left</code> and <code>right</code> where <code>left &lt;= right</code>, reverse the nodes of the list from position <code>left</code> to position <code>right</code>, and return <em>the reversed list</em>.</p> <p>&nbsp;</p> <p><strong class="...
48.320393
Medium
[ "linked-list" ]
{"lang": "python3", "distribution": [["15", 0.0287], ["16", 0.0096], ["17", 0.0383], ["18", 0.0766], ["19", 0.2872], ["20", 0.1053], ["21", 0.1149], ["22", 0.6224], ["23", 0.766], ["24", 0.5649], ["25", 0.6128], ["26", 2.4512], ["27", 2.049], ["28", 1.4267], ["29", 2.8437], ["30", 6.2237], ["31", 4.9789], ["32", 3.3416...
{"lang": "python3", "distribution": [["15900", 0.0096], ["16200", 0.0096], ["16300", 0.067], ["16400", 2.2501], ["16500", 19.3891], ["16600", 38.2038]]}
[ { "code": "# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:\n cur = dummy = List...
46
[ { "code": "# Definition for singly-linked list.\n\nclass Solution:\n def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:\n \n if left == right:\n return head\n \n cur = head\n cnt = 1\n \n prev = None\n ...
9
93
restore-ip-addresses
<p>A <strong>valid IP address</strong> consists of exactly four integers separated by single dots. Each integer is between <code>0</code> and <code>255</code> (<strong>inclusive</strong>) and cannot have leading zeros.</p> <ul> <li>For example, <code>&quot;0.1.2.201&quot;</code> and <code>&quot;192.168.1.1&quot;</cod...
51.043219
Medium
[ "string", "backtracking" ]
{"lang": "python3", "distribution": [["16", 0.0234], ["19", 0.0701], ["20", 0.0468], ["22", 0.2104], ["23", 0.2572], ["24", 0.3741], ["25", 0.4442], ["26", 0.6079], ["27", 1.0288], ["28", 1.0054], ["29", 1.3093], ["30", 1.7769], ["31", 3.2733], ["32", 3.1798], ["33", 2.8525], ["34", 3.8578], ["35", 5.6114], ["36", 5.54...
{"lang": "python3", "distribution": [["16100", 0.0234], ["16300", 0.8183], ["16400", 10.2174], ["16500", 28.174], ["16600", 40.7996]]}
[ { "code": "class Solution:\n def restoreIpAddresses(self, s):\n res = []\n def backtrack(st, current_ans):\n if len(current_ans.split(\".\")) > 4:\n return\n\n if st == \"\" and len(current_ans.split(\".\")) == 4:\n res.append(current_ans)\n ...
78
[ { "code": "class Solution(object):\n # method to check if a part of the string is within the range 0-255,\n # returns True if part is within range 0-255 else False\n def valid(self, s, start, length):\n return length == 1 or (\n s[start] != \"0\"\n and (length < 3 or s[star...
8