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
263
ugly-number
<p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p> <p>Given an integer <code>n</code>, return <code>true</code> <em>if</em> <code>n</code> <em>is an <strong>ugly number</strong></em>.</p> <p>&nbsp;</p> <p><strong class="e...
42.154041
Easy
[ "math" ]
{"lang": "python3", "distribution": [["15", 0.0166], ["16", 0.0166], ["18", 0.0166], ["19", 0.1827], ["20", 0.1163], ["21", 0.0498], ["22", 0.2492], ["23", 0.7973], ["24", 0.8306], ["25", 0.5648], ["26", 0.8306], ["27", 2.5249], ["28", 2.4252], ["29", 2.0432], ["30", 2.4585], ["31", 6.9103], ["32", 4.8837], ["33", 4.43...
{"lang": "python3", "distribution": [["16100", 0.0166], ["16200", 0.0332], ["16300", 0.9635], ["16400", 14.9668], ["16500", 34.6512], ["16600", 42.9734]]}
[ { "code": "class Solution:\n def isUgly(self, n: int) -> bool:\n if n==0: return False\n if n in [1,2,3,5]: return True\n elif n%2==0 and self.isUgly(n//2): return True\n elif n%3==0 and self.isUgly(n//3): return True\n elif n%5==0 and self.isUgly(n//5): return True\n ...
48
[ { "code": "class Solution:\n def isUgly(self, n: int) -> bool:\n if n == 0:\n return False\n if n == 1:\n return True\n mod_2, mod_3, mod_5 = False, False, False\n if not n % 2:\n mod_2 = self.isUgly(n // 2)\n if not n % 3:\n mod_...
10
264
ugly-number-ii
<p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p> <p>Given an integer <code>n</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>ugly number</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Ex...
49.144553
Medium
[ "hash-table", "math", "dynamic-programming", "heap-priority-queue" ]
{"lang": "python3", "distribution": [["22", 0.0617], ["28", 0.35050000000000003], ["34", 0.878], ["40", 0.8864000000000001], ["46", 0.31750000000000006], ["52", 0.2187], ["58", 0.169], ["64", 0.8246], ["70", 3.5168999999999997], ["76", 8.6085], ["82", 10.9751], ["88", 8.4313], ["94", 4.2547999999999995], ["101", 2.6839...
{"lang": "python3", "distribution": [["16100", 0.0165], ["16200", 0.0495], ["16300", 0.5071], ["16400", 7.149], ["16500", 20.0206], ["16600", 36.644], ["16700", 19.6702], ["16800", 6.7821], ["16900", 2.4778], ["17000", 1.4224]]}
[ { "code": "ugly = [1]\npq = [(2,2,1), (3,3,1), (5,5,1)]\nwhile len(ugly) < 1691:\n num, factor, idx = heapq.heappop(pq)\n if ugly[-1] != num:\n ugly.append(num)\n heapq.heappush(pq, (ugly[idx] * factor, factor, idx + 1))\n\nclass Solution:\n def nthUglyNumber(self, n: int) -> int:\n re...
153
[ { "code": "class Solution:\n def nthUglyNumber(self, n: int) -> int:\n dp = [1] * n\n p2, p3, p5 = 0, 0, 0\n for i in range(1, n):\n dp[i] = min(dp[p2]*2, dp[p3]*3, dp[p5]*5)\n if dp[i] == dp[p2]*2:\n p2 += 1\n if dp[i] == dp[p3]*3:\n ...
15
71
simplify-path
<p>You are given an <em>absolute</em> path for a Unix-style file system, which always begins with a slash <code>&#39;/&#39;</code>. Your task is to transform this absolute path into its <strong>simplified canonical path</strong>.</p> <p>The <em>rules</em> of a Unix-style file system are as follows:</p> <ul> <li>A si...
44.158439
Medium
[ "string", "stack" ]
{"lang": "python3", "distribution": [["7", 0.0062], ["15", 0.0248], ["16", 0.0124], ["18", 0.0248], ["19", 0.1054], ["20", 0.0682], ["21", 0.0868], ["22", 0.1923], ["23", 0.5893], ["24", 0.4528], ["25", 0.3784], ["26", 0.7443], ["27", 1.8174], ["28", 1.6499], ["29", 1.5569], ["30", 2.1089], ["31", 5.3343], ["32", 3.907...
{"lang": "python3", "distribution": [["16000", 0.0062], ["16100", 0.0062], ["16200", 0.0372], ["16300", 0.6637], ["16400", 9.242], ["16500", 26.3615], ["16600", 42.5506]]}
[ { "code": "class Solution:\n def simplifyPath(self, path: str) -> str:\n stack = []\n\n toks = path.split(\"/\")\n \n for tok in toks:\n \n if tok == \".\" or tok == \"\":\n continue\n\n if tok == \"..\":\n if not stac...
54
[ { "code": "class Solution:\n def simplifyPath(self, path):\n dir_stack = []\n path = path.split(\"/\")\n for elem in path:\n if dir_stack and elem == \"..\":\n dir_stack.pop()\n elif elem not in [\".\", \"\", \"..\"]:\n dir_stack.append...
10
72
edit-distance
<p>Given two strings <code>word1</code> and <code>word2</code>, return <em>the minimum number of operations required to convert <code>word1</code> to <code>word2</code></em>.</p> <p>You have the following three operations permitted on a word:</p> <ul> <li>Insert a character</li> <li>Delete a character</li> <li>Rep...
57.32858
Medium
[ "string", "dynamic-programming" ]
{"lang": "python3", "distribution": [["40", 0.03950000000000001], ["42", 0.0474], ["44", 0.0079], ["46", 0.055400000000000005], ["48", 0.0237], ["49", 0.10289999999999999], ["51", 0.1978], ["53", 0.19790000000000002], ["55", 0.4195], ["57", 0.2533], ["58", 0.7202], ["60", 0.6806], ["62", 0.8548], ["64", 1.1793], ["66",...
{"lang": "python3", "distribution": [["16262", 0.5778], ["16387", 2.0103], ["16512", 2.7938], ["16637", 1.0605], ["16762", 0.49860000000000004], ["16887", 0.0475], ["17012", 0.0079], ["17137", 0.0158], ["17262", 0.17409999999999998], ["17387", 0.0237], ["17512", 0.0237], ["17637", 0.1425], ["17762", 0.1503], ["17887", ...
[ { "code": "class Solution:\n def minDistance(self, word1: str, word2: str) -> int:\n n, m = len(word1), len(word2)\n to_visit, visited, dist = [(0, 0)], set(), 0\n\n while to_visit:\n nxt_lvl = []\n while to_visit:\n i, j = to_visit.pop()\n ...
144
[ { "code": "class Solution:\n def minDistance(self, word1: str, word2: str) -> int:\n n= len(word1)\n dp = range(n+1)\n for j, c in enumerate(word2):\n new_dp = [0]*(n+1)\n new_dp[0] = j+1\n for i in range(1, n+1):\n if c == word1[i-1]:\n ...
110
73
set-matrix-zeroes
<p>Given an <code>m x n</code> integer matrix <code>matrix</code>, if an element is <code>0</code>, set its entire row and column to <code>0</code>&#39;s.</p> <p>You must do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in place</a>.</p> <p>&nbsp;</p> <p><strong class="example">Example...
57.409966
Medium
[ "array", "hash-table", "matrix" ]
{"lang": "python3", "distribution": [["26", 0.0048], ["35", 0.0048], ["38", 0.0048], ["40", 0.0048], ["41", 0.0096], ["42", 0.0144], ["43", 0.0192], ["44", 0.0096], ["45", 0.0048], ["48", 0.0096], ["50", 0.0048], ["54", 0.0048], ["73", 0.0048], ["77", 0.0048], ["78", 0.0096], ["79", 0.0144], ["80", 0.0048], ["81", 0.02...
{"lang": "python3", "distribution": [["16700", 0.0144], ["16800", 0.0144], ["16900", 0.0192], ["17000", 0.048], ["17100", 0.1487], ["17200", 1.6353], ["17300", 12.5599], ["17400", 28.4241], ["17500", 43.4683]]}
[ { "code": "def setZeroes(matrix: List[List[int]]) -> None:\n if not matrix:return []\n row=len(matrix);col=len(matrix[0])\n zr=[False]*row;zc=[False]*col\n for i in range(row):\n for j in range(col):\n if matrix[i][j]==0:zr[i]=True;zc[j]=True\n for i in range(row):\n for ...
83
[ { "code": "class Solution:\n def setZeroes(self, matrix: List[List[int]]) -> None:\n \"\"\"\n Do not return anything, modify matrix in-place instead.\n \"\"\"\n for i in range (0, len(matrix)): \n for j in range (0, len(matrix[i])): \n # print (i,j)\n ...
13
74
search-a-2d-matrix
<p>You are given an <code>m x n</code> integer matrix <code>matrix</code> with the following two properties:</p> <ul> <li>Each row is sorted in non-decreasing order.</li> <li>The first integer of each row is greater than the last integer of the previous row.</li> </ul> <p>Given an integer <code>target</code>, retur...
51.017291
Medium
[ "array", "binary-search", "matrix" ]
{"lang": "python3", "distribution": [["20", 0.0036], ["21", 0.0072], ["22", 0.0072], ["23", 0.0108], ["24", 0.0144], ["25", 0.0179], ["26", 0.0251], ["27", 0.0359], ["28", 0.061], ["29", 0.079], ["30", 0.1615], ["31", 0.28], ["32", 0.4882], ["33", 0.4451], ["34", 0.6928], ["35", 0.9799], ["36", 1.8558], ["37", 1.8486],...
{"lang": "python3", "distribution": [["16200", 0.0072], ["16400", 0.0036], ["16500", 0.0108], ["16600", 0.0179], ["16700", 0.0359], ["16800", 0.6425], ["16900", 8.7189], ["17000", 27.0613], ["17100", 43.9248]]}
[ { "code": "class Solution:\n def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:\n ROWS, COLS = len(matrix), len(matrix[0])\n\n L, R = 0, ROWS * COLS - 1\n while L <= R:\n idx = L + (R - L) // 2\n r, c = idx // COLS, idx % COLS\n if matr...
53
[ { "code": "class Solution:\n def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:\n\n for row in matrix:\n if target > row[len(row)-1]:\n continue\n else:\n for i in row:\n if target == i:\n ...
14
75
sort-colors
<p>Given an array <code>nums</code> with <code>n</code> objects colored red, white, or blue, sort them <strong><a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> </strong>so that objects of the same color are adjacent, with the colors in the order red, white, and blue.</p> <p>We wi...
64.986571
Medium
[ "array", "two-pointers", "sorting" ]
{"lang": "python3", "distribution": [["14", 0.0041], ["15", 0.0041], ["16", 0.0204], ["17", 0.0123], ["18", 0.0204], ["19", 0.0491], ["20", 0.1104], ["21", 0.0491], ["22", 0.139], ["23", 0.3762], ["24", 0.4293], ["25", 0.3394], ["26", 0.7114], ["27", 1.5987], ["28", 1.6273], ["29", 1.5782], ["30", 2.0157], ["31", 4.730...
{"lang": "python3", "distribution": [["16000", 0.0082], ["16100", 0.0164], ["16200", 0.3762], ["16300", 6.3701], ["16400", 25.4068], ["16500", 44.6889]]}
[ { "code": "class Solution:\n def sortColors(self, nums: List[int]) -> None:\n \"\"\"\n Do not return anything, modify nums in-place instead.\n \n \"\"\"\n i = current = 0\n \n j = len(nums) - 1\n \n while current <= j:\n if nums[curren...
51
[ { "code": "class Solution:\n def sortColors(self, nums: List[int]) -> None:\n n = len(nums)\n low=0\n mid=0\n high = n-1\n while mid <= high:\n if nums[mid] == 0:\n nums[mid],nums[low] = nums[low],nums[mid]\n low+=1\n ...
10
76
minimum-window-substring
<p>Given two strings <code>s</code> and <code>t</code> of lengths <code>m</code> and <code>n</code> respectively, return <em>the <strong>minimum window</strong></em> <span data-keyword="substring-nonempty"><strong><em>substring</em></strong></span><em> of </em><code>s</code><em> such that every character in </em><code>...
43.687391
Hard
[ "hash-table", "string", "sliding-window" ]
{"lang": "python3", "distribution": [["59", 0.0406], ["69", 0.6453], ["79", 3.5225], ["89", 7.2075000000000005], ["99", 14.466000000000001], ["109", 14.2473], ["119", 7.7514], ["129", 4.3458], ["139", 2.8971999999999998], ["149", 2.4652999999999996], ["158", 3.9037], ["168", 4.8896], ["178", 3.5681999999999996], ["188"...
{"lang": "python3", "distribution": [["16100", 0.0051], ["16700", 0.0102], ["16800", 0.0203], ["16900", 0.0254], ["17000", 0.1271], ["17100", 1.3114], ["17200", 21.5716], ["17300", 55.6826], ["17400", 15.0351]]}
[ { "code": "class Solution:\n def minWindow(self, s: str, t: str) -> str:\n if len(s) < len(t):\n return \"\"\n char_count = defaultdict(int)\n for ch in t:\n char_count[ch] += 1\n \n tcr = len(t)\n min_w = (0, float(\"inf\"))\n start_...
153
[ { "code": "class Solution:\n def minWindow(self, s: str, t: str) -> str:\n if t == \"\":\n return \"\"\n\n countT, window = {}, {}\n for c in t:\n countT[c] = 1 + countT.get(c, 0)\n\n have, need = 0, len(countT)\n res, resLen = [-1, -1], float(\"infini...
13
77
combinations
<p>Given two integers <code>n</code> and <code>k</code>, return <em>all possible combinations of</em> <code>k</code> <em>numbers chosen from the range</em> <code>[1, n]</code>.</p> <p>You may return the answer in <strong>any order</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <...
71.279933
Medium
[ "backtracking" ]
{"lang": "python3", "distribution": [["169", 0.04740000000000001], ["179", 0.0079], ["189", 0.0079], ["199", 0.0079], ["209", 0.0079], ["219", 0.0079], ["229", 0.0237], ["239", 0.0395], ["249", 0.0158], ["259", 0.0079], ["269", 0.0237], ["279", 0.0316], ["289", 0.0316], ["299", 0.0395], ["309", 0.0237], ["319", 0.0633]...
{"lang": "python3", "distribution": [["36930", 0.5534], ["37391", 0.9092], ["37853", 0.0079], ["38314", 0.0079], ["38775", 0.0079], ["39236", 0.0158], ["39698", 0.0158], ["40159", 0.0237], ["40620", 0.0158], ["41081", 0.0079], ["41543", 0.0158], ["42004", 0.0712], ["42465", 0.2767], ["42926", 0.8302], ["43388", 0.7985]...
[ { "code": "\nif __name__ == '__main__':\n with open('user.out','w') as f:\n for n, k in zip(map(loads, stdin), map(loads, stdin)):\n def combine(n: int, k: int) -> List[List[int]]:\n return combinations(range(1, n+1), k) \n print(json.dumps(tuple(combine(n, k))),...
114
[ { "code": "import itertools\n\nclass Solution:\n def combine(self, n: int, k: int) -> List[List[int]]:\n arr = [x for x in range(1, n+1)]\n return itertools.combinations(arr, r=k)", "memory": "36930" }, { "code": "class Solution:\n def combine(self, n: int, k: int) -> List[List[i...
108
78
subsets
<p>Given an integer array <code>nums</code> of <strong>unique</strong> elements, 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>...
79.220735
Medium
[ "array", "backtracking", "bit-manipulation" ]
{"lang": "python3", "distribution": [["14", 0.0036], ["16", 0.0107], ["17", 0.0071], ["18", 0.0356], ["19", 0.1069], ["20", 0.0641], ["21", 0.0392], ["22", 0.1746], ["23", 0.4846], ["24", 0.3884], ["25", 0.4027], ["26", 0.7055], ["27", 1.9741], ["28", 1.5144], ["29", 1.4431], ["30", 2.2413], ["31", 5.5908], ["32", 4.19...
{"lang": "python3", "distribution": [["16000", 0.0071], ["16100", 0.0036], ["16200", 0.0071], ["16300", 0.0285], ["16400", 0.3171], ["16500", 5.8972], ["16600", 25.3136], ["16700", 44.5268]]}
[ { "code": "class Solution:\n def subsets(self, nums: List[int]) -> List[List[int]]:\n res = []\n\n def backtrack(subset, index):\n res.append(subset[:])\n\n for i in range(index, len(nums)):\n subset.append(nums[i])\n backtrack(subset, i + 1)\...
53
[ { "code": "class Solution:\n def subsets(self, nums: List[int]) -> List[List[int]]:\n n = len(nums)\n result = []\n for i in range(1 << n):\n subset = []\n for j in range(n):\n if i & (1 << j):\n subset.append(nums[j])\n ...
8
79
word-search
<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p> <p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically ne...
43.604457
Medium
[ "array", "string", "backtracking", "matrix" ]
{"lang": "python3", "distribution": [["71", 1.3189], ["167", 0.0336], ["262", 0.0084], ["358", 0.06720000000000001], ["453", 0.6396999999999999], ["549", 0.6433999999999995], ["644", 1.095199999999999], ["740", 0.42029999999999984], ["835", 0.30239999999999995], ["931", 0.12599999999999997], ["1027", 0.0881999999999999...
{"lang": "python3", "distribution": [["15900", 0.0042], ["16000", 0.0042], ["16100", 0.0674], ["16200", 0.4046], ["16300", 1.6649], ["16400", 5.5638], ["16500", 23.4226], ["16600", 40.1686]]}
[ { "code": "class Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n m = len(board)\n n = len(board[0])\n if len(word) > m*n: \n return False\n\n counter = defaultdict(int)\n for i in range(m):\n for j in range(n):\n ...
155
[ { "code": "class Solution:\n def exist(self, board: List[List[str]], word: str) -> bool:\n def dfs(r, c, path):\n if not path:\n return True\n if r < 0 or r >= len(board) or c < 0 or c>=len(board[0]):\n return False\n if path[0] != board[r...
13
80
remove-duplicates-from-sorted-array-ii
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove some duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears <strong>at most twice</strong>. The <strong>relative order</s...
60.151274
Medium
[ "array", "two-pointers" ]
{"lang": "python3", "distribution": [["29", 0.0038], ["30", 0.0114], ["31", 0.019], ["32", 0.0304], ["33", 0.0418], ["34", 0.0874], ["35", 0.095], ["36", 0.1635], ["37", 0.2471], ["38", 0.5284], ["39", 0.555], ["40", 0.8933], ["41", 1.1746], ["42", 1.8436], ["43", 1.6764], ["44", 2.467], ["45", 3.4516], ["46", 4.4247],...
{"lang": "python3", "distribution": [["15900", 0.0076], ["16100", 0.0152], ["16200", 0.076], ["16300", 1.129], ["16400", 15.3077], ["16500", 34.2494], ["16600", 40.5139]]}
[ { "code": "class Solution:\n def removeDuplicates(self, nums: List[int]) -> int:\n j = 1\n for i in range(1, len(nums)):\n if j == 1 or nums[i] != nums[j-2]:\n nums[j] = nums[i]\n j += 1\n return j ", "runtime": "29" }, { "code": "clas...
63
[ { "code": "class Solution:\n def removeDuplicates(self, nums: List[int]) -> int:\n if not nums:\n return 0\n \n write = 1 # Position to write the next valid element\n count = 1 # Counter to track occurrences of current element\n \n for read in range(1, l...
11
21
merge-two-sorted-lists
<p>You are given the heads of two sorted linked lists <code>list1</code> and <code>list2</code>.</p> <p>Merge the two lists into one <strong>sorted</strong> list. The list should be made by splicing together the nodes of the first two lists.</p> <p>Return <em>the head of the merged linked list</em>.</p> <p>&nbsp;</p...
65.252677
Easy
[ "linked-list", "recursion" ]
{"lang": "python3", "distribution": [["12", 0.0019], ["14", 0.0019], ["15", 0.0019], ["16", 0.0039], ["17", 0.0039], ["18", 0.0214], ["19", 0.0312], ["20", 0.0799], ["21", 0.0468], ["22", 0.1891], ["23", 0.1793], ["24", 0.3878], ["25", 0.2631], ["26", 0.8692], ["27", 0.6997], ["28", 1.641], ["29", 1.298], ["30", 2.4362...
{"lang": "python3", "distribution": [["16100", 0.0097], ["16200", 0.1033], ["16300", 2.5337], ["16400", 20.8969], ["16500", 42.6241]]}
[ { "code": "# Definition for singly-linked list.\nclass ListNode:\n def __init__(self, val=0, next=None):\n self.val = val\n self.next = next\n\nclass Solution:\n def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:\n\n if not list1:\n ...
59
[ { "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 mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:\n temp1 = list1\...
9
22
generate-parentheses
<p>Given <code>n</code> pairs of parentheses, write a function to <em>generate all combinations of well-formed parentheses</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> n = 3 <strong>Output:</strong> ["((()))","(()())","(())()","()(())","()()()"] </pre><p><stro...
75.488898
Medium
[ "string", "dynamic-programming", "backtracking" ]
{"lang": "python3", "distribution": [["15", 0.0146], ["16", 0.0073], ["17", 0.0146], ["18", 0.0219], ["19", 0.0512], ["20", 0.0914], ["21", 0.0402], ["22", 0.1755], ["23", 0.3875], ["24", 0.4168], ["25", 0.3364], ["26", 0.6654], ["27", 1.4697], ["28", 1.3527], ["29", 1.4661], ["30", 1.9048], ["31", 4.2447], ["32", 4.06...
{"lang": "python3", "distribution": [["16200", 0.0037], ["16300", 0.0073], ["16400", 0.0183], ["16500", 0.0695], ["16600", 1.499], ["16700", 16.1341], ["16800", 32.0781], ["16900", 38.9222]]}
[ { "code": "class Solution:\n def generateParenthesis(self, n: int) -> List[str]:\n\n def form(s, o, c):\n if o == n:\n return [s + ')' * (n - c)]\n else:\n if o == c:\n return form(s + '(', o + 1, c)\n else:\n ...
58
[ { "code": "class Solution:\n def generateParenthesis(self, n: int) -> List[str]:\n tack = []\n\n res = []\n\n def backtrack(openN, closedN):\n if openN == closedN == n:\n res.append(\"\".join(tack))\n\n if openN < n:\n tack.append(\"(\"...
13
23
merge-k-sorted-lists
<p>You are given an array of <code>k</code> linked-lists <code>lists</code>, each linked-list is sorted in ascending order.</p> <p><em>Merge all the linked-lists into one sorted linked-list and return it.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> lists = ...
54.114388
Hard
[ "linked-list", "divide-and-conquer", "heap-priority-queue", "merge-sort" ]
{"lang": "python3", "distribution": [["49", 11.2184], ["87", 67.37310000000001], ["125", 2.6217], ["164", 0.35969999999999996], ["202", 0.15619999999999998], ["240", 0.0379], ["278", 0.0421], ["317", 0.038], ["355", 0.0168], ["393", 0.0338], ["432", 0.05040000000000001], ["470", 0.0336], ["508", 0.020999999999999998], ...
{"lang": "python3", "distribution": [["17400", 0.0211], ["17500", 0.0677], ["17600", 0.0042], ["17800", 0.0085], ["18100", 0.0042], ["18400", 0.0042], ["18500", 0.0042], ["18600", 0.0042], ["18700", 0.0296], ["18800", 0.1395], ["18900", 0.0803], ["19000", 0.0592], ["19100", 0.2072], ["19200", 1.7717], ["19300", 21.3371...
[ { "code": "class Solution:\n f = open(\"user.out\", 'w')\n print(sys.stdin)\n for s in sys.stdin:\n print(s)\n print('[', ','.join(\n map(str, sorted(int(v) for v in s.rstrip().replace('[', ',').replace(']', ',').split(',') if v))), ']', sep='', file=f)\n exit()", "runti...
148
[ { "code": "class Solution:\n f = open(\"user.out\", 'w')\n print(sys.stdin)\n for s in sys.stdin:\n print(s)\n print('[', ','.join(\n map(str, sorted(int(v) for v in s.rstrip().replace('[', ',').replace(']', ',').split(',') if v))), ']', sep='', file=f)\n exit()\n", "mem...
38
24
swap-nodes-in-pairs
<p>Given a&nbsp;linked list, swap every two adjacent nodes and return its head. You must solve the problem without&nbsp;modifying the values in the list&#39;s nodes (i.e., only nodes themselves may be changed.)</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong...
65.290615
Medium
[ "linked-list", "recursion" ]
{"lang": "python3", "distribution": [["13", 0.009], ["14", 0.009], ["15", 0.027], ["16", 0.009], ["17", 0.027], ["18", 0.0989], ["19", 0.2068], ["20", 0.1259], ["21", 0.1529], ["22", 0.5395], ["23", 0.6384], ["24", 0.6025], ["25", 0.8183], ["26", 2.5357], ["27", 2.0502], ["28", 1.7804], ["29", 3.1922], ["30", 5.9167], ...
{"lang": "python3", "distribution": [["16100", 0.0629], ["16200", 0.3417], ["16300", 6.4473], ["16400", 27.8752], ["16500", 46.8753]]}
[ { "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 swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:\n if head == None:\n return None\n ...
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 swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:\n dummy = ListNode(next=head)\n prev, cur...
8
25
reverse-nodes-in-k-group
<p>Given the <code>head</code> of a linked list, reverse the nodes of the list <code>k</code> at a time, and return <em>the modified list</em>.</p> <p><code>k</code> is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of <code>k</code> then left-ou...
60.400675
Hard
[ "linked-list", "recursion" ]
{"lang": "python3", "distribution": [["20", 0.0087], ["21", 0.0087], ["22", 0.026], ["23", 0.0346], ["24", 0.0433], ["25", 0.0433], ["26", 0.1039], ["27", 0.1472], ["28", 0.3031], ["29", 0.3291], ["30", 0.7448], ["31", 0.7015], ["32", 1.2298], ["33", 1.325], ["34", 2.2863], ["35", 1.7321], ["36", 2.8579], ["37", 3.6893...
{"lang": "python3", "distribution": [["16800", 0.0087], ["17000", 0.0173], ["17100", 0.0433], ["17200", 0.485], ["17300", 7.7423], ["17400", 26.3878], ["17500", 42.7124], ["17600", 14.3933], ["17700", 1.4549], ["17800", 1.6021]]}
[ { "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 reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:\n if not head or k == 1:\n ...
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 reverseKGroup(self, head: ListNode, k: int) -> ListNode: \n # Check if we need to reverse the group\n ...
13
26
remove-duplicates-from-sorted-array
<p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing order</strong>, remove the duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a> such that each unique element appears only <strong>once</strong>. The <strong>relative order</strong...
57.610665
Easy
[ "array", "two-pointers" ]
{"lang": "python3", "distribution": [["25", 0.0028], ["27", 0.0014], ["29", 0.0057], ["30", 0.0043], ["32", 0.0014], ["33", 0.0072], ["35", 0.01], ["37", 0.01], ["38", 0.01], ["40", 0.0057], ["41", 0.0086], ["43", 0.0071], ["45", 0.0014], ["46", 0.0172], ["48", 0.0114], ["49", 0.0128], ["51", 0.0442], ["53", 0.0413], [...
{"lang": "python3", "distribution": [["16800", 0.0014], ["16900", 0.0029], ["17100", 0.0143], ["17200", 0.0428], ["17300", 0.0271], ["17400", 0.0228], ["17500", 0.0456], ["17600", 0.2822], ["17700", 4.6551], ["17800", 16.8757], ["17900", 33.6288], ["18000", 36.1374]]}
[ { "code": "# Define the main function that removes duplicates from a sorted list of integers and returns the new length.\ndef main(nums: list[int]) -> int:\n # Initialize the current position index.\n cur = 0\n # Initialize the variable to keep track of the last unique number processed.\n last = Non...
140
[ { "code": "class Solution:\n def removeDuplicates(self, nums: List[int]) -> int:\n # freq = Counter(nums)\n # nums[:] = freq.keys()\n seen = []\n for i in nums:\n if i not in seen:\n seen.append(i)\n nums[:] = seen\n ", "memory": "16800"...
17
27
remove-element
<p>Given an integer array <code>nums</code> and an integer <code>val</code>, remove all occurrences of <code>val</code> in <code>nums</code> <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"><strong>in-place</strong></a>. The order of the elements may be changed. Then return <em>the number of e...
58.176832
Easy
[ "array", "two-pointers" ]
{"lang": "python3", "distribution": [["12", 0.0017], ["13", 0.0017], ["14", 0.0017], ["15", 0.0102], ["16", 0.0153], ["17", 0.0017], ["18", 0.0289], ["19", 0.0783], ["20", 0.0987], ["21", 0.0579], ["22", 0.1702], ["23", 0.3795], ["24", 0.4408], ["25", 0.3932], ["26", 0.7012], ["27", 1.7224], ["28", 1.8433], ["29", 1.63...
{"lang": "python3", "distribution": [["15700", 0.0017], ["15800", 0.0017], ["15900", 0.0017], ["16000", 0.0051], ["16100", 0.034], ["16200", 0.3506], ["16300", 6.1341], ["16400", 26.1123], ["16500", 45.5458]]}
[ { "code": "class Solution:\n def removeElement(self, nums: List[int], val: int) -> int:\n k = 0\n\n for i in range(0, len(nums)):\n if nums[i] != val:\n nums[k] = nums[i]\n k = k+1\n return k\n ", "runtime": "12" }, { "code"...
54
[ { "code": "class Solution:\n def removeElement(self, nums: List[int], val: int) -> int:\n r=0\n for i in nums:\n if val==i:\n r=r+1\n for a in range(0,r):\n nums.remove(val)\n return len(nums)", "memory": "15700" }, { "code": "class...
13
28
find-the-index-of-the-first-occurrence-in-a-string
<p>Given two strings <code>needle</code> and <code>haystack</code>, return the index of the first occurrence of <code>needle</code> in <code>haystack</code>, or <code>-1</code> if <code>needle</code> is not part of <code>haystack</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <stro...
43.42327
Easy
[ "two-pointers", "string", "string-matching" ]
{"lang": "python3", "distribution": [["8", 0.0025], ["10", 0.0025], ["11", 0.0025], ["12", 0.0025], ["14", 0.005], ["15", 0.0552], ["16", 0.0401], ["17", 0.0201], ["18", 0.1153], ["19", 0.1931], ["20", 0.2282], ["21", 0.2282], ["22", 0.6444], ["23", 0.7372], ["24", 0.8551], ["25", 0.9729], ["26", 2.3044], ["27", 1.9935...
{"lang": "python3", "distribution": [["15700", 0.0025], ["15800", 0.005], ["15900", 0.005], ["16000", 0.0125], ["16100", 0.0201], ["16200", 0.3109], ["16300", 6.4669], ["16400", 25.5216], ["16500", 45.1103]]}
[ { "code": "class Solution:\n def strStr(self, haystack: str, needle: str) -> int:\n if needle == \"\":\n return 0\n for i in range(len(haystack) - (len(needle) - 1)):\n if haystack[i: i + len(needle)] == needle:\n return i\n return -1", "runtime":...
53
[ { "code": "class Solution:\n def strStr(self, haystack: str, needle: str) -> int:\n return haystack.find(needle)", "memory": "15700" }, { "code": "class Solution:\n def strStr(self, haystack: str, needle: str) -> int:\n p=-1\n for i in range (len(haystack)):\n k...
12
29
divide-two-integers
<p>Given two integers <code>dividend</code> and <code>divisor</code>, divide two integers <strong>without</strong> using multiplication, division, and mod operator.</p> <p>The integer division should truncate toward zero, which means losing its fractional part. For example, <code>8.345</code> would be truncated to <co...
17.766223
Medium
[ "math", "bit-manipulation" ]
{"lang": "python3", "distribution": [["15", 0.0322], ["16", 0.0161], ["19", 0.0805], ["20", 0.1772], ["21", 0.0483], ["22", 0.3865], ["23", 0.3865], ["24", 0.6925], ["25", 0.5798], ["26", 0.9824], ["27", 2.1098], ["28", 2.3192], ["29", 1.836], ["30", 2.3997], ["31", 3.8654], ["32", 5.3471], ["33", 4.7351], ["34", 4.799...
{"lang": "python3", "distribution": [["16100", 0.0161], ["16200", 0.1288], ["16300", 1.7394], ["16400", 13.867], ["16500", 34.2406], ["16600", 39.781]]}
[ { "code": "class Solution:\n def divide(self, dividend: int, divisor: int) -> int:\n if dividend == -2**31 and divisor == -1:\n return 2**31 - 1\n sign = 1 if (dividend > 0) == (divisor > 0) else -1\n abs_dividend = abs(dividend)\n abs_divisor = abs(divisor)\n qu...
49
[ { "code": "class Solution:\n def divide(self, dividend: int, divisor: int) -> int:\n if divisor==0 or dividend<-2**31 or dividend>2**31-1:\n return 0\n ans= dividend/divisor\n if ans==2147483648:\n return 2147483647\n return int(ans)", "memory": "16100" ...
9
30
substring-with-concatenation-of-all-words
<p>You are given a string <code>s</code> and an array of strings <code>words</code>. All the strings of <code>words</code> are of <strong>the same length</strong>.</p> <p>A <strong>concatenated string</strong> is a string that exactly contains all the strings of any permutation of <code>words</code> concatenated.</p> ...
32.544823
Hard
[ "hash-table", "string", "sliding-window" ]
{"lang": "python3", "distribution": [["112", 38.64350000000005], ["229", 2.3082000000000003], ["346", 1.9828000000000006], ["463", 0.7283999999999998], ["580", 0.4960000000000002], ["698", 0.48050000000000026], ["815", 0.35650000000000015], ["932", 0.1085], ["1049", 0.31000000000000016], ["1166", 0.37200000000000016], ...
{"lang": "python3", "distribution": [["16300", 0.0155], ["16400", 0.0619], ["16500", 0.0929], ["16600", 0.0774], ["16700", 0.3871], ["16800", 0.4645], ["16900", 0.4645], ["17000", 1.2695], ["17100", 7.1838], ["17200", 21.5668], ["17300", 34.8506], ["17400", 21.5049], ["17500", 3.8396], ["17600", 1.1612], ["17700", 0.27...
[ { "code": "class Solution:\n def findSubstring(self, s: str, words: List[str]) -> List[int]:\n \n\n # if len(s) < len(words[0]) * len(words):\n # return []\n\n # word_len = len(words[0])\n # word_map = {}\n # total_score = 0\n\n # for i in range(len(words)...
153
[ { "code": "from collections import defaultdict\nclass Solution:\n def findSubstring(self, s: str, words: List[str]) -> List[int]:\n res = []\n\n word_count = defaultdict(int)\n for word in words:\n word_count[word] += 1\n\n word_len = len(words[0])\n substr_len =...
43
61
rotate-list
<p>Given the <code>head</code> of a linked&nbsp;list, rotate the list to the right by <code>k</code> places.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg" style="width: 450px; height: 191px;" /> <pre> <strong>Input:</st...
38.633055
Medium
[ "linked-list", "two-pointers" ]
{"lang": "python3", "distribution": [["12", 0.0091], ["16", 0.0274], ["18", 0.0091], ["19", 0.0091], ["20", 0.0822], ["21", 0.0274], ["22", 0.1369], ["23", 0.2009], ["24", 0.4382], ["25", 0.21], ["26", 0.8034], ["27", 0.5661], ["28", 1.4151], ["29", 1.2873], ["30", 2.2003], ["31", 2.0634], ["32", 4.6015], ["33", 4.2545...
{"lang": "python3", "distribution": [["16100", 0.0183], ["16200", 0.0365], ["16300", 0.3378], ["16400", 5.7062], ["16500", 28.0197], ["16600", 46.7361]]}
[ { "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 rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:\n if not head or not k:\n r...
49
[ { "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 rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:\n if not head or k == 0:\n ...
10
62
unique-paths
<p>There is a robot on an <code>m x n</code> grid. The robot is initially located at the <strong>top-left corner</strong> (i.e., <code>grid[0][0]</code>). The robot tries to move to the <strong>bottom-right corner</strong> (i.e., <code>grid[m - 1][n - 1]</code>). The robot can only move either down or right at any poin...
64.817698
Medium
[ "math", "dynamic-programming", "combinatorics" ]
{"lang": "python3", "distribution": [["15", 0.0122], ["16", 0.0162], ["17", 0.0122], ["18", 0.065], ["19", 0.1543], ["20", 0.134], ["21", 0.1462], ["22", 0.4183], ["23", 0.658], ["24", 0.6214], ["25", 0.7758], ["26", 2.0714], ["27", 2.1201], ["28", 2.0673], ["29", 2.9365], ["30", 4.9998], ["31", 4.6749], ["32", 4.2443]...
{"lang": "python3", "distribution": [["15900", 0.0041], ["16000", 0.0162], ["16100", 0.0081], ["16200", 0.195], ["16300", 3.7935], ["16400", 19.5808], ["16500", 37.3299], ["16600", 23.6709], ["16700", 5.609], ["16800", 4.4149]]}
[ { "code": "class Solution:\n def uniquePaths(self, m: int, n: int) -> int:\n return comb(m+n-2, m-1)", "runtime": "15" }, { "code": "class Solution:\n def uniquePaths(self, m: int, n: int) -> int:\n dp = [[0]*n for i in range(m)]\n dp[0][-1] = 1\n if n == 1 or m == ...
49
[ { "code": "class Solution:\n def uniquePaths(self, m: int, n: int) -> int:\n dp=[[0]*n for _ in range(m)]\n for i in range(m):\n dp[i][0] = 1\n for j in range(n):\n dp[0][j] = 1\n for i in range(1, m):\n for j in range(1, n):\n dp[i]...
16
63
unique-paths-ii
<p>You are given an <code>m x n</code> integer array <code>grid</code>. There is a robot initially located at the <b>top-left corner</b> (i.e., <code>grid[0][0]</code>). The robot tries to move to the <strong>bottom-right corner</strong> (i.e., <code>grid[m - 1][n - 1]</code>). The robot can only move either down or ri...
42.186963
Medium
[ "array", "dynamic-programming", "matrix" ]
{"lang": "python3", "distribution": [["19", 0.0268], ["22", 0.0089], ["23", 0.0179], ["25", 0.0179], ["26", 0.0536], ["27", 0.1073], ["28", 0.0894], ["29", 0.1877], ["30", 0.4291], ["31", 0.6704], ["32", 0.5095], ["33", 0.8313], ["34", 2.0738], ["35", 1.9219], ["36", 1.5017], ["37", 2.4314], ["38", 4.6572], ["39", 4.51...
{"lang": "python3", "distribution": [["16200", 0.0179], ["16300", 0.1073], ["16400", 2.0291], ["16500", 15.375], ["16600", 29.1857], ["16700", 32.35], ["16800", 9.9759]]}
[ { "code": "class Solution:\n def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:\n n,m = len(obstacleGrid), len(obstacleGrid[0])\n dp = [[0 for _ in range(m)] for _ in range(n)]\n way=1\n for i in range(n):\n if obstacleGrid[i][0]:\n ...
53
[ { "code": "class Solution:\n def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:\n '''\n [1 1 1],\n [1 -1 0],\n [1 0 0]\n '''\n if obstacleGrid[0][0] == 1 or obstacleGrid[-1][-1] == 1:\n return 0\n\n rows, cols = len(ob...
13
64
minimum-path-sum
<p>Given a <code>m x n</code> <code>grid</code> filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.</p> <p><strong>Note:</strong> You can only move either down or right at any point in time.</p> <p>&nbsp;</p> <p><strong class="example">Ex...
64.953042
Medium
[ "array", "dynamic-programming", "matrix" ]
{"lang": "python3", "distribution": [["31", 0.0142], ["33", 0.0071], ["34", 0.0071], ["36", 0.0071], ["38", 0.0071], ["39", 0.0214], ["41", 0.0286], ["42", 0.0143], ["44", 0.0143], ["45", 0.0143], ["47", 0.035699999999999996], ["49", 0.0071], ["50", 0.0214], ["52", 0.0143], ["53", 0.0214], ["55", 0.0071], ["56", 0.0143...
{"lang": "python3", "distribution": [["16800", 0.0714], ["16900", 0.1642], ["17000", 1.0423], ["17100", 1.9633], ["17200", 2.0133], ["17300", 1.8776], ["17400", 1.0566], ["17500", 0.2285], ["17600", 0.0857], ["17700", 0.0357], ["17800", 0.05], ["17900", 0.1], ["18000", 0.1428], ["18100", 0.3784], ["18200", 6.5753], ["1...
[ { "code": "def minPathSum(grid: List[List[int]]) -> int:\n x,y=len(grid),len(grid[0])\n for i in range(1,x):\n grid[i][0]+=grid[i-1][0]\n for i in range(1,y):\n grid[0][i]+=grid[0][i-1]\n for i in range(1,x):\n for j in range(1,y):\n grid[i][j]+=min(grid[i-1][j],grid[...
130
[ { "code": "class Solution:\n def minPathSum(self, grid: List[List[int]]) -> int:\n m = len(grid)\n n = len(grid[0])\n\n prev_row = [math.inf] * (n + 1)\n prev_row[n - 1] = 0\n\n for r in range(m - 1, -1, -1):\n for c in range(n - 1, -1, -1):\n prev...
91
65
valid-number
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> <br /> For example, all the following are valid numbers: <code>&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&...
20.34475
Hard
[ "string" ]
{"lang": "python3", "distribution": [["18", 0.0272], ["19", 0.109], ["20", 0.0817], ["21", 0.0817], ["22", 0.0817], ["23", 0.3269], ["24", 0.3541], ["25", 0.2996], ["26", 0.681], ["27", 1.6344], ["28", 1.0624], ["29", 1.2258], ["30", 2.9965], ["31", 3.4595], ["32", 2.3427], ["33", 3.2144], ["34", 6.6467], ["35", 6.565]...
{"lang": "python3", "distribution": [["16200", 0.0272], ["16300", 2.5879], ["16400", 14.0834], ["16500", 32.28], ["16600", 35.0313]]}
[ { "code": "class Solution(object):\n def isNumber(self, s):\n # This is the DFA we have designed above\n dfa = [\n {\"digit\": 1, \"sign\": 2, \"dot\": 3},\n {\"digit\": 1, \"dot\": 4, \"exponent\": 5},\n {\"digit\": 1, \"dot\": 3},\n {\"digit\": 4},\...
48
[ { "code": "class Solution:\n def isNumber(self, s: str) -> bool:\n import re\n integer = r\"[-+]?\\d+\"\n decimal = r\"[-+]?(?=\\.?\\d)\\d*\\.\\d*\"\n return re.compile(\"(%s|%s)([eE]%s)?\" % (integer, decimal, integer)).fullmatch(s)", "memory": "16200" }, { "code": "c...
9
66
plus-one
<p>You are given a <strong>large integer</strong> represented as an integer array <code>digits</code>, where each <code>digits[i]</code> is the <code>i<sup>th</sup></code> digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain a...
46.141425
Easy
[ "array", "math" ]
{"lang": "python3", "distribution": [["14", 0.0032], ["15", 0.0095], ["16", 0.0095], ["17", 0.0032], ["18", 0.0379], ["19", 0.1294], ["20", 0.101], ["21", 0.0821], ["22", 0.1768], ["23", 0.704], ["24", 0.363], ["25", 0.5335], ["26", 0.7545], ["27", 2.475], ["28", 1.6574], ["29", 1.7647], ["30", 2.6107], ["31", 6.6073],...
{"lang": "python3", "distribution": [["15700", 0.0032], ["16000", 0.0095], ["16100", 0.041], ["16200", 0.5556], ["16300", 7.6807], ["16400", 25.5012], ["16500", 45.4778]]}
[ { "code": "class Solution:\n def plusOne(self, digits: List[int]) -> List[int]:\n for i in range(len(digits)-1, -1, -1):\n if digits[i] == 9:\n digits[i] = 0\n if i == 0:\n digits.insert(0, 1)\n return digits\n e...
52
[ { "code": "class Solution:\n def plusOne(self, digits: List[int]) -> List[int]:\n for i, d in reversed(list(enumerate(digits))):\n if d < 9:\n digits[i] += 1\n return digits\n digits[i] = 0\n\n return [1] + digits", "memory": "15700" }, { "code": "class Solution:\n ...
10
67
add-binary
<p>Given two binary strings <code>a</code> and <code>b</code>, return <em>their sum as a binary string</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> a = "11", b = "1" <strong>Output:</strong> "100" </pre><p><strong class="example">Example 2:</strong></p> <pre><...
54.321136
Easy
[ "math", "string", "bit-manipulation", "simulation" ]
{"lang": "python3", "distribution": [["14", 0.0049], ["15", 0.0147], ["16", 0.0049], ["17", 0.0098], ["18", 0.0393], ["19", 0.0983], ["20", 0.0885], ["21", 0.118], ["22", 0.2458], ["23", 0.4817], ["24", 0.4178], ["25", 0.4325], ["26", 1.16], ["27", 1.5827], ["28", 1.1698], ["29", 1.5876], ["30", 3.3915], ["31", 3.6422]...
{"lang": "python3", "distribution": [["16000", 0.0098], ["16100", 0.0049], ["16200", 0.1622], ["16300", 2.669], ["16400", 12.6321], ["16500", 30.9658], ["16600", 30.8282]]}
[ { "code": "class Solution:\n def addBinary(self, a: str, b: str) -> str:\n return str(bin(int(a,2)+int(b,2)))[2:]\n\n \n ", "runtime": "14" }, { "code": "class Solution:\n def addBinary(self, a: str, b: str) -> str:\n res = int(a, 2) + int(b, 2)\n return str(...
53
[ { "code": "class Solution:\n def addBinary(self, a: str, b: str) -> str:\n a=int(a,2)+int(b,2)\n a=bin(a)\n return a[2:]", "memory": "16000" }, { "code": "class Solution:\n def addBinary(self, a: str, b: str) -> str:\n return bin(int(a,2)+int(b,2))[2:]\n ", ...
11
68
text-justification
<p>Given an array of strings <code>words</code> and a width <code>maxWidth</code>, format the text such that each line has exactly <code>maxWidth</code> characters and is fully (left and right) justified.</p> <p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad e...
45.108506
Hard
[ "array", "string", "simulation" ]
{"lang": "python3", "distribution": [["15", 0.0132], ["16", 0.0132], ["18", 0.0659], ["19", 0.1845], ["20", 0.1054], ["21", 0.1186], ["22", 0.2372], ["23", 0.7379], ["24", 0.3558], ["25", 0.593], ["26", 1.3045], ["27", 2.6617], ["28", 1.5285], ["29", 1.7789], ["30", 3.9663], ["31", 6.2854], ["32", 3.8213], ["33", 3.966...
{"lang": "python3", "distribution": [["16200", 0.0132], ["16300", 0.1054], ["16400", 0.7116], ["16500", 13.0716], ["16600", 38.0683], ["16700", 38.4767]]}
[ { "code": "class Solution:\n def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:\n res = []\n temp = []\n tempLength = 0\n\n for word in words:\n if tempLength + len(temp) + len(word) <= maxWidth:\n temp.append(word)\n temp...
48
[ { "code": "import re\nclass Solution:\n def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:\n # Words must have at minimum 1 space inbetween\n res = []\n curline = []\n curlinelen = 0\n n = len(words)\n for word in words: # Not last line\n ...
10
69
sqrtx
<p>Given a non-negative integer <code>x</code>, return <em>the square root of </em><code>x</code><em> rounded down to the nearest integer</em>. The returned integer should be <strong>non-negative</strong> as well.</p> <p>You <strong>must not use</strong> any built-in exponent function or operator.</p> <ul> <li>For e...
39.354226
Easy
[ "math", "binary-search" ]
{"lang": "python3", "distribution": [["24", 23.8811], ["44", 54.7945], ["65", 0.9332999999999999], ["85", 0.15959999999999996], ["105", 0.1178], ["126", 0.09879999999999999], ["146", 0.0342], ["167", 0.06459999999999999], ["187", 0.04939999999999999], ["207", 0.045599999999999995], ["228", 0.053200000000000004], ["248"...
{"lang": "python3", "distribution": [["15900", 0.0076], ["16000", 0.0152], ["16100", 0.0721], ["16200", 0.5879], ["16300", 7.4192], ["16400", 26.8131], ["16500", 45.6152]]}
[ { "code": "class Solution:\n def mySqrt(self, x: int) -> int:\n return int(x**0.5)", "runtime": "24" }, { "code": "class Solution:\n def mySqrt(self, x: int) -> int:\n if x == 0:\n return 0\n if x < 4:\n return 1\n left = 0\n right = x-1...
145
[ { "code": "class Solution:\n def mySqrt(self, x: int) -> int:\n r = x + 1\n\n while r*r > x:\n r = int((r + x/r) / 2)\n return r ", "memory": "15900" }, { "code": "class Solution:\n def mySqrt(self, x: int) -> int:\n l, r = 0, x\n res = 0\n ...
12
70
climbing-stairs
<p>You are climbing a staircase. It takes <code>n</code> steps to reach the top.</p> <p>Each time you can either climb <code>1</code> or <code>2</code> steps. In how many distinct ways can you climb to the top?</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> n = 2 <...
53.121319
Easy
[ "math", "dynamic-programming", "memoization" ]
{"lang": "python3", "distribution": [["12", 0.0039], ["13", 0.0039], ["14", 0.0039], ["15", 0.0409], ["16", 0.0507], ["17", 0.0214], ["18", 0.1383], ["19", 0.152], ["20", 0.3234], ["21", 0.2221], ["22", 0.6897], ["23", 0.6039], ["24", 1.3111], ["25", 1.0579], ["26", 2.4255], ["27", 1.7592], ["28", 3.8048], ["29", 3.964...
{"lang": "python3", "distribution": [["15000", 0.0019], ["15800", 0.0039], ["16000", 0.0097], ["16100", 0.0409], ["16200", 0.6234], ["16300", 9.0999], ["16400", 28.9071], ["16500", 47.8999]]}
[ { "code": "class Solution:\n def climbStairs(self, n: int) -> int:\n \n ways = [1,2]\n for i in range(2,n):\n ways.append(ways[i-1] + ways[i-2])\n \n return ways[n-1]\n\n", "runtime": "12" }, { "code": "class Solution:\n def climbStairs(self, n...
53
[ { "code": "class Solution:\n def climbStairs(self, n: int) -> int:\n if n <= 3: return n \n\n n_minus_one = 3\n n_minus_two = 2\n curr = 0\n\n for _ in range(3, n): \n curr = n_minus_one + n_minus_two\n n_minus_two = n_minus_one\n n_minus_on...
13
51
n-queens
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <st...
69.973633
Hard
[ "array", "backtracking" ]
{"lang": "python3", "distribution": [["32", 1.2712999999999999], ["40", 10.583099999999998], ["47", 28.995199999999997], ["54", 15.264199999999997], ["62", 7.607099999999999], ["69", 7.294199999999999], ["77", 7.072100000000001], ["84", 5.1253], ["92", 2.9259], ["99", 1.5537999999999998], ["106", 1.1198], ["114", 0.877...
{"lang": "python3", "distribution": [["16500", 0.0101], ["16600", 0.0605], ["16700", 0.3733], ["16800", 3.7429], ["16900", 15.0626], ["17000", 29.9334], ["17100", 34.9274], ["17200", 9.655]]}
[ { "code": "class Solution:\n def solveNQueens(self, n: int) -> List[List[str]]:\n def createBoard(state):\n board = []\n for row in state:\n board.append(''.join(row))\n return board\n\n def backtrack(row, cols, diags, antiDiags, state):\n ...
123
[ { "code": "class Solution:\n\n def solveNQueens(self, n: int) -> List[List[str]]:\n self.vis = [[0] * (2 * n) for i in range(3)]\n self.ans = []\n self.mapping = [\".\" * i + \"Q\" + \".\" * (n - 1 - i) for i in range(n)]\n\n def cal(x, y):\n return y, x - y + n, y + x\...
10
52
n-queens-ii
<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p> <p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p> <p>&nbsp;</p...
74.939941
Hard
[ "backtracking" ]
{"lang": "python3", "distribution": [["20", 0.2494], ["23", 0.3628], ["26", 0.6801], ["29", 1.8363], ["32", 3.3779000000000003], ["35", 5.9623], ["38", 8.7055], ["41", 11.879299999999999], ["44", 12.786200000000001], ["47", 9.7483], ["50", 6.0077], ["53", 3.0831999999999997], ["56", 2.1083], ["60", 2.3577], ["63", 2.92...
{"lang": "python3", "distribution": [["16000", 0.0227], ["16100", 0.0453], ["16200", 0.1134], ["16300", 3.2192], ["16400", 18.1818], ["16500", 33.3711], ["16600", 25.5271], ["16700", 5.1689], ["16800", 3.786], ["16900", 3.2646], ["17000", 1.1109], ["17100", 0.5441]]}
[ { "code": "class Solution:\n def totalNQueens(self, n: int) -> int:\n if n==1:\n return 1\n if n == 2 or n == 3:\n return 0\n if n == 5:\n return 10\n if n == 6:\n return 4\n if n == 7:\n return 40 \n if n == 8:\...
131
[ { "code": "class Solution:\n def totalNQueens(self, n: int) -> int:\n def check(queens: list[int]):\n m = len(queens)\n return (\n len(set(queens)) == m\n and (len(set(map(lambda t: t[0] + t[1], enumerate(queens)))) == m)\n and (len(se...
21
53
maximum-subarray
<p>Given an integer array <code>nums</code>, find the <span data-keyword="subarray-nonempty">subarray</span> with the largest sum, and return <em>its sum</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [-2,1,-3,4,-1,2,1,-5,4] <strong>Output:</strong> 6 <...
51.169537
Medium
[ "array", "divide-and-conquer", "dynamic-programming" ]
{"lang": "python3", "distribution": [["65", 0.0115], ["71", 0.0184], ["78", 0.041400000000000006], ["85", 0.0161], ["91", 0.0138], ["98", 0.0023], ["105", 0.0069], ["111", 0.0138], ["118", 0.0207], ["125", 0.0138], ["131", 0.0092], ["138", 0.0023], ["145", 0.0069], ["151", 0.0069], ["158", 0.0046], ["165", 0.0069], ["1...
{"lang": "python3", "distribution": [["25400", 0.0046], ["25500", 0.0046], ["25600", 0.0023], ["25700", 0.0046], ["25800", 0.0069], ["25900", 0.0023], ["26000", 0.0046], ["26100", 0.0092], ["26200", 0.0574], ["26300", 0.0873], ["26400", 0.0115], ["27300", 0.0023], ["27700", 0.0023], ["27800", 0.0069], ["27900", 0.0046]...
[ { "code": "def solve():\n f = open('user.out', 'w')\n for nums in map(loads, stdin):\n maxSum, curSum = nums[0], nums[0]\n for num in nums[1:]:\n if curSum < 0:\n curSum = num\n else:\n curSum += num\n if curSum > maxSum:\n ...
130
[ { "code": "def maxSubArray(nums: List[int]) -> int:\n dp = [0] * len(nums)\n ans = float('-inf')\n for i, el in enumerate(nums):\n dp[i] = max(el, el + dp[i-1])\n ans = max(dp[i], ans)\n return ans\n\nf = open('user.out', 'w')\nfor case in map(loads, stdin):\n f.write(f\"{maxSubArra...
54
54
spiral-matrix
<p>Given an <code>m x n</code> <code>matrix</code>, return <em>all elements of the</em> <code>matrix</code> <em>in spiral order</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spiral1.jpg" style="width: 242px; height: 242px;" />...
51.285346
Medium
[ "array", "matrix", "simulation" ]
{"lang": "python3", "distribution": [["12", 0.0041], ["14", 0.0083], ["15", 0.0248], ["16", 0.0165], ["17", 0.0207], ["18", 0.091], ["19", 0.153], ["20", 0.1199], ["21", 0.1737], ["22", 0.6246], ["23", 0.7486], ["24", 0.6204], ["25", 0.9513], ["26", 2.552], ["27", 2.2128], ["28", 1.6379], ["29", 2.916], ["30", 5.8527],...
{"lang": "python3", "distribution": [["16000", 0.0041], ["16100", 0.0248], ["16200", 0.1241], ["16300", 2.6099], ["16400", 18.5507], ["16500", 35.3807], ["16600", 35.8523]]}
[ { "code": "class Solution:\n def spiralOrder(self, matrix: List[List[int]]) -> List[int]:\n moves = [[0, 1], [1, 0], [0, -1], [-1, 0]]\n visited = set()\n arr = []\n rows, cols = len(matrix), len(matrix[0])\n x = y = 0\n direction = 0\n for _ in range(rows*col...
49
[ { "code": "class Solution:\n def spiralOrder(self, matrix: List[List[int]]) -> List[int]:\n m = len(matrix)\n n = len(matrix[0])\n\n result = []\n i,j = 0,0\n direction = 0 # 0:right, 1:down, 2:left, 3:up\n right_bound = n - 1\n down_bound = m-1\n left_...
11
55
jump-game
<p>You are given an integer array <code>nums</code>. You are initially positioned at the array&#39;s <strong>first index</strong>, and each element in the array represents your maximum jump length at that position.</p> <p>Return <code>true</code><em> if you can reach the last index, or </em><code>false</code><em> othe...
38.777051
Medium
[ "array", "dynamic-programming", "greedy" ]
{"lang": "python3", "distribution": [["88", 0.14619999999999994], ["165", 0.0028], ["242", 0.0028], ["318", 46.6343], ["395", 33.329800000000006], ["472", 1.2564999999999997], ["549", 0.5696000000000003], ["626", 0.17689999999999992], ["702", 0.0308], ["779", 0.08959999999999994], ["856", 0.04489999999999999], ["933", ...
{"lang": "python3", "distribution": [["16700", 0.0028], ["17000", 0.0113], ["17100", 0.0254], ["17200", 0.1158], ["17300", 0.1158], ["17400", 0.2795], ["17500", 1.8014], ["17600", 8.4115], ["17700", 22.2301], ["17800", 32.0222], ["17900", 23.7859], ["18000", 2.9874], ["18100", 0.4772], ["18200", 0.2852], ["18300", 0.25...
[ { "code": "# class Solution:\n# def canJump(self, nums: List[int]) -> bool:\n# maax=0\n# for i,jump in enumerate(nums):\n# if i>maax:\n# return False\n# maax=max(maax,i+jump)\n# return maax>=len(nums)-1\nclass Solution:\n def canJump(nums: L...
150
[ { "code": "class Solution:\n def canJump(self, nums: List[int]) -> bool:\n maxReach = 0 # Initialize the maximum reachable index\n \n for i in range(len(nums)):\n if i > maxReach:\n # If we are at an index we can't reach, return False\n return Fa...
45
56
merge-intervals
<p>Given an array&nbsp;of <code>intervals</code>&nbsp;where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>, merge all overlapping intervals, and return <em>an array of the non-overlapping intervals that cover all the intervals in the input</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:...
47.951953
Medium
[ "array", "sorting" ]
{"lang": "python3", "distribution": [["34", 0.0025], ["38", 0.005], ["39", 0.0025], ["40", 0.0025], ["41", 0.0076], ["42", 0.0126], ["43", 0.0025], ["44", 0.0126], ["45", 0.0025], ["47", 0.0076], ["48", 0.0126], ["49", 0.0025], ["50", 0.0151], ["51", 0.0151], ["52", 0.005], ["53", 0.0076], ["54", 0.005], ["56", 0.0076]...
{"lang": "python3", "distribution": [["19000", 0.0177], ["19100", 0.0404], ["19200", 0.0227], ["19300", 0.0101], ["19400", 0.005], ["19600", 0.0025], ["19700", 0.0151], ["19800", 0.0177], ["19900", 0.0429], ["20000", 0.0303], ["20100", 0.0328], ["20200", 0.3028], ["20300", 5.1167], ["20400", 13.5512], ["20500", 7.4101]...
[ { "code": "class Solution:\n def merge(intervals: List[List[int]]) -> List[List[int]]:\n n = len(intervals)\n i = 0\n intervals.sort(key=lambda e: e[0])\n res = [intervals[0]]\n \n for start, end in intervals[1:]:\n\n lastEnd = res[-1][1]\n if s...
98
[ { "code": "class Solution:\n def merge(self, intervals: List[List[int]]) -> List[List[int]]:\n intervals.sort(key=lambda interval: interval[0])\n merged = []\n\n for interval in intervals:\n if not merged or merged[-1][1] < interval[0]:\n merged.append(interval)...
36
57
insert-interval
<p>You are given an array of non-overlapping intervals <code>intervals</code> where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> represent the start and the end of the <code>i<sup>th</sup></code> interval and <code>intervals</code> is sorted in ascending order by <code>start<sub>i</sub></code>. You ...
42.170595
Medium
[ "array" ]
{"lang": "python3", "distribution": [["28", 0.0048], ["30", 0.0048], ["31", 0.0048], ["32", 0.0048], ["35", 0.0048], ["37", 0.0048], ["38", 0.0048], ["39", 0.0194], ["41", 0.0145], ["42", 0.0048], ["43", 0.0097], ["44", 0.0048], ["45", 0.0048], ["46", 0.0048], ["47", 0.0048], ["48", 0.0048], ["49", 0.0048], ["50", 0.00...
{"lang": "python3", "distribution": [["18100", 0.0291], ["18200", 0.0581], ["18300", 0.0097], ["18400", 0.0048], ["19200", 0.0048], ["19300", 0.0145], ["19400", 0.0824], ["19500", 0.0291], ["19600", 0.1114], ["19700", 1.2645], ["19800", 23.4291], ["19900", 59.0863], ["20000", 10.5857]]}
[ { "code": "# class Solution(object):\n# def insert(self, intervals, newInterval):\n# merged = []\n# i = 0\n\n# while i < len(intervals) and intervals[i][1] < newInterval[0]:\n# merged.append(intervals[i])\n# i += 1\n \n# while i < len(intervals)...
79
[ { "code": "class Solution:\n def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:\n if not intervals:\n return [newInterval]\n\n ans = []\n\n i = 0\n\n while i < len(intervals) and intervals[i][1] < newInterval[0]: \n ans.a...
19
58
length-of-last-word
<p>Given a string <code>s</code> consisting of words and spaces, return <em>the length of the <strong>last</strong> word in the string.</em></p> <p>A <strong>word</strong> is a maximal <span data-keyword="substring-nonempty">substring</span> consisting of non-space characters only.</p> <p>&nbsp;</p> <p><strong class=...
53.652213
Easy
[ "string" ]
{"lang": "python3", "distribution": [["7", 0.0026], ["11", 0.0053], ["13", 0.0026], ["14", 0.0105], ["15", 0.042], ["16", 0.0394], ["17", 0.0184], ["18", 0.1129], ["19", 0.1786], ["20", 0.2154], ["21", 0.1838], ["22", 0.7196], ["23", 0.6539], ["24", 1.082], ["25", 1.1162], ["26", 2.4267], ["27", 1.8778], ["28", 3.1909]...
{"lang": "python3", "distribution": [["16000", 0.0026], ["16100", 0.0053], ["16200", 0.0604], ["16300", 0.8956], ["16400", 11.545], ["16500", 29.3195], ["16600", 47.8005]]}
[ { "code": "class Solution:\n def lengthOfLastWord(self, s: str) -> int:\n i = len(s) - 1\n while s[i] == \" \":\n i -= 1\n x = i\n while x >= 0 and s[x] != \" \":\n x -= 1\n return i - x", "runtime": "7" }, { "code": "class Solution:\n d...
53
[ { "code": "class Solution:\n def lengthOfLastWord(self, s: str) -> int:\n trimmed_string = s.strip()\n list_of_words = trimmed_string.split(' ')\n return len(list_of_words[len(list_of_words)-1])", "memory": "16000" }, { "code": "class Solution:\n def lengthOfLastWord(self, s: st...
11
59
spiral-matrix-ii
<p>Given a positive integer <code>n</code>, generate an <code>n x n</code> <code>matrix</code> filled with elements from <code>1</code> to <code>n<sup>2</sup></code> in spiral order.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/spi...
72.130152
Medium
[ "array", "matrix", "simulation" ]
{"lang": "python3", "distribution": [["14", 0.0172], ["19", 0.1373], ["20", 0.0343], ["21", 0.2231], ["22", 0.3261], ["23", 0.8411], ["24", 0.3605], ["25", 0.8926], ["26", 1.5791], ["27", 2.1627], ["28", 1.253], ["29", 2.3859], ["30", 5.0635], ["31", 5.4926], ["32", 3.3986], ["33", 4.806], ["34", 9.3889], ["35", 8.4964...
{"lang": "python3", "distribution": [["16000", 0.0172], ["16200", 0.0858], ["16300", 1.4075], ["16400", 13.3196], ["16500", 33.1102], ["16600", 38.8774]]}
[ { "code": "from typing import List\n\nclass Solution:\n def generateMatrix(self, n: int) -> List[List[int]]:\n matrix = [[0] * n for _ in range(n)]\n num = 1\n left, right, up, down = 0, n - 1, 0, n - 1\n \n while left <= right and up <= down:\n # Move from left ...
43
[ { "code": "class Solution:\n def generateMatrix(self, n: int) -> List[List[int]]:\n out = [[0] * n for i in range(n)]\n x, y, dx, dy = 0, 0, 1, 0\n\n for counter in range(1, n ** 2 + 1):\n out[y][x] = counter\n\n # dx = 1, dy = 0\n # dx = 0, dy = 1\n ...
10
60
permutation-sequence
<p>The set <code>[1, 2, 3, ...,&nbsp;n]</code> contains a total of <code>n!</code> unique permutations.</p> <p>By listing and labeling all of the permutations in order, we get the following sequence for <code>n = 3</code>:</p> <ol> <li><code>&quot;123&quot;</code></li> <li><code>&quot;132&quot;</code></li> <li><co...
48.090107
Hard
[ "math", "recursion" ]
{"lang": "python3", "distribution": [["49", 81.87460000000004], ["117", 0.0732], ["186", 0.5491], ["254", 0.0732], ["323", 0.0366], ["392", 0.5124000000000001], ["460", 0.36600000000000005], ["529", 0.0366], ["597", 0.0366], ["666", 0.0366], ["734", 0.0366], ["803", 0.0366], ["871", 0.0366], ["940", 0.0366], ["1008", 0...
{"lang": "python3", "distribution": [["16000", 0.0732], ["16200", 0.1465], ["16300", 0.8788], ["16400", 7.3233], ["16500", 21.97], ["16600", 34.0901], ["16700", 21.2743], ["16800", 3.5884], ["16900", 0.2929], ["17000", 0.0366], ["17100", 0.2563], ["17200", 0.0366], ["19100", 0.0366], ["19400", 0.0732], ["19500", 0.1098...
[ { "code": "class Solution:\n def getPermutation(self, n: int, k: int) -> str:\n factorials = [1] * n\n for i in range(1, n):\n factorials[i] = factorials[i-1] * i\n \n k -= 1\n numbers = list(range(1, n + 1))\n permutation = []\n\n for i in range(n, 0, ...
119
[ { "code": "class Solution:\n def getPermutation(self, n: int, k: int) -> str:\n rawlist = [i for i in range(1, n+1)]\n perms = itertools.permutations(rawlist)\n \n kth = \"\"\n \n for i in range(k):\n kth =''.join(map(str, perms.__next__())) \n \n ...
48
278
first-bad-version
<p>You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.</p> <p>Suppose you have <code>n</code> version...
44.878675
Easy
[ "binary-search", "interactive" ]
{"lang": "python3", "distribution": [["11", 0.0071], ["14", 0.0071], ["15", 0.0428], ["16", 0.0571], ["18", 0.157], ["19", 0.2568], ["20", 0.4138], ["21", 0.1641], ["22", 0.7063], ["23", 0.6778], ["24", 1.4697], ["25", 1.1701], ["26", 2.3045], ["27", 1.9763], ["28", 3.8456], ["29", 4.0168], ["30", 5.0942], ["31", 3.488...
{"lang": "python3", "distribution": [["15900", 0.0071], ["16100", 0.0285], ["16200", 0.4067], ["16300", 7.3773], ["16400", 29.8445], ["16500", 47.2103]]}
[ { "code": "# The isBadVersion API is already defined for you.\n# def isBadVersion(version: int) -> bool:\n\nclass Solution:\n def firstBadVersion(self, n: int) -> int:\n\n l = 1\n r = n\n\n while(l <= r):\n\n mid = (l+r) // 2\n flag = isBadVersion(mid)\n\n ...
51
[ { "code": "# The isBadVersion API is already defined for you.\n# def isBadVersion(version: int) -> bool:\n\nclass Solution:\n def firstBadVersion(self, n: int) -> int:\n l,r = 0, n\n\n while l <= r:\n mid = (l + r) // 2\n \n\n if isBadVersion(mid) == False:\n ...
8
279
perfect-squares
<p>Given an integer <code>n</code>, return <em>the least number of perfect square numbers that sum to</em> <code>n</code>.</p> <p>A <strong>perfect square</strong> is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, <code>1</code>, <code>4</code>,...
55.105347
Medium
[ "math", "dynamic-programming", "breadth-first-search" ]
{"lang": "python3", "distribution": [["48", 3.2256000000000005], ["107", 3.3024000000000022], ["166", 3.8976000000000006], ["225", 0.9983999999999998], ["285", 0.4799999999999999], ["344", 0.5184], ["403", 0.8831999999999999], ["462", 0.6335999999999999], ["521", 0.5952], ["580", 0.7295999999999999], ["639", 0.42239999...
{"lang": "python3", "distribution": [["16186", 5.819], ["16560", 47.7817], ["16934", 13.3474], ["17308", 3.649], ["17681", 4.2059], ["18055", 1.7093], ["18429", 0.8066], ["18803", 0.6721999999999999], ["19176", 1.0371], ["19550", 0.6720999999999999], ["19924", 0.1344], ["20298", 0.48], ["20671", 0.192], ["21045", 0.384...
[ { "code": "import gc\ngc.disable()\nfrom os import _exit\nimport zlib, base64\n\nl = zlib.decompress(base64.a85decode( b'GQDZL$[*IL$j=lja7hgQG#\\\\ViV9M-)3!B\\'_C+MGbs8;]gS(I+)hYq-coCJ1-mr+,`B4jFtqJ66/FX*Kii)7dAF9_B\\\\.N_XZ&T\"L:p#SaeJ+U\\'Cdlt6s^unFAMr6el;jJmMr9uUOJsKcKbT#X<;.<j9UX-Kt[n_s\"a?.Ajs3?!1>;&uSp<Br...
153
[ { "code": "class Solution:\n def numSquares(self, n: int) -> int:\n dp = [n] * (n + 1)\n\n dp[0] = 0\n\n for target in range(1, n + 1):\n for s in range(1, target + 1):\n square = s * s\n if target - square < 0:\n break\n ...
144
282
expression-add-operators
<p>Given a string <code>num</code> that contains only digits and an integer <code>target</code>, return <em><strong>all possibilities</strong> to insert the binary operators </em><code>&#39;+&#39;</code><em>, </em><code>&#39;-&#39;</code><em>, and/or </em><code>&#39;*&#39;</code><em> between the digits of </em><code>nu...
40.28312
Hard
[ "math", "string", "backtracking" ]
{"lang": "python3", "distribution": [["104", 0.26510000000000006], ["196", 0.0442], ["288", 0.7954000000000002], ["380", 7.1979], ["472", 36.381900000000016], ["564", 23.22500000000003], ["656", 6.624800000000004], ["748", 3.2247999999999997], ["840", 0.8838000000000003], ["932", 0.5745000000000001], ["1024", 3.534], [...
{"lang": "python3", "distribution": [["16400", 0.0442], ["16500", 0.1766], ["16600", 0.0442], ["16700", 0.1325], ["16800", 0.2208], ["16900", 0.2649], ["17000", 3.1347], ["17100", 21.9868], ["17200", 37.1302], ["17300", 23.5762], ["17400", 3.5762], ["17500", 1.2362], ["17600", 0.0883], ["17700", 0.2208], ["17800", 0.17...
[ { "code": "from typing import List\n\nclass Solution:\n def __init__(self):\n self.known_ao = {}\n self.known_mul = {}\n\n def check_multiplication(self, num, target):\n if len(num) < len(str(target)):\n return []\n\n if len(num) == 1:\n return [num] if in...
133
[ { "code": "class Solution:\n def addOperators(self, num: str, target: int) -> List[str]:\n def helper(index,target,ans,powerset,n):\n # print(ans,target,index)\n if index>=n:\n if eval(ans)==target:\n powerset.append(ans[2:].strip())\n ...
80
283
move-zeroes
<p>Given an integer array <code>nums</code>, move all <code>0</code>&#39;s to the end of it while maintaining the relative order of the non-zero elements.</p> <p><strong>Note</strong> that you must do this in-place without making a copy of the array.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p...
62.093273
Easy
[ "array", "two-pointers" ]
{"lang": "python3", "distribution": [["48", 0.11599999999999999], ["74", 0.0168], ["100", 5.2010000000000005], ["126", 75.29260000000001], ["151", 2.4711999999999996], ["177", 0.14730000000000004], ["203", 0.1376], ["229", 0.07709999999999999], ["254", 0.033699999999999994], ["280", 0.023999999999999997], ["306", 0.021...
{"lang": "python3", "distribution": [["17100", 0.0048], ["17200", 0.0121], ["17300", 0.0145], ["17400", 0.0242], ["17500", 0.0169], ["17600", 0.0242], ["17700", 0.0991], ["17800", 0.3796], ["17900", 3.9873], ["18000", 36.6162], ["18100", 37.2788]]}
[ { "code": "import os\n\nprint(os.listdir('./data'))\n\n\nfout = open('user.out', 'w')\n\nfor nums in map(loads, sys.stdin):\n N = len(nums)\n rv = [_ for _ in nums if _ != 0]\n rv.extend(\n [0] * (N - len(rv))\n )\n print(\n dumps(rv).replace(' ', ''), \n file=fout\n )\n\n...
153
[ { "code": "class Solution:\n def moveZeroes(self, nums: List[int]) -> None:\n \"\"\"\n Do not return anything, modify nums in-place instead.\n \"\"\"\n\nwith open('user.out', 'w') as f:\n for nums in map(loads, sys.stdin):\n left = 0\n for right in range(len(nums)):\n ...
16
284
peeking-iterator
<p>Design an iterator that supports the <code>peek</code> operation on an existing iterator in addition to the <code>hasNext</code> and the <code>next</code> operations.</p> <p>Implement the <code>PeekingIterator</code> class:</p> <ul> <li><code>PeekingIterator(Iterator&lt;int&gt; nums)</code> Initializes the object...
59.894193
Medium
[ "array", "design", "iterator" ]
{"lang": "python3", "distribution": [["18", 0.0943], ["19", 0.1887], ["21", 0.1887], ["22", 0.3774], ["23", 0.8491], ["24", 0.6604], ["25", 1.1321], ["26", 1.4151], ["27", 3.3962], ["28", 1.5094], ["29", 1.9811], ["30", 4.0566], ["31", 5.8491], ["32", 2.9245], ["33", 2.8302], ["34", 6.6981], ["35", 8.7736], ["36", 6.69...
{"lang": "python3", "distribution": [["16400", 0.0943], ["16500", 0.1887], ["16600", 9.0566], ["16700", 30.9434], ["16800", 47.1698]]}
[ { "code": "# Below is the interface for Iterator, which is already defined for you.\n#\n# class Iterator:\n# def __init__(self, nums):\n# \"\"\"\n# Initializes an iterator object to the beginning of a list.\n# :type nums: List[int]\n# \"\"\"\n#\n# def hasNext(self):\n# ...
32
[ { "code": "# Below is the interface for Iterator, which is already defined for you.\n#\n# class Iterator:\n# def __init__(self, nums):\n# \"\"\"\n# Initializes an iterator object to the beginning of a list.\n# :type nums: List[int]\n# \"\"\"\n#\n# def hasNext(self):\n# ...
5
287
find-the-duplicate-number
<p>Given an array of integers <code>nums</code> containing&nbsp;<code>n + 1</code> integers where each integer is in the range <code>[1, n]</code> inclusive.</p> <p>There is only <strong>one repeated number</strong> in <code>nums</code>, return <em>this&nbsp;repeated&nbsp;number</em>.</p> <p>You must solve the proble...
61.571378
Medium
[ "array", "two-pointers", "binary-search", "bit-manipulation" ]
{"lang": "python3", "distribution": [["78", 0.0183], ["84", 0.0061], ["91", 0.0608], ["97", 0.0488], ["103", 0.0122], ["110", 0.0061], ["116", 0.0061], ["123", 0.0061], ["129", 0.0061], ["135", 0.0243], ["142", 0.0061], ["148", 0.0365], ["155", 0.0486], ["161", 0.0425], ["167", 0.0243], ["174", 0.0365], ["180", 0.0547]...
{"lang": "python3", "distribution": [["25679", 0.0183], ["25838", 0.0122], ["25996", 0.0182], ["26155", 0.0365], ["26314", 0.1945], ["26473", 0.0182], ["26631", 0.0061], ["26790", 0.0061], ["26949", 0.0365], ["27108", 0.0182], ["27266", 0.0425], ["27425", 0.0122], ["27584", 0.0547], ["27743", 0.4862], ["27901", 0.8388]...
[ { "code": "sys.stdout = open('user.out', 'w')\nfor nums in map(loads, stdin):\n print(Counter(nums).most_common(1)[0][0])\n", "runtime": "78" }, { "code": "class Solution:\n def findDuplicate(self, nums: List[int]) -> int:\n left, right = nums[0], nums[0]\n while True:\n ...
125
[ { "code": "class Solution:\n def findDuplicate(self, nums: List[int]) -> int:\n left, right = nums[0], nums[0]\n while True:\n left, right = nums[left], nums[nums[right]]\n if left == right:\n left = nums[0]\n break\n while left != righ...
98
306
additive-number
<p>An <strong>additive number</strong> is a string whose digits can form an <strong>additive sequence</strong>.</p> <p>A valid <strong>additive sequence</strong> should contain <strong>at least</strong> three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the prece...
31.969264
Medium
[ "string", "backtracking" ]
{"lang": "python3", "distribution": [["20", 0.2639], ["22", 0.5277], ["23", 0.5277], ["24", 0.5277], ["25", 1.1873], ["26", 1.9789], ["27", 1.847], ["28", 1.4512], ["29", 2.3747], ["30", 4.8813], ["31", 4.7493], ["32", 3.562], ["33", 5.409], ["34", 8.8391], ["35", 6.7282], ["36", 5.1451], ["37", 7.3879], ["38", 8.7071]...
{"lang": "python3", "distribution": [["16300", 2.9024], ["16400", 21.8997], ["16500", 36.0158], ["16600", 32.4538]]}
[ { "code": "class Solution:\n\n def isAdditiveNumber(self, num: str) -> bool:\n\n n = len(num)\n\n def dfs(firstNum: int, secondNum: int, s: int) -> bool:\n\n if s == len(num):\n\n return True\n\n thirdNum = firstNum + secondNum\n\n thirdNumStr = str(thirdNum)\n\n return (num.fi...
32
[ { "code": "class Solution:\n def isAdditiveNumber(self, num: str) -> bool:\n seq = []\n def backtrack(num: str, cur: List[int]):\n if not num:\n return len(cur) >= 3\n\n for i in range(1,len(num)+1):\n if i > 1 and num[0] == \"0\": break\n ...
7
318
maximum-product-of-word-lengths
<p>Given a string array <code>words</code>, return <em>the maximum value of</em> <code>length(word[i]) * length(word[j])</code> <em>where the two words do not share common letters</em>. If no such two words exist, return <code>0</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <stron...
60.211545
Medium
[ "array", "string", "bit-manipulation" ]
{"lang": "python3", "distribution": [["172", 3.765699999999999], ["259", 8.894100000000002], ["346", 28.846400000000024], ["433", 4.166299999999997], ["521", 2.3234], ["608", 0.7210000000000001], ["695", 1.7625000000000008], ["782", 8.0127], ["869", 5.047599999999997], ["956", 3.765599999999998], ["1043", 1.92280000000...
{"lang": "python3", "distribution": [["18700", 0.0801], ["18900", 0.0801], ["19300", 0.2404], ["19500", 0.3205], ["19600", 4.6474], ["19700", 32.9327], ["19800", 24.1987], ["19900", 5.8494], ["20000", 11.9391], ["20100", 12.9006]]}
[ { "code": "class Solution:\n def maxProduct(self, words: List[str]) -> int:\n def mask(w):\n ret = 0\n for c in set(w):\n b = ord(c)-ord('a')\n ret |= 1<<b\n return ret\n lens = defaultdict(int)\n for w in words:\n ...
132
[ { "code": "class Solution:\n def maxProduct(self, words: List[str]) -> int:\n \n n = len(words)\n res = 0\n for i in range(n):\n for j in range(i+1,n):\n if len(set(words[i]) & set(words[j])) == 0:\n res = max(res, len(words[i]) * len(w...
16
319
bulb-switcher
<p>There are <code>n</code> bulbs that are initially off. You first turn on all the bulbs, then&nbsp;you turn off every second bulb.</p> <p>On the third round, you toggle every third bulb (turning on if it&#39;s off or turning off if it&#39;s on). For the <code>i<sup>th</sup></code> round, you toggle every <code>i</co...
53.114802
Medium
[ "math", "brainteaser" ]
{"lang": "python3", "distribution": [["16", 0.0495], ["17", 0.0495], ["18", 0.297], ["19", 0.099], ["20", 0.297], ["21", 0.099], ["22", 0.7426], ["23", 0.6436], ["24", 1.6337], ["25", 1.1386], ["26", 3.0693], ["27", 1.7327], ["28", 3.9109], ["29", 3.9109], ["30", 4.703], ["31", 4.2079], ["32", 7.2772], ["33", 7.8713], ...
{"lang": "python3", "distribution": [["16100", 0.099], ["16200", 0.5941], ["16300", 11.7822], ["16400", 32.6238], ["16500", 46.6337]]}
[ { "code": "class Solution:\n def bulbSwitch(self, n: int) -> int:\n return int(math.sqrt(n))", "runtime": "16" }, { "code": "class Solution:\n def bulbSwitch(self, n: int) -> int:\n return int(n**0.5)", "runtime": "17" }, { "code": "class Solution:\n def bulbSwitch...
39
[ { "code": "class Solution:\n def bulbSwitch(self, n: int) -> int:\n return int(math.sqrt(n))", "memory": "16100" }, { "code": "class Solution:\n def bulbSwitch(self, n: int) -> int:\n # bulbs = [0]*n\n # for i in range(1, n+1):\n # if i==1:\n # bu...
8
321
create-maximum-number
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> of lengths <code>m</code> and <code>n</code> respectively. <code>nums1</code> and <code>nums2</code> represent the digits of two numbers. You are also given an integer <code>k</code>.</p> <p>Create the maximum number of length <code>k &lt;= ...
30.773804
Hard
[ "array", "two-pointers", "stack", "greedy", "monotonic-stack" ]
{"lang": "python3", "distribution": [["103", 0.4866], ["117", 0.2433], ["131", 0.2433], ["145", 5.8392], ["159", 7.542400000000001], ["173", 18.734700000000004], ["187", 4.3794], ["201", 3.8928000000000003], ["216", 3.6495000000000006], ["230", 3.1629], ["244", 2.1896999999999998], ["258", 6.5691], ["272", 3.4062000000...
{"lang": "python3", "distribution": [["16500", 0.7299], ["16600", 0.9732], ["16700", 10.7056], ["16800", 25.5474], ["16900", 33.09], ["17000", 14.3552], ["17100", 3.8929], ["17200", 0.9732], ["17300", 0.2433], ["18400", 0.2433], ["18700", 0.7299], ["18800", 0.4866], ["18900", 0.4866], ["19000", 0.4866], ["19200", 0.243...
[ { "code": "import bisect\nimport collections as cc\n\nclass Solution:\n def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]:\n nums = [nums1, nums2]\n \n def generateMaxPos(aid: int) -> List[List[int]]:\n ret = [[] for _ in range(10)]\n for ...
76
[ { "code": "class Solution:\n def maxNumber(self, nums1: List[int], nums2: List[int], k: int) -> List[int]:\n output = []\n\n def get_monotonic_stack(nums, upper_bound):\n stack = []\n\n n = len(nums)\n for idx, num in enumerate(nums):\n remain = n...
28
322
coin-change
<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p> <p>Return <em>the fewest number of coins that you need to make up that amount</em>. If that amount of money cannot be made up by any combinatio...
44.813918
Medium
[ "array", "dynamic-programming", "breadth-first-search" ]
{"lang": "python3", "distribution": [["41", 0.1489], ["61", 0.04170000000000001], ["81", 0.0035], ["100", 0.0035], ["120", 0.0035], ["140", 0.0035], ["160", 0.007], ["179", 0.0105], ["199", 0.0139], ["219", 0.0245], ["238", 0.0104], ["258", 0.0555], ["278", 0.10750000000000003], ["298", 0.1797], ["317", 0.3007999999999...
{"lang": "python3", "distribution": [["16301", 1.012], ["16703", 60.6776], ["17106", 5.654], ["17508", 3.0498000000000003], ["17911", 0.4282], ["18313", 1.2365], ["18716", 1.2814], ["19118", 2.176], ["19521", 1.2294999999999998], ["19923", 0.5249999999999999], ["20326", 0.7289], ["20728", 0.5595], ["21131", 0.4006], ["...
[ { "code": "class Solution:\n def coinChange(self, coins: List[int], amount: int) -> int:\n if amount == 0: return 0\n dp = {}\n coins.sort(reverse=True)\n def makeChange(curr, coinHigh, coinsUsed):\n if dp.get(curr) == -1: return\n if dp.get(0, amount) < (cur...
146
[ { "code": "class Solution:\n def coinChange(self, coins: List[int], amount: int) -> int:\n dp = {0: 0}\n for i in range(1,amount+1):\n for coin in coins:\n if i - coin in dp and i not in dp:\n dp[i] = 1 + dp[i-coin]\n elif i-coin in dp...
154
324
wiggle-sort-ii
<p>Given an integer array <code>nums</code>, reorder it such that <code>nums[0] &lt; nums[1] &gt; nums[2] &lt; nums[3]...</code>.</p> <p>You may assume the input array always has a valid answer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,5,1,1,6,4] <s...
34.780338
Medium
[ "array", "divide-and-conquer", "greedy", "sorting", "quickselect" ]
{"lang": "python3", "distribution": [["112", 23.5427], ["128", 55.9418], ["143", 6.726199999999998], ["159", 2.0178000000000003], ["174", 1.4573], ["189", 0.4484], ["205", 0.4484], ["220", 0.2242], ["236", 0.33630000000000004], ["251", 0.2242], ["267", 0.1121], ["282", 0.1121], ["297", 0.1121], ["313", 0.1121], ["328",...
{"lang": "python3", "distribution": [["18900", 0.1121], ["19000", 0.1121], ["19100", 0.1121], ["19200", 1.1211], ["19300", 16.3677], ["19400", 37.8924], ["19500", 19.2825], ["19600", 8.0717], ["19700", 3.4753], ["19800", 3.6996], ["19900", 2.13], ["20000", 1.4574], ["20100", 0.8969]]}
[ { "code": "class Solution:\n def wiggleSort(self, nums: List[int]) -> None:\n \"\"\"\n Do not return anything, modify nums in-place instead.\n \"\"\"\n nums.sort()\n half = len(nums[::2])\n nums[::2], nums[1::2] = nums[:half][::-1], nums[half:][::-1]", "runtime":...
52
[ { "code": "class Solution:\n def wiggleSort(self, nums: List[int]) -> None:\n \"\"\"\n Do not return anything, modify nums in-place instead.\n \"\"\"\n nums.sort() # in place Nlog(N)\n N = len(nums)\n medianidx = N // 2 # \n if N % 2 == 0:\n nums[:...
13
326
power-of-three
<p>Given an integer <code>n</code>, return <em><code>true</code> if it is a power of three. Otherwise, return <code>false</code></em>.</p> <p>An integer <code>n</code> is a power of three, if there exists an integer <code>x</code> such that <code>n == 3<sup>x</sup></code>.</p> <p>&nbsp;</p> <p><strong class="example"...
47.053169
Easy
[ "math", "recursion" ]
{"lang": "python3", "distribution": [["30", 0.0728], ["31", 0.0728], ["32", 0.0182], ["33", 0.0728], ["34", 0.0546], ["35", 0.2002], ["36", 0.2184], ["37", 0.1092], ["38", 0.2548], ["39", 0.4914], ["41", 0.3822], ["42", 0.2912], ["43", 0.4914], ["44", 0.9647], ["45", 1.0739], ["46", 0.9101], ["47", 1.2559], ["48", 1.80...
{"lang": "python3", "distribution": [["15800", 0.0182], ["16000", 0.0182], ["16100", 0.0182], ["16200", 0.1274], ["16300", 0.6553], ["16400", 8.9916], ["16500", 39.6432], ["16600", 43.0288]]}
[ { "code": "class Solution:\n def isPowerOfThree(self, n: int) -> bool:\n flag = True\n if n == 0:\n return False\n while flag:\n if n == 1:\n return True\n if n % 3 != 0:\n return False\n n = n // 3\n", "runtim...
116
[ { "code": "class Solution:\n def isPowerOfThree(self, n: int) -> bool:\n start = 0\n end = 93\n\n while start <= end:\n mid = (start + end) // 2\n\n if 3**mid == n:\n return True\n elif 3**mid < n:\n start = mid + 1\n ...
11
327
count-of-range-sum
<p>Given an integer array <code>nums</code> and two integers <code>lower</code> and <code>upper</code>, return <em>the number of range sums that lie in</em> <code>[lower, upper]</code> <em>inclusive</em>.</p> <p>Range sum <code>S(i, j)</code> is defined as the sum of the elements in <code>nums</code> between indices <...
36.155822
Hard
[ "array", "binary-search", "divide-and-conquer", "binary-indexed-tree", "segment-tree", "merge-sort", "ordered-set" ]
{"lang": "python3", "distribution": [["1110", 1.0505], ["1178", 3.3615000000000004], ["1246", 7.983699999999996], ["1315", 13.656100000000006], ["1383", 7.1434], ["1451", 3.9919000000000007], ["1520", 1.8908999999999998], ["1588", 3.781800000000001], ["1656", 5.672600000000001], ["1725", 3.991900000000001], ["1793", 3....
{"lang": "python3", "distribution": [["30033", 20.588400000000004], ["31701", 31.9331], ["33368", 1.0505], ["35036", 0.8404], ["36703", 0.6303000000000001], ["38371", 0.6303000000000001], ["40038", 2.5211000000000006], ["41706", 0.6303000000000001], ["43373", 0.2101], ["45041", 0.4202], ["46708", 0.2101], ["48376", 0.4...
[ { "code": "from sortedcontainers import SortedList\n\nclass Solution:\n def countRangeSum(self, nums: List[int], lower: int, upper: int) -> int:\n st = SortedList([])\n l = len(nums)\n p = nums.copy()\n for i in range(1, l):\n p[i] += p[i - 1]\n ans = 0\n ...
98
[ { "code": "class Solution:\n def countRangeSum(self, nums: list[int], lower: int, upper: int) -> int:\n prefix_sums = [0] \n for num in nums:\n prefix_sums.append(prefix_sums[-1] + num)\n def count_while_sorting(prefix_sums, start, end):\n if end - start <= 1:\n ...
37
313
super-ugly-number
<p>A <strong>super ugly number</strong> is a positive integer whose prime factors are in the array <code>primes</code>.</p> <p>Given an integer <code>n</code> and an array of integers <code>primes</code>, return <em>the</em> <code>n<sup>th</sup></code> <em><strong>super ugly number</strong></em>.</p> <p>The <code>n<s...
45.355986
Medium
[ "array", "math", "dynamic-programming" ]
{"lang": "python3", "distribution": [["152", 2.4191], ["197", 7.3616], ["242", 0.7363999999999999], ["287", 6.940600000000002], ["333", 5.889200000000001], ["378", 0.3155], ["423", 0.2103], ["468", 8.728300000000003], ["513", 2.1034999999999995], ["558", 1.1569999999999998], ["603", 0.8415999999999999], ["648", 0.3156]...
{"lang": "python3", "distribution": [["23436", 58.88610000000001], ["33110", 1.5777999999999996], ["42784", 0.6312], ["52458", 0.1052], ["62131", 0.1052], ["71805", 0.1052], ["81479", 0.1052], ["91153", 0.1052], ["100826", 0.1052], ["110500", 0.1052], ["120174", 0.1052], ["129848", 0.1052], ["139521", 0.1052], ["149195...
[ { "code": "class Solution:\n def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:\n ans = [1]\n ans_idx = [0]\n primes_idx = [0 for _ in range(len(primes))]\n pq = [(prime, idx) for idx, prime in enumerate(primes)]\n heapq.heapify(pq)\n for _ in range(n - ...
140
[ { "code": "class Solution:\n def nthSuperUglyNumber(self, n: int, primes: List[int]) -> int:\n ugly_numbers = [1]\n pointers = [0] * len(primes)\n\n while len(ugly_numbers) < n:\n next_ugly = min(ugly_numbers[pointers[i]] * primes[i] for i in range(len(primes)))\n u...
89
328
odd-even-linked-list
<p>Given the <code>head</code> of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return <em>the reordered list</em>.</p> <p>The <strong>first</strong> node is considered <strong>odd</strong>, and the <strong>second</strong> node is <strong>even</strong>...
61.618807
Medium
[ "linked-list" ]
{"lang": "python3", "distribution": [["17", 0.0095], ["19", 0.0285], ["20", 0.0095], ["21", 0.0095], ["22", 0.0759], ["23", 0.1329], ["24", 0.1709], ["25", 0.1139], ["26", 0.3418], ["27", 1.0063], ["28", 0.4747], ["29", 0.8069], ["30", 1.2626], ["31", 2.7435], ["32", 1.5758], ["33", 1.8322], ["34", 3.1517], ["35", 6.51...
{"lang": "python3", "distribution": [["16900", 0.0095], ["17000", 0.019], ["17900", 0.0095], ["18000", 0.0095], ["18100", 0.019], ["18200", 0.0475], ["18300", 1.0632], ["18400", 16.6129], ["18500", 43.4878], ["18600", 30.2354]]}
[ { "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 oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:\n if not head:\n return None\n ...
53
[ { "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 oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:\n pass\n\nsys.stdout=open('user.out','w')\n\nf...
14
329
longest-increasing-path-in-a-matrix
<p>Given an <code>m x n</code> integers <code>matrix</code>, return <em>the length of the longest increasing path in </em><code>matrix</code>.</p> <p>From each cell, you can either move in four directions: left, right, up, or down. You <strong>may not</strong> move <strong>diagonally</strong> or move <strong>outside t...
54.101804
Hard
[ "array", "dynamic-programming", "depth-first-search", "breadth-first-search", "graph", "topological-sort", "memoization", "matrix" ]
{"lang": "python3", "distribution": [["218", 0.08990000000000001], ["222", 0.1437], ["226", 0.4131], ["230", 0.2515], ["233", 0.5747], ["237", 0.43100000000000005], ["241", 0.6285999999999999], ["245", 0.5209], ["248", 0.9158999999999999], ["252", 0.8441], ["256", 0.7901999999999999], ["259", 0.6646], ["263", 0.8981], ...
{"lang": "python3", "distribution": [["17165", 0.8980000000000001], ["17295", 1.5984], ["17425", 2.694], ["17555", 8.5489], ["17685", 6.1961], ["17815", 6.268], ["17945", 5.8549], ["18075", 0.5029], ["18205", 0.3592], ["18335", 1.4009], ["18465", 2.2629], ["18595", 0.6286], ["18725", 0.3592], ["18855", 1.52660000000000...
[ { "code": "class Solution:\n def longestIncreasingPath(self, matrix: List[List[int]]) -> int:\n\n if not matrix or not matrix[0]:\n return 0\n rows, cols = len(matrix), len(matrix[0])\n\n dp = [[0] * cols for _ in range(rows)]\n\n def dfs(i, j):\n\n if not dp...
153
[ { "code": "class Solution:\n def longestIncreasingPath(self, matrix: List[List[int]]) -> int:\n ln_i = len(matrix)\n ln_j = len(matrix[0])\n\n path_matrix = [[1 for _ in range(ln_j)] for _ in range(ln_i)]\n\n flag = True\n while flag:\n flag = False\n ...
114
330
patching-array
<p>Given a sorted integer array <code>nums</code> and an integer <code>n</code>, add/patch elements to the array such that any number in the range <code>[1, n]</code> inclusive can be formed by the sum of some elements in the array.</p> <p>Return <em>the minimum number of patches required</em>.</p> <p>&nbsp;</p> <p><...
53.172962
Hard
[ "array", "greedy" ]
{"lang": "python3", "distribution": [["31", 0.13], ["38", 0.13], ["39", 0.13], ["42", 0.3901], ["44", 0.3901], ["45", 0.5202], ["46", 1.0403], ["47", 0.7802], ["48", 1.6905], ["49", 1.3004], ["50", 2.8609], ["51", 4.0312], ["52", 4.4213], ["53", 4.0312], ["54", 4.5514], ["55", 7.8023], ["56", 5.7217], ["57", 6.3719], [...
{"lang": "python3", "distribution": [["16200", 0.13], ["16400", 0.2601], ["16500", 6.8921], ["16600", 27.4382], ["16700", 48.5046]]}
[ { "code": "class Solution:\n def minPatches(self, nums: List[int], n: int) -> int:\n miss, result, i, l = 1, 0, 0, len(nums)\n\n while miss <= n:\n if i < l and nums[i] <= miss:\n miss += nums[i]\n i += 1\n else:\n miss += miss\n result += 1\n\n return result\n\ni...
32
[ { "code": "from typing import List\n\nclass Solution:\n def minPatches(self, nums: List[int], n: int) -> int:\n patches = 0\n miss = 1\n i = 0\n while miss <= n:\n if i < len(nums) and nums[i] <= miss:\n miss += nums[i]\n i += 1\n ...
7
331
verify-preorder-serialization-of-a-binary-tree
<p>One way to serialize a binary tree is to use <strong>preorder traversal</strong>. When we encounter a non-null node, we record the node&#39;s value. If it is a null node, we record using a sentinel value such as <code>&#39;#&#39;</code>.</p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/pre-tree.jp...
45.525128
Medium
[ "string", "stack", "tree", "binary-tree" ]
{"lang": "python3", "distribution": [["19", 0.1927], ["20", 0.1927], ["22", 0.1927], ["23", 0.578], ["24", 0.7707], ["25", 0.1927], ["26", 1.1561], ["27", 2.3121], ["28", 1.3487], ["29", 1.3487], ["30", 2.3121], ["31", 3.6609], ["32", 5.0096], ["33", 4.2389], ["34", 7.1291], ["35", 6.7437], ["36", 6.3584], ["37", 4.431...
{"lang": "python3", "distribution": [["16100", 0.1927], ["16300", 0.9634], ["16400", 7.1291], ["16500", 28.5164], ["16600", 38.7283], ["16700", 18.1118]]}
[ { "code": "class Solution:\n def isValidSerialization(self, preorder: str) -> bool:\n stack_ = []\n N = 0\n preorder = preorder.split(\",\")\n\n if len(preorder)==1 and preorder[0]!=\"#\":\n return False\n\n if preorder[0]==\"#\" and len(preorder)>1:\n ...
36
[ { "code": "class Solution:\n def isValidSerialization(self, preorder: str) -> bool:\n pre = preorder.split(\",\")\n stack = []\n \n for node in pre:\n while stack and node == \"#\" and stack[-1] == \"#\":\n stack.pop()\n if not stack:\n ...
9
332
reconstruct-itinerary
<p>You are given a list of airline <code>tickets</code> where <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it.</p> <p>All of the tickets belong to a man who departs from <code>&quot;JFK&quot;</...
43.29478
Hard
[ "depth-first-search", "graph", "eulerian-circuit" ]
{"lang": "python3", "distribution": [["53", 0.0213], ["54", 0.0213], ["55", 0.0851], ["56", 0.0851], ["57", 0.1276], ["58", 0.0851], ["59", 0.2126], ["60", 0.2977], ["61", 0.5741], ["62", 0.489], ["63", 0.7229], ["64", 1.1695], ["65", 1.5309], ["66", 1.6373], ["67", 2.4878], ["68", 3.1682], ["69", 3.0406], ["70", 3.976...
{"lang": "python3", "distribution": [["16300", 0.0213], ["16500", 0.0851], ["16600", 1.701], ["16700", 8.8029], ["16800", 24.495], ["16900", 28.1097], ["17000", 21.3906], ["17100", 7.2294]]}
[ { "code": "class Solution:\n def findItinerary(self, tickets: List[List[str]]) -> List[str]:\n graph = defaultdict(list)\n\n for src, dest in sorted(tickets, reverse = True):\n graph[src].append(dest)\n \n itinerary = []\n\n def dfs(node):\n while grap...
57
[ { "code": "class Solution:\n def findItinerary(self, tickets: List[List[str]]) -> List[str]:\n targets = collections.defaultdict(list)\n for a, b in sorted(tickets)[::-1]:\n targets[a] += b,\n route, stack = [], ['JFK']\n while stack:\n while targets[stack[-1...
13
334
increasing-triplet-subsequence
<p>Given an integer array <code>nums</code>, return <code>true</code><em> if there exists a triple of indices </em><code>(i, j, k)</code><em> such that </em><code>i &lt; j &lt; k</code><em> and </em><code>nums[i] &lt; nums[j] &lt; nums[k]</code>. If no such indices exists, return <code>false</code>.</p> <p>&nbsp;</p> ...
39.430707
Medium
[ "array", "greedy" ]
{"lang": "python3", "distribution": [["72", 0.0324], ["84", 0.2015], ["96", 0.1291], ["108", 0.0081], ["121", 0.0081], ["133", 0.0081], ["145", 0.0081], ["158", 0.0081], ["170", 0.0081], ["182", 0.0081], ["194", 0.0161], ["207", 0.0403], ["219", 0.0161], ["231", 0.0483], ["243", 0.0483], ["256", 0.0564], ["268", 0.0483...
{"lang": "python3", "distribution": [["26200", 0.0242], ["26300", 0.0161], ["26400", 0.2095], ["26500", 0.1209], ["26600", 0.0081], ["30000", 0.0081], ["30200", 0.0081], ["30300", 0.0081], ["30400", 0.0081], ["30500", 0.0161], ["30700", 0.0081], ["30900", 0.0242], ["31000", 0.0161], ["31100", 0.0242], ["31200", 0.0161]...
[ { "code": "class Solution:\n def increasingTriplet(self, nums: List[int]) -> bool:\n if len(nums) < 3:\n return False\n\n f = float('inf')\n s = float('inf')\n\n for num in nums:\n if num <= f:\n f = num\n elif num <= s:\n ...
126
[ { "code": "class Solution:\n def increasingTriplet(self, nums: List[int]) -> bool:\n first = float('inf')\n second = float('inf')\n \n \n for num in nums:\n\n if num <= first:\n first = num\n \n elif num <= second:\n ...
33
335
self-crossing
<p>You are given an array of integers <code>distance</code>.</p> <p>You start at the point <code>(0, 0)</code> on an <strong>X-Y plane,</strong> and you move <code>distance[0]</code> meters to the north, then <code>distance[1]</code> meters to the west, <code>distance[2]</code> meters to the south, <code>distance[3]</...
30.959405
Hard
[ "array", "math", "geometry" ]
{"lang": "python3", "distribution": [["102", 0.3788], ["104", 0.3788], ["106", 0.3788], ["108", 0.3788], ["109", 1.1364], ["110", 0.3788], ["111", 1.8939], ["112", 1.1364], ["113", 0.7576], ["114", 1.1364], ["115", 0.7576], ["116", 1.1364], ["117", 2.6515], ["118", 2.6515], ["119", 1.1364], ["120", 0.7576], ["121", 1.5...
{"lang": "python3", "distribution": [["25100", 0.3788], ["25200", 0.7576], ["25300", 17.0455], ["25400", 59.0909], ["25500", 14.3939]]}
[ { "code": "\n\nclass Solution:\n def isSelfCrossing(self, distance: List[int]) -> bool:\n x, y = 0, 0\n spiral_out = True\n bottom_y, top_y, prev_bottom_y, prev_top_y = 0, 0, 0, 0\n left_x, right_x, prev_left_x, prev_right_x = 0, 0, 0, 0\n\n for i, d in enumerate(distance):...
54
[ { "code": "class Solution:\n def isSelfCrossing(self, distance: List[int]) -> bool:\n b=c=d=e=f=0\n for a in distance:\n if d>=b>0 and (a>=c or a>=c-e>=0 and f>=d-b): return True\n b,c,d,e,f=a,b,c,d,e\n return False", "memory": "25100" }, { "code": "clas...
6
336
palindrome-pairs
<p>You are given a <strong>0-indexed</strong> array of <strong>unique</strong> strings <code>words</code>.</p> <p>A <strong>palindrome pair</strong> is a pair of integers <code>(i, j)</code> such that:</p> <ul> <li><code>0 &lt;= i, j &lt; words.length</code>,</li> <li><code>i != j</code>, and</li> <li><code>words[...
35.613985
Hard
[ "array", "hash-table", "string", "trie" ]
{"lang": "python3", "distribution": [["363", 0.7960000000000002], ["444", 0.0995], ["525", 0.199], ["606", 0.0995], ["687", 0.0995], ["768", 0.0995], ["849", 0.0995], ["929", 1.8904999999999998], ["1010", 15.124000000000013], ["1091", 0.0995], ["1172", 0.398], ["1253", 0.2985], ["1334", 0.199], ["1415", 0.2985], ["1496...
{"lang": "python3", "distribution": [["29508", 75.12400000000001], ["33326", 0.8955000000000001], ["37143", 0.0995], ["40961", 0.0995], ["44778", 0.398], ["48596", 0.199], ["52413", 0.199], ["56231", 0.0995], ["60048", 0.0995], ["63866", 0.0995], ["67683", 0.199], ["71501", 0.0995], ["75318", 0.199], ["79136", 0.199], ...
[ { "code": "class Solution:\n def palindromePairs(self, words: List[str]) -> List[List[int]]:\n \"\"\"\n answers = list()\n for i in range(len(words)):\n for j in range(i+1, len(words)):\n s1 = words[i] + words[j]\n s2 = words[j] + words[i]\n ...
117
[ { "code": "class Solution:\n def palindromePairs(self, words: List[str]) -> List[List[int]]:\n # list of (index, word, is_reversed), `word` can be either a orginal word or a reversed word\n # the list is sorted so that words with similar prefix are consecutive\n words = sorted(chain(((i,...
72
337
house-robber-iii
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will auto...
54.446571
Medium
[ "dynamic-programming", "tree", "depth-first-search", "binary-tree" ]
{"lang": "python3", "distribution": [["21", 0.0385], ["23", 0.0771], ["27", 0.0771], ["28", 0.1541], ["29", 0.1156], ["30", 0.1541], ["31", 0.578], ["32", 0.3854], ["33", 0.578], ["34", 1.0405], ["35", 1.657], ["36", 1.657], ["37", 1.657], ["38", 3.5067], ["39", 3.2755], ["40", 2.8131], ["41", 3.2755], ["42", 5.8574], ...
{"lang": "python3", "distribution": [["17700", 0.0385], ["17800", 0.1156], ["17900", 0.8478], ["18000", 13.2948], ["18100", 39.3834], ["18200", 5.8189], ["18300", 0.2312], ["18400", 0.6166], ["18500", 2.158], ["18600", 4.7784], ["18700", 2.3892], ["18800", 0.4624], ["18900", 0.1927], ["19000", 0.501], ["19100", 2.158],...
[ { "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 rob_helper(self, root):\n if not root:\n return (0, 0)\n \n l = self.rob_h...
47
[ { "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 rob(self, root: Optional[TreeNode]) -> int:\n def helper(node):\n ...
38
338
counting-bits
<p>Given an integer <code>n</code>, return <em>an array </em><code>ans</code><em> of length </em><code>n + 1</code><em> such that for each </em><code>i</code><em> </em>(<code>0 &lt;= i &lt;= n</code>)<em>, </em><code>ans[i]</code><em> is the <strong>number of </strong></em><code>1</code><em><strong>&#39;s</strong> in t...
78.849976
Easy
[ "dynamic-programming", "bit-manipulation" ]
{"lang": "python3", "distribution": [["31", 0.0138], ["33", 0.0274], ["34", 0.0137], ["36", 0.0275], ["38", 0.0617], ["39", 0.1441], ["41", 0.1235], ["42", 0.3018], ["44", 0.7272], ["45", 0.6037], ["47", 1.2555], ["48", 1.2418], ["50", 3.197], ["52", 1.8386], ["53", 5.7629], ["55", 3.1559], ["56", 7.2173], ["58", 7.43]...
{"lang": "python3", "distribution": [["21800", 0.0069], ["22300", 0.0137], ["22400", 0.0137], ["22500", 0.0755], ["22600", 0.1098], ["22700", 0.048], ["22800", 0.096], ["22900", 0.2813], ["23000", 1.6397], ["23100", 10.1125], ["23200", 44.292], ["23300", 37.9391]]}
[ { "code": "class Solution:\n def countBits(self, n: int) -> List[int]:\n if n == 0:\n return [0]\n if n == 1:\n return [0, 1]\n ans = [0, 1]\n for i in range(2, n + 1):\n if i % 2 == 0:\n ans.append(ans[i // 2])\n else:\n ...
137
[ { "code": "def countSetBits(n):\n count = 0\n # while n > 0:\n # count += n & 1\n # n = n >> 1\n while n > 0:\n n = n & n - 1\n count += 1\n return count\n\n\nclass Solution:\n def countBits(self, n: int) -> List[int]:\n ans = []\n for i in range(n + 1):\...
16
341
flatten-nested-list-iterator
<p>You are given a nested list of integers <code>nestedList</code>. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.</p> <p>Implement the <code>NestedIterator</code> class:</p> <ul> <li><code>NestedIterator(List&lt;NestedInteger&gt; ...
64.778312
Medium
[ "stack", "tree", "depth-first-search", "design", "queue", "iterator" ]
{"lang": "python3", "distribution": [["29", 0.0423], ["30", 0.0423], ["32", 0.0423], ["33", 0.1268], ["34", 0.1691], ["35", 0.3804], ["36", 0.2959], ["37", 0.2113], ["38", 0.5495], ["39", 0.803], ["40", 1.0144], ["41", 1.3525], ["42", 1.9442], ["43", 2.3246], ["44", 2.7473], ["45", 3.4235], ["46", 4.2265], ["47", 4.353...
{"lang": "python3", "distribution": [["18300", 0.0423], ["18400", 0.0845], ["18500", 0.5917], ["18600", 1.2257], ["18700", 0.2959], ["18800", 0.2959], ["18900", 0.0845], ["19000", 0.1268], ["19100", 0.4649], ["19200", 5.1141], ["19300", 34.8267], ["19400", 28.4024], ["19500", 7.8191]]}
[ { "code": "# \"\"\"\n# This is the interface that allows for creating nested lists.\n# You should not implement it, or speculate about its implementation\n# \"\"\"\n#class NestedInteger:\n# def isInteger(self) -> bool:\n# \"\"\"\n# @return True if this NestedInteger holds a single integer, rath...
43
[ { "code": "# \"\"\"\n# This is the interface that allows for creating nested lists.\n# You should not implement it, or speculate about its implementation\n# \"\"\"\n#class NestedInteger:\n# def isInteger(self) -> bool:\n# \"\"\"\n# @return True if this NestedInteger holds a single integer, rath...
18
342
power-of-four
<p>Given an integer <code>n</code>, return <em><code>true</code> if it is a power of four. Otherwise, return <code>false</code></em>.</p> <p>An integer <code>n</code> is a power of four, if there exists an integer <code>x</code> such that <code>n == 4<sup>x</sup></code>.</p> <p>&nbsp;</p> <p><strong class="example">E...
48.439607
Easy
[ "math", "bit-manipulation", "recursion" ]
{"lang": "python3", "distribution": [["15", 0.0454], ["16", 0.0227], ["17", 0.0227], ["18", 0.0908], ["19", 0.0908], ["20", 0.0908], ["21", 0.0454], ["22", 0.2724], ["23", 0.9535], ["24", 0.5902], ["25", 0.7491], ["26", 1.0443], ["27", 3.0193], ["28", 2.4745], ["29", 2.0431], ["30", 2.6788], ["31", 7.0375], ["32", 5.06...
{"lang": "python3", "distribution": [["16200", 0.3632], ["16300", 4.1317], ["16400", 21.4983], ["16500", 41.5437]]}
[ { "code": "class Solution:\n def isPowerOfFour(self, n: int) -> bool:\n if n <= 0:\n return False\n while n % 4 == 0:\n n//=4\n return n == 1\n ", "runtime": "15" }, { "code": "class Solution:\n def isPowerOfFour(self, n: int) -> bool:\n z=0...
43
[ { "code": "class Solution:\n def isPowerOfFour(self, n: int) -> bool:\n for i in range(31):\n if 4**i==n:\n return True\n\n ", "memory": "16200" }, { "code": "class Solution:\n def isPowerOfFour(self, num: int) -> bool:\n if num <= 0:\n ...
7
343
integer-break
<p>Given an integer <code>n</code>, break it into the sum of <code>k</code> <strong>positive integers</strong>, where <code>k &gt;= 2</code>, and maximize the product of those integers.</p> <p>Return <em>the maximum product you can get</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> ...
60.628607
Medium
[ "math", "dynamic-programming" ]
{"lang": "python3", "distribution": [["19", 0.0505], ["20", 0.101], ["22", 0.101], ["23", 0.3534], ["24", 0.5048], ["25", 0.6562], ["26", 1.5144], ["27", 1.5144], ["28", 1.9182], ["29", 1.9182], ["30", 2.423], ["31", 3.9879], ["32", 4.6441], ["33", 5.4518], ["34", 6.108], ["35", 5.3004], ["36", 6.7138], ["37", 6.9662],...
{"lang": "python3", "distribution": [["16200", 0.3029], ["16300", 6.3099], ["16400", 23.6244], ["16500", 41.3428], ["16600", 20.1918]]}
[ { "code": "mapper = {1:1, 2:1, 3:2}\n\nclass Solution:\n def integerBreak(self, n: int) -> int:\n def recursive(n):\n global mapper\n if n in mapper:\n return mapper[n]\n tmp_max = 1\n for i in range(2, n//2 + 1):\n if recursive...
36
[ { "code": "class Solution:\n def integerBreak(self, n: int) -> int:\n if n <= 2: return 1\n dp = [0] * (n + 1)\n dp[1] = 1\n \n for i in range(2, n + 1):\n for j in range(1, i): dp[i] = max(dp[i],j*(i-j),j*dp[i- j])\n return dp[n]", "memory": "16200" ...
8
344
reverse-string
<p>Write a function that reverses a string. The input string is given as an array of characters <code>s</code>.</p> <p>You must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with <code>O(1)</code> extra memory.</p> <p>&nbsp;</p> <p><strong...
79.016655
Easy
[ "two-pointers", "string" ]
{"lang": "python3", "distribution": [["30", 0.0095], ["31", 0.0048], ["34", 0.0095], ["35", 0.0095], ["36", 0.0048], ["37", 0.0048], ["38", 0.0286], ["40", 0.0048], ["41", 0.0191], ["42", 0.0095], ["43", 0.0095], ["44", 0.0048], ["45", 0.0095], ["46", 0.0095], ["47", 0.0143], ["48", 0.0048], ["54", 0.0048], ["139", 0.0...
{"lang": "python3", "distribution": [["16800", 0.0095], ["17000", 0.0239], ["17100", 0.0286], ["19600", 0.0095], ["19900", 0.0239], ["20000", 0.0048], ["20100", 0.0048], ["20200", 0.0095], ["20300", 0.0286], ["20400", 0.0334], ["20500", 0.1289], ["20600", 0.5586], ["20700", 11.7827], ["20800", 57.6291], ["20900", 14.71...
[ { "code": "r0='\"\\\\'\nr1='\\\\\"'\nnl=\"\\n\"\nwith open(\"user.out\", \"w\") as f:\n for z in stdin:\n f.write(f'[{z[-2-(z[-1]==nl):0:-1].replace(r0,r1)}]\\n')\nexit()", "runtime": "30" }, { "code": "with open(\"user.out\", \"w\") as f:\n for case in stdin:\n f.write(f\"{dumps...
80
[ { "code": "r0='\"\\\\'\nr1='\\\\\"'\nnl=\"\\n\"\nwith open(\"user.out\", \"w\") as f:\n for z in stdin:\n f.write(f'[{z[-2-(z[-1]==nl):0:-1].replace(r0,r1)}]\\n')\nexit()", "memory": "16800" }, { "code": "r0='\"\\\\'\nr1='\\\\\"'\nnl=\"\\n\"\nwith open(\"user.out\", \"w\") as f:\n for z...
19
345
reverse-vowels-of-a-string
<p>Given a string <code>s</code>, reverse only all the vowels in the string and return it.</p> <p>The vowels are <code>&#39;a&#39;</code>, <code>&#39;e&#39;</code>, <code>&#39;i&#39;</code>, <code>&#39;o&#39;</code>, and <code>&#39;u&#39;</code>, and they can appear in both lower and upper cases, more than once.</p> ...
54.341585
Easy
[ "two-pointers", "string" ]
{"lang": "python3", "distribution": [["22", 0.023899999999999998], ["24", 0.0428], ["26", 0.11900000000000001], ["28", 0.19030000000000002], ["30", 0.5615], ["33", 1.6559], ["35", 1.8749], ["37", 3.4547], ["39", 4.3255], ["41", 6.5429], ["43", 10.5923], ["46", 8.6462], ["48", 7.6279], ["50", 7.618399999999999], ["52", ...
{"lang": "python3", "distribution": [["16800", 0.0143], ["16900", 0.2236], ["17000", 1.8891], ["17100", 3.4309], ["17200", 4.592], ["17300", 2.955], ["17400", 16.1742], ["17500", 33.8092], ["17600", 4.6729], ["17700", 3.1454], ["17800", 2.3031], ["17900", 0.8708], ["18000", 4.0685], ["18100", 1.3181], ["18200", 2.0319]...
[ { "code": "class Solution:\n def reverseVowels(self, s: str) -> str:\n i, j = 0, len(s)-1\n vowel = \"aeiouAEIOU\"\n st = list(s)\n while i < j:\n if st[i] in vowel and st[j] in vowel:\n temp = st[i]\n st[i] = st[j]\n st[j] =...
147
[ { "code": "class Solution:\n def reverseVowels(self, s: str) -> str:\n c = []\n v = []\n r = ''\n vowels = set([\"i\", \"e\", \"a\", \"o\", \"u\", \"A\", \"E\", \"I\", \"O\", \"U\"])\n backpointer = len(s)\n for i in range(len(s)):\n if s[i] in vowels:\n ...
43
347
top-k-frequent-elements
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the</em> <code>k</code> <em>most frequent elements</em>. You may return the answer in <strong>any order</strong>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre><strong>Input:</strong> nums = [1,1,1,2,2,3],...
63.290959
Medium
[ "array", "hash-table", "divide-and-conquer", "sorting", "heap-priority-queue", "bucket-sort", "counting", "quickselect" ]
{"lang": "python3", "distribution": [["34", 0.0022], ["36", 0.0022], ["37", 0.0022], ["38", 0.0044], ["40", 0.0044], ["42", 0.0022], ["44", 0.0089], ["45", 0.0111], ["47", 0.0022], ["48", 0.0089], ["49", 0.0044], ["50", 0.0044], ["51", 0.0044], ["53", 0.0022], ["55", 0.0022], ["57", 0.0022], ["59", 0.0022], ["62", 0.00...
{"lang": "python3", "distribution": [["18800", 0.0022], ["18900", 0.0044], ["19000", 0.0378], ["19100", 0.0111], ["20000", 0.0022], ["20200", 0.0067], ["20300", 0.0089], ["20400", 0.0178], ["20500", 0.0422], ["20600", 0.1866], ["20700", 1.2529], ["20800", 2.5635], ["20900", 1.3151], ["21000", 6.9263], ["21100", 18.8573...
[ { "code": "class Solution:\n def topKFrequent(self, nums: List[int], k: int) -> List[int]:\n fmap = Counter(nums)\n heap = []\n heapq.heapify(heap)\n \n for num, freq in fmap.items():\n if len(heap) < k:\n heapq.heappush(heap, (freq, num))\n ...
115
[ { "code": "sys.stdout = open('user.out', 'a')\nfor nums in map(loads, sys.stdin):\n print(dumps(list(map(itemgetter(0), Counter(nums).most_common(int(sys.stdin.readline()))))))\n\nexit()", "memory": "18800" }, { "code": "sys.stdout = open('user.out', 'a')\nfor nums in map(loads, sys.stdin):\n ...
38
520
detect-capital
<p>We define the usage of capitals in a word to be right when one of the following cases holds:</p> <ul> <li>All letters in this word are capitals, like <code>&quot;USA&quot;</code>.</li> <li>All letters in this word are not capitals, like <code>&quot;leetcode&quot;</code>.</li> <li>Only the first letter in this wo...
56.387469
Easy
[ "string" ]
{"lang": "python3", "distribution": [["11", 0.0388], ["14", 0.0388], ["15", 0.0388], ["18", 0.0388], ["19", 0.3488], ["20", 0.155], ["21", 0.1163], ["22", 0.4264], ["23", 1.3566], ["24", 0.6202], ["25", 0.7364], ["26", 1.8992], ["27", 3.062], ["28", 2.3256], ["29", 2.3256], ["30", 4.9225], ["31", 6.7829], ["32", 4.2636...
{"lang": "python3", "distribution": [["16200", 0.2713], ["16300", 3.2171], ["16400", 21.3566], ["16500", 36.7054]]}
[ { "code": "class Solution:\n def detectCapitalUse(self, word: str) -> bool:\n upper_count = 0\n for i in range(len(word)):\n if word[i].isupper():\n upper_count += 1\n # small optimization\n if upper_count > 1 and upper_count < i:\n ...
43
[ { "code": "class Solution:\n def detectCapitalUse(self, word: str) -> bool:\n return word.isupper() or word.islower() or word.istitle()", "memory": "16200" }, { "code": "class Solution:\n def detectCapitalUse(self, word: str) -> bool:\n capitals = set(\"ABCDEFGHIJKLMNOPQRSTUVWXYZ...
7
521
longest-uncommon-subsequence-i
<p>Given two strings <code>a</code> and <code>b</code>, return <em>the length of the <strong>longest uncommon subsequence</strong> between </em><code>a</code> <em>and</em> <code>b</code>. <em>If no such uncommon subsequence exists, return</em> <code>-1</code><em>.</em></p> <p>An <strong>uncommon subsequence</strong> b...
60.938828
Easy
[ "string" ]
{"lang": "python3", "distribution": [["16", 0.0952], ["18", 0.0952], ["19", 0.1905], ["20", 0.381], ["21", 0.2857], ["22", 0.4762], ["23", 0.7619], ["24", 1.1429], ["25", 0.6667], ["26", 2.2857], ["27", 1.9048], ["28", 3.619], ["29", 4.8571], ["30", 4.1905], ["31", 4.1905], ["32", 5.5238], ["33", 6.0], ["34", 8.6667], ...
{"lang": "python3", "distribution": [["16200", 0.4762], ["16300", 8.4762], ["16400", 28.6667], ["16500", 47.7143]]}
[ { "code": "class Solution:\n def findLUSlength(self, a: str, b: str) -> int:\n if a==b:\n return -1\n else:\n return max(len(a),len(b))\n ", "runtime": "16" }, { "code": "class Solution:\n def findLUSlength(self, a: str, b: str) -> int:\n if a ...
30
[ { "code": "class Solution:\n def findLUSlength(self, a: str, b: str) -> int:\n return -1 if a == b else max(len(a), len(b))", "memory": "16200" }, { "code": "class Solution:\n def findLUSlength(self, a: str, b: str) -> int:\n if a == b:\n return -1\n else:\n ...
6
522
longest-uncommon-subsequence-ii
<p>Given an array of strings <code>strs</code>, return <em>the length of the <strong>longest uncommon subsequence</strong> between them</em>. If the longest uncommon subsequence does not exist, return <code>-1</code>.</p> <p>An <strong>uncommon subsequence</strong> between an array of strings is a string that is a <st...
42.086452
Medium
[ "array", "hash-table", "two-pointers", "string", "sorting" ]
{"lang": "python3", "distribution": [["14", 0.3247], ["20", 0.3247], ["22", 0.3247], ["23", 0.3247], ["24", 0.3247], ["25", 0.3247], ["26", 0.3247], ["27", 0.974], ["28", 0.3247], ["29", 1.2987], ["30", 2.9221], ["31", 2.9221], ["32", 2.9221], ["33", 3.2468], ["34", 3.5714], ["35", 4.8701], ["36", 6.4935], ["37", 4.870...
{"lang": "python3", "distribution": [["16200", 0.3247], ["16300", 5.5195], ["16400", 21.1039], ["16500", 28.8961], ["16600", 32.7922], ["16700", 4.2208], ["16800", 0.974], ["18800", 0.3247], ["18900", 0.3247]]}
[ { "code": "from typing import List\n\nclass Solution:\n def findLUSlength(self, strs: List[str]) -> int:\n def isSubsequence(a: str, b: str) -> bool:\n i = 0\n for char in b:\n if i < len(a) and a[i] == char:\n i += 1\n return i == len...
41
[ { "code": "class Solution:\n def findLUSlength(self, words):\n def isSubsequence(s, t):\n t = iter(t)\n return all(c in t for c in s)\n \n words.sort(key = lambda x:-len(x))\n for i, word in enumerate(words):\n if all(not isSubsequence(word, words[j]) for...
11
523
continuous-subarray-sum
<p>Given an integer array nums and an integer k, return <code>true</code> <em>if </em><code>nums</code><em> has a <strong>good subarray</strong> or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>good subarray</strong> is a subarray where:</p> <ul> <li>its length is <strong>at least two</strong>, and</l...
30.365273
Medium
[ "array", "hash-table", "math", "prefix-sum" ]
{"lang": "python3", "distribution": [["122", 0.050699999999999995], ["133", 0.0508], ["144", 0.0338], ["154", 0.0169], ["165", 0.0338], ["176", 0.0508], ["187", 0.0338], ["198", 0.0169], ["208", 0.0508], ["219", 0.0846], ["230", 0.0508], ["241", 0.1015], ["252", 0.0846], ["262", 0.1184], ["273", 0.1015], ["284", 0.0846...
{"lang": "python3", "distribution": [["30400", 0.0169], ["30600", 0.0338], ["30700", 0.0508], ["31000", 0.0169], ["31200", 0.0338], ["31300", 0.0338], ["31400", 0.1353], ["31500", 0.1015], ["31600", 0.812], ["31700", 4.4662], ["31800", 1.5564], ["32000", 0.0508], ["32100", 0.0169], ["32200", 0.0169], ["32299", 0.0338],...
[ { "code": "def checkSubarraySum(nums: List[int], k: int) -> bool:\n d={0:-1}\n ream=0\n\n for i in range(len(nums)):\n ream=(ream+nums[i])%k\n if ream in d:\n if i-d[ream]>=2: return 'true'\n else: d[ream]=i\n\n return 'false'\n\nsys.stdout=open('user.out','w')\nfor n...
111
[ { "code": "class Solution:\n def checkSubarraySum(self, nums: List[int], k: int) -> bool:\n def rf(ls):\n if len(ls) == 1:\n return False\n\n m = (0 + len(ls) - 1) // 2\n nextm = m + 1\n # blocks 0 .. m, m + 1 .. len(ls) - 1\n\n sum...
74
524
longest-word-in-dictionary-through-deleting
<p>Given a string <code>s</code> and a string array <code>dictionary</code>, return <em>the longest string in the dictionary that can be formed by deleting some of the given string characters</em>. If there is more than one possible result, return the longest word with the smallest lexicographical order. If there is no...
51.356699
Medium
[ "array", "two-pointers", "string", "sorting" ]
{"lang": "python3", "distribution": [["67", 0.45120000000000005], ["71", 1.5038], ["74", 0.45120000000000005], ["78", 1.0527], ["81", 0.7519], ["85", 1.3534000000000002], ["88", 0.6015], ["92", 1.6542000000000001], ["96", 1.9548999999999999], ["99", 2.7067], ["103", 1.9549], ["106", 1.955], ["110", 0.6016], ["113", 0.4...
{"lang": "python3", "distribution": [["18700", 0.3008], ["18800", 2.1053], ["18900", 16.0902], ["19000", 32.1805], ["19100", 36.6917]]}
[ { "code": "class Solution:\n def findLongestWord(self, s: str, dictionary: List[str]) -> str:\n next_index = [[None] * 26 for char in s]\n last_index = [None] * 26\n for index in range(len(s) - 1, -1, -1):\n next_index[index] = last_index.copy()\n last_index[ord(s[i...
138
[ { "code": "class Solution:\n def findLongestWord(self, s: str, dictionary: List[str]) -> str:\n\n ret = ''\n\n for i in dictionary:\n flag = False\n k = 0\n\n for j in s:\n if j == i[k]:\n k+=1\n if k==len(i):...
6
525
contiguous-array
<p>Given a binary array <code>nums</code>, return <em>the maximum length of a contiguous subarray with an equal number of </em><code>0</code><em> and </em><code>1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [0,1] <strong>Output:</strong> 2 <strong>...
48.952433
Medium
[ "array", "hash-table", "prefix-sum" ]
{"lang": "python3", "distribution": [["141", 0.0278], ["148", 0.0139], ["154", 0.0139], ["161", 0.0139], ["168", 0.0139], ["174", 0.0139], ["181", 0.0139], ["188", 0.0139], ["194", 0.0139], ["201", 0.0139], ["208", 0.0139], ["214", 0.0139], ["221", 0.0139], ["228", 0.0139], ["234", 0.0557], ["241", 0.0139], ["248", 0.0...
{"lang": "python3", "distribution": [["18200", 0.0139], ["18300", 0.0139], ["18400", 0.167], ["18500", 0.0418], ["19000", 0.0139], ["19100", 0.1253], ["19200", 0.3897], ["19300", 0.0278], ["19400", 0.0278], ["19500", 0.0139], ["19600", 0.0139], ["19900", 0.0696], ["20000", 0.0557], ["20100", 0.1113], ["20200", 0.1253],...
[ { "code": "def findMaxLength(nums: List[int]) -> int:\n n = len(nums)\n pre = 0\n seen = {}\n seen[0] = -1\n\n res = 0\n for i in range(n):\n num = nums[i] * 2 - 1\n pre += num\n if pre not in seen:\n seen[pre] = i\n else:\n res = max(res, i - ...
104
[ { "code": "class Solution:\n def findMaxLength(self, nums: List[int]) -> int:\n ## tackle the cases when half of the elements are 1s\n max_len = len(nums)\n if 2*sum(nums) == max_len:\n return max_len\n\n nums = [x or -1 for x in nums]\n prefix_sum = 0\n h...
63
526
beautiful-arrangement
<p>Suppose you have <code>n</code> integers labeled <code>1</code> through <code>n</code>. A permutation of those <code>n</code> integers <code>perm</code> (<strong>1-indexed</strong>) is considered a <strong>beautiful arrangement</strong> if for every <code>i</code> (<code>1 &lt;= i &lt;= n</code>), <strong>either</st...
64.332519
Medium
[ "array", "dynamic-programming", "backtracking", "bit-manipulation", "bitmask" ]
{"lang": "python3", "distribution": [["38", 1.5661], ["55", 1.4543999999999997], ["73", 2.5728], ["90", 5.145600000000001], ["107", 1.7899999999999996], ["124", 1.2306999999999997], ["142", 2.3492999999999995], ["159", 1.3425999999999996], ["176", 0.1119], ["193", 0.1119], ["210", 0.6713], ["228", 0.3357], ["245", 0.55...
{"lang": "python3", "distribution": [["16100", 0.1119], ["16200", 0.1119], ["16300", 1.6779], ["16400", 10.179], ["16500", 26.9575], ["16600", 36.1298], ["16700", 8.0537], ["16800", 1.4541], ["16900", 0.1119], ["17000", 0.1119], ["17300", 0.2237], ["17400", 0.4474], ["17600", 0.1119], ["17800", 0.5593], ["17900", 0.447...
[ { "code": "class Solution:\n def countArrangement(self, n: int) -> int:\n return [1,2,3,8,10,36,41,132,250,700,750,4010,4237,10680,24679][n-1]\n ", "runtime": "38" }, { "code": "class Solution:\n def countArrangement(self, n: int) -> int:\n return [1,2,3,8,10,36,41,132,250...
142
[ { "code": "class Solution:\n def countArrangement(self, n: int) -> int:\n path = []\n ans = [0]\n\n def is_divisible(index, value):\n return index % value == 0 or value % index == 0 \n\n def backtracking(path, ans):\n if len(path) == n:\n ans[0...
43
912
random-pick-with-weight
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>w</code> where <code>w[i]</code> describes the <strong>weight</strong> of the <code>i<sup>th</sup></code> index.</p> <p>You need to implement the function <code>pickIndex()</code>, which <strong>randomly</strong> picks an index in the range...
47.208101
Medium
[ "array", "math", "binary-search", "prefix-sum", "randomized" ]
{"lang": "python3", "distribution": [["120", 26.173399999999994], ["172", 46.262499999999996], ["224", 1.7454000000000012], ["276", 0.5781999999999999], ["329", 0.31860000000000005], ["381", 0.21240000000000006], ["433", 0.34219999999999995], ["485", 0.0826], ["538", 0.09440000000000001], ["590", 0.0472], ["642", 0.118...
{"lang": "python3", "distribution": [["19200", 0.0236], ["19500", 0.0236], ["19600", 0.0118], ["19700", 0.0118], ["19800", 0.0589], ["19900", 0.0118], ["20000", 0.0236], ["20100", 0.0236], ["20200", 0.0236], ["20300", 0.0118], ["20400", 0.0118], ["20500", 0.0354], ["20600", 0.0825], ["20700", 0.1061], ["20800", 0.1886]...
[ { "code": "\nclass Solution:\n\n def __init__(self, w: List[int]):\n total = sum(w)\n self.weights = []\n acc = 0.0\n for weight in w:\n acc += weight / total\n self.weights.append(acc)\n self.weights[-1] = 1.0\n\n\n def pickIndex(self) -> int:\n ...
142
[ { "code": "class Solution:\n\n def __init__(self, w: List[int]):\n self.w = w\n\n weight_sum = sum(w)\n\n for i in range(len(w)):\n self.w[i]/=weight_sum\n \n for i in range(1,len(w)):\n self.w[i]+=self.w[i-1]\n\n\n\n def pickIndex(self) -> int:\n ...
40
529
minesweeper
<p>Let&#39;s play the minesweeper game (<a href="https://en.wikipedia.org/wiki/Minesweeper_(video_game)" target="_blank">Wikipedia</a>, <a href="http://minesweeperonline.com" target="_blank">online game</a>)!</p> <p>You are given an <code>m x n</code> char matrix <code>board</code> representing the game board where:</...
67.084857
Medium
[ "array", "depth-first-search", "breadth-first-search", "matrix" ]
{"lang": "python3", "distribution": [["136", 0.07], ["138", 0.07], ["140", 0.2101], ["141", 0.2801], ["142", 0.7703], ["143", 0.8403], ["144", 0.6303], ["145", 1.4006], ["146", 1.1204], ["147", 1.8908], ["148", 2.1709], ["149", 3.5014], ["150", 2.6611], ["151", 3.9216], ["152", 3.1513], ["153", 4.6919], ["154", 5.7423]...
{"lang": "python3", "distribution": [["15800", 0.07], ["16500", 0.2101], ["16600", 1.8207], ["16700", 4.2017], ["16800", 10.084], ["16900", 10.2941], ["17000", 8.3333], ["17100", 12.3249], ["17200", 12.1849], ["17300", 5.8824], ["17400", 5.4622], ["17500", 4.6919], ["17600", 2.1008], ["17700", 1.3305], ["17800", 1.4706...
[ { "code": "class Solution(object):\n def updateBoard(self, board, click):\n \"\"\"\n :type board: List[List[str]]\n :type click: List[int]\n :rtype: List[List[str]]\n \"\"\"\n if board[click[0]][click[1]] == 'M':\n board[click[0]][click[1]] = 'X'\n ...
45
[ { "code": "class Solution:\n def updateBoard(self, board: List[List[str]], click: List[int]) -> List[List[str]]:\n\n def checker(x,y):\n if (x>=0) and (y>=0) and (x<len(board)) and (y<len(board[0])):\n return True\n return False \n\n queue = []\n queu...
39
378
kth-smallest-element-in-a-sorted-matrix
<p>Given an <code>n x n</code> <code>matrix</code> where each of the rows and columns is sorted in ascending order, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest element in the matrix</em>.</p> <p>Note that it is the <code>k<sup>th</sup></code> smallest element <strong>in the sorted order</strong>, not ...
62.770267
Medium
[ "array", "binary-search", "sorting", "heap-priority-queue", "matrix" ]
{"lang": "python3", "distribution": [["43", 0.114], ["46", 0.0285], ["49", 0.0856], ["52", 0.0285], ["55", 0.1142], ["58", 0.0571], ["61", 0.1427], ["64", 0.1427], ["67", 0.3425], ["70", 0.1142], ["73", 0.2854], ["76", 0.4281], ["79", 0.5993], ["82", 0.7135], ["85", 0.9418], ["88", 1.1416], ["91", 0.9418], ["94", 1.512...
{"lang": "python3", "distribution": [["18900", 0.0285], ["20800", 0.0856], ["20900", 0.2568], ["21000", 1.6267], ["21100", 20.3196], ["21200", 38.7842], ["21300", 13.0422], ["21400", 11.3299], ["21500", 3.3961], ["21600", 0.9132], ["21700", 0.7705], ["21800", 0.3139], ["21900", 0.371], ["22000", 0.0856], ["22100", 0.11...
[ { "code": "\ndef kthSmallest(matrix: List[List[int]], k: int) -> int:\n temp_arr=[]\n for i in matrix:\n temp_arr.extend(i)\n temp_arr.sort()\n return temp_arr[k-1]\n\nwith open('user.out', 'w') as f:\n inputs = list(map(loads, stdin))\n for matrix, k in zip(inputs[::2], inputs[1::2]):\...
125
[ { "code": "class Solution:\n def kthSmallest(self, matrix: List[List[int]], k: int) -> int:\n n = len(matrix)\n left, right = matrix[0][0], matrix[n - 1][n - 1]\n\n while left < right:\n mid = left + (right - left) // 2\n if self.countSmall(matrix, mid) < k:\n ...
43
380
insert-delete-getrandom-o1
<p>Implement the <code>RandomizedSet</code> class:</p> <ul> <li><code>RandomizedSet()</code> Initializes the <code>RandomizedSet</code> object.</li> <li><code>bool insert(int val)</code> Inserts an item <code>val</code> into the set if not present. Returns <code>true</code> if the item was not present, <code>false</...
54.632014
Medium
[ "array", "hash-table", "math", "design", "randomized" ]
{"lang": "python3", "distribution": [["198", 0.0146], ["202", 0.029], ["206", 0.0508], ["210", 0.1089], ["215", 0.1017], ["219", 0.2249], ["223", 0.3555], ["228", 0.885], ["232", 1.2767], ["236", 2.394], ["240", 4.7157], ["245", 5.9054], ["249", 6.841200000000001], ["253", 6.9429], ["257", 6.797699999999999], ["262", 4...
{"lang": "python3", "distribution": [["62100", 0.0145], ["62200", 0.0073], ["62300", 0.0145], ["62400", 0.0145], ["62500", 0.0073], ["62600", 0.029], ["62700", 0.029], ["62800", 0.0218], ["62900", 0.0653], ["63000", 0.0508], ["63100", 0.5949], ["63200", 2.1474], ["63300", 0.8923], ["63400", 2.3215], ["63500", 2.8149], ...
[ { "code": "class RandomizedSet:\n\n def __init__(self):\n self.data = {}\n self.nums = []\n\n def insert(self, val: int) -> bool:\n if val in self.data:\n return False\n \n self.nums.append(val)\n self.data[val] = len(self.nums) - 1\n return True...
147
[ { "code": "import random\nclass RandomizedSet:\n\n def __init__(self):\n self.setContents = set()\n \n\n def insert(self, val: int) -> bool:\n if val in self.setContents:\n return False\n self.setContents.add(val)\n return True\n\n def remove(self, val: int...
27
381
insert-delete-getrandom-o1-duplicates-allowed
<p><code>RandomizedCollection</code> is a data structure that contains a collection of numbers, possibly duplicates (i.e., a multiset). It should support inserting and removing specific elements and also reporting a random element.</p> <p>Implement the <code>RandomizedCollection</code> class:</p> <ul> <li><code>Rand...
35.615737
Hard
[ "array", "hash-table", "math", "design", "randomized" ]
{"lang": "python3", "distribution": [["229", 0.232], ["236", 0.5800000000000001], ["243", 0.9279999999999999], ["250", 0.6960000000000001], ["257", 3.9443], ["263", 9.0486], ["270", 12.993], ["277", 14.5012], ["284", 9.5127], ["291", 7.076400000000001], ["298", 6.9607], ["305", 6.148499999999999], ["312", 4.29240000000...
{"lang": "python3", "distribution": [["66500", 0.116], ["66600", 0.58], ["66700", 0.8121], ["66800", 3.2483], ["66900", 2.7842], ["67000", 0.9281], ["67100", 1.2761], ["67200", 1.2761], ["67300", 0.8121], ["67400", 0.58], ["67600", 1.6241], ["67700", 1.7401], ["67800", 1.5081], ["67900", 1.6241], ["68000", 1.5081], ["6...
[ { "code": "class RandomizedCollection:\n def __init__(self):\n self.dict = collections.defaultdict(set)\n self.lst = []\n \n def insert(self, val: int) -> bool:\n self.dict[val].add(len(self.lst))\n self.lst.append(val)\n return len(self.dict[val]) == 1\n \...
77
[ { "code": "class RandomizedCollection:\n\n def __init__(self):\n self.a = []\n\n def insert(self, val: int) -> bool:\n if val in self.a:\n self.a.append(val)\n return False\n else:\n self.a.append(val)\n return True\n \n def remove...
27
382
linked-list-random-node
<p>Given a singly linked list, return a random node&#39;s value from the linked list. Each node must have the <strong>same probability</strong> of being chosen.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(ListNode head)</code> Initializes the object with the head of the singly-linke...
63.409736
Medium
[ "linked-list", "math", "reservoir-sampling", "randomized" ]
{"lang": "python3", "distribution": [["40", 0.1093], ["42", 0.2186], ["43", 0.3279], ["44", 0.6557], ["45", 0.5464], ["46", 0.765], ["47", 0.765], ["48", 0.8743], ["49", 1.6393], ["50", 1.5301], ["51", 1.4208], ["52", 2.623], ["53", 2.7322], ["54", 3.2787], ["55", 5.3552], ["56", 3.388], ["57", 3.2787], ["58", 4.0437],...
{"lang": "python3", "distribution": [["18900", 0.1093], ["19000", 0.3279], ["19100", 2.623], ["19200", 26.6667], ["19300", 52.0219], ["19400", 12.459], ["19500", 0.6557]]}
[ { "code": "from random import choice\n\n\n# 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\n def __init__(self, head: Optional[ListNode]):\n self.vals = []\n while head i...
80
[ { "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\n def __init__(self, head: Optional[ListNode]):\n self.head = head\n\n def get_length(self):\n count = 0\n ...
10
383
ransom-note
<p>Given two strings <code>ransomNote</code> and <code>magazine</code>, return <code>true</code><em> if </em><code>ransomNote</code><em> can be constructed by using the letters from </em><code>magazine</code><em> and </em><code>false</code><em> otherwise</em>.</p> <p>Each letter in <code>magazine</code> can only be us...
62.614992
Easy
[ "hash-table", "string", "counting" ]
{"lang": "python3", "distribution": [["17", 0.0037], ["20", 0.0037], ["22", 0.0148], ["23", 0.0111], ["24", 0.0185], ["25", 0.0148], ["26", 0.0554], ["27", 0.0406], ["28", 0.0665], ["29", 0.0849], ["30", 0.2401], ["31", 0.2068], ["32", 0.2511], ["33", 0.3361], ["34", 0.4986], ["35", 0.5134], ["36", 0.4912], ["37", 0.63...
{"lang": "python3", "distribution": [["16000", 0.0037], ["16200", 0.0037], ["16300", 0.048], ["16400", 0.7202], ["16500", 9.0006], ["16600", 26.1375], ["16700", 41.9634], ["16800", 14.921]]}
[ { "code": "class Solution:\n def canConstruct(self, ransomNote: str, magazine: str) -> bool:\n if len(ransomNote) > len(magazine):\n return False\n \n for c in set(ransomNote):\n if magazine.count(c) < ransomNote.count(c):\n return False\n ...
110
[ { "code": "class Solution:\n def canConstruct(self, ransomNote: str, magazine: str) -> bool:\n if len(magazine) < len(ransomNote):\n return False\n \n s = {}\n for i, c in enumerate(magazine):\n if c in s:\n s[c] += 1\n else:\n ...
13
384
shuffle-an-array
<p>Given an integer array <code>nums</code>, design an algorithm to randomly shuffle the array. All permutations of the array should be <strong>equally likely</strong> as a result of the shuffling.</p> <p>Implement the <code>Solution</code> class:</p> <ul> <li><code>Solution(int[] nums)</code> Initializes the object...
58.536316
Medium
[ "array", "math", "randomized" ]
{"lang": "python3", "distribution": [["87", 0.0621], ["88", 0.1241], ["89", 0.0621], ["90", 0.1241], ["91", 0.1862], ["92", 0.1862], ["93", 0.3104], ["94", 0.3104], ["95", 0.5587], ["96", 0.6828], ["97", 0.6207], ["98", 1.3035], ["99", 1.0552], ["100", 1.9243], ["101", 1.9863], ["102", 1.9243], ["103", 2.2346], ["104",...
{"lang": "python3", "distribution": [["19700", 0.0621], ["19800", 0.0621], ["19900", 1.0552], ["20000", 14.401], ["20100", 60.211]]}
[ { "code": "import math\nimport random\nclass doubleLinked:\n def __init__(self, val=-1, prev=None, next=None):\n self.val = val\n self.prev = prev\n self.next = next\n\nclass Solution:\n\n def __init__(self, nums: List[int]):\n self.original = nums\n\n def reset(self) -> Lis...
56
[ { "code": "class Solution:\n def __init__(self, nums: List[int]):\n self.nums = nums\n\n def reset(self) -> List[int]:\n return self.nums\n\n def shuffle(self) -> List[int]:\n keys = self.nums.copy()\n perm = []\n while keys:\n index = floor(random.random()...
7
385
mini-parser
<p>Given a string s represents the serialization of a nested list, implement a parser to deserialize it and return <em>the deserialized</em> <code>NestedInteger</code>.</p> <p>Each element is either an integer or a list whose elements may also be integers or other lists.</p> <p>&nbsp;</p> <p><strong class="example">E...
38.763476
Medium
[ "string", "stack", "depth-first-search" ]
{"lang": "python3", "distribution": [["35", 0.2591], ["39", 0.2591], ["40", 0.2591], ["41", 1.0363], ["42", 1.2953], ["43", 1.2953], ["44", 0.7772], ["45", 1.0363], ["46", 3.6269], ["47", 3.1088], ["48", 3.1088], ["49", 0.7772], ["50", 3.3679], ["51", 7.772], ["52", 6.4767], ["53", 6.2176], ["54", 4.6632], ["55", 6.735...
{"lang": "python3", "distribution": [["18100", 0.5181], ["18200", 0.7772], ["18300", 0.2591], ["18400", 0.7772], ["18500", 0.5181], ["18900", 2.8497], ["19000", 34.9741], ["19100", 39.8964], ["19200", 4.6632], ["19300", 2.0725], ["19400", 0.7772], ["19500", 0.2591], ["19700", 0.2591], ["20000", 0.2591], ["20100", 1.036...
[ { "code": "# \"\"\"\n# This is the interface that allows for creating nested lists.\n# You should not implement it, or speculate about its implementation\n# \"\"\"\n#class NestedInteger:\n# def __init__(self, value=None):\n# \"\"\"\n# If value is not specified, initializes an empty list.\n# ...
36
[ { "code": "class NestedInteger:\n def __init__(self, value=None):\n if value is None:\n self.is_integer = False\n self.value = []\n elif isinstance(value, int):\n self.is_integer = True\n self.value = value\n else:\n raise ValueError...
24
386
lexicographical-numbers
<p>Given an integer <code>n</code>, return all the numbers in the range <code>[1, n]</code> sorted in lexicographical order.</p> <p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and uses <code>O(1)</code> extra space.&nbsp;</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></...
64.739153
Medium
[ "depth-first-search", "trie" ]
{"lang": "python3", "distribution": [["42", 0.24], ["44", 0.2401], ["46", 0.12], ["49", 0.4802], ["51", 0.3601], ["53", 0.7203], ["56", 0.9604], ["58", 1.4405000000000001], ["60", 3.6015], ["63", 4.0816], ["65", 4.2017], ["67", 10.2041], ["70", 5.5222], ["72", 7.8031], ["74", 10.444199999999999], ["77", 5.5222], ["79",...
{"lang": "python3", "distribution": [["20400", 0.12], ["20500", 0.12], ["22000", 1.0804], ["22100", 3.1212], ["22200", 3.3613], ["22300", 5.042], ["22400", 12.7251], ["22500", 7.2029], ["22600", 1.0804], ["22700", 0.8403], ["22800", 1.5606], ["22900", 2.1609], ["23000", 0.3601], ["23100", 0.9604], ["23200", 2.1609], ["...
[ { "code": "class Solution:\n def lexicalOrder(self, n: int) -> List[int]:\n return sorted(range(1, n + 1), key=str)\n\nwith open(\"user.out\", \"w\") as f:\n inputs = map(loads, stdin)\n for nums in inputs:\n print(str(Solution().lexicalOrder(nums)).replace(\" \", \"\"), file=f)\nexit(0)\...
109
[ { "code": "class Solution:\n def lexicalOrder(self, n: int) -> List[int]:\n results = []\n \n def bkt(n: int, k: int, current: int):\n start = int(k == 0)\n for i in range(start, 10):\n if current * 10 + i <= n:\n yield current * 10...
50
387
first-unique-character-in-a-string
<p>Given a string <code>s</code>, find the <strong>first</strong> non-repeating character in it and return its index. If it <strong>does not</strong> exist, return <code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="...
62.554928
Easy
[ "hash-table", "string", "queue", "counting" ]
{"lang": "python3", "distribution": [["27", 0.0499], ["30", 0.01], ["34", 0.0299], ["37", 0.1199], ["40", 0.0699], ["44", 0.06989999999999999], ["47", 0.11979999999999999], ["50", 0.2695], ["54", 0.3094], ["57", 0.7586], ["60", 0.7885], ["64", 1.0181], ["67", 1.5371], ["70", 1.1279000000000001], ["74", 1.2576], ["77", ...
{"lang": "python3", "distribution": [["16400", 0.01], ["16500", 0.0299], ["16600", 0.02], ["16700", 0.02], ["16800", 0.0898], ["16900", 1.0081], ["17000", 15.0514], ["17100", 53.3187], ["17200", 18.0457], ["17300", 4.9007], ["17400", 2.106]]}
[ { "code": "class Solution:\n def firstUniqChar(self, s: str) -> int:\n \"\"\"map = {}\n for i in s:\n if i in map:\n map[i] = map[i] + 1\n else:\n map[i] = 1\n \n for i in map:\n if map[i] == 1:\n ...
141
[ { "code": "class Solution:\n def firstUniqChar(self, s: str) -> int:\n for i in range(len(s)):\n if not s[i] in (s[i + 1:] + s[:i]):\n return i\n else:\n return -1\n ", "memory": "16400" }, { "code": "class Solution:\n def firstUniqChar...
12
389
find-the-difference
<p>You are given two strings <code>s</code> and <code>t</code>.</p> <p>String <code>t</code> is generated by random shuffling string <code>s</code> and then add one more letter at a random position.</p> <p>Return the letter that was added to <code>t</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</st...
59.565891
Easy
[ "hash-table", "string", "bit-manipulation", "sorting" ]
{"lang": "python3", "distribution": [["18", 0.0456], ["19", 0.0608], ["20", 0.0608], ["21", 0.076], ["22", 0.2583], ["23", 0.4407], ["24", 0.3799], ["25", 0.4862], ["26", 0.9117], ["27", 1.4739], ["28", 1.2308], ["29", 1.246], ["30", 2.7351], ["31", 3.7532], ["32", 3.3126], ["33", 3.6621], ["34", 5.7438], ["35", 6.1389...
{"lang": "python3", "distribution": [["16200", 0.0608], ["16300", 0.3039], ["16400", 7.7952], ["16500", 31.91], ["16600", 44.0814]]}
[ { "code": "class Solution:\n def findTheDifference(self, s: str, t: str) -> str:\n s_bin = [0] * 26\n t_bin = [0] * 26\n for i in s:\n s_bin[ord(i) - ord(\"a\")] += 1 # Count the occurrences of each character in s\n for i in t:\n t_bin[ord(i) - ord(\"a\")] +...
59
[ { "code": "class Solution:\n def findTheDifference(self, s: str, t: str) -> str:\n s = sorted(s)\n t = sorted(t)\n\n for i in range(len(t)-1):\n if s[i]!=t[i]:\n return t[i]\n \n return t[-1]\n\n \n\n\n ", "memory": "16200...
8
390
elimination-game
<p>You have a list <code>arr</code> of all integers in the range <code>[1, n]</code> sorted in a strictly increasing order. Apply the following algorithm on <code>arr</code>:</p> <ul> <li>Starting from left to right, remove the first number and every other number afterward until you reach the end of the list.</li> <...
45.718634
Medium
[ "math", "recursion" ]
{"lang": "python3", "distribution": [["23", 0.1873], ["24", 0.1873], ["25", 0.3745], ["26", 0.1873], ["27", 0.5618], ["28", 1.1236], ["29", 0.3745], ["30", 1.3109], ["31", 2.2472], ["32", 2.0599], ["33", 1.1236], ["34", 2.809], ["35", 5.8052], ["36", 4.8689], ["37", 3.7453], ["38", 3.3708], ["39", 8.2397], ["40", 6.179...
{"lang": "python3", "distribution": [["16300", 0.1873], ["16400", 7.1161], ["16500", 30.1498], ["16600", 44.1948]]}
[ { "code": "class Solution:\n def lastRemaining(self, n: int) -> int:\n left = True # flag to keep track of left-to-right or right-to-left elimination\n remaining = n # keep track of the remaining numbers\n step = 1 # keep track of the step size\n head = 1 # keep track of the h...
36
[ { "code": "class Solution:\n def lastRemaining(self, n: int) -> int:\n I=1\n First=1\n B=True\n while n>1:\n if n%2==0 and B:\n First+=I\n elif n%2!=0:\n First+=I\n B=not B\n I=2*I\n n=n//2 \n ...
7
391
perfect-rectangle
<p>Given an array <code>rectangles</code> where <code>rectangles[i] = [x<sub>i</sub>, y<sub>i</sub>, a<sub>i</sub>, b<sub>i</sub>]</code> represents an axis-aligned rectangle. The bottom-left point of the rectangle is <code>(x<sub>i</sub>, y<sub>i</sub>)</code> and the top-right point of it is <code>(a<sub>i</sub>, b<s...
34.098889
Hard
[ "array", "line-sweep" ]
{"lang": "python3", "distribution": [["81", 0.3115], ["88", 0.3115], ["256", 0.3115], ["260", 0.3115], ["265", 0.3115], ["266", 0.3115], ["267", 0.3115], ["268", 0.6231], ["270", 0.6231], ["271", 0.9346], ["272", 1.2461], ["273", 0.9346], ["274", 0.9346], ["275", 1.8692], ["276", 2.1807], ["277", 1.5576], ["278", 0.623...
{"lang": "python3", "distribution": [["20100", 0.3115], ["20200", 0.3115], ["21500", 0.6231], ["21700", 0.3115], ["21800", 0.6231], ["21900", 0.9346], ["22000", 0.9346], ["22100", 2.4922], ["22200", 25.5452], ["22300", 33.6449], ["22400", 6.8536], ["22500", 0.3115], ["22600", 0.9346], ["22900", 0.3115], ["23300", 0.311...
[ { "code": "class Solution:\n def isRectangleCover(self, rectangles: List[List[int]]) -> bool:\n left, right, top, bot= float('inf'), float('-inf'), float('-inf'), float('inf')\n area = 0\n corners = set()\n\n for x, y, a, b in rectangles:\n left = min(left, x)\n ...
80
[ { "code": "class Solution:\n def isRectangleCover(self, rectangles: List[List[int]]) -> bool:\n left, right, top, bot= float('inf'), float('-inf'), float('-inf'), float('inf')\n area = 0\n corners = set()\n\n for x, y, a, b in rectangles:\n left = min(left, x)\n ...
37
392
is-subsequence
<p>Given two strings <code>s</code> and <code>t</code>, return <code>true</code><em> if </em><code>s</code><em> is a <strong>subsequence</strong> of </em><code>t</code><em>, or </em><code>false</code><em> otherwise</em>.</p> <p>A <strong>subsequence</strong> of a string is a new string that is formed from the original...
48.088948
Easy
[ "two-pointers", "string", "dynamic-programming" ]
{"lang": "python3", "distribution": [["12", 0.0026], ["14", 0.0026], ["15", 0.0158], ["16", 0.0185], ["17", 0.0079], ["18", 0.0343], ["19", 0.211], ["20", 0.1266], ["21", 0.124], ["22", 0.4958], ["23", 0.6937], ["24", 0.4774], ["25", 0.7622], ["26", 1.7645], ["27", 2.2788], ["28", 1.4453], ["29", 2.4713], ["30", 5.1642...
{"lang": "python3", "distribution": [["16000", 0.0026], ["16100", 0.0105], ["16200", 0.0185], ["16300", 0.4616], ["16400", 8.2738], ["16500", 26.3299], ["16600", 46.657]]}
[ { "code": "class Solution:\n def isSubsequence(self, s: str, t: str) -> bool:\n counter = len(s)\n j = 0\n for i in range(len(s)):\n if j == len(t):\n break\n while j < len(t):\n if s[i] == t[j]:\n counter -= 1\n ...
53
[ { "code": "class Solution:\n def isSubsequence(self, s: str, t: str) -> bool:\n if len(s) == 0:\n return True\n if len(s) > len(t):\n return False\n sind = 0; tind = 0;\n while tind < len(t):\n if s[sind] == t[tind]:\n sind = sind +...
10
393
utf-8-validation
<p>Given an integer array <code>data</code> representing the data, return whether it is a valid <strong>UTF-8</strong> encoding (i.e. it translates to a sequence of valid UTF-8 encoded characters).</p> <p>A character in <strong>UTF8</strong> can be from <strong>1 to 4 bytes</strong> long, subjected to the following ru...
45.238052
Medium
[ "array", "bit-manipulation" ]
{"lang": "python3", "distribution": [["71", 0.1757], ["73", 0.3515], ["74", 0.5272], ["76", 0.703], ["77", 0.3515], ["78", 1.2302], ["79", 0.3515], ["80", 0.3515], ["81", 1.5817], ["82", 1.9332], ["83", 2.109], ["84", 2.4605], ["85", 3.8664], ["86", 4.3937], ["87", 2.109], ["88", 3.6907], ["89", 4.2179], ["90", 4.2179]...
{"lang": "python3", "distribution": [["16600", 1.2302], ["16700", 10.7206], ["16800", 18.6292], ["16900", 32.3374], ["17000", 9.4903], ["17100", 2.109], ["17200", 1.2302], ["17300", 0.5272], ["17400", 0.8787], ["17500", 2.6362], ["17600", 6.8541], ["17700", 4.5694], ["17800", 2.2847], ["17900", 1.2302], ["18000", 0.175...
[ { "code": "class Solution:\n def validUtf8(self, data: List[int]) -> bool:\n num = 0\n mask1 = 1 << 7\n mask2 = 1 << 6\n\n for byte in data:\n mask = 1 << 7\n if num == 0:\n while (mask&byte):\n num += 1\n ...
65
[ { "code": "class Solution:\n def validUtf8(self, data: List[int]) -> bool:\n def ones(num):\n \n ans = 0\n done = False\n\n for i in range(8):\n if num % 2:\n ans += 1\n else:\n ans = 0\n ...
18
394
decode-string
<p>Given an encoded string, return its decoded string.</p> <p>The encoding rule is: <code>k[encoded_string]</code>, where the <code>encoded_string</code> inside the square brackets is being repeated exactly <code>k</code> times. Note that <code>k</code> is guaranteed to be a positive integer.</p> <p>You may assume th...
59.856204
Medium
[ "string", "stack", "recursion" ]
{"lang": "python3", "distribution": [["12", 0.0073], ["14", 0.0073], ["15", 0.0147], ["16", 0.066], ["17", 0.0147], ["18", 0.088], ["19", 0.176], ["20", 0.264], ["21", 0.2054], ["22", 0.7115], ["23", 0.6381], ["24", 1.2249], ["25", 1.1442], ["26", 2.6991], ["27", 1.8337], ["28", 3.3446], ["29", 3.704], ["30", 5.7943], ...
{"lang": "python3", "distribution": [["15900", 0.0073], ["16000", 0.0073], ["16100", 0.0073], ["16200", 0.2787], ["16300", 4.7602], ["16400", 21.9085], ["16500", 41.2572]]}
[ { "code": "class Solution:\n def decodeString(self, s: str) -> str:\n stack = []\n for char in s:\n if char != ']':\n stack.append(char)\n else:\n num = \"\"\n temp = \"\"\n while stack and stack[-1] != '[':\n ...
45
[ { "code": "class Solution:\n def decodeString(self, s: str) -> str:\n stack = []\n curstr = ''\n curnum = 0\n for i in s:\n if i.isdigit():\n curnum = curnum * 10 + int(i)\n elif i == '[':\n stack.append(curnum)\n ...
11
395
longest-substring-with-at-least-k-repeating-characters
<p>Given a string <code>s</code> and an integer <code>k</code>, return <em>the length of the longest substring of</em> <code>s</code> <em>such that the frequency of each character in this substring is greater than or equal to</em> <code>k</code>.</p> <p data-pm-slice="1 1 []">if no such substring exists, return 0.</p>...
45.096334
Medium
[ "hash-table", "string", "divide-and-conquer", "sliding-window" ]
{"lang": "python3", "distribution": [["33", 35.2739], ["62", 11.144499999999999], ["90", 13.084799999999998], ["119", 2.8363999999999985], ["148", 2.5879999999999983], ["177", 0.9457], ["205", 0.6472], ["234", 0.39830000000000004], ["263", 0.29869999999999997], ["292", 0.29869999999999997], ["321", 0.2988], ["349", 0.2...
{"lang": "python3", "distribution": [["16463", 65.6717], ["16991", 2.7861999999999996], ["17518", 0.4479], ["18046", 0.1991], ["18573", 0.5474], ["19101", 0.0996], ["19628", 0.1493], ["20156", 0.1493], ["20683", 0.0498], ["21211", 0.0498], ["21738", 0.0498], ["22266", 0.0498], ["22793", 0.0995], ["23321", 0.0498], ["23...
[ { "code": "class Solution:\n def longestSubstring(self, s: str, k: int) -> int:\n if len(s) < k:\n return 0\n for c in set(s):\n if s.count(c) < k:\n return max(self.longestSubstring(t, k) for t in s.split(c))\n return len(s)", "runtime": "33" }...
110
[ { "code": "class Solution:\n def longestSubstring(self, s: str, k: int) -> int:\n res = 0\n max_chars = len(set(s))\n for available_char in range(1, max_chars+1):\n freq = defaultdict(int)\n l, r = 0, 0\n for r in range(len(s)):\n \n ...
103
396
rotate-function
<p>You are given an integer array <code>nums</code> of length <code>n</code>.</p> <p>Assume <code>arr<sub>k</sub></code> to be an array obtained by rotating <code>nums</code> by <code>k</code> positions clock-wise. We define the <strong>rotation function</strong> <code>F</code> on <code>nums</code> as follow:</p> <ul...
42.983322
Medium
[ "array", "math", "dynamic-programming" ]
{"lang": "python3", "distribution": [["250", 0.3338], ["260", 0.1669], ["270", 0.1669], ["281", 0.1669], ["291", 0.1669], ["301", 0.1669], ["312", 0.1669], ["322", 0.1669], ["332", 0.1669], ["343", 0.1669], ["353", 0.3339], ["363", 0.1669], ["374", 0.5008], ["384", 0.3339], ["394", 0.1669], ["405", 0.1669], ["415", 0.3...
{"lang": "python3", "distribution": [["22400", 0.1669], ["23700", 0.5008], ["23800", 0.6678], ["24200", 0.5008], ["24500", 0.1669], ["25200", 0.8347], ["25300", 6.177], ["25400", 12.3539], ["25500", 8.5142], ["25600", 8.1803], ["25700", 4.8414], ["25800", 26.5442], ["25900", 18.5309], ["26000", 2.1703], ["26100", 1.502...
[ { "code": "class Solution:\n def maxRotateFunction(self, nums: List[int]) -> int:\n total = sum(nums)\n n = len(nums)\n f = sum(i * nums[i] for i in range(n))\n ans = float(-inf)\n\n for i in range(n - 1, -1, -1):\n f += total\n f -= n * nums[i]\n ...
89
[ { "code": "class Solution:\n def maxRotateFunction(self, nums: List[int]) -> int:\n total = sum(nums)\n n = len(nums)\n f = sum(i * nums[i] for i in range(n))\n ans = float(-inf)\n\n for i in range(n - 1, -1, -1):\n f += total\n f -= n * nums[i]\n ...
31
397
integer-replacement
<p>Given a positive integer <code>n</code>,&nbsp;you can apply one of the following&nbsp;operations:</p> <ol> <li>If <code>n</code> is even, replace <code>n</code> with <code>n / 2</code>.</li> <li>If <code>n</code> is odd, replace <code>n</code> with either <code>n + 1</code> or <code>n - 1</code>.</li> </ol> <p>R...
36.078834
Medium
[ "dynamic-programming", "greedy", "bit-manipulation", "memoization" ]
{"lang": "python3", "distribution": [["20", 0.6128], ["22", 1.1236], ["24", 3.1665], ["26", 3.4729], ["28", 6.2308], ["30", 7.6609], ["32", 11.236], ["34", 10.6231], ["36", 11.6445], ["38", 7.763], ["40", 4.4944], ["42", 2.6558], ["44", 0.4086], ["46", 0.4086], ["48", 0.1021], ["50", 0.3064], ["52", 0.1021], ["54", 0.2...
{"lang": "python3", "distribution": [["16200", 0.1021], ["16300", 1.2257], ["16400", 11.6445], ["16500", 30.2349], ["16600", 33.81], ["16700", 12.1553], ["16800", 5.1073]]}
[ { "code": "class Solution:\n def integerReplacement(self, n: int) -> int:\n if n == 1:\n return 0\n if n == 2:\n return 1\n \n ops = 0\n while (n > 1):\n if n == 3: \n return ops + 2\n\n if n % 2 == 0:\n ...
102
[ { "code": "class Solution:\n def integerReplacement(self, n: int) -> int:\n operations = 0\n while n > 1:\n if n % 2 == 0:\n n //= 2\n elif n == 3 or n % 4 == 1:\n n -= 1\n else:\n n += 1\n operations += 1\...
12