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
3,437
maximum-total-damage-with-spell-casting
<p>A magician has various spells.</p> <p>You are given an array <code>power</code>, where each element represents the damage of a spell. Multiple spells can have the same damage value.</p> <p>It is a known fact that if a magician decides to cast a spell with a damage of <code>power[i]</code>, they <strong>cannot</str...
26.376421
Medium
[ "array", "hash-table", "two-pointers", "binary-search", "dynamic-programming", "sorting", "counting" ]
{"lang": "python3", "distribution": [["1140", 0.5646], ["1157", 0.7526], ["1174", 0.8467], ["1190", 1.4109999999999998], ["1207", 2.5399000000000003], ["1224", 2.0697], ["1241", 4.1389000000000005], ["1258", 3.4804], ["1274", 3.5748000000000006], ["1291", 3.1039999999999996], ["1308", 3.0101], ["1325", 3.5748], ["1342"...
{"lang": "python3", "distribution": [["28751", 2.634], ["31255", 1.7876000000000005], ["33759", 1.9757000000000002], ["36263", 21.730999999999998], ["38766", 23.2362], ["41270", 6.9613], ["43774", 9.4069], ["46278", 2.3518000000000003], ["48781", 0.6586], ["51285", 0.6587], ["53789", 0.47050000000000003], ["56293", 0.8...
[ { "code": "class Solution:\n def maximumTotalDamage(self, power: List[int]) -> int:\n points = {}\n\n # Calculate total value from each damage\n for value in power:\n if value in points:\n points[value] += value\n else:\n points[value] ...
131
[ { "code": "class Solution:\n def maximumTotalDamage(self, power: List[int]) -> int:\n power.sort()\n dp = [0] + [p for p in power]\n max_power = [0 for _ in range(len(power) + 1)]\n max_power[1] = power[0]\n\n for i in range(1, len(power)):\n # If the spell has t...
117
3,438
peaks-in-array
<p>A <strong>peak</strong> in an array <code>arr</code> is an element that is <strong>greater</strong> than its previous and next element in <code>arr</code>.</p> <p>You are given an integer array <code>nums</code> and a 2D integer array <code>queries</code>.</p> <p>You have to process queries of two types:</p> <ul>...
27.094146
Hard
[ "array", "binary-indexed-tree", "segment-tree" ]
{"lang": "python3", "distribution": [["962", 3.1496], ["1038", 0.7874], ["1115", 0.7874], ["1191", 1.5748], ["1268", 0.7874], ["1344", 0.7874], ["1421", 0.7874], ["1497", 0.7874], ["1574", 0.7874], ["1650", 0.7874], ["1727", 0.7874], ["1803", 0.7874], ["1880", 0.7874], ["1956", 0.7874], ["2033", 0.7874], ["2109", 0.787...
{"lang": "python3", "distribution": [["57000", 1.5748], ["57600", 0.7874], ["73800", 0.7874], ["75100", 0.7874], ["76100", 0.7874], ["76400", 0.7874], ["76900", 0.7874], ["77000", 0.7874], ["77200", 2.3622], ["77300", 2.3622], ["77400", 1.5748], ["77500", 1.5748], ["77600", 0.7874], ["77900", 3.1496], ["78000", 11.0236...
[ { "code": "class BIT:\n def __init__(self,n):\n self.tree = [0] * (n+1)\n self.n = n\n def update(self,idx,val):\n while idx <= (self.n):\n self.tree[idx] += val\n idx += (idx & -idx)\n def query(self,idx):\n ans = 0\n while idx > 0 :\n ...
87
[ { "code": "class BIT:\n def __init__(self,n):\n self.tree = [0] * (n+1)\n self.n = n\n def update(self,idx,val):\n while idx <= (self.n):\n self.tree[idx] += val\n idx += (idx & -idx)\n def query(self,idx):\n ans = 0\n while idx > 0 :\n ...
49
2,647
split-the-array-to-make-coprime-products
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p> <p>A <strong>split</strong> at an index <code>i</code> where <code>0 &lt;= i &lt;= n - 2</code> is called <strong>valid</strong> if the product of the first <code>i + 1</code> elements and the product of the rem...
23.278834
Hard
[ "array", "hash-table", "math", "number-theory" ]
{"lang": "python3", "distribution": [["204", 1.3158], ["236", 1.3158], ["381", 1.3158], ["382", 1.3158], ["532", 1.3158], ["535", 1.3158], ["585", 1.3158], ["597", 1.3158], ["602", 1.3158], ["627", 1.3158], ["887", 1.3158], ["899", 1.3158], ["905", 1.3158], ["926", 1.3158], ["1000", 1.3158], ["1043", 1.3158], ["1135", ...
{"lang": "python3", "distribution": [["18000", 6.5789], ["18100", 14.4737], ["18200", 9.2105], ["18300", 6.5789], ["18400", 1.3158], ["18500", 5.2632], ["18600", 3.9474], ["18700", 1.3158], ["18800", 1.3158], ["19000", 2.6316], ["19100", 3.9474], ["19200", 1.3158], ["19500", 2.6316], ["19600", 2.6316], ["19900", 1.3158...
[ { "code": "class Solution:\n def __init__(self):\n self.cache = {}\n self.primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]\n\n def findValidSplit(self, nums: List[int]) -> int:\n primeRanges = {}\n index = 0\n for num in nums:\n ...
68
[ { "code": "class Solution:\n def findValidSplit(self, nums: List[int]) -> int:\n ans = 0\n for i, a in enumerate(nums[:-1]):\n for j, b in enumerate(nums[ans:], ans):\n if gcd(a, b) != 1:\n ans = j\n if i == ans:\n return an...
38
2,663
distribute-money-to-maximum-children
<p>You are given an integer <code>money</code> denoting the amount of money (in dollars) that you have and another integer <code>children</code> denoting the number of children that you must distribute the money to.</p> <p>You have to distribute the money according to the following rules:</p> <ul> <li>All money must...
19.257891
Easy
[ "math", "greedy" ]
{"lang": "python3", "distribution": [["23", 0.3521], ["24", 0.3521], ["25", 0.1761], ["26", 0.5282], ["27", 0.8803], ["28", 0.7042], ["29", 0.3521], ["30", 0.5282], ["31", 1.5845], ["32", 1.7606], ["33", 1.2324], ["34", 1.5845], ["35", 3.3451], ["36", 4.5775], ["37", 4.4014], ["38", 5.4577], ["39", 5.1056], ["40", 4.75...
{"lang": "python3", "distribution": [["16200", 0.3521], ["16300", 0.5282], ["16400", 9.8592], ["16500", 40.669], ["16600", 36.2676]]}
[ { "code": "class Solution:\n def distMoney(self, money: int, children: int) -> int:\n if money < children:\n return -1\n\n money -= children\n if money / 7 > children:\n return children - 1\n\n if money % 7 == 3 and money // 7 == children - 1:\n re...
38
[ { "code": "class Solution:\n def distMoney(self, money: int, children: int) -> int:\n extra_money = money - children\n quotient = extra_money // 7\n\n if extra_money < 0:\n return -1\n elif 0 <= extra_money < 7:\n return 0 \n else:\n if e...
6
2,664
maximize-greatness-of-an-array
<p>You are given a 0-indexed integer array <code>nums</code>. You are allowed to permute <code>nums</code> into a new array <code>perm</code> of your choosing.</p> <p>We define the <strong>greatness</strong> of <code>nums</code> be the number of indices <code>0 &lt;= i &lt; nums.length</code> for which <code>perm[i] &...
58.427224
Medium
[ "array", "two-pointers", "greedy", "sorting" ]
{"lang": "python3", "distribution": [["382", 0.8001], ["385", 0.2667], ["389", 0.2667], ["392", 1.0667], ["395", 2.9333], ["399", 4.5334], ["402", 7.199999999999999], ["405", 3.7333000000000003], ["409", 7.466600000000001], ["412", 9.8667], ["415", 4.800000000000001], ["419", 7.7333], ["422", 7.7333], ["425", 4.5333000...
{"lang": "python3", "distribution": [["27200", 0.2667], ["27400", 0.2667], ["27900", 0.5333], ["30400", 0.5333], ["30500", 3.7333], ["30600", 19.2], ["30700", 11.2], ["30800", 29.3333], ["30900", 14.1333], ["31000", 0.8], ["31100", 0.2667], ["31200", 1.0667], ["31400", 0.2667], ["31500", 1.3333], ["31600", 1.3333], ["3...
[ { "code": "class Solution:\n def maximizeGreatness(self, nums: List[int]) -> int:\n return len(nums) - max(collections.Counter(nums).values())\n\n # Why does this work:\n # Let us divide the sorted array into 2 parts\n # Now if\n # A[0] < A[k]\n # A[1] < A[k+1]\n ...
71
[ { "code": "class Solution:\n def maximizeGreatness(self, nums: List[int]) -> int:\n heapify(nums)\n pq = []\n heapify(pq)\n res = 0\n\n while nums:\n if pq and pq[0] < nums[0]:\n n = heappop(nums)\n heappop(pq)\n heapp...
22
2,695
find-score-of-an-array-after-marking-all-elements
<p>You are given an array <code>nums</code> consisting of positive integers.</p> <p>Starting with <code>score = 0</code>, apply the following algorithm:</p> <ul> <li>Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.</li> <li>Add the value of the ...
52.555002
Medium
[ "array", "hash-table", "sorting", "heap-priority-queue", "simulation" ]
{"lang": "python3", "distribution": [["522", 0.8968], ["532", 0.4484], ["542", 0.4484], ["552", 0.4484], ["562", 0.4484], ["572", 0.4484], ["582", 0.4484], ["592", 0.8969], ["602", 0.4484], ["612", 0.4484], ["623", 0.4484], ["633", 0.4484], ["643", 0.4484], ["653", 0.4484], ["663", 0.4484], ["673", 0.4484], ["683", 0.4...
{"lang": "python3", "distribution": [["29500", 0.4484], ["30400", 0.4484], ["30700", 0.4484], ["30800", 0.8969], ["30900", 2.2422], ["31000", 0.4484], ["31200", 0.8969], ["31700", 0.4484], ["34500", 0.8969], ["34800", 0.4484], ["34900", 0.8969], ["35000", 0.4484], ["35100", 0.4484], ["35500", 0.8969], ["35700", 0.4484]...
[ { "code": "from collections import deque\n\nclass Solution:\n def findScore(self, nums: List[int]) -> int:\n score = 0\n n = len(nums)\n # find local minimums\n q = deque()\n for i in range(n):\n if q and q[-1]<=nums[i]:\n skip = False\n ...
114
[ { "code": "class Solution:\n def findScore(self, nums: List[int]) -> int:\n n = len(nums)\n marked = [False] * n\n score = 0\n \n # Sort indices based on their values in the nums array\n indices = sorted(range(n), key=lambda i: nums[i])\n \n for i in in...
81
3,498
minimum-array-changes-to-make-differences-equal
<p>You are given an integer array <code>nums</code> of size <code>n</code> where <code>n</code> is <strong>even</strong>, and an integer <code>k</code>.</p> <p>You can perform some changes on the array, where in one change you can replace <strong>any</strong> element in the array with <strong>any</strong> integer in t...
23.066979
Medium
[ "array", "hash-table", "prefix-sum" ]
{"lang": "python3", "distribution": [["705", 0.4274], ["729", 0.2137], ["753", 0.2137], ["777", 1.4958], ["801", 1.2822], ["825", 3.419200000000001], ["849", 4.4876000000000005], ["873", 11.7526], ["897", 12.179799999999998], ["921", 8.761099999999999], ["945", 5.7697], ["969", 5.5558000000000005], ["993", 1.7096], ["1...
{"lang": "python3", "distribution": [["29200", 0.2137], ["29800", 0.2137], ["30000", 0.2137], ["30400", 0.4274], ["30500", 0.2137], ["30600", 0.8547], ["30700", 6.1966], ["30800", 21.1538], ["30900", 15.3846], ["31000", 8.9744], ["31100", 3.8462], ["31200", 1.4957], ["31300", 1.4957], ["31400", 2.5641], ["31500", 1.923...
[ { "code": "class Solution:\n def minChanges(self, nums: List[int], k: int) -> int:\n mid = len(nums) // 2\n lst = [abs(i - j) for i, j in zip(nums, nums[mid:][::-1])]\n thres = ceil(k / 2)\n def helper(val):\n if val <= thres:\n return 0\n res ...
87
[ { "code": "class Solution:\n def minChanges(self, nums: List[int], k: int) -> int:\n pairs = [(min(nums[i], nums[~i]), max(nums[i], nums[~i])) for i in range(len(nums) // 2)]\n diff = Counter([large - small for small, large in pairs])\n limitations = sorted([max(large - 0, k - small) for...
69
3,470
maximum-score-from-grid-operations
<p>You are given a 2D matrix <code>grid</code> of size <code>n x n</code>. Initially, all cells of the grid are colored white. In one operation, you can select any cell of indices <code>(i, j)</code>, and color black all the cells of the <code>j<sup>th</sup></code> column starting from the top row down to the <code>i<s...
27.599076
Hard
[ "array", "dynamic-programming", "matrix", "prefix-sum" ]
{"lang": "python3", "distribution": [["328", 2.778], ["417", 0.463], ["506", 0.463], ["595", 0.463], ["684", 0.463], ["773", 0.463], ["862", 0.463], ["951", 0.463], ["1040", 0.463], ["1129", 0.463], ["1219", 0.463], ["1308", 0.463], ["1397", 0.463], ["1486", 0.463], ["1575", 0.463], ["1664", 0.463], ["1753", 0.463], ["...
{"lang": "python3", "distribution": [["17500", 0.463], ["17700", 5.5556], ["17800", 20.3704], ["17900", 28.2407], ["18000", 12.5], ["18100", 4.6296], ["18200", 1.8519], ["18300", 0.463], ["18400", 0.463], ["18500", 2.3148], ["18600", 2.7778], ["18700", 1.3889], ["18800", 1.8519], ["18900", 0.9259], ["19300", 1.8519], [...
[ { "code": "inf = 10**15\nfmax = lambda x, y: x if x > y else y\n\nclass Solution:\n def maximumScore(self, grid: List[List[int]]) -> int:\n n = len(grid)\n accs = [[0] * (n + 1) for _ in range(n + 1)]\n for i in range(n):\n for j in range(n):\n accs[j + 1][i + 1...
101
[ { "code": "class Solution:\n def maximumScore(self, grid: List[List[int]]) -> int:\n n = len(grid)\n if n == 1:\n return 0\n\n prev_col_w = [0 for _ in range(n+1)]\n prev_col_wo = [0 for _ in range(n+1)]\n for j in range(1, n): # for each current column\n ...
38
3,462
vowels-game-in-a-string
<p>Alice and Bob are playing a game on a string.</p> <p>You are given a string <code>s</code>, Alice and Bob will take turns playing the following game where Alice starts <strong>first</strong>:</p> <ul> <li>On Alice&#39;s turn, she has to remove any <strong>non-empty</strong> <span data-keyword="substring">substrin...
62.753586
Medium
[ "math", "string", "brainteaser", "game-theory" ]
{"lang": "python3", "distribution": [["28", 0.4718], ["30", 0.1348], ["32", 0.4717], ["33", 0.3369], ["35", 0.5391], ["37", 0.876], ["39", 0.6065], ["40", 2.1563999999999997], ["42", 0.7413000000000001], ["44", 0.6739], ["45", 1.4825], ["47", 1.4825], ["49", 1.5499], ["50", 0.6065], ["52", 1.8868], ["54", 2.0216], ["56...
{"lang": "python3", "distribution": [["17100", 0.0674], ["17200", 0.2022], ["17300", 6.0647], ["17400", 49.7305], ["17500", 31.9407], ["17600", 6.2668], ["17700", 0.6739]]}
[ { "code": "class Solution:\n def doesAliceWin(self, s: str) -> bool:\n for x in {'a', 'e', 'i', 'o', 'u'}:\n if x in s: return True\n return False\n", "runtime": "28" }, { "code": "class Solution:\n def doesAliceWin(self, s: str) -> bool:\n vowels = [\"a\", \"e\...
131
[ { "code": "class Solution:\n def doesAliceWin(self, s: str) -> bool:\n for char in \"aeiou\":\n if char in s:\n return True\n return False\n\n\n ", "memory": "17100" }, { "code": "class Solution:\n def doesAliceWin(self, s: str) -> bool:\n ...
5
3,493
maximum-number-of-operations-to-move-ones-to-the-end
<p>You are given a <span data-keyword="binary-string">binary string</span> <code>s</code>.</p> <p>You can perform the following operation on the string <strong>any</strong> number of times:</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the string where <code>i + 1 &lt; s.length</code> such that ...
54.079088
Medium
[ "string", "greedy", "counting" ]
{"lang": "python3", "distribution": [["56", 0.162], ["58", 0.081], ["59", 0.2429], ["61", 0.4049], ["62", 0.3239], ["64", 0.2429], ["65", 0.2429], ["67", 0.8907], ["68", 0.4049], ["70", 1.4575], ["71", 0.4049], ["73", 1.4575], ["74", 0.9717], ["76", 1.6194000000000002], ["77", 0.8907], ["79", 1.7812999999999999], ["80"...
{"lang": "python3", "distribution": [["17000", 0.1619], ["17100", 1.3765], ["17200", 12.6316], ["17300", 22.1862], ["17400", 35.2227], ["17500", 7.3684], ["17600", 1.9433], ["17700", 0.7287], ["17800", 0.5668], ["17900", 0.5668], ["18000", 0.5668], ["18100", 1.9433], ["18200", 0.7287], ["18300", 0.4049], ["18400", 0.72...
[ { "code": "class Solution:\n def maxOperations(self, s: str) -> int:\n \n needs_shift = False \n\n block_size = 0\n\n ret = 0\n\n for c in s:\n if c == \"1\":\n if needs_shift:\n ret += block_size\n needs_shift...
118
[ { "code": "class Solution:\n def maxOperations(self, s: str) -> int:\n cnt = 0\n ones = 0\n last_is_one = False\n for i, x in enumerate(s):\n if x == \"0\":\n if last_is_one:\n cnt += ones\n last_is_one = False\n ...
36
3,454
minimum-operations-to-make-array-equal-to-target
<p>You are given two positive integer arrays <code>nums</code> and <code>target</code>, of the same length.</p> <p>In a single operation, you can select any subarray of <code>nums</code> and increment each element within that subarray by 1 or decrement each element within that subarray by 1.</p> <p>Return the <strong...
38.525693
Hard
[ "array", "dynamic-programming", "stack", "greedy", "monotonic-stack" ]
{"lang": "python3", "distribution": [["711", 0.36719999999999997], ["719", 0.36719999999999997], ["727", 1.4688], ["735", 4.4064], ["743", 5.263199999999999], ["751", 6.12], ["759", 6.120000000000001], ["767", 6.12], ["775", 4.651199999999999], ["783", 4.5287999999999995], ["791", 3.7944], ["799", 2.4480000000000004], ...
{"lang": "python3", "distribution": [["29400", 0.1224], ["29500", 0.9792], ["29600", 1.4688], ["29700", 0.612], ["29800", 0.3672], ["29900", 0.7344], ["30000", 0.3672], ["30100", 0.7344], ["30200", 1.224], ["30300", 12.7295], ["30400", 19.2166], ["30500", 6.6095], ["30600", 2.448], ["30700", 0.612], ["30800", 0.4896], ...
[ { "code": "class Solution:\n def minimumOperations(self, nums: List[int], target: List[int]) -> int:\n \n diff = [nums[i] - target[i] for i in range(len(nums))]\n\n\n # -4 -3 -2 -1 0 1 2 3 4 5\n\n # 1 2 3 4 5 6 7 8 9\n # print(diff)\n prev = 0\n res = 0\n ...
131
[ { "code": "class Solution:\n def minimumOperations(self, nums: List[int], target: List[int]) -> int:\n current = 0\n res = 0\n for i in range(len(nums)):\n diff = nums[i] - target[i]\n if current >= 0 and diff >= current:\n res += diff - current\n ...
60
3,507
find-the-count-of-numbers-which-are-not-special
<p>You are given 2 <strong>positive</strong> integers <code>l</code> and <code>r</code>. For any number <code>x</code>, all positive divisors of <code>x</code> <em>except</em> <code>x</code> are called the <strong>proper divisors</strong> of <code>x</code>.</p> <p>A number is called <strong>special</strong> if it has ...
26.701087
Medium
[ "array", "math", "number-theory" ]
{"lang": "python3", "distribution": [["48", 3.6359], ["85", 1.5470999999999995], ["122", 0.7825999999999999], ["158", 1.1180999999999996], ["195", 1.547], ["232", 11.2249], ["269", 17.676600000000004], ["306", 4.5684000000000005], ["343", 3.5240999999999993], ["379", 2.9643], ["416", 3.3934], ["453", 2.3303000000000003...
{"lang": "python3", "distribution": [["16100", 0.0186], ["16300", 0.1492], ["16400", 1.6782], ["16500", 7.5704], ["16600", 14.7679], ["16700", 15.7188], ["16800", 17.3037], ["16900", 14.0406], ["17000", 11.915], ["17100", 6.2838], ["17200", 1.9765], ["17300", 1.0069], ["17400", 0.7645], ["17500", 0.6713], ["17600", 0.5...
[ { "code": "mx = 31622\nsp = []\n\n\nclass Solution:\n def nonSpecialCount(self, l: int, r: int) -> int:\n if not sp:\n sieve = [False] * mx\n for i in range(2, mx):\n if not sieve[i]:\n for j in range(i * 2, mx, i):\n sieve...
150
[ { "code": "import math\n\nclass Solution:\n def nonSpecialCount(self, l, r):\n def is_prime(n):\n if n < 2:\n return False\n for i in range(2, int(math.sqrt(n)) + 1):\n if n % i == 0:\n return False\n return True\n\n ...
18
3,479
count-the-number-of-substrings-with-dominant-ones
<p>You are given a binary string <code>s</code>.</p> <p>Return the number of <span data-keyword="substring-nonempty">substrings</span> with <strong>dominant</strong> ones.</p> <p>A string has <strong>dominant</strong> ones if the number of ones in the string is <strong>greater than or equal to</strong> the <strong>sq...
15.725352
Medium
[ "string", "sliding-window", "enumeration" ]
{"lang": "python3", "distribution": [["628", 1.0764], ["850", 0.0828], ["1071", 0.1656], ["1292", 0.0828], ["1514", 0.0828], ["1735", 0.0828], ["1957", 0.1656], ["2178", 0.3312], ["2400", 0.1656], ["2621", 0.414], ["2842", 0.2484], ["3064", 0.1656], ["3285", 0.2484], ["3507", 0.1656], ["3728", 0.2484], ["3949", 0.0828]...
{"lang": "python3", "distribution": [["16300", 0.1656], ["16400", 0.0828], ["16500", 0.3311], ["16600", 1.3245], ["16700", 11.9205], ["16800", 21.3576], ["16900", 3.6424], ["17000", 0.745], ["17100", 0.3311], ["17200", 0.5795], ["17300", 0.4967], ["17400", 0.4139], ["17500", 0.745], ["17600", 0.4967], ["17700", 0.745],...
[ { "code": "class Solution:\n def numberOfSubstrings(self, s: str) -> int:\n n = len(s)\n ans = 0\n ones = [0] * (n + 1)\n zeroes = [0] * (n + 1)\n pos0 = [-1]\n pos1 = [-1]\n for i, x in enumerate(s):\n if x == '1':\n ans += 1\n ...
142
[ { "code": "class Solution:\n def numberOfSubstrings(self, s: str) -> int:\n n = len(s)\n result = 0\n \n # Iterate through possible zero counts (1 to sqrt(n))\n for k in range(1, int(n**0.5) + 1):\n zeros = deque() # Queue to store positions of zeros\n ...
82
3,328
apply-operations-to-make-sum-of-array-greater-than-or-equal-to-k
<p>You are given a <strong>positive</strong> integer <code>k</code>. Initially, you have an array <code>nums = [1]</code>.</p> <p>You can perform <strong>any</strong> of the following operations on the array <strong>any</strong> number of times (<strong>possibly zero</strong>):</p> <ul> <li>Choose any element in the...
44.62449
Medium
[ "math", "greedy", "enumeration" ]
{"lang": "python3", "distribution": [["21", 2.2123999999999997], ["26", 7.5221], ["31", 12.831700000000001], ["36", 20.7964], ["41", 11.946900000000001], ["46", 2.2125], ["51", 0.4425], ["56", 0.885], ["62", 0.4425], ["67", 0.4425], ["72", 0.4425], ["77", 0.4425], ["82", 0.4425], ["87", 0.4425], ["92", 0.4425], ["97", ...
{"lang": "python3", "distribution": [["16200", 0.4425], ["16300", 1.3274], ["16400", 9.292], ["16500", 27.8761], ["16600", 35.3982], ["16700", 15.0442], ["16800", 1.7699], ["16900", 0.885], ["17600", 0.4425], ["17900", 0.885], ["18000", 0.4425], ["18100", 0.4425], ["18200", 0.4425]]}
[ { "code": "class Solution:\n def minOperations(self, k: int) -> int:\n x = math.isqrt(k)\n y = (k - 1) // x + 1 - 1 \n return x - 1 + y", "runtime": "21" }, { "code": "\"\"\"\n\n(1+y)*(j) > k \nmin (y+j) /2 > yx > k\n\"\"\"\n\nclass Solution:\n def minOperations(self, k: int) -> int:\n ...
63
[ { "code": "class Solution:\n def minOperations(self, k: int) -> int:\n cnt = 0\n cur_min = k\n def cnt_times_of_dup(cur, k):\n if k % cur == 0:\n cnt = k // cur - 1\n else:\n cnt = k // cur\n return cnt\n \n for...
15
3,363
most-frequent-ids
<p>The problem involves tracking the frequency of IDs in a collection that changes over time. You have two integer arrays, <code>nums</code> and <code>freq</code>, of equal length <code>n</code>. Each element in <code>nums</code> represents an ID, and the corresponding element in <code>freq</code> indicates how many ti...
42.785346
Medium
[ "array", "hash-table", "heap-priority-queue", "ordered-set" ]
{"lang": "python3", "distribution": [["763", 3.1388], ["779", 0.4484], ["794", 1.7936999999999999], ["810", 0.4484], ["825", 0.4484], ["841", 0.4484], ["856", 0.4484], ["872", 0.4484], ["887", 0.4484], ["903", 1.3452000000000002], ["918", 3.1389], ["934", 4.4841], ["949", 8.071700000000002], ["965", 4.9328], ["980", 5....
{"lang": "python3", "distribution": [["41100", 0.4484], ["41200", 0.4484], ["41300", 0.4484], ["41400", 1.7937], ["41500", 1.7937], ["41600", 0.8969], ["41700", 1.3453], ["42000", 0.4484], ["42200", 0.4484], ["42300", 0.4484], ["42400", 0.8969], ["42500", 1.3453], ["42600", 0.4484], ["42700", 1.3453], ["42800", 1.3453]...
[ { "code": "class Solution:\n def mostFrequentIDs(self, nums: List[int], freq: List[int]) -> List[int]:\n counts = defaultdict(int)\n max_count = 0\n res = []\n\n for i in range(len(nums)):\n id = nums[i]\n frequency = freq[i]\n \n if cou...
91
[ { "code": "class Solution:\n def mostFrequentIDs(self, nums: List[int], freq: List[int]) -> List[int]:\n res = []\n cmf, cmn = 0, 0\n count = {}\n for i, f in enumerate(freq):\n x = nums[i]\n count[x] = count.get(x, 0) + f\n if x == cmn and count[x...
70
3,376
longest-common-suffix-queries
<p>You are given two arrays of strings <code>wordsContainer</code> and <code>wordsQuery</code>.</p> <p>For each <code>wordsQuery[i]</code>, you need to find a string from <code>wordsContainer</code> that has the <strong>longest common suffix</strong> with <code>wordsQuery[i]</code>. If there are two or more strings in...
40.967211
Hard
[ "array", "string", "trie" ]
{"lang": "python3", "distribution": [["785", 1.0928], ["845", 2.732], ["905", 1.6392], ["965", 0.5464], ["1025", 1.6392], ["1084", 3.8249000000000004], ["1144", 2.1856], ["1204", 2.1856], ["1264", 1.6392], ["1324", 0.5464], ["1383", 2.1856], ["1443", 4.371200000000001], ["1503", 4.371300000000001], ["1563", 3.278400000...
{"lang": "python3", "distribution": [["23065", 2.1856], ["26196", 1.6393], ["29328", 2.7322], ["32459", 1.0929], ["35590", 0.5464], ["38721", 1.0929], ["41853", 0.5464], ["44984", 0.5464], ["48115", 0.5464], ["51246", 0.5464], ["54378", 0.5464], ["57509", 0.5464], ["60640", 1.6393], ["63771", 0.5464], ["66903", 0.5464]...
[ { "code": "class Solution:\n def stringIndices(self, wordsContainer: List[str], wordsQuery: List[str]) -> List[int]:\n \n trie = {}\n \n for i, word in enumerate(wordsContainer):\n size = len(word)\n \n t = trie\n for c in word[::-1]:\n ...
88
[ { "code": "from bisect import bisect_left, bisect_right\n\nclass Solution:\n def stringIndices(self, wordsContainer: List[str], wordsQuery: List[str]) -> List[int]:\n rwords = list([word[-1::-1], len(word), index] for index, word in enumerate(wordsContainer))\n rwords.sort()\n cnt = len(...
86
3,355
minimum-levels-to-gain-more-points
<p>You are given a binary array <code>possible</code> of length <code>n</code>.</p> <p>Alice and Bob are playing a game that consists of <code>n</code> levels. Some of the levels in the game are <strong>impossible</strong> to clear while others can <strong>always</strong> be cleared. In particular, if <code>possible[i...
40.790676
Medium
[ "array", "prefix-sum" ]
{"lang": "python3", "distribution": [["206", 0.66], ["232", 0.33], ["257", 0.33], ["282", 0.33], ["308", 0.33], ["333", 0.33], ["359", 0.33], ["384", 0.33], ["410", 0.6601], ["435", 0.33], ["460", 0.33], ["486", 0.33], ["511", 0.33], ["537", 0.6601], ["562", 0.33], ["588", 0.33], ["613", 0.33], ["638", 0.33], ["664", 1...
{"lang": "python3", "distribution": [["19600", 0.33], ["19700", 1.6502], ["19800", 1.3201], ["19900", 6.6007], ["20000", 27.0627], ["20100", 3.9604], ["20200", 9.2409], ["20300", 6.6007], ["20400", 0.6601], ["20500", 0.33], ["20600", 2.6403], ["20700", 3.9604], ["20800", 1.6502], ["20900", 0.9901], ["21000", 0.33], ["2...
[ { "code": "class Solution:\n def minimumLevels(self, possible: List[int]) -> int:\n score=possible.count(1)-possible.count(0)\n s=0\n for i in range(len(possible)-1):\n s+=2*possible[i]-1\n if s*2>score: return i+1 \n return -1\nwith open(\"user.out\", \"w\")...
87
[ { "code": "class Solution:\n def minimumLevels(self, possible: List[int]) -> int:\n score=possible.count(1)-possible.count(0)\n s=0\n for i in range(len(possible)-1):\n s+=2*possible[i]-1\n if s*2>score: return i+1 \n return -1\nwith open(\"user.out\", \"w\")...
36
3,380
shortest-subarray-with-or-at-least-k-ii
<p>You are given an array <code>nums</code> of <strong>non-negative</strong> integers and an integer <code>k</code>.</p> <p>An array is called <strong>special</strong> if the bitwise <code>OR</code> of all of its elements is <strong>at least</strong> <code>k</code>.</p> <p>Return <em>the length of the <strong>shortes...
30.502783
Medium
[ "array", "bit-manipulation", "sliding-window" ]
{"lang": "python3", "distribution": [["838", 4.0], ["886", 0.8], ["934", 0.8], ["982", 0.8], ["1030", 4.8], ["1079", 3.2], ["1127", 1.6], ["1175", 0.8], ["1223", 0.8], ["1271", 0.8], ["1320", 0.8], ["1368", 0.8], ["1416", 0.8], ["1464", 0.8], ["1512", 0.8], ["1561", 2.4000000000000004], ["1609", 2.4000000000000004], ["...
{"lang": "python3", "distribution": [["36200", 0.8], ["36500", 0.8], ["36700", 3.2], ["36800", 2.4], ["37000", 0.8], ["37100", 0.8], ["37200", 0.8], ["37300", 0.8], ["37400", 0.8], ["37600", 1.6], ["37700", 0.8], ["37800", 16.0], ["37900", 39.2], ["38000", 16.8], ["38100", 4.8], ["38200", 1.6], ["46000", 0.8], ["49100"...
[ { "code": "class Solution:\n def minimumSubarrayLength(self, nums: List[int], k: int) -> int:\n dp = {}\n ans = inf\n for i in range(len(nums)):\n next_dp = {}\n if nums[i] >= k:\n return 1\n for val, j in dp.items():\n new_v...
73
[ { "code": "class Solution:\n def minimumSubarrayLength(self, nums: List[int], k: int) -> int:\n result = float('inf')\n track = defaultdict(int)\n ors = 0\n left = 0\n for right in range(len(nums)):\n number = nums[right]\n ors |= number\n f...
19
3,316
find-the-sum-of-subsequence-powers
<p>You are given an integer array <code>nums</code> of length <code>n</code>, and a <strong>positive</strong> integer <code>k</code>.</p> <p>The <strong>power</strong> of a <span data-keyword="subsequence-array">subsequence</span> is defined as the <strong>minimum</strong> absolute difference between <strong>any</stro...
24.661707
Hard
[ "array", "dynamic-programming", "sorting" ]
{"lang": "python3", "distribution": [["109", 1.1494], ["111", 1.1494], ["123", 2.2989], ["161", 1.1494], ["212", 1.1494], ["374", 1.1494], ["396", 1.1494], ["433", 1.1494], ["440", 1.1494], ["450", 1.1494], ["455", 1.1494], ["486", 1.1494], ["538", 1.1494], ["557", 1.1494], ["847", 1.1494], ["878", 1.1494], ["1147", 1....
{"lang": "python3", "distribution": [["17200", 2.2989], ["17300", 1.1494], ["18800", 2.2989], ["19200", 2.2989], ["19300", 1.1494], ["21000", 2.2989], ["21100", 2.2989], ["21200", 1.1494], ["22100", 1.1494], ["25800", 2.2989], ["25900", 3.4483], ["26000", 1.1494], ["26600", 1.1494], ["38600", 1.1494], ["53600", 1.1494]...
[ { "code": "class Solution:\n def sumOfPowers(self, nums: List[int], k: int) -> int:\n # subsequence has no order, if we need to find the mindiff between 2 elements -> o(n^2)\n # sort the nums and them mindiff is just among 2 nearby elements\n nums.sort()\n\n # one trick for subseq...
77
[ { "code": "class Solution:\n def sumOfPowers(self, nums: List[int], k: int) -> int:\n n = len(nums)\n nums.sort()\n\n @cache\n def dp(i, j):\n if j == k:\n return 1 \n if i >= n:\n return 0\n return (dp(i+1, j) + dp(ju...
61
3,019
furthest-point-from-origin
<p>You are given a string <code>moves</code> of length <code>n</code> consisting only of characters <code>&#39;L&#39;</code>, <code>&#39;R&#39;</code>, and <code>&#39;_&#39;</code>. The string represents your movement on a number line starting from the origin <code>0</code>.</p> <p>In the <code>i<sup>th</sup></code> m...
65.052437
Easy
[ "string", "counting" ]
{"lang": "python3", "distribution": [["19", 0.1934], ["22", 0.3868], ["23", 0.1934], ["24", 0.1934], ["25", 0.3868], ["26", 1.354], ["27", 0.7737], ["28", 2.5145], ["29", 1.5474], ["30", 2.9014], ["31", 1.9342], ["32", 4.2553], ["33", 4.0619], ["34", 6.5764], ["35", 4.8356], ["36", 7.5435], ["37", 10.4449], ["38", 7.15...
{"lang": "python3", "distribution": [["16200", 0.1934], ["16300", 2.9014], ["16400", 24.9516], ["16500", 37.7176], ["16600", 27.0793]]}
[ { "code": "class Solution:\n def furthestDistanceFromOrigin(self, m: str) -> int:\n l,r,b=0,0,0\n for i in m:\n if i==\"L\":l+=1\n elif i==\"R\":r+=1\n else:b+=1\n return abs(l-r)+b", "runtime": "19" }, { "code": "class Solution:\n def furt...
29
[ { "code": "class Solution:\n def furthestDistanceFromOrigin(self, moves: str) -> int:\n pos = 0\n u = 0\n for l in moves:\n if l == 'L':\n pos -= 1\n elif l == 'R':\n pos += 1\n else:\n u += 1\n return a...
8
3,026
find-the-minimum-possible-sum-of-a-beautiful-array
<p>You are given positive integers <code>n</code> and <code>target</code>.</p> <p>An array <code>nums</code> is <strong>beautiful</strong> if it meets the following conditions:</p> <ul> <li><code>nums.length == n</code>.</li> <li><code>nums</code> consists of pairwise <strong>distinct</strong> <strong>positive</str...
37.719223
Medium
[ "math", "greedy" ]
{"lang": "python3", "distribution": [["26", 4.2553], ["27", 3.1915], ["28", 1.0638], ["29", 3.1915], ["30", 5.3191], ["31", 6.383], ["32", 3.1915], ["33", 5.3191], ["34", 9.5745], ["35", 10.6383], ["36", 3.1915], ["37", 4.2553], ["38", 13.8298], ["39", 7.4468], ["40", 3.1915], ["41", 2.1277], ["42", 6.383], ["43", 2.12...
{"lang": "python3", "distribution": [["16300", 1.0638], ["16400", 14.8936], ["16500", 29.7872], ["16600", 46.8085]]}
[ { "code": "class Solution:\n def minimumPossibleSum(self, n: int, target: int) -> int:\n m = min(target // 2, n)\n res = (m * (m + 1) // 2)\n n -= m\n res += (target + n) * (target + n - 1) // 2 - target * (target - 1) // 2\n\n return res % (10 ** 9 + 7)", "runtime": "2...
19
[ { "code": "class Solution:\n def minimumPossibleSum(self, n: int, t: int) -> int:\n f_limit = min(n,t//2)\n f_sum = (f_limit**2 + f_limit)//2\n s_limit = max(0, n-f_limit)\n s_sum = (s_limit*(2*t+s_limit-1))//2\n return (f_sum + s_sum) % (10**9 + 7)", "memory": "16300" ...
4
3,025
minimum-operations-to-form-subsequence-with-target-sum
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of <strong>non-negative</strong> powers of <code>2</code>, and an integer <code>target</code>.</p> <p>In one operation, you must apply the following changes to the array:</p> <ul> <li>Choose any element of the array <code>nums[i]</code>...
32.618965
Hard
[ "array", "greedy", "bit-manipulation" ]
{"lang": "python3", "distribution": [["40", 1.4286], ["43", 2.8571], ["46", 1.4286], ["47", 1.4286], ["48", 2.8571], ["49", 7.1429], ["50", 1.4286], ["51", 4.2857], ["52", 7.1429], ["53", 7.1429], ["54", 2.8571], ["55", 7.1429], ["56", 8.5714], ["57", 4.2857], ["58", 1.4286], ["59", 4.2857], ["60", 2.8571], ["61", 8.57...
{"lang": "python3", "distribution": [["16500", 4.2857], ["16600", 10.0], ["16700", 25.7143], ["16800", 35.7143], ["16900", 12.8571], ["17000", 2.8571], ["17100", 1.4286]]}
[ { "code": "class Solution:\n def minOperations(self, nums: List[int], target: int) -> int:\n if target > sum(nums):\n return -1\n \n nums.sort()\n summed = sum(nums)\n ops = 0\n\n while target:\n current = nums.pop()\n if summed - cur...
25
[ { "code": "class Solution:\n def minOperations(self, nums: List[int], target: int) -> int:\n available = [0] * 31 # Indices from 0 to 30\n for num in nums:\n power = num.bit_length() - 1\n available[power] += 1\n\n operations = 0\n for i in range(31):\n ...
5
3,057
count-k-subsequences-of-a-string-with-maximum-beauty
<p>You are given a string <code>s</code> and an integer <code>k</code>.</p> <p>A <strong>k-subsequence</strong> is a <strong>subsequence</strong> of <code>s</code>, having length <code>k</code>, and all its characters are <strong>unique</strong>, <strong>i.e</strong>., every character occurs once.</p> <p>Let <code>f(...
29.573363
Hard
[ "hash-table", "math", "string", "greedy", "combinatorics" ]
{"lang": "python3", "distribution": [["61", 0.8547], ["65", 1.7094], ["67", 0.8547], ["68", 0.8547], ["69", 1.7094], ["70", 0.8547], ["71", 1.7094], ["72", 2.5641], ["73", 4.2735], ["74", 1.7094], ["75", 4.2735], ["76", 0.8547], ["77", 0.8547], ["78", 4.2735], ["79", 5.1282], ["80", 2.5641], ["81", 2.5641], ["82", 1.70...
{"lang": "python3", "distribution": [["18200", 0.8547], ["18300", 4.2735], ["18500", 11.1111], ["18600", 25.641], ["18700", 15.3846], ["18800", 9.4017], ["18900", 5.1282], ["19000", 5.1282], ["19100", 4.2735], ["19200", 5.9829], ["19300", 2.5641], ["20800", 0.8547], ["26100", 0.8547], ["26400", 0.8547], ["49200", 0.854...
[ { "code": "from collections import Counter\nfrom math import comb\nfrom functools import reduce\nfrom operator import mul\n\n\nclass Solution:\n def countKSubsequencesWithMaxBeauty(self, s: str, k: int) -> int:\n MOD = 10**9 + 7\n \n # Frequency count of each character in s\n freq...
56
[ { "code": "class Solution:\n def countKSubsequencesWithMaxBeauty(self, s: str, k: int) -> int:\n counter = {}\n for c in s:\n if c in counter:\n counter[c] += 1\n else:\n counter[c] = 1\n beauties = sorted(counter.values(), reverse=True...
20
2,599
take-k-of-each-character-from-left-and-right
<p>You are given a string <code>s</code> consisting of the characters <code>&#39;a&#39;</code>, <code>&#39;b&#39;</code>, and <code>&#39;c&#39;</code> and a non-negative integer <code>k</code>. Each minute, you may take either the <strong>leftmost</strong> character of <code>s</code>, or the <strong>rightmost</strong> ...
34.543666
Medium
[ "hash-table", "string", "sliding-window" ]
{"lang": "python3", "distribution": [["171", 5.2884], ["181", 7.692400000000001], ["191", 4.327], ["200", 3.8461999999999996], ["210", 3.3655], ["220", 5.2884], ["229", 1.9230999999999998], ["239", 6.2501], ["249", 5.2885], ["258", 4.8077000000000005], ["268", 4.3268], ["277", 3.3653], ["287", 2.8847], ["297", 5.2885],...
{"lang": "python3", "distribution": [["17200", 0.4808], ["17300", 3.3654], ["17400", 37.9808], ["17500", 36.5385], ["17600", 6.7308], ["17700", 0.9615], ["17800", 2.4038], ["21000", 0.4808], ["21900", 0.4808], ["22600", 0.4808], ["22700", 0.9615], ["25200", 1.9231], ["27700", 0.4808], ["28900", 0.4808], ["30600", 0.480...
[ { "code": "class Solution:\n def takeCharacters(self, s: str, k: int) -> int:\n limits = {c: s.count(c) - k for c in 'abc'}\n if any(x < 0 for x in limits.values()):\n return -1\n\n cnts = {c: 0 for c in 'abc'}\n ans = l = 0\n for r, c in enumerate(s):\n ...
71
[ { "code": "class Solution:\n def takeCharacters(self, s: str, k: int) -> int:\n ra = s.count('a') - k\n rb = s.count('b') - k\n rc = s.count('c') - k\n \n if any(i < 0 for i in [ra, rb, rc]):\n return -1\n \n hm = defaultdict(int)\n length = ...
17
3,485
maximize-score-of-numbers-in-ranges
<p>You are given an array of integers <code>start</code> and an integer <code>d</code>, representing <code>n</code> intervals <code>[start[i], start[i] + d]</code>.</p> <p>You are asked to choose <code>n</code> integers where the <code>i<sup>th</sup></code> integer must belong to the <code>i<sup>th</sup></code> interv...
33.612678
Medium
[ "array", "binary-search", "greedy", "sorting" ]
{"lang": "python3", "distribution": [["1119", 0.1076], ["1147", 0.0538], ["1176", 0.0269], ["1205", 0.0269], ["1233", 0.0269], ["1262", 0.0269], ["1290", 0.0269], ["1319", 0.0269], ["1348", 0.0538], ["1376", 0.21520000000000003], ["1405", 0.16140000000000002], ["1434", 0.37659999999999993], ["1462", 0.0807], ["1491", 0...
{"lang": "python3", "distribution": [["25200", 0.0269], ["27800", 0.0269], ["28300", 0.0269], ["28400", 0.0269], ["28700", 0.0269], ["29100", 0.0269], ["29300", 0.0269], ["29500", 0.0269], ["29600", 0.0269], ["29700", 0.0269], ["29800", 0.0269], ["29900", 0.1076], ["30000", 0.0269], ["30100", 0.1076], ["30200", 0.0538]...
[ { "code": "class Solution:\n def maxPossibleScore(self, start: List[int], d: int) -> int:\n start = sorted(start)\n n = len(start)\n hi = (start[-1] + d - start[0]) // (len(start)-1)\n lo = 0\n \n def test(x, jmp):\n cur = start[jmp]\n last_jump...
149
[ { "code": "with open(\"user.out\", \"w\") as Solution:\n for start,d in zip(map(loads, stdin), map(int, stdin)):\n\n minv = 0\n maxv = int(2E9)\n start.sort()\n\n while minv <maxv:\n\n midv = (minv +maxv +1) >>1\n bsv = -midv\n\n for v in start:\n ...
74
3,528
reach-end-of-array-with-max-score
<p>You are given an integer array <code>nums</code> of length <code>n</code>.</p> <p>Your goal is to start at index <code>0</code> and reach index <code>n - 1</code>. You can only jump to indices <strong>greater</strong> than your current index.</p> <p>The score for a jump from index <code>i</code> to index <code>j</...
30.721424
Medium
[ "array", "greedy" ]
{"lang": "python3", "distribution": [["234", 0.102], ["249", 0.0255], ["265", 0.0255], ["280", 0.0255], ["295", 0.0255], ["310", 0.0255], ["326", 0.051], ["341", 0.0765], ["356", 0.0255], ["371", 0.051], ["387", 0.0765], ["402", 0.051], ["417", 0.0765], ["433", 0.0255], ["448", 0.0255], ["463", 0.153], ["478", 0.102], ...
{"lang": "python3", "distribution": [["25900", 0.0255], ["29200", 0.102], ["29300", 0.1275], ["29600", 0.0765], ["29700", 0.0255], ["29900", 0.0255], ["30100", 0.0765], ["30300", 0.0765], ["30400", 0.0765], ["30500", 0.2805], ["30600", 0.2805], ["30700", 0.204], ["30800", 0.306], ["30900", 0.3825], ["31000", 0.4844], [...
[ { "code": "class Solution:\n def findMaximumScore(self, nums: List[int]) -> int:\n max_so_far = 0\n total_score = 0\n \n for num in nums[:-1]: \n max_so_far = max(max_so_far, num)\n total_score += max_so_far\n \n return total_score\n\ndef kdsmai...
101
[ { "code": "from typing import List\n\nclass Solution:\n def findMaximumScore(self, nums: List[int]) -> int:\n curr = nums[0]\n ind = 0\n score = 0\n \n for i in range(1, len(nums)):\n if nums[i] > curr or i == len(nums) - 1:\n score += (i - ind) * ...
55
3,558
find-a-safe-walk-through-a-grid
<p>You are given an <code>m x n</code> binary matrix <code>grid</code> and an integer <code>health</code>.</p> <p>You start on the upper-left corner <code>(0, 0)</code> and would like to get to the lower-right corner <code>(m - 1, n - 1)</code>.</p> <p>You can move up, down, left, or right from one cell to another ad...
26.822409
Medium
[ "array", "breadth-first-search", "graph", "heap-priority-queue", "matrix", "shortest-path" ]
{"lang": "python3", "distribution": [["172", 1.9681], ["194", 7.4051], ["216", 6.870900000000001], ["238", 3.535599999999999], ["260", 3.1019999999999994], ["282", 1.6681000000000004], ["304", 1.4347000000000003], ["326", 6.838100000000001], ["348", 8.272200000000002], ["370", 2.6685999999999996], ["392", 1.60140000000...
{"lang": "python3", "distribution": [["16758", 53.002], ["17476", 5.903899999999999], ["18193", 1.6677], ["18911", 0.46699999999999997], ["19628", 1.5010000000000001], ["20346", 0.20029999999999998], ["21063", 0.0334], ["21781", 0.0334], ["22498", 0.0334], ["23216", 0.0667], ["23933", 0.0334], ["24651", 0.0334], ["2536...
[ { "code": "class Solution:\n def findSafeWalk(self, grid: list[list[int]], health: int) -> bool:\n if health == grid[0][0]:\n return False\n HEIGHT = len(grid)\n WIDTH = len(grid[0])\n to_check = [(grid[0][0] - health, 0, 0)]\n grid[0][0] = -1\n while to_c...
152
[ { "code": "class Solution:\n def findSafeWalk(self, grid: List[List[int]], health: int) -> bool:\n R, C = len(grid), len(grid[0])\n distances = [[float('inf') for col in range(C)] for row in range(R)]\n q = deque([(grid[0][0], 0, 0)])\n distances[0][0] = grid[0][0]\n\n is_s...
137
3,575
find-the-maximum-sequence-value-of-array
<p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>The <strong>value</strong> of a sequence <code>seq</code> of size <code>2 * x</code> is defined as:</p> <ul> <li><code>(seq[0] OR seq[1] OR ... OR seq[x - 1]) XOR (seq[x] OR seq[x + 1] OR ... OR seq[2 *...
13.509469
Hard
[ "array", "dynamic-programming", "bit-manipulation" ]
{"lang": "python3", "distribution": [["340", 0.6816], ["499", 0.1704], ["659", 0.1704], ["818", 1.0224], ["977", 1.1926999999999999], ["1136", 1.0224], ["1295", 1.3632], ["1455", 2.726399999999999], ["1614", 1.8743999999999996], ["1773", 2.726399999999999], ["1932", 3.9190999999999985], ["2092", 2.215099999999999], ["2...
{"lang": "python3", "distribution": [["19784", 37.8197], ["26553", 1.7037], ["33321", 0.3408], ["40090", 0.1704], ["46859", 1.874], ["53628", 0.5112], ["60396", 0.1704], ["67165", 0.1704], ["73934", 0.6814], ["80703", 0.1704], ["87471", 0.1704], ["94240", 0.5111], ["101009", 0.1704], ["107778", 0.1704], ["114546", 0.17...
[ { "code": "# https://space.bilibili.com/206214\nclass Solution:\n def maxValue(self, nums: List[int], k: int) -> int:\n n = len(nums)\n mx = reduce(or_, nums)\n w = mx.bit_length()\n k2 = min(k, w) # 至多选 k2 个数\n\n suf = [None] * (n - k + 1)\n f = [set() for _ in ran...
117
[ { "code": "class Solution:\n def is_sub_mask(self, subset, superset):\n return (subset | superset) == superset\n\n def maxValue(self, arr, K):\n n = len(arr)\n k = K\n prefix = [-1] * 130\n suffix = [-1] * 130\n dp = [float('inf')] * 128\n count = [0] * 128...
97
3,571
length-of-the-longest-increasing-path
<p>You are given a 2D array of integers <code>coordinates</code> of length <code>n</code> and an integer <code>k</code>, where <code>0 &lt;= k &lt; n</code>.</p> <p><code>coordinates[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> indicates the point <code>(x<sub>i</sub>, y<sub>i</sub>)</code> in a 2D plane.</p> <p>An <st...
14.638187
Hard
[ "array", "binary-search", "sorting" ]
{"lang": "python3", "distribution": [["1424", 2.9169], ["1461", 16.5281], ["1498", 12.222799999999992], ["1535", 10.417299999999994], ["1571", 11.945199999999996], ["1608", 8.889499999999995], ["1645", 7.778299999999997], ["1682", 2.6391], ["1719", 3.1947], ["1756", 1.6668], ["1793", 0.5556], ["1830", 0.8334], ["1866",...
{"lang": "python3", "distribution": [["71100", 0.1389], ["71300", 0.1389], ["71600", 0.1389], ["72100", 0.1389], ["72200", 0.2778], ["72300", 0.4167], ["72500", 0.5556], ["72600", 1.3889], ["72700", 6.5278], ["72800", 30.8333], ["72900", 32.6389], ["73000", 6.6667], ["73100", 1.1111], ["73200", 0.4167], ["73300", 1.111...
[ { "code": "class Solution:\n def maxPathLength(self, coordinates: List[List[int]], k: int) -> int:\n small, big = [], []\n for x, y in coordinates:\n if x < coordinates[k][0] and y < coordinates[k][1]:\n small.append((x, -y))\n if x > coordinates[k][0] and y...
104
[ { "code": "class Solution:\n def maxPathLength(self, coordinates: List[List[int]], k: int) -> int:\n if not coordinates:\n return 0\n\n left = []\n right = []\n x_mark, y_mark = coordinates[k]\n for x, y in coordinates:\n if (x, y) == (x_mark, y_mark):...
73
3,518
maximum-multiplication-score
<p>You are given an integer array <code>a</code> of size 4 and another integer array <code>b</code> of size <strong>at least</strong> 4.</p> <p>You need to choose 4 indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, and <code>i<sub>3</sub></code> from the array <code>b</code> s...
41.596687
Medium
[ "array", "dynamic-programming" ]
{"lang": "python3", "distribution": [["875", 0.0573], ["911", 0.0382], ["946", 0.0382], ["981", 0.19100000000000003], ["1017", 0.0191], ["1052", 0.0763], ["1088", 0.22890000000000002], ["1123", 0.5721000000000002], ["1158", 2.8032000000000004], ["1194", 4.4434], ["1229", 2.669399999999999], ["1265", 2.3069999999999995]...
{"lang": "python3", "distribution": [["33523", 56.770100000000035], ["42569", 16.724], ["51615", 2.612399999999999], ["60661", 0.8206000000000001], ["69708", 2.0598999999999985], ["78754", 1.0112000000000003], ["87800", 0.0764], ["96846", 0.3052], ["105893", 0.34360000000000007], ["114939", 0.30540000000000006], ["1239...
[ { "code": "class Solution:\n def maxScore(self, a: List[int], b: List[int]) -> int:\n a0,a1,a2,a3 = a\n w,x,y,z = -inf,-inf,-inf,-inf\n for c in b:\n if a3*c+y > z:\n z = a3*c+y\n if a2*c+x > y:\n y = a2*c+x\n if a1*c+w > x:\...
147
[ { "code": "from itertools import combinations\nclass Solution:\n def maxScore(self, a: List[int], b: List[int]) -> int:\n dp = [-inf] * 5\n dp[0] = 0\n for x in b:\n for i in range(4, 0, -1):\n dp[i] = max(dp[i], dp[i-1] + a[i-1] * x)\n return dp[-1]\n\n ...
112
3,374
count-alternating-subarrays
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>We call a <span data-keyword="subarray-nonempty">subarray</span> <strong>alternating</strong> if <strong>no</strong> two <strong>adjacent</strong> elements in the subarray have the <strong>same</strong> value.</p> <p>Re...
57.326069
Medium
[ "array", "math" ]
{"lang": "python3", "distribution": [["748", 0.2286], ["750", 0.1143], ["751", 0.3429], ["753", 0.1143], ["754", 0.3429], ["756", 0.2286], ["757", 0.2286], ["759", 0.3429], ["760", 0.4571], ["762", 0.5715], ["764", 0.2286], ["765", 0.8], ["767", 0.8], ["768", 1.0286], ["770", 0.6857], ["771", 1.3715], ["773", 1.0286], ...
{"lang": "python3", "distribution": [["18500", 0.1143], ["19000", 0.1143], ["19700", 0.4571], ["19800", 1.3714], ["19900", 2.0571], ["20000", 11.7714], ["20100", 19.4286], ["20200", 13.8286], ["20300", 15.3143], ["20400", 5.8286], ["20500", 1.1429], ["20600", 0.4571], ["20700", 0.1143], ["20900", 0.2286], ["21200", 0.1...
[ { "code": "class Solution:\n def countAlternatingSubarrays(self, nums: List[int]) -> int:\n runLen = 0\n prev = -1\n tot = 0\n for num in nums:\n if num!=prev:\n runLen+=1\n else:\n tot+= ((runLen)*(runLen+1))//2\n ...
109
[ { "code": "class Solution:\n def countAlternatingSubarrays(self, nums: List[int]) -> int:\n n =len(nums)\n count = 1\n length =1 \n\n for i in range(1, n):\n if nums[i] != nums[i-1]:\n length +=1\n else:\n length = 1\n ...
39
3,344
minimize-manhattan-distances
<p>You are given an array <code>points</code> representing integer coordinates of some points on a 2D plane, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p> <p>The distance between two points is defined as their <span data-keyword="manhattan-distance">Manhattan distance</span>.</p> <p>Return <em>th...
32.788945
Hard
[ "array", "math", "geometry", "sorting", "ordered-set" ]
{"lang": "python3", "distribution": [["1101", 2.5425], ["1122", 3.39], ["1143", 10.1698], ["1163", 3.39], ["1184", 7.627400000000001], ["1205", 1.695], ["1226", 4.2375], ["1247", 5.0849], ["1268", 6.7799000000000005], ["1288", 1.695], ["1309", 0.8475], ["1330", 2.5425], ["1351", 3.39], ["1372", 1.695], ["1392", 0.8475]...
{"lang": "python3", "distribution": [["63400", 0.8475], ["63500", 0.8475], ["63600", 0.8475], ["63700", 1.6949], ["63800", 14.4068], ["63900", 5.9322], ["64000", 3.3898], ["64099", 4.2373], ["64200", 5.9322], ["64300", 8.4746], ["64400", 14.4068], ["64500", 10.1695], ["64599", 5.9322], ["64700", 1.6949], ["65000", 2.54...
[ { "code": "class Solution:\n def minimumDistance(self, points: List[List[int]]) -> int:\n a1 = a2 = b1 = b2 = None\n minSum = minSum2nd = minDiff = minDiff2nd = float('inf')\n maxSum = maxSum2nd = maxDiff = maxDiff2nd = float('-inf')\n ans = float('inf')\n for i, (x, y) in ...
65
[ { "code": "class Solution:\n def minimumDistance(self, points: List[List[int]]) -> int:\n def find(emb):\n min_val, max_val, extremmax_idx, extremmin_idx=10**10, -10**10, set(), set()\n for i, x in enumerate(emb):\n if x<min_val:\n extremmin_idx=...
28
3,394
minimum-array-end
<p>You are given two integers <code>n</code> and <code>x</code>. You have to construct an array of <strong>positive</strong> integers <code>nums</code> of size <code>n</code> where for every <code>0 &lt;= i &lt; n - 1</code>, <code>nums[i + 1]</code> is <strong>greater than</strong> <code>nums[i]</code>, and the result...
38.116724
Medium
[ "bit-manipulation" ]
{"lang": "python3", "distribution": [["19", 0.6024], ["22", 0.6024], ["23", 0.6024], ["26", 1.2048], ["27", 0.6024], ["28", 0.6024], ["29", 1.2048], ["30", 5.4217], ["31", 3.012], ["32", 4.8193], ["33", 7.2289], ["34", 2.4096], ["35", 6.6265], ["36", 8.4337], ["37", 4.8193], ["38", 7.2289], ["39", 9.0361], ["40", 9.036...
{"lang": "python3", "distribution": [["16300", 0.6024], ["16400", 18.0723], ["16500", 45.1807]]}
[ { "code": "class Solution:\n def minEnd(self, n: int, x: int) -> int:\n temp = bin(n - 1)[2:]\n newX = bin(x)[2:]\n for i in reversed(range(len(newX))):\n if newX[i] == '1':\n index = len(temp) - len(newX) + i + 1\n while index < 0:\n ...
26
[ { "code": "class Solution:\n # 0000\n # 100\n # 101\n # 110\n\n # X111\n # 0111\n\n # 1XX1X1\n # 1XX1X1\n # ...\n # ...\n\n # We can fit 2^3 = 8 numbers in between\n # X100101\n def minEnd(self, n: int, x: int) -> int:\n msb_pos = self.msb(x)\n zero_position ...
3
3,362
find-the-median-of-the-uniqueness-array
<p>You are given an integer array <code>nums</code>. The <strong>uniqueness array</strong> of <code>nums</code> is the sorted array that contains the number of distinct elements of all the <span data-keyword="subarray-nonempty">subarrays</span> of <code>nums</code>. In other words, it is a sorted array consisting of <c...
28.81892
Hard
[ "array", "hash-table", "binary-search", "sliding-window" ]
{"lang": "python3", "distribution": [["886", 1.2988], ["960", 0.6494], ["1034", 0.6494], ["1108", 0.6494], ["1182", 0.6494], ["1256", 0.6494], ["1330", 0.6494], ["1404", 0.6494], ["1478", 0.6494], ["1552", 0.6494], ["1626", 0.6494], ["1700", 0.6494], ["1774", 0.6494], ["1848", 0.6494], ["1922", 0.6494], ["1996", 1.2987...
{"lang": "python3", "distribution": [["33000", 1.9481], ["33100", 1.9481], ["33200", 1.2987], ["33400", 2.5974], ["33500", 1.2987], ["33600", 6.4935], ["33700", 3.8961], ["33800", 0.6494], ["34400", 2.5974], ["34500", 1.9481], ["34600", 7.1429], ["34700", 2.5974], ["34800", 1.2987], ["34900", 2.5974], ["35000", 0.6494]...
[ { "code": "class Solution:\n def medianOfUniquenessArray(self, nums: List[int]) -> int:\n n = len(nums)\n m = n * (n + 1) // 2 + 1 >> 1\n d, pre, nxt = {}, [-1] * n, [n] * n\n for i, x in enumerate(nums):\n if x in d:\n j = d[x]\n pre[i] = ...
70
[ { "code": "class Solution:\n def medianOfUniquenessArray(self, nums: List[int]) -> int:\n n = len(nums)\n target = (((1 + n) * n // 2) - 1) // 2\n\n l, r = 1, n + 1\n while l < r:\n mid = l + r >> 1\n total = j = 0\n cnt = Counter()\n fo...
39
3,395
minimum-length-of-anagram-concatenation
<p>You are given a string <code>s</code>, which is known to be a concatenation of <strong>anagrams</strong> of some string <code>t</code>.</p> <p>Return the <strong>minimum</strong> possible length of the string <code>t</code>.</p> <p>An <strong>anagram</strong> is formed by rearranging the letters of a string. For e...
41.865577
Medium
[ "hash-table", "string", "counting" ]
{"lang": "python3", "distribution": [["87", 1.6128], ["113", 0.5376], ["139", 0.5376], ["165", 0.5376], ["191", 0.5376], ["217", 5.3761], ["244", 1.0752], ["270", 0.5376], ["296", 1.6128], ["322", 2.1504], ["348", 5.913800000000001], ["374", 1.0752], ["401", 4.8385], ["427", 1.0752], ["453", 4.8385], ["479", 4.8385], [...
{"lang": "python3", "distribution": [["17200", 0.5376], ["17400", 9.1398], ["17500", 34.4086], ["17600", 15.5914], ["17700", 5.3763], ["17800", 3.2258], ["18000", 1.0753], ["18100", 2.6882], ["18200", 0.5376], ["18500", 2.1505], ["18600", 1.6129], ["18900", 0.5376], ["19200", 1.0753], ["19300", 3.2258], ["19400", 3.225...
[ { "code": "from math import gcd\nclass Solution:\n def minAnagramLength(self, s: str) -> int:\n if s==\"ccccaaaa\":\n return 8\n a=Counter(s)\n x=list(set(a.values()))\n m=x[0]\n for i in range(1,len(x)):\n m=gcd(m,x[i])\n l=len(s)//m\n l...
90
[ { "code": "class Solution:\n def minAnagramLength(self, s: str) -> int:\n counter = dict()\n n = len(s)\n for i in range(n):\n if s[i] not in counter:\n counter[s[i]] = 1\n else:\n counter[s[i]] += 1\n \n distinct_letters ...
29
3,402
minimum-cost-to-equalize-array
<p>You are given an integer array <code>nums</code> and two integers <code>cost1</code> and <code>cost2</code>. You are allowed to perform <strong>either</strong> of the following operations <strong>any</strong> number of times:</p> <ul> <li>Choose an index <code>i</code> from <code>nums</code> and <strong>increase</...
16.868399
Hard
[ "array", "greedy", "enumeration" ]
{"lang": "python3", "distribution": [["559", 0.9901], ["560", 0.9901], ["565", 0.9901], ["567", 0.9901], ["568", 1.9802], ["569", 0.9901], ["570", 2.9703], ["571", 0.9901], ["572", 1.9802], ["574", 2.9703], ["575", 4.9505], ["576", 2.9703], ["577", 0.9901], ["578", 2.9703], ["579", 0.9901], ["580", 2.9703], ["581", 1.9...
{"lang": "python3", "distribution": [["30600", 0.9901], ["31000", 0.9901], ["31400", 0.9901], ["31600", 3.9604], ["31700", 38.6139], ["31800", 48.5149]]}
[ { "code": "class Solution:\n def minCostToEqualizeArray(self, nums: List[int], cost1: int, cost2: int) -> int:\n n = len(nums)\n m = 10 ** 9 + 7\n \n # simple edge cases: if len(nums) < 3 then there is no reason to worry about cost2, as the second operation never does anything\n ...
65
[ { "code": "class Solution:\n def minCostToEqualizeArray(self, nums: List[int], cost1: int, cost2: int) -> int:\n max_nums, min_nums = max(nums), min(nums)\n n = len(nums)\n diff = max_nums - min_nums\n tot = max_nums * n - sum(nums)\n \n def test(extra):\n ...
6
3,419
maximum-points-inside-the-square
<p>You are given a 2D<strong> </strong>array <code>points</code> and a string <code>s</code> where, <code>points[i]</code> represents the coordinates of point <code>i</code>, and <code>s[i]</code> represents the <strong>tag</strong> of point <code>i</code>.</p> <p>A <strong>valid</strong> square is a square centered a...
39.028592
Medium
[ "array", "hash-table", "string", "binary-search", "sorting" ]
{"lang": "python3", "distribution": [["182", 1.4868999999999999], ["196", 0.3717], ["210", 0.3717], ["224", 0.7435], ["238", 0.3717], ["253", 1.1152], ["267", 0.3717], ["281", 1.1152], ["295", 0.7435], ["309", 0.3717], ["324", 0.7435], ["338", 0.7435], ["352", 0.7435], ["366", 1.1152], ["380", 2.2305], ["395", 0.3717],...
{"lang": "python3", "distribution": [["40500", 0.3717], ["40600", 0.3717], ["50600", 0.3717], ["50800", 0.3717], ["55400", 0.3717], ["58200", 0.3717], ["58900", 0.3717], ["59000", 0.3717], ["59200", 1.1152], ["59300", 0.3717], ["59600", 0.7435], ["59700", 1.1152], ["59800", 14.8699], ["59900", 28.9963], ["60000", 26.39...
[ { "code": "class Solution:\n def maxPointsInsideSquare(self, points: List[List[int]], \n s: str) -> int:\n\n mapper = [max(abs(x), abs(y)) for x, y in points]\n hash_table = defaultdict(list)\n for i, c in sorted(zip(mapper, s)):\n hash_table...
89
[ { "code": "class Solution:\n def maxPointsInsideSquare(self, points: List[List[int]], \n s: str) -> int:\n\n mapper = [max(abs(x), abs(y)) for x, y in points]\n hash_table = defaultdict(list)\n for i, c in sorted(zip(mapper, s)):\n hash_table...
21
3,313
maximum-strength-of-k-disjoint-subarrays
<p>You are given an array of integers <code>nums</code> with length <code>n</code>, and a positive <strong>odd</strong> integer <code>k</code>.</p> <p>Select exactly <b><code>k</code></b> disjoint <span data-keyword="subarray-nonempty">subarrays</span> <b><code>sub<sub>1</sub>, sub<sub>2</sub>, ..., sub<sub>k</sub></c...
27.997735
Hard
[ "array", "dynamic-programming", "prefix-sum" ]
{"lang": "python3", "distribution": [["1504", 2.0408], ["1541", 2.0408], ["1636", 2.0408], ["1670", 2.0408], ["1753", 2.0408], ["2177", 2.0408], ["2178", 2.0408], ["2187", 2.0408], ["2191", 2.0408], ["2202", 4.0816], ["2205", 2.0408], ["2212", 2.0408], ["2270", 2.0408], ["2301", 2.0408], ["2315", 2.0408], ["2587", 2.04...
{"lang": "python3", "distribution": [["17900", 2.0408], ["18000", 6.1224], ["18100", 2.0408], ["18200", 4.0816], ["18400", 2.0408], ["18800", 2.0408], ["63400", 2.0408], ["63500", 6.1224], ["63600", 8.1633], ["63700", 2.0408], ["63800", 4.0816], ["64400", 2.0408], ["64500", 2.0408], ["64599", 2.0408], ["64700", 2.0408]...
[ { "code": "class Solution:\n def maximumStrength(self, nums: List[int], k: int) -> int:\n number = -1_000_000_000_000_000\n dp=[[number]*len(nums) for i in range(k)]\n dp[0][0]=nums[0]*k\n for i in range(1,len(nums)):\n dp[0][i]=max(dp[0][i-1]+nums[i]*k,nums[i]*k)\n ...
44
[ { "code": "class Solution:\n def maximumStrength(self, nums: List[int], k: int) -> int:\n dp = [[-inf]*2 for _ in range(k+1)]\n dp[0][0] = dp[0][1] = 0\n for i in range(len(nums)-1, -1, -1): \n for j in range(k, 0, -1): \n cand = pow(-1, k-j)*nums[i]*j + max(dp[...
35
3,337
count-substrings-starting-and-ending-with-given-character
<p>You are given a string <code>s</code> and a character <code>c</code>. Return <em>the total number of <span data-keyword="substring-nonempty">substrings</span> of </em><code>s</code><em> that start and end with </em><code>c</code><em>.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div c...
48.937688
Medium
[ "math", "string", "counting" ]
{"lang": "python3", "distribution": [["26", 1.2048], ["27", 0.4016], ["29", 0.4016], ["30", 0.4016], ["31", 0.8032], ["32", 2.008], ["33", 1.2048], ["34", 2.008], ["35", 1.2048], ["36", 2.8112], ["37", 2.4096], ["38", 4.8193], ["39", 4.8193], ["40", 2.008], ["41", 3.2129], ["42", 4.8193], ["43", 1.6064], ["44", 1.2048]...
{"lang": "python3", "distribution": [["17100", 8.4337], ["17200", 43.3735], ["17300", 32.9317], ["17400", 3.6145], ["17800", 0.4016], ["17900", 0.4016], ["18100", 0.4016], ["18200", 0.4016], ["21200", 3.6145]]}
[ { "code": "class Solution:\n def countSubstrings(self, s: str, c: str) -> int:\n m = s.count(c)\n return m * (m + 1) // 2", "runtime": "26" }, { "code": "class Solution:\n def countSubstrings(self, s: str, c: str) -> int:\n n = s.count(c)\n return n*(n+1)//2\n ...
45
[ { "code": "class Solution:\n def countSubstrings(self, s: str, c: str) -> int:\n cnt = 0\n ans = 0\n for i in range(len(s)):\n if s[i]==c:\n cnt+=1\n ans+=cnt \n\n return ans", "memory": "17100" }, { "code": "class Solution:\n ...
10
3,360
minimum-deletions-to-make-string-k-special
<p>You are given a string <code>word</code> and an integer <code>k</code>.</p> <p>We consider <code>word</code> to be <strong>k-special</strong> if <code>|freq(word[i]) - freq(word[j])| &lt;= k</code> for all indices <code>i</code> and <code>j</code> in the string.</p> <p>Here, <code>freq(x)</code> denotes the <span ...
44.177106
Medium
[ "hash-table", "string", "greedy", "sorting", "counting" ]
{"lang": "python3", "distribution": [["63", 4.3957999999999995], ["74", 4.9453000000000005], ["86", 7.1432], ["97", 32.418], ["108", 11.538800000000002], ["119", 1.0989], ["130", 3.2969], ["142", 10.989200000000002], ["153", 4.9453], ["164", 1.6484999999999999], ["175", 1.099], ["187", 0.5495], ["198", 2.198], ["209", ...
{"lang": "python3", "distribution": [["17300", 1.0989], ["17400", 7.1429], ["17500", 31.3187], ["17600", 39.011], ["17700", 3.8462], ["17800", 7.6923]]}
[ { "code": "class Solution:\n def minimumDeletions(self, word: str, k: int) -> int:\n lst = [] \n for char in set(word):\n lst.append(word.count(char))\n # Now, lst records the frequency of every char in word\n # find the minimum number of deletions so that any pa...
37
[ { "code": "class Solution:\n def minimumDeletions(self, word: str, k: int) -> int:\n frequency = {}\n for letter in word:\n if letter in frequency:\n frequency[letter]+=1\n else:\n frequency[letter]=1\n \n freqs = frequency.value...
6
3,327
minimum-moves-to-pick-k-ones
<p>You are given a binary array <code>nums</code> of length <code>n</code>, a <strong>positive</strong> integer <code>k</code> and a <strong>non-negative</strong> integer <code>maxChanges</code>.</p> <p>Alice plays a game, where the goal is for Alice to pick up <code>k</code> ones from <code>nums</code> using the <str...
23.45954
Hard
[ "array", "greedy", "sliding-window", "prefix-sum" ]
{"lang": "python3", "distribution": [["500", 16.438000000000002], ["541", 19.1777], ["581", 21.9174], ["622", 23.287499999999998], ["663", 6.164199999999999], ["703", 0.6849], ["744", 0.6849], ["785", 0.6849], ["826", 0.6849], ["866", 0.6849], ["907", 0.6849], ["948", 0.6849], ["988", 0.6849], ["1029", 0.6849], ["1070"...
{"lang": "python3", "distribution": [["19700", 1.3699], ["22400", 1.3699], ["22500", 2.0548], ["22600", 2.0548], ["22700", 0.6849], ["23000", 0.6849], ["23400", 0.6849], ["23500", 8.2192], ["23600", 2.0548], ["25700", 4.1096], ["25800", 4.1096], ["25900", 2.0548], ["26000", 2.7397], ["26400", 1.3699], ["26500", 6.1644]...
[ { "code": "class Solution:\n def minimumMoves(self, nums: List[int], k: int, maxChanges: int) -> int:\n\n t = max((len(list(g)) for k, g in groupby(nums) if k == 1), default=0)\n t = min(t, 3)\n\n if k - maxChanges <= t:\n if k <= t:\n return k - 1\n ...
21
[ { "code": "from typing import List\n\n\nclass Solution:\n def minimumMoves(self, nums: List[int], k: int, maxChanges: int) -> int:\n n = len(nums)\n \n if n == 2:\n ones_in_2_window = nums[0] + nums[1]\n if k == 1:\n return 0 if ones_in_2_window >= 1 ...
22
3,278
find-the-number-of-ways-to-place-people-i
<p>You are given a 2D array <code>points</code> of size <code>n x 2</code> representing integer coordinates of some points on a 2D plane, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p> <p>Count the number of pairs of points <code>(A, B)</code>, where</p> <ul> <li><code>A</code> is on the <strong>...
41.532576
Medium
[ "array", "math", "geometry", "sorting", "enumeration" ]
{"lang": "python3", "distribution": [["88", 3.7266000000000004], ["93", 4.3477], ["98", 8.6958], ["104", 9.3168], ["109", 5.5901], ["114", 1.8633], ["119", 2.4844], ["124", 1.2422], ["130", 0.6211], ["135", 2.4844999999999997], ["140", 2.4844], ["145", 1.8633], ["150", 0.6211], ["156", 0.6211], ["161", 0.6211], ["166",...
{"lang": "python3", "distribution": [["16300", 0.6211], ["16400", 14.2857], ["16500", 27.3292], ["16600", 45.3416]]}
[ { "code": "class Solution:\n def numberOfPairs(self, points: List[List[int]]) -> int:\n res = 0\n # if two points have the same x-coordinate, they are sorted by their y-coordinate in descending order\n points.sort(key=lambda p: (p[0], -p[1]))\n for i, (x1, y1) in enumerate(points)...
78
[ { "code": "class Solution:\n def numberOfPairs(self, v):\n n = len(v)\n ans = 0\n\n for i in range(n):\n x1, y1 = v[i][0], v[i][1]\n\n for j in range(n):\n if i == j:\n continue\n\n x2, y2 = v[j][0], v[j][1]\n ...
5
3,265
maximum-good-subarray-sum
<p>You are given an array <code>nums</code> of length <code>n</code> and a <strong>positive</strong> integer <code>k</code>.</p> <p>A <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> is called <strong>good</strong> if the <strong>absolute difference</strong> between its first and last elemen...
19.12832
Medium
[ "array", "hash-table", "prefix-sum" ]
{"lang": "python3", "distribution": [["795", 0.1716], ["799", 0.0858], ["803", 1.0292000000000001], ["808", 0.4289], ["812", 0.42889999999999995], ["816", 0.9435], ["821", 0.6003999999999999], ["825", 0.8577], ["829", 1.1149], ["834", 1.4579999999999997], ["838", 1.0291000000000001], ["842", 1.8010000000000002], ["847"...
{"lang": "python3", "distribution": [["29100", 0.0858], ["29400", 0.1715], ["29700", 0.3431], ["29800", 0.0858], ["30000", 0.1715], ["30200", 0.4288], ["30300", 0.9434], ["30400", 17.4957], ["30500", 20.5832], ["30600", 16.4666], ["30700", 6.0034], ["30800", 3.9451], ["30900", 0.8576], ["31000", 1.0292], ["31100", 1.37...
[ { "code": "class Solution:\n def maximumSubarraySum(self, nums: List[int], k: int) -> int:\n result = -math.inf\n prefix = 0\n numToPrefix = {}\n for num in nums:\n if num not in numToPrefix or numToPrefix[num] > prefix:\n numToPrefix[num] = prefix\n ...
142
[ { "code": "class Solution:\n def maximumSubarraySum(self, nums: List[int], k: int) -> int:\n # [-1,3,2,4,5]\n # [0,-1,2,4,8,13]\n\n # [-1] = 0...........-1--------->\n # [-1,3] = -4.........2--------->\n # [-1,3,2] = -3.......4--------->\n # [-1,3,2,4] = -5.....8--------->\n # [-1,3,2,4,5]...
62
3,296
minimum-time-to-revert-word-to-initial-state-ii
<p>You are given a <strong>0-indexed</strong> string <code>word</code> and an integer <code>k</code>.</p> <p>At every second, you must perform the following operations:</p> <ul> <li>Remove the first <code>k</code> characters of <code>word</code>.</li> <li>Add any <code>k</code> characters to the end of <code>word</...
35.648074
Hard
[ "string", "rolling-hash", "string-matching", "hash-function" ]
{"lang": "python3", "distribution": [["446", 2.381], ["466", 2.381], ["488", 2.381], ["492", 2.381], ["548", 2.381], ["560", 2.381], ["586", 2.381], ["602", 2.381], ["608", 2.381], ["613", 2.381], ["617", 2.381], ["627", 2.381], ["704", 2.381], ["837", 2.381], ["861", 2.381], ["863", 2.381], ["878", 2.381], ["894", 2.3...
{"lang": "python3", "distribution": [["25300", 4.7619], ["58100", 4.7619], ["58200", 38.0952], ["58300", 26.1905], ["58400", 7.1429], ["64700", 4.7619], ["88200", 2.381], ["97800", 4.7619]]}
[ { "code": "class Solution:\n def get_lps(self, word) -> list[int]:\n n = len(word)\n lps = [0] * n\n left, right = 0, 1 \n while right < n:\n if word[left] == word[right]:\n left += 1\n lps[right] = left\n right += 1\n ...
37
[ { "code": "class Solution:\n def minimumTimeToInitialState(self, word: str, k: int) -> int: \n first_indices = {}\n for idx, letter in enumerate(word):\n if letter not in first_indices:\n first_indices[letter] = idx\n minimum_i = max(first_indices.values(...
9
1,282
number-of-valid-words-for-each-puzzle
With respect to a given <code>puzzle</code> string, a <code>word</code> is <em>valid</em> if both the following conditions are satisfied: <ul> <li><code>word</code> contains the first letter of <code>puzzle</code>.</li> <li>For each letter in <code>word</code>, that letter is in <code>puzzle</code>. <ul> <li>For e...
46.847886
Hard
[ "array", "hash-table", "string", "bit-manipulation", "trie" ]
{"lang": "python3", "distribution": [["665", 5.3438], ["721", 13.7409], ["777", 20.611400000000007], ["833", 16.794300000000003], ["888", 0.7634], ["944", 4.5802], ["1000", 4.580299999999999], ["1056", 3.8169999999999997], ["1112", 0.7634], ["1167", 1.5268], ["1223", 0.7634], ["1279", 0.7634], ["1335", 0.7634], ["1390"...
{"lang": "python3", "distribution": [["43800", 0.7634], ["44100", 4.5802], ["44200", 14.5038], ["44300", 12.9771], ["44400", 12.2137], ["44500", 2.2901], ["44600", 1.5267], ["44700", 0.7634], ["44800", 0.7634], ["44900", 1.5267], ["45100", 2.2901], ["45200", 2.2901], ["45300", 0.7634], ["45400", 0.7634], ["45600", 1.52...
[ { "code": "from collections import Counter\nfrom itertools import combinations\n\nclass Solution:\n def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]:\n word_count = Counter()\n for word in words:\n mask = 0\n for c in set(word):\n ...
45
[ { "code": "class Solution:\n def findNumOfValidWords(self, words: List[str], puzzles: List[str]) -> List[int]:\n return sol3(words, puzzles)\n\ndef sol1(words, puzzles):\n def is_good(puzzle, word):\n if puzzle[0] not in word:\n return False\n for ch in word:\n i...
36
3,434
find-the-number-of-distinct-colors-among-the-balls
<p>You are given an integer <code>limit</code> and a 2D array <code>queries</code> of size <code>n x 2</code>.</p> <p>There are <code>limit + 1</code> balls with <strong>distinct</strong> labels in the range <code>[0, limit]</code>. Initially, all balls are uncolored. For every query in <code>queries</code> that is of...
41.242734
Medium
[ "array", "hash-table", "simulation" ]
{"lang": "python3", "distribution": [["1109", 0.939], ["1111", 0.4695], ["1113", 0.939], ["1115", 0.4695], ["1117", 1.4085], ["1119", 1.8779], ["1121", 1.4085], ["1122", 1.4085], ["1124", 0.4695], ["1126", 0.939], ["1128", 0.4695], ["1130", 1.8779], ["1132", 0.939], ["1133", 2.3474], ["1135", 3.2864], ["1137", 0.939], ...
{"lang": "python3", "distribution": [["67100", 0.4695], ["67800", 0.4695], ["68200", 2.3474], ["68300", 15.0235], ["68400", 30.0469], ["68500", 15.9624], ["68600", 10.3286], ["68700", 3.2864], ["68800", 0.4695], ["69000", 0.4695], ["69100", 0.939], ["69200", 1.4085], ["69300", 0.939], ["69800", 1.4085], ["70400", 0.469...
[ { "code": "class Solution:\n def queryResults(self, limit: int, queries: List[List[int]]) -> List[int]:\n colors = {}\n ht={}\n count = 0\n ans=[]\n for qr in queries:\n [x,y]=qr\n if(colors.get(x) is None):\n \n colors[x]...
83
[ { "code": "class Solution:\n def queryResults(self, limit: int, queries: List[List[int]]) -> List[int]:\n b_colors = {}\n col_count = {}\n dis_colors = set()\n res = []\n\n for x, y in queries:\n if x in b_colors:\n old_color = b_colors[x]\n ...
21
3,435
block-placement-queries
<p>There exists an infinite number line, with its origin at 0 and extending towards the <strong>positive</strong> x-axis.</p> <p>You are given a 2D array <code>queries</code>, which contains two types of queries:</p> <ol> <li>For a query of type 1, <code>queries[i] = [1, x]</code>. Build an obstacle at distance <cod...
14.175699
Hard
[ "array", "binary-search", "binary-indexed-tree", "segment-tree" ]
{"lang": "python3", "distribution": [["4109", 5.5559], ["4196", 3.1748], ["4283", 0.7937], ["4370", 0.7937], ["4456", 0.7937], ["4543", 0.7937], ["4630", 0.7937], ["4717", 0.7937], ["4804", 1.5874], ["4891", 3.1748], ["4978", 1.5874], ["5064", 5.5558], ["5151", 3.1748], ["5238", 1.5874], ["5325", 0.7937], ["5412", 0.79...
{"lang": "python3", "distribution": [["86200", 0.7937], ["86400", 0.7937], ["86600", 2.381], ["86700", 1.5873], ["86800", 1.5873], ["86900", 7.1429], ["87000", 2.381], ["87200", 2.381], ["87300", 1.5873], ["87400", 6.3492], ["87500", 7.9365], ["87600", 1.5873], ["87900", 1.5873], ["88000", 7.9365], ["88100", 3.1746], [...
[ { "code": "class Solution:\n def getResults(self, queries: List[List[int]]) -> List[bool]:\n def beam_search(list_of_values, value):\n start = -1\n end = len(list_of_values)\n while end - start > 1:\n mid = (start + end) // 2\n if list_of_...
72
[ { "code": "class BIT:\n def __init__(self, n):\n self.n = n\n self.l = [0] * (n + 1)\n def add(self, i, x):\n i += 1\n while i <= self.n:\n self.l[i] = max(x, self.l[i])\n i += i & -i\n def query(self, i):\n i += 1\n ans = 0\n while...
32
3,444
find-the-number-of-good-pairs-ii
<p>You are given 2 integer arrays <code>nums1</code> and <code>nums2</code> of lengths <code>n</code> and <code>m</code> respectively. You are also given a <strong>positive</strong> integer <code>k</code>.</p> <p>A pair <code>(i, j)</code> is called <strong>good</strong> if <code>nums1[i]</code> is divisible by <code>...
25.722809
Medium
[ "array", "hash-table" ]
{"lang": "python3", "distribution": [["954", 12.631899999999998], ["1055", 23.509899999999984], ["1156", 3.1581], ["1257", 5.263500000000001], ["1357", 10.526999999999996], ["1458", 2.1054], ["1559", 0.3509], ["1660", 0.3509], ["1761", 0.3509], ["1862", 0.3509], ["1963", 3.1581], ["2063", 0.7018], ["2164", 0.3509], ["2...
{"lang": "python3", "distribution": [["38400", 0.3509], ["39700", 0.3509], ["40700", 0.3509], ["40800", 0.3509], ["41600", 0.3509], ["42300", 1.0526], ["42500", 0.3509], ["42600", 1.0526], ["42700", 0.7018], ["42800", 5.9649], ["42900", 10.1754], ["43000", 4.2105], ["43100", 4.2105], ["43200", 9.1228], ["43300", 4.2105...
[ { "code": "class Solution:\n def numberOfPairs(self, nums1: List[int], nums2: List[int], k: int) -> int:\n n,m = len(nums1),len(nums2)\n imax = max(nums1)//k\n cnt = [0]*(imax+1)\n for i in range(n):\n if nums1[i]%k==0:\n cnt[nums1[i]//k] += 1\n to...
88
[ { "code": "class Solution:\n def numberOfPairs(self, nums1: List[int], nums2: List[int], k: int) -> int:\n counter = Counter(nums1)\n minimum = min(nums1)\n maximum = max(nums1)\n result = 0\n for num in nums2:\n base = num * k\n multiply = 1 + (minimu...
69
3,423
maximum-sum-of-subsequence-with-non-adjacent-elements
<p>You are given an array <code>nums</code> consisting of integers. You are also given a 2D array <code>queries</code>, where <code>queries[i] = [pos<sub>i</sub>, x<sub>i</sub>]</code>.</p> <p>For query <code>i</code>, we first set <code>nums[pos<sub>i</sub>]</code> equal to <code>x<sub>i</sub></code>, then we calcula...
16.964719
Hard
[ "array", "divide-and-conquer", "dynamic-programming", "segment-tree" ]
{"lang": "python3", "distribution": [["3679", 1.2048], ["3680", 1.2048], ["3783", 1.2048], ["4072", 1.2048], ["4078", 1.2048], ["4085", 1.2048], ["4151", 1.2048], ["4156", 1.2048], ["4180", 1.2048], ["4293", 1.2048], ["4303", 1.2048], ["4413", 1.2048], ["4522", 1.2048], ["4531", 1.2048], ["4606", 1.2048], ["4620", 1.20...
{"lang": "python3", "distribution": [["43400", 1.2048], ["43500", 2.4096], ["43900", 1.2048], ["44000", 3.6145], ["44100", 1.2048], ["44600", 1.2048], ["44800", 1.2048], ["46700", 2.4096], ["46800", 2.4096], ["46900", 2.4096], ["47000", 3.6145], ["47300", 1.2048], ["47800", 3.6145], ["49000", 3.6145], ["50700", 1.2048]...
[ { "code": "class Solution:\n def maximumSumSubsequence(self, nums: List[int], queries: List[List[int]]) -> int:\n class node:\n def __init__(self,num,val):\n self.range_left=num\n self.range_right=num+1\n self.full_v=max(val,0)\n s...
76
[ { "code": "class SegTree:\n def __init__(self, nums):\n self.n = len(nums)\n self.tree = [0] * (4 * self.n)\n self.build(nums, 0, 0, self.n - 1)\n \n def build(self, nums, node, start, end):\n if start == end:\n self.tree[node] = (max(nums[start], 0), 0, 0, 0)\n ...
51
1,644
maximum-number-of-non-overlapping-substrings
<p>Given a string <code>s</code> of lowercase letters, you need to find the maximum number of <strong>non-empty</strong> substrings of <code>s</code> that meet the following conditions:</p> <ol> <li>The substrings do not overlap, that is for any two substrings <code>s[i..j]</code> and <code>s[x..y]</code>, either <co...
39.741199
Hard
[ "string", "greedy" ]
{"lang": "python3", "distribution": [["186", 8.485199999999997], ["234", 4.8488], ["283", 13.333899999999996], ["331", 12.727599999999995], ["379", 6.060899999999998], ["428", 1.8182], ["476", 7.879199999999998], ["525", 2.4244], ["573", 0.6061], ["622", 3.0305], ["670", 1.8182999999999998], ["719", 4.2425], ["767", 2....
{"lang": "python3", "distribution": [["16400", 0.6061], ["17400", 0.6061], ["17600", 1.8182], ["17700", 18.1818], ["17800", 21.2121], ["17900", 8.4848], ["18000", 6.6667], ["18100", 4.8485], ["18200", 1.8182], ["18300", 0.6061], ["18400", 0.6061], ["18500", 1.2121], ["18600", 1.8182], ["18700", 2.4242], ["18800", 1.818...
[ { "code": "class Solution:\n def dfs(self, i):\n if i in self.dfs_store:\n return self.dfs_store[i]\n \n v = self.dfs(i+1)\n \n _, end = self.valid_intervals[i]\n j = i + 1\n while j < self.len and self.valid_intervals[j][0] < end:\n j +=...
62
[ { "code": "class Solution:\n def maxNumOfSubstrings(self, s: str) -> List[str]:\n d = {}\n for i in range(len(s)):\n if s[i] in d:\n d[s[i]][1] = i\n else:\n d[s[i]] = [i, i]\n for key in d:\n start, end = d[key]\n ...
38
2,837
minimum-operations-to-make-the-integer-zero
<p>You are given two integers <code>num1</code> and <code>num2</code>.</p> <p>In one operation, you can choose integer <code>i</code> in the range <code>[0, 60]</code> and subtract <code>2<sup>i</sup> + num2</code> from <code>num1</code>.</p> <p>Return <em>the integer denoting the <strong>minimum</strong> number of o...
30.313918
Medium
[ "bit-manipulation", "brainteaser" ]
{"lang": "python3", "distribution": [["21", 0.6098], ["23", 1.2195], ["24", 1.2195], ["25", 0.6098], ["26", 0.6098], ["27", 3.6585], ["28", 1.2195], ["29", 1.8293], ["30", 0.6098], ["31", 4.878], ["32", 5.4878], ["33", 4.878], ["34", 6.7073], ["35", 10.3659], ["36", 7.3171], ["37", 2.439], ["38", 3.6585], ["39", 4.2683...
{"lang": "python3", "distribution": [["16300", 4.878], ["16400", 18.2927], ["16500", 39.0244]]}
[ { "code": "class Solution:\n def makeTheIntegerZero(self, num1: int, num2: int) -> int:\n \"\"\"\n if we pick integers a, b, c, ... in our operations, then the sum is\n\n num1 - num2 * len(ops) - (2**a + 2**b + 2**c + ...)\n\n \"\"\"\n\n ops = 1\n while True:\n ...
27
[ { "code": "class Solution:\n def makeTheIntegerZero(self, num1: int, num2: int) -> int:\n if num1 < num2:\n return -1\n for k in range(61):\n if num1 - k * num2 < 0:\n break\n target = num1 - k * num2\n if target.bit_count() <= k <= tar...
3
2,873
prime-pairs-with-target-sum
<p>You are given an integer <code>n</code>. We say that two integers <code>x</code> and <code>y</code> form a prime number pair if:</p> <ul> <li><code>1 &lt;= x &lt;= y &lt;= n</code></li> <li><code>x + y == n</code></li> <li><code>x</code> and <code>y</code> are prime numbers</li> </ul> <p>Return <em>the 2D sorte...
34.553092
Medium
[ "array", "math", "enumeration", "number-theory" ]
{"lang": "python3", "distribution": [["381", 3.6584999999999996], ["466", 0.4065], ["552", 0.4065], ["637", 0.4065], ["722", 0.4065], ["808", 0.4065], ["893", 0.4065], ["978", 0.813], ["1064", 2.0324999999999998], ["1149", 0.4065], ["1234", 0.4065], ["1320", 0.4065], ["1405", 0.4065], ["1490", 0.4065], ["1576", 0.4065]...
{"lang": "python3", "distribution": [["20700", 0.4065], ["20800", 0.4065], ["25200", 0.4065], ["25800", 0.4065], ["26600", 0.4065], ["26700", 0.4065], ["28400", 0.4065], ["28600", 0.4065], ["28700", 0.4065], ["29000", 0.4065], ["29700", 0.4065], ["30200", 1.2195], ["30300", 0.4065], ["30400", 0.4065], ["30500", 0.813],...
[ { "code": "def getSieve(n: int) -> int:\n sqrt_n = int(sqrt(n))\n # print('srqrt_n: ', sqrt_n)\n sieve = [0, 0] + [1]*(n - 2)\n\n k = 2\n while k <= sqrt_n:\n if sieve[k]:\n multiple = k * k\n # print('k: ', k, ' multiple: ', multiple)\n while multiple < n:...
102
[ { "code": "class Solution:\n def findPrimePairs(self, n: int) -> List[List[int]]:\n def isPrime(v):\n upper = math.floor(math.sqrt(v)) + 1\n if v < 2:\n return False\n if v == 2:\n return True\n if v % 2 == 0:\n r...
59
2,849
sum-of-imbalance-numbers-of-all-subarrays
<p>The <strong>imbalance number</strong> of a <strong>0-indexed</strong> integer array <code>arr</code> of length <code>n</code> is defined as the number of indices in <code>sarr = sorted(arr)</code> such that:</p> <ul> <li><code>0 &lt;= i &lt; n - 1</code>, and</li> <li><code>sarr[i+1] - sarr[i] &gt; 1</code></li> ...
47.096586
Hard
[ "array", "hash-table", "ordered-set" ]
{"lang": "python3", "distribution": [["66", 24.792699999999993], ["110", 1.6529], ["155", 0.8264], ["199", 0.8264], ["244", 3.3056], ["288", 16.528299999999998], ["333", 18.1814], ["377", 4.958500000000001], ["421", 2.4792], ["466", 0.8264], ["510", 0.8264], ["555", 0.8264], ["599", 0.8264], ["644", 0.8264], ["688", 0....
{"lang": "python3", "distribution": [["16500", 4.9587], ["16600", 17.3554], ["16700", 33.8843], ["16800", 15.7025], ["16900", 6.6116], ["17000", 2.4793], ["17100", 4.1322], ["17200", 8.2645]]}
[ { "code": "class Solution:\n def sumImbalanceNumbers(self, A: List[int]) -> int:\n n = len(A)\n left = [-1] * n\n last = [-1] * (n + 2)\n for i in range(n):\n left[i] = last[A[i] + 1]\n last[A[i]] = i\n\n right = [n] * n\n last = [n] * (n + 2)\n...
41
[ { "code": "class Solution:\n def sumImbalanceNumbers(self, nums: List[int]) -> int:\n n = len(nums)\n res = 0\n for i in range(n):\n s = set()\n cur = -1\n for j in range(i, n):\n cur += 0 if nums[j] in s else 1 - (nums[j] + 1 in s) - (nums...
7
2,588
maximum-number-of-points-from-grid-queries
<p>You are given an <code>m x n</code> integer matrix <code>grid</code> and an array <code>queries</code> of size <code>k</code>.</p> <p>Find an array <code>answer</code> of size <code>k</code> such that for each integer <code>queries[i]</code> you start in the <strong>top left</strong> cell of the matrix and repeat t...
37.995223
Hard
[ "array", "two-pointers", "breadth-first-search", "union-find", "sorting", "heap-priority-queue", "matrix" ]
{"lang": "python3", "distribution": [["637", 2.0724], ["663", 0.5181], ["689", 0.5181], ["715", 1.0363], ["741", 1.0363], ["767", 0.5181], ["793", 10.880600000000001], ["819", 5.699399999999999], ["845", 4.6631], ["871", 1.5543], ["897", 2.0724], ["923", 4.1449], ["949", 6.735400000000002], ["975", 9.326200000000002], ...
{"lang": "python3", "distribution": [["28600", 1.5544], ["28700", 1.0363], ["28900", 0.5181], ["29500", 2.5907], ["29600", 2.5907], ["29700", 1.0363], ["29800", 4.6632], ["29900", 1.5544], ["30100", 1.0363], ["30200", 0.5181], ["30300", 0.5181], ["31800", 1.0363], ["32000", 0.5181], ["32100", 0.5181], ["32200", 0.5181]...
[ { "code": "class Solution:\n def maxPoints(self, grid: List[List[int]], queries: List[int]) -> List[int]:\n m = len(grid)\n n = len(grid[0])\n q = len(queries)\n\n sorted_indices = sorted(range(q), key=lambda k: queries[k])\n answers = [0] * q\n prev_query = 0\n ...
61
[ { "code": "class Solution:\n def maxPoints(self, grid: List[List[int]], queries: List[int]) -> List[int]:\n rows, cols = len(grid), len(grid[0])\n sorted_q = [(b, a) for a, b in enumerate(queries) if grid[0][0] < b]\n sorted_q.sort()\n pq = []\n heappush(pq, (grid[0][0], 0,...
61
2,711
minimum-time-to-visit-a-cell-in-a-grid
<p>You are given a <code>m x n</code> matrix <code>grid</code> consisting of <b>non-negative</b> integers where <code>grid[row][col]</code> represents the <strong>minimum</strong> time required to be able to visit the cell <code>(row, col)</code>, which means you can visit the cell <code>(row, col)</code> only when the...
35.758013
Hard
[ "array", "breadth-first-search", "graph", "heap-priority-queue", "matrix", "shortest-path" ]
{"lang": "python3", "distribution": [["1324", 1.8867], ["1342", 0.6289], ["1361", 0.6289], ["1380", 0.6289], ["1398", 0.6289], ["1417", 0.6289], ["1435", 1.2579], ["1454", 0.6289], ["1473", 1.2578], ["1491", 0.6289], ["1510", 1.2578], ["1529", 3.7736], ["1547", 2.5158], ["1566", 1.8867], ["1584", 2.5156], ["1603", 1.88...
{"lang": "python3", "distribution": [["29200", 0.6289], ["31000", 0.6289], ["31500", 0.6289], ["31600", 0.6289], ["31700", 1.2579], ["31800", 1.8868], ["31900", 1.8868], ["32000", 1.2579], ["32200", 1.2579], ["32299", 0.6289], ["32400", 0.6289], ["32800", 0.6289], ["33100", 0.6289], ["33800", 0.6289], ["33900", 0.6289]...
[ { "code": "class Solution:\n def minimumTime(self, grid: List[List[int]]) -> int:\n m, n = len(grid), len(grid[0])\n if grid[0][1] > 1 and grid[1][0] > 1: return -1\n q = [(0, 0, 0)]\n while True:\n t, r, c = heappop(q)\n t += 1\n for i, j in ((r-1...
96
[ { "code": "class Solution:\n def minimumTime(self, grid: List[List[int]]) -> int:\n m, n = len(grid), len(grid[0])\n if grid[0][1] > 1 and grid[1][0] > 1: return -1\n q = [(0, 0, 0)]\n while True:\n t, r, c = heappop(q)\n t += 1\n for i, j in ((r-1...
70
2,135
maximum-number-of-ways-to-partition-an-array
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>. The number of ways to <strong>partition</strong> <code>nums</code> is the number of <code>pivot</code> indices that satisfy both conditions:</p> <ul> <li><code>1 &lt;= pivot &lt; n</code></li> <li><code>nums[0] + ...
34.525407
Hard
[ "array", "hash-table", "counting", "enumeration", "prefix-sum" ]
{"lang": "python3", "distribution": [["1614", 0.8955], ["1676", 0.2985], ["1738", 1.791], ["1800", 4.178999999999999], ["1862", 11.940000000000003], ["1925", 10.745999999999999], ["1987", 16.119], ["2049", 7.462499999999998], ["2111", 15.223500000000003], ["2173", 11.044500000000001], ["2236", 2.9849999999999994], ["22...
{"lang": "python3", "distribution": [["30838", 6.2685], ["31314", 0.2985], ["31790", 0.2985], ["32266", 0.2985], ["32743", 0.2985], ["33219", 0.8955], ["33695", 0.597], ["34171", 0.8955], ["34648", 0.2985], ["35124", 0.597], ["35600", 0.2985], ["36076", 0.2985], ["36553", 0.8955], ["37029", 7.1642], ["37505", 4.7761], ...
[ { "code": "import bisect\nimport collections\nfrom typing import List, Tuple, Mapping\n\n\nclass Solution:\n def waysToPartition(self, nums: List[int], k: int) -> int:\n diffsIfReplacedWithK = [k - num for num in nums]\n diffsToReplaceIndices = collections.defaultdict[int, list[int]](list)\n for i in ...
36
[ { "code": "class Solution:\n def waysToPartition(self, nums: List[int], k: int) -> int:\n before = {k:0}; after = {}; counter = Counter(nums)\n p = list(accumulate(nums))\n n = len(nums)\n for i, num in enumerate(nums[:n-1]):\n before[num] = max(before.get(num, 0), afte...
83
1,545
form-largest-integer-with-digits-that-add-up-to-target
<p>Given an array of integers <code>cost</code> and an integer <code>target</code>, return <em>the <strong>maximum</strong> integer you can paint under the following rules</em>:</p> <ul> <li>The cost of painting a digit <code>(i + 1)</code> is given by <code>cost[i]</code> (<strong>0-indexed</strong>).</li> <li>The ...
48.398432
Hard
[ "array", "dynamic-programming" ]
{"lang": "python3", "distribution": [["96", 11.046600000000002], ["130", 20.930400000000006], ["163", 24.4188], ["197", 11.046600000000003], ["230", 1.1628], ["264", 5.814], ["297", 4.6512], ["331", 2.3256], ["364", 0.5814], ["398", 0.5814], ["431", 0.5814], ["465", 0.5814], ["498", 0.5814], ["532", 0.5814], ["565", 0....
{"lang": "python3", "distribution": [["16500", 0.5814], ["16600", 0.5814], ["16700", 1.7442], ["17000", 4.0698], ["17100", 6.9767], ["17200", 3.4884], ["17300", 0.5814], ["17700", 1.7442], ["18800", 0.5814], ["18900", 0.5814], ["19000", 2.3256], ["19100", 4.0698], ["19200", 2.3256], ["19900", 0.5814], ["20100", 1.1628]...
[ { "code": "class Solution:\n def largestNumber(self, cost: List[int], target: int) -> str:\n # Initialize dp array, dp[i] represents the maximum length of the number with cost i\n dp = [-1] * (target + 1)\n dp[0] = 0 # Base case, cost 0 can form a number of length 0\n\n # Iterate...
34
[ { "code": "class Solution:\n def largestNumber(self, cost: List[int], target: int) -> str:\n dp = [0] * (target+1)\n for i in range(target):\n if dp[i] or i==0:\n for c in cost:\n if i+c < len(dp):\n dp[i+c] = max(dp[i+c],dp[i]...
72
3,430
count-days-without-meetings
<p>You are given a positive integer <code>days</code> representing the total number of days an employee is available for work (starting from day 1). You are also given a 2D array <code>meetings</code> of size <code>n</code> where, <code>meetings[i] = [start_i, end_i]</code> represents the starting and ending days of me...
33.875758
Medium
[ "array", "sorting" ]
{"lang": "python3", "distribution": [["285", 0.4024], ["323", 0.2012], ["361", 0.2012], ["399", 0.2012], ["438", 0.2012], ["476", 0.2012], ["514", 0.2012], ["552", 0.2012], ["591", 0.2012], ["629", 0.2012], ["667", 0.2012], ["705", 0.2012], ["744", 0.2012], ["782", 0.6036], ["820", 0.2012], ["858", 0.2012], ["897", 0.4...
{"lang": "python3", "distribution": [["34700", 0.2012], ["51600", 0.2012], ["51700", 0.2012], ["51800", 0.2012], ["51900", 0.2012], ["52000", 0.2012], ["53000", 0.2012], ["53200", 0.2012], ["53400", 0.4024], ["53500", 1.006], ["53600", 0.6036], ["53700", 0.6036], ["62100", 0.2012], ["62200", 0.2012], ["62600", 0.2012],...
[ { "code": "class Solution:\n def countDays(self, days: int, meetings: List[List[int]]) -> int:\n meetings.sort()\n ans = meetings[0][0]-1\n curr_end = meetings[0][1]\n for i in range(1,len(meetings)):\n s,e = meetings[i]\n if s>curr_end:\n ans+...
57
[ { "code": "class Solution:\n def countDays(self, days: int, meetings: List[List[int]]) -> int:\n meetings.sort()\n ans = meetings[0][0]-1\n curr_end = meetings[0][1]\n for i in range(1,len(meetings)):\n s,e = meetings[i]\n if s>curr_end:\n ans+...
38
3,445
lexicographically-minimum-string-after-removing-stars
<p>You are given a string <code>s</code>. It may contain any number of <code>&#39;*&#39;</code> characters. Your task is to remove all <code>&#39;*&#39;</code> characters.</p> <p>While there is a <code>&#39;*&#39;</code>, do the following operation:</p> <ul> <li>Delete the leftmost <code>&#39;*&#39;</code> and the <...
35.90259
Medium
[ "hash-table", "string", "stack", "greedy", "heap-priority-queue" ]
{"lang": "python3", "distribution": [["307", 0.8474], ["325", 0.4237], ["342", 0.4237], ["359", 0.4237], ["377", 0.4237], ["394", 2.5423000000000004], ["412", 1.6948], ["429", 2.1185], ["446", 0.8474], ["464", 0.4237], ["481", 2.1186000000000003], ["498", 0.8474], ["516", 2.1186000000000003], ["533", 1.2712], ["551", 3...
{"lang": "python3", "distribution": [["18100", 0.4237], ["20400", 0.4237], ["20600", 0.4237], ["21200", 0.4237], ["21700", 2.1186], ["21800", 0.4237], ["21900", 0.4237], ["22000", 0.4237], ["22100", 2.1186], ["22200", 2.1186], ["22300", 1.2712], ["22400", 1.2712], ["22500", 0.8475], ["22800", 5.5085], ["22900", 2.1186]...
[ { "code": "class Solution:\n def clearStars(self, s: str) -> str:\n heap = []\n charIndex = defaultdict(list)\n s = list(s)\n for i, c in enumerate(s):\n if c == \"*\":\n toRemove = heap[0]\n s[charIndex[toRemove].pop()] = \"\"\n ...
73
[ { "code": "class Solution:\n def clearStars(self, s: str) -> str:\n if s == 'a' * 33333 + 'b' * 33333 + '*' * 33333:\n return 'b' * 33333\n string = []\n hashmap = collections.defaultdict(int)\n for char in s:\n if char == '*' and string:\n min...
70
3,436
find-subarray-with-bitwise-or-closest-to-k
<p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small...
31.078913
Hard
[ "array", "binary-search", "bit-manipulation", "segment-tree" ]
{"lang": "python3", "distribution": [["220", 7.638599999999999], ["292", 0.6944], ["365", 0.6944], ["438", 0.6944], ["510", 0.6944], ["583", 0.6944], ["656", 0.6944], ["728", 0.6944], ["801", 0.6944], ["874", 0.6944], ["946", 0.6944], ["1019", 0.6944], ["1091", 15.277299999999999], ["1164", 14.5825], ["1237", 6.9442], ...
{"lang": "python3", "distribution": [["30600", 2.0833], ["30700", 17.3611], ["30800", 22.2222], ["30900", 4.8611], ["31500", 6.9444], ["31600", 2.0833], ["32800", 0.6944], ["34600", 2.7778], ["34700", 3.4722], ["34800", 0.6944], ["43200", 0.6944], ["43300", 6.25], ["43400", 0.6944], ["43500", 0.6944], ["43600", 0.6944]...
[ { "code": "class Solution:\n def minimumDifference(self, array, target):\n array_length = len(array)\n start_index = -1\n current_result = abs(array[0] - target)\n or_accumulator = 0\n\n for end_index in range(array_length):\n or_accumulator |= array[end_index]\n...
67
[ { "code": "class Solution:\n def minimumDifference(self, nums: List[int], k: int) -> int:\n ans = float('inf')\n curr_or_set = set()\n for num in nums:\n next_or_set = set()\n next_or_set.add(num)\n ans = min(ans, abs(k - num))\n for prev_or in...
42
3,456
find-the-maximum-length-of-a-good-subsequence-i
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <cod...
30.702636
Medium
[ "array", "hash-table", "dynamic-programming" ]
{"lang": "python3", "distribution": [["140", 37.30589999999997], ["254", 0.5181], ["368", 2.0726000000000004], ["482", 0.2591], ["596", 0.5182], ["710", 0.2591], ["824", 0.2591], ["938", 0.2591], ["1052", 0.2591], ["1166", 0.2591], ["1280", 0.2591], ["1394", 0.5181], ["1508", 0.2591], ["1623", 0.2591], ["1737", 0.2591]...
{"lang": "python3", "distribution": [["16400", 1.0363], ["16500", 4.1451], ["16600", 6.2176], ["16700", 11.9171], ["16800", 24.0933], ["16900", 17.3575], ["17000", 8.0311], ["17100", 3.6269], ["17200", 0.2591], ["17700", 0.2591], ["17800", 0.2591], ["17900", 0.5181], ["18400", 0.2591], ["18500", 0.2591], ["18600", 0.25...
[ { "code": "class Solution:\n def maximumLength(self, A: List[int], k: int) -> int:\n dp = [defaultdict(int) for i in range(k + 1)]\n res = [0] * (k + 1)\n for a in A:\n for i in range(k, -1, -1):\n dp[i][a] = max(dp[i][a] + 1, res[i - 1] + 1 if i else 0)\n ...
104
[ { "code": "class Solution:\n def maximumLength(self, nums: List[int], k: int) -> int:\n dp = [0 for _ in nums]\n for _ in range(k+1):\n prev = dp\n dp = [1]\n for idx in range(1, len(nums)):\n ans = max(\n dp[j] if nums[j] == nu...
41
3,452
find-the-maximum-length-of-a-good-subsequence-ii
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <cod...
24.547925
Hard
[ "array", "hash-table", "dynamic-programming" ]
{"lang": "python3", "distribution": [["538", 0.901], ["658", 0.901], ["778", 0.4505], ["898", 8.108699999999999], ["1017", 18.469900000000006], ["1137", 30.632500000000025], ["1257", 7.207999999999999], ["1376", 6.3069999999999995], ["1496", 0.901], ["1616", 1.3515000000000001], ["1736", 4.9555], ["1855", 4.05439999999...
{"lang": "python3", "distribution": [["17300", 0.9009], ["17400", 3.6036], ["19200", 1.3514], ["19300", 2.2523], ["19400", 9.009], ["19500", 3.1532], ["19600", 0.4505], ["19800", 0.4505], ["20000", 0.4505], ["20200", 0.4505], ["20300", 2.2523], ["20400", 2.2523], ["20500", 0.4505], ["22000", 0.4505], ["22100", 0.4505],...
[ { "code": "from typing import List\n\nclass Solution:\n def maximumLength(self, nums: List[int], k: int) -> int:\n max_length_by_num = {-1: [0]*(k+1)}\n total_max_length = [0]*(k+1)\n for n in nums:\n n_max_length = [1] + [1+a for a in total_max_length[:-1]]\n if n ...
38
[ { "code": "class Solution:\n def maximumLength(self, nums: List[int], k: int) -> int:\n n = len(nums)\n dp = defaultdict(int)\n prev_max = [0] * n\n cur_max = [0] * n\n\n for _ in range(k + 1):\n for j in range(n - 1, -1, -1):\n dp[nums[j]] = max(\...
50
2,710
minimum-operations-to-reduce-an-integer-to-0
<p>You are given a positive integer <code>n</code>, you can do the following operation <strong>any</strong> number of times:</p> <ul> <li>Add or subtract a <strong>power</strong> of <code>2</code> from <code>n</code>.</li> </ul> <p>Return <em>the <strong>minimum</strong> number of operations to make </em><code>n</co...
55.759532
Medium
[ "dynamic-programming", "greedy", "bit-manipulation" ]
{"lang": "python3", "distribution": [["14", 0.1473], ["19", 0.1473], ["20", 0.2946], ["21", 0.1473], ["22", 0.1473], ["23", 0.7364], ["24", 0.7364], ["25", 0.7364], ["26", 1.4728], ["27", 2.7982], ["28", 3.0928], ["29", 3.2401], ["30", 4.8601], ["31", 3.0928], ["32", 5.5965], ["33", 7.2165], ["34", 6.6274], ["35", 6.77...
{"lang": "python3", "distribution": [["16100", 0.1473], ["16200", 0.7364], ["16300", 3.2401], ["16400", 24.3004], ["16500", 37.2607], ["16600", 25.3314], ["16700", 3.9764]]}
[ { "code": "import math\n\nclass Solution:\n def minOperations(self, n: int) -> int:\n try:\n ops = 0\n while True:\n closest_power_2 = 2**round(math.log2(n))\n print(closest_power_2, closest_power_2-n, n, ops)\n power2 = round(math.log...
36
[ { "code": "class Solution:\n def minOperations(self, n: int) -> int:\n ans = 0\n while n > 0:\n expo = int(log2(n))\n n = min(n-2**expo, 2 ** (expo+1) - n)\n ans += 1\n return ans", "memory": "16100" }, { "code": "class Solution:\n def minO...
8
2,708
find-the-string-with-lcp
<p>We define the <code>lcp</code> matrix of any <strong>0-indexed</strong> string <code>word</code> of <code>n</code> lowercase English letters as an <code>n x n</code> grid such that:</p> <ul> <li><code>lcp[i][j]</code> is equal to the length of the <strong>longest common prefix</strong> between the substrings <code...
31.586096
Hard
[ "array", "string", "dynamic-programming", "greedy", "union-find", "matrix" ]
{"lang": "python3", "distribution": [["1398", 1.4286], ["1424", 1.4286], ["1452", 1.4286], ["1453", 1.4286], ["1458", 1.4286], ["1466", 1.4286], ["1467", 1.4286], ["1483", 1.4286], ["1490", 1.4286], ["1494", 1.4286], ["1499", 1.4286], ["1507", 1.4286], ["1514", 1.4286], ["1519", 1.4286], ["1528", 1.4286], ["1530", 2.85...
{"lang": "python3", "distribution": [["76700", 1.4286], ["77400", 4.2857], ["77700", 1.4286], ["77800", 1.4286], ["77900", 24.2857], ["78000", 38.5714], ["78100", 14.2857], ["78200", 5.7143], ["78300", 1.4286], ["78600", 1.4286]]}
[ { "code": "class Solution:\n def findTheString(self, lcp: List[List[int]]) -> str:\n # First of all, lcp[i][i] = n - i. If this does not hold, then the lcp matrix is invalid. Also, lcp must be a symmetric matrix, because we have lcp[i][j] == lcp[j][i]. If it is not symmetric, the matrix is not valid.\...
60
[ { "code": "class Solution:\n def findTheString(self, lcp: List[List[int]]) -> str:\n # simple validation\n n = len(lcp)\n for i in range(n):\n for j in range(i + 1, n):\n if lcp[i][j] != lcp[j][i]:\n return ''\n if lcp[i][j] > n...
10
3,476
find-minimum-operations-to-make-all-elements-divisible-by-three
<p>You are given an integer array <code>nums</code>. In one operation, you can add or subtract 1 from <strong>any</strong> element of <code>nums</code>.</p> <p>Return the <strong>minimum</strong> number of operations to make all elements of <code>nums</code> divisible by 3.</p> <p>&nbsp;</p> <p><strong class="example...
89.931963
Easy
[ "array", "math" ]
{"lang": "python3", "distribution": [["18", 0.0312], ["19", 0.0312], ["21", 0.0156], ["22", 0.078], ["23", 0.0624], ["24", 0.1248], ["25", 0.1248], ["26", 0.2652], ["27", 0.4835], ["28", 0.6551], ["29", 0.6083], ["30", 0.8891], ["31", 1.4038], ["32", 2.1681], ["33", 2.2149], ["34", 2.4489], ["35", 3.5876], ["36", 5.272...
{"lang": "python3", "distribution": [["15900", 0.0156], ["16100", 0.0312], ["16200", 0.0624], ["16300", 1.6534], ["16400", 16.7213], ["16500", 32.569], ["16600", 43.5813]]}
[ { "code": "class Solution:\n def minimumOperations(self, nums: List[int]) -> int:\n sum = 0 \n for i in nums:\n if i % 3 != 0:\n sum+=1\n \n return sum", "runtime": "18" }, { "code": "class Solution:\n def minimumOperations(self, nums: List...
44
[ { "code": "class Solution:\n def minimumOperations(self, nums: List[int]) -> int:\n return sum(1 if i % 3 != 0 else 0 for i in nums)", "memory": "15900" }, { "code": "class Solution:\n def minimumOperations(self, nums: List[int]) -> int:\n c=0\n for i in nums:\n ...
8
3,475
minimum-operations-to-make-binary-array-elements-equal-to-one-i
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> 3 <strong>consecutive</strong> elements from the array and <strong>flip</st...
71.066341
Medium
[ "array", "bit-manipulation", "queue", "sliding-window", "prefix-sum" ]
{"lang": "python3", "distribution": [["196", 0.2946], ["208", 0.2946], ["220", 0.1473], ["232", 0.1473], ["244", 0.2946], ["256", 0.2946], ["269", 0.4418], ["281", 0.1473], ["293", 0.2946], ["305", 0.2946], ["317", 0.1473], ["330", 0.4418], ["342", 0.5891], ["354", 0.5891], ["366", 0.5891], ["378", 0.8837], ["390", 0.7...
{"lang": "python3", "distribution": [["18600", 0.1473], ["19400", 0.1473], ["19500", 0.1473], ["19700", 0.1473], ["19800", 0.4418], ["19900", 1.9146], ["20000", 3.9764], ["20100", 6.1856], ["20200", 11.4875], ["20300", 17.5258], ["20400", 17.0839], ["20500", 17.9676], ["20600", 5.1546], ["20700", 3.0928], ["20800", 4.1...
[ { "code": "\ndef minOperations(nums: List[int]) -> int:\n count = 0\n for i in range(len(nums) - 2):\n if nums[i] == 0:\n count += 1\n nums[i] = 1\n nums[i + 1] = abs(nums[i + 1] - 1)\n nums[i + 2] = abs(nums[i + 2] - 1)\n if nums[-1] == 0 or nums[-2] ...
93
[ { "code": "\ndef minOperations(nums: List[int]) -> int:\n count = 0\n for i in range(len(nums) - 2):\n if nums[i] == 0:\n count += 1\n nums[i] = 1\n nums[i + 1] = abs(nums[i + 1] - 1)\n nums[i + 2] = abs(nums[i + 2] - 1)\n if nums[-1] == 0 or nums[-2] ...
20
3,477
minimum-operations-to-make-binary-array-elements-equal-to-one-ii
<p>You are given a <span data-keyword="binary-array">binary array</span> <code>nums</code>.</p> <p>You can do the following operation on the array <strong>any</strong> number of times (possibly zero):</p> <ul> <li>Choose <strong>any</strong> index <code>i</code> from the array and <strong>flip</strong> <strong>all</...
65.339487
Medium
[ "array", "dynamic-programming", "greedy" ]
{"lang": "python3", "distribution": [["1024", 0.4854], ["1026", 0.2427], ["1027", 0.4854], ["1029", 0.2427], ["1030", 0.4854], ["1031", 0.2427], ["1033", 0.4854], ["1034", 0.4854], ["1036", 0.2427], ["1037", 0.2427], ["1039", 0.2427], ["1040", 1.2136], ["1042", 0.4854], ["1043", 0.4854], ["1045", 0.2427], ["1046", 0.24...
{"lang": "python3", "distribution": [["19400", 0.4854], ["19500", 0.4854], ["19600", 2.4272], ["19700", 1.4563], ["19800", 1.2136], ["19900", 0.7282], ["20000", 0.2427], ["20100", 2.1845], ["20200", 14.8058], ["20300", 35.6796], ["20400", 17.9612], ["20500", 8.7379], ["20600", 6.5534]]}
[ { "code": "class Solution:\n def minOperations(self, nums: List[int]) -> int:\n flips = current = 0\n for num in nums:\n if num == current:\n flips += 1\n current = not current\n return flips\n", "runtime": "1024" }, { "code": "class S...
96
[ { "code": "class Solution:\n def minOperations(self, nums: List[int]) -> int:\n flipped = False\n cnt = 0\n for b in nums:\n if flipped:\n b ^= 1\n\n if b == 0:\n flipped = not flipped\n cnt += 1\n\n return cnt", ...
16
3,460
count-the-number-of-inversions
<p>You are given an integer <code>n</code> and a 2D array <code>requirements</code>, where <code>requirements[i] = [end<sub>i</sub>, cnt<sub>i</sub>]</code> represents the end index and the <strong>inversion</strong> count of each requirement.</p> <p>A pair of indices <code>(i, j)</code> from an integer array <code>nu...
33.701488
Hard
[ "array", "dynamic-programming" ]
{"lang": "python3", "distribution": [["132", 19.753600000000002], ["259", 3.7038], ["387", 3.7038], ["514", 0.6173], ["641", 0.6173], ["769", 1.2346], ["896", 6.173000000000001], ["1023", 0.6173], ["1151", 0.6173], ["1278", 0.6173], ["1405", 0.6173], ["1533", 0.6173], ["1660", 0.6173], ["1787", 0.6173], ["1915", 0.6173...
{"lang": "python3", "distribution": [["16600", 6.1728], ["16700", 6.7901], ["16800", 12.963], ["16900", 1.2346], ["17000", 0.6173], ["18400", 0.6173], ["19000", 3.0864], ["19100", 10.4938], ["19200", 13.5802], ["19300", 8.0247], ["19900", 0.6173], ["21100", 0.6173], ["21900", 0.6173], ["27700", 0.6173], ["27800", 0.617...
[ { "code": "class Solution:\n def numberOfPermutations(self, n: int, requirements: List[List[int]]) -> int:\n mod =10**9+7\n def cnt1(k,target): # partition target into k int\n return comb(k+target-1,target)%mod\n def cnt(low,up,target): # x1<=low, x2<=low+1...xk<up\n ...
88
[ { "code": "class Solution:\n def numberOfPermutations(self, n: int, requirements: List[List[int]]) -> int:\n requirements.sort()\n dp = [0] * (requirements[-1][-1] + 1)\n dp[0] = 1\n r_map = {}\n for r in requirements:\n r_map[r[0]] = r[1]\n for i in range...
41
3,464
maximize-total-cost-of-alternating-subarrays
<p>You are given an integer array <code>nums</code> with length <code>n</code>.</p> <p>The <strong>cost</strong> of a <span data-keyword="subarray-nonempty">subarray</span> <code>nums[l..r]</code>, where <code>0 &lt;= l &lt;= r &lt; n</code>, is defined as:</p> <p><code>cost(l, r) = nums[l] - nums[l + 1] + ... + nums...
28.645006
Medium
[ "array", "dynamic-programming" ]
{"lang": "python3", "distribution": [["448", 1.2765], ["456", 4.255199999999999], ["465", 3.8299000000000003], ["474", 9.7871], ["482", 6.8085], ["491", 10.638299999999997], ["499", 4.2553], ["508", 3.4042000000000003], ["517", 2.5531], ["525", 1.2765], ["534", 0.4255], ["542", 0.8511], ["551", 0.4255], ["560", 0.4255]...
{"lang": "python3", "distribution": [["29900", 0.4255], ["30300", 0.4255], ["30600", 6.383], ["30700", 25.5319], ["30800", 5.9574], ["30900", 0.4255], ["31000", 1.2766], ["31100", 4.2553], ["31200", 1.2766], ["31300", 0.8511], ["31400", 0.8511], ["32299", 0.4255], ["32600", 1.2766], ["32700", 0.4255], ["33100", 0.4255]...
[ { "code": "class Solution:\n def maximumTotalCost(self, nums: List[int]) -> int:\n pos, neg = -inf, 0\n for val in nums:\n pos, neg = max(pos,neg)+val, pos-val\n return max(pos,neg)", "runtime": "465" }, { "code": "class Solution:\n def maximumTotalCost(self, nu...
98
[ { "code": "class Solution:\n def maximumTotalCost(self, nums: List[int]) -> int:\n first, second = 0, nums[0]\n for i in range(1, len(nums)):\n val = max([second + nums[i], nums[i] * -1 + nums[i-1] + first])\n first, second = second, val\n return second\n", "mem...
77
3,469
maximum-height-of-a-triangle
<p>You are given two integers <code>red</code> and <code>blue</code> representing the count of red and blue colored balls. You have to arrange these balls to form a triangle such that the 1<sup>st</sup> row will have 1 ball, the 2<sup>nd</sup> row will have 2 balls, the 3<sup>rd</sup> row will have 3 balls, and so on.<...
42.794357
Easy
[ "array", "enumeration" ]
{"lang": "python3", "distribution": [["17", 0.1416], ["19", 0.1416], ["23", 0.4249], ["24", 0.7082], ["25", 0.4249], ["26", 1.1331], ["27", 1.5581], ["28", 2.5496], ["29", 2.6912], ["30", 2.4079], ["31", 3.8244], ["32", 5.5241], ["33", 3.8244], ["34", 4.8159], ["35", 6.3739], ["36", 9.0652], ["37", 6.5156], ["38", 5.94...
{"lang": "python3", "distribution": [["16200", 0.7082], ["16300", 0.9915], ["16400", 15.1558], ["16500", 35.8357], ["16600", 38.9518]]}
[ { "code": "'''\nyou are given 2 integers\n red and blue\n\neach int represents the count of red and blue colored balls\n\nyou have to arrage these balls to form a trianlge \n\nsuch that the 1st row will have 1 ball,\n teh second row will have 2 balls,\n the third row 3 balls,\n etc...\n\nall the bal...
27
[ { "code": "class Solution:\n def maxHeightOfTriangle(self, red: int, blue: int) -> int:\n def check(a,b):\n c=0\n f=0\n l=1\n while(a>0 or b>0):\n if(f==0):\n if(a>=l):\n a-=l\n else...
7
2,687
lexicographically-smallest-beautiful-string
<p>A string is <strong>beautiful</strong> if:</p> <ul> <li>It consists of the first <code>k</code> letters of the English lowercase alphabet.</li> <li>It does not contain any substring of length <code>2</code> or more which is a palindrome.</li> </ul> <p>You are given a beautiful string <code>s</code> of length <co...
37.951849
Hard
[ "string", "greedy" ]
{"lang": "python3", "distribution": [["199", 1.3333], ["201", 1.3333], ["203", 1.3333], ["204", 1.3333], ["205", 1.3333], ["208", 1.3333], ["216", 1.3333], ["218", 1.3333], ["223", 4.0], ["225", 1.3333], ["230", 1.3333], ["234", 1.3333], ["235", 1.3333], ["239", 1.3333], ["247", 1.3333], ["248", 2.6667], ["250", 1.3333...
{"lang": "python3", "distribution": [["17800", 1.3333], ["18200", 1.3333], ["18300", 14.6667], ["18400", 38.6667], ["18500", 6.6667], ["18600", 2.6667], ["18700", 1.3333], ["18900", 2.6667], ["19200", 5.3333], ["19300", 13.3333]]}
[ { "code": "class Solution:\n def smallestBeautifulString(self, s: str, k: int) -> str:\n n = len(s)\n s = list(s)\n \n # Find the first position from the right that can be incremented\n for i in range(n - 1, -1, -1):\n ch = ord(s[i]) - ord('a') + 1\n w...
57
[ { "code": "class Solution:\n def smallestBeautifulString(self, s: str, k: int) -> str:\n chrs, n = [\"a\", \"b\", \"c\"], len(s)\n i, ci = n-1, \"\"\n while i >= 0:\n c = ord(s[i]) + 1\n nc = set()\n if i-1 >= 0: nc.add(s[i-1])\n if i-2 >= 0: n...
11
1,730
special-array-with-x-elements-greater-than-or-equal-x
<p>You are given an array <code>nums</code> of non-negative integers. <code>nums</code> is considered <strong>special</strong> if there exists a number <code>x</code> such that there are <strong>exactly</strong> <code>x</code> numbers in <code>nums</code> that are <strong>greater than or equal to</strong> <code>x</code...
66.873637
Easy
[ "array", "binary-search", "sorting" ]
{"lang": "python3", "distribution": [["20", 0.0655], ["21", 0.0655], ["22", 0.1966], ["23", 0.1966], ["24", 0.1311], ["25", 0.2621], ["26", 0.5898], ["27", 0.3277], ["28", 0.7208], ["29", 0.7864], ["30", 1.7038], ["31", 1.7038], ["32", 2.6212], ["33", 3.2765], ["34", 3.9974], ["35", 4.2595], ["36", 5.308], ["37", 4.849...
{"lang": "python3", "distribution": [["16200", 0.2621], ["16300", 4.5872], ["16400", 21.7562], ["16500", 43.3159]]}
[ { "code": "class Solution:\n def specialArray(self, nums: List[int]) -> int:\n \"\"\"\n 0 4 3 0 4 - nums\n 0 1 2 3 4 5 - index of new_arr\n 2 0 0 1 2 0 - count \n 3 2 0 - prefix_sum\n \"\"\"\n count = [0] * (len(nums) + 1)\n for ...
42
[ { "code": "class Solution:\n def specialArray(self, nums: List[int]) -> int:\n nums.sort(reverse=True)\n n = len(nums)\n \n for x in range(n + 1):\n count = 0\n for num in nums:\n if num >= x:\n count += 1\n el...
6
1,400
find-winner-on-a-tic-tac-toe-game
<p><strong>Tic-tac-toe</strong> is played by two players <code>A</code> and <code>B</code> on a <code>3 x 3</code> grid. The rules of Tic-Tac-Toe are:</p> <ul> <li>Players take turns placing characters into empty squares <code>&#39; &#39;</code>.</li> <li>The first player <code>A</code> always places <code>&#39;X&#3...
53.961911
Easy
[ "array", "hash-table", "matrix", "simulation" ]
{"lang": "python3", "distribution": [["19", 0.0695], ["21", 0.0695], ["22", 0.2085], ["23", 0.417], ["24", 0.278], ["25", 0.4864], ["26", 0.6949], ["27", 1.8763], ["28", 1.8068], ["29", 1.3899], ["30", 1.3204], ["31", 4.795], ["32", 4.1001], ["33", 4.517], ["34", 4.656], ["35", 6.6713], ["36", 7.8527], ["37", 6.8798], ...
{"lang": "python3", "distribution": [["16000", 0.0695], ["16100", 0.0695], ["16200", 0.0695], ["16300", 0.9034], ["16400", 10.7714], ["16500", 25.8513], ["16600", 39.2634]]}
[ { "code": "class Solution:\n def tictactoe(self, moves: List[List[int]]) -> str:\n # Approach 2024.09.19\n cache = defaultdict(int)\n for i, pt in enumerate(moves):\n r_key = f\"{i%2}_p_{pt[0]}\"\n c_key = f\"{i%2}_c_{pt[1]}\"\n pd_key = f\"{i%2}_pd_{pt[0...
33
[ { "code": "class Solution:\n def tictactoe(self, moves: List[List[int]]) -> str:\n M = [ [0]*3 for _ in range(3) ]\n L = len(moves) \n s = ''\n def check(M):\n for r in range(3):\n n = sum(M[r])\n if n==3: return 'A'\n elif n...
10
3,490
find-the-maximum-length-of-valid-subsequence-i
You are given an integer array <code>nums</code>. <p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p> <ul> <li><code>(sub[0] + sub[1]) % 2 == (sub[1] + sub[2]) % 2 == ... == (sub[x - 2] + ...
37.921712
Medium
[ "array", "dynamic-programming" ]
{"lang": "python3", "distribution": [["560", 0.7968], ["565", 0.9959999999999999], ["570", 2.7888], ["575", 3.984], ["580", 2.9879999999999995], ["585", 6.1752], ["590", 3.5856], ["595", 4.7808], ["600", 3.7848], ["605", 4.7808], ["610", 5.1792], ["615", 3.984], ["620", 4.7808], ["625", 3.3864], ["630", 3.1872000000000...
{"lang": "python3", "distribution": [["35200", 0.3984], ["35300", 0.1992], ["35900", 0.5976], ["36000", 0.5976], ["36100", 0.5976], ["36800", 0.1992], ["36900", 0.1992], ["37600", 0.1992], ["37900", 0.1992], ["38000", 0.1992], ["38100", 0.1992], ["38200", 0.5976], ["38300", 3.9841], ["38400", 3.3865], ["38500", 0.1992]...
[ { "code": "class Solution:\n def maximumLength(self, nums: List[int]) -> int:\n a,b,c,d = 0,0,0,0\n for v in nums:\n if v%2==0: a,b = a+1, c+1\n else: c,d = b+1,d+1\n return max(a,b,c,d)\n\n ", "runtime": "560" }, { "code": "class Solution:\n d...
103
[ { "code": "class Solution:\n def maximumLength(self, nums: List[int]) -> int:\n N = len(nums)\n for i in range(N):\n nums[i] %= 2\n ret = max(nums.count(0),nums.count(1))\n dp = [0]*2\n for i in range(N):\n dp[nums[i]] = dp[1-nums[i]]+1\n return...
46
3,491
find-the-maximum-length-of-valid-subsequence-ii
You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>. <p>A <span data-keyword="subsequence-array">subsequence</span> <code>sub</code> of <code>nums</code> with length <code>x</code> is called <strong>valid</strong> if it satisfies:</p> <ul> <li><code>(sub[0] + sub[1]...
39.596208
Medium
[ "array", "dynamic-programming" ]
{"lang": "python3", "distribution": [["182", 1.6058000000000001], ["228", 0.4588], ["274", 0.2294], ["320", 0.2294], ["366", 0.2294], ["412", 0.6881999999999999], ["458", 0.2294], ["504", 0.2294], ["550", 0.4587], ["596", 0.2294], ["642", 0.2294], ["689", 0.2294], ["735", 0.4588], ["781", 5.2755], ["827", 2.294], ["873...
{"lang": "python3", "distribution": [["16400", 0.2294], ["16500", 0.9174], ["16600", 0.6881], ["16700", 4.1284], ["16800", 8.2569], ["16900", 6.8807], ["17000", 2.7523], ["17100", 0.6881], ["22500", 0.2294], ["24300", 0.2294], ["24400", 3.4404], ["24500", 8.945], ["24600", 19.7248], ["24700", 12.844], ["24800", 1.8349]...
[ { "code": "class Solution:\n def maximumLength(self, nums: List[int], k: int) -> int:\n nums = [num % k for num in nums]\n def check(a, b):\n alternating = [num for num in nums if num == a or num == b]\n return 1 + sum([1 for a, b in pairwise(alternating) if a != b])\n ...
140
[ { "code": "class Solution:\n def maximumLength(self, nums: List[int], k: int) -> int:\n max_length = 0\n \n # Iterate over each possible remainder value from 0 to k-1\n for target_remainder in range(k):\n\n # Initialize the dynamic programming array to store the maximum...
53
3,439
find-minimum-diameter-after-merging-two-trees
<p>There exist two <strong>undirected </strong>trees with <code>n</code> and <code>m</code> nodes, numbered from <code>0</code> to <code>n - 1</code> and from <code>0</code> to <code>m - 1</code>, respectively. You are given two 2D integer arrays <code>edges1</code> and <code>edges2</code> of lengths <code>n - 1</code>...
35.953827
Hard
[ "tree", "depth-first-search", "breadth-first-search", "graph" ]
{"lang": "python3", "distribution": [["604", 0.7936], ["638", 0.3968], ["672", 0.3968], ["706", 0.3968], ["741", 0.3968], ["775", 0.3968], ["809", 0.3968], ["844", 0.3968], ["878", 0.3968], ["912", 0.3968], ["946", 0.3968], ["981", 0.3968], ["1015", 0.3968], ["1049", 1.5873], ["1083", 0.3968], ["1118", 0.3968], ["1152"...
{"lang": "python3", "distribution": [["84558", 0.7936], ["85074", 0.3968], ["85590", 0.3968], ["86106", 0.3968], ["86623", 13.889], ["87139", 7.539599999999999], ["87655", 9.9207], ["88171", 3.1745999999999994], ["88688", 1.1905], ["89204", 1.5873], ["89720", 0.3968], ["90236", 1.5873], ["90753", 0.3968], ["91269", 0.3...
[ { "code": "def diameter(edges: list[list[int]]) -> int:\n n = len(edges) + 1\n g = [[] for _ in range(n)]\n for u, v in edges:\n g[u].append(v)\n g[v].append(u)\n\n def bfs(s: int) -> list[int]:\n d = [-1] * n\n d[s] = 0\n q = deque([s])\n while q:\n ...
95
[ { "code": "def diameter(edges: list[list[int]]) -> int:\n n = len(edges) + 1\n g = [[] for _ in range(n)]\n for u, v in edges:\n g[u].append(v)\n g[v].append(u)\n\n def bfs(s: int) -> list[int]:\n d = [-1] * n\n d[s] = 0\n q = deque([s])\n while q:\n ...
83
3,264
maximum-points-after-enemy-battles
<p>You are given an integer array <code>enemyEnergies</code> denoting the energy values of various enemies.</p> <p>You are also given an integer <code>currentEnergy</code> denoting the amount of energy you have initially.</p> <p>You start with 0 points, and all the enemies are unmarked initially.</p> <p>You can perf...
31.897317
Medium
[ "array", "greedy" ]
{"lang": "python3", "distribution": [["113", 0.5222], ["119", 0.2611], ["125", 0.2611], ["131", 0.5222], ["137", 0.5222], ["142", 0.2611], ["148", 0.5222], ["154", 0.2611], ["160", 1.3055], ["166", 0.7833], ["172", 1.3055], ["177", 0.7833], ["183", 1.0444], ["189", 0.5222], ["195", 1.5666], ["201", 1.3055], ["206", 1.0...
{"lang": "python3", "distribution": [["25500", 0.2611], ["25600", 0.2611], ["27200", 0.2611], ["27300", 0.5222], ["27400", 1.0444], ["27500", 2.8721], ["27600", 1.0444], ["28800", 0.2611], ["28900", 0.2611], ["30800", 0.2611], ["31100", 0.2611], ["31200", 0.7833], ["31300", 0.5222], ["31400", 0.2611], ["31500", 0.2611]...
[ { "code": "class Solution:\n def maximumPoints(self, enemyEnergies: List[int], currentEnergy: int) -> int:\n enemyEnergies.sort()\n if currentEnergy<enemyEnergies[0]:\n return 0\n point=(currentEnergy+sum(enemyEnergies))//enemyEnergies[0] - 1\n return point\n\nwith open...
95
[ { "code": "class Solution:\n def maximumPoints(self, enemyEnergies: List[int], currentEnergy: int) -> int:\n enemyEnergies.sort()\n if currentEnergy<enemyEnergies[0]:\n return 0\n point=(currentEnergy+sum(enemyEnergies))//enemyEnergies[0] - 1\n return point\n\nwith open...
20
3,483
alternating-groups-ii
<p>There is a circle of red and blue tiles. You are given an array of integers <code>colors</code> and an integer <code>k</code>. The color of tile <code>i</code> is represented by <code>colors[i]</code>:</p> <ul> <li><code>colors[i] == 0</code> means that tile <code>i</code> is <strong>red</strong>.</li> <li><code>...
40.693874
Medium
[ "array", "sliding-window" ]
{"lang": "python3", "distribution": [["610", 0.4652], ["621", 0.2326], ["631", 0.9303], ["642", 0.4652], ["653", 2.7908000000000004], ["664", 3.4885000000000006], ["674", 4.4187], ["685", 8.6047], ["696", 11.1627], ["707", 11.3952], ["717", 10.4651], ["728", 8.1397], ["739", 5.813899999999999], ["749", 6.74419999999999...
{"lang": "python3", "distribution": [["19500", 0.2326], ["19800", 0.2326], ["19900", 1.6279], ["20000", 5.814], ["20100", 25.1163], ["20200", 10.2326], ["20300", 0.9302], ["20400", 2.7907], ["20500", 7.2093], ["20600", 13.9535], ["20700", 9.5349], ["20800", 2.3256], ["20900", 4.186], ["21000", 3.2558], ["21100", 1.1628...
[ { "code": "class Solution:\n def numberOfAlternatingGroups(self, colors: List[int], k: int) -> int:\n n = len(colors)\n if n == 100000:\n return 100_000\n elif n == 100_000 and k == 31623:\n return 100_000\n elif n == 99_995 and k == 8000:\n return...
62
[ { "code": "class Solution:\n def numberOfAlternatingGroups(self, colors: List[int], k: int) -> int:\n m=1\n res=0\n for i in range(1-k,len(colors)-1):\n if colors[i]==colors[i+1]: m=1\n else: m+=1\n res+=m>=k\n return res", "memory": "19500" ...
35
2,620
find-consecutive-integers-from-a-data-stream
<p>For a stream of integers, implement a data structure that checks if the last <code>k</code> integers parsed in the stream are <strong>equal</strong> to <code>value</code>.</p> <p>Implement the <strong>DataStream</strong> class:</p> <ul> <li><code>DataStream(int value, int k)</code> Initializes the object with an ...
49.52014
Medium
[ "hash-table", "design", "queue", "counting", "data-stream" ]
{"lang": "python3", "distribution": [["339", 3.6475], ["344", 3.6475], ["349", 5.7751], ["354", 7.9028], ["358", 13.9818], ["363", 10.9424], ["368", 10.3345], ["373", 8.2067], ["378", 4.5594], ["383", 5.1673], ["388", 4.5594], ["393", 2.4318], ["398", 0.304], ["402", 2.4316999999999998], ["407", 0.9119999999999999], ["...
{"lang": "python3", "distribution": [["41900", 0.304], ["42700", 0.304], ["42800", 0.304], ["42900", 1.5198], ["43000", 9.4225], ["43100", 45.2888], ["43200", 10.6383], ["43300", 0.304], ["43500", 0.304], ["43900", 2.4316], ["44000", 7.2948], ["44100", 2.4316], ["44200", 0.6079], ["44300", 2.7356], ["44400", 0.9119], [...
[ { "code": "class DataStream:\n def __init__(self, value, k):\n self.value = value\n self.k = k\n self.consec_value = 0\n \n def consec(self, num):\n if num == self.value:\n self.consec_value += 1\n else:\n self.consec_value = 0\n \n ...
51
[ { "code": "class DataStream:\n def __init__(self, value: int, k: int) -> None:\n self.value, self.k = value, k\n self.stream_len, self.last_missed_idx = 0, -1\n\n def consec(self, num: int) -> bool:\n self.stream_len += 1\n if num != self.value:\n self.last_missed_id...
28
2,618
maximize-the-minimum-powered-city
<p>You are given a <strong>0-indexed</strong> integer array <code>stations</code> of length <code>n</code>, where <code>stations[i]</code> represents the number of power stations in the <code>i<sup>th</sup></code> city.</p> <p>Each power station can provide power to every city in a fixed <strong>range</strong>. In oth...
32.771727
Hard
[ "array", "binary-search", "greedy", "queue", "sliding-window", "prefix-sum" ]
{"lang": "python3", "distribution": [["1344", 1.1765], ["1501", 1.1765], ["1608", 1.1765], ["1627", 1.1765], ["1642", 1.1765], ["1693", 1.1765], ["1703", 1.1765], ["1758", 1.1765], ["1778", 1.1765], ["1806", 1.1765], ["1835", 1.1765], ["1841", 1.1765], ["1855", 1.1765], ["1856", 1.1765], ["1865", 1.1765], ["1867", 1.17...
{"lang": "python3", "distribution": [["28900", 3.5294], ["29000", 2.3529], ["29600", 4.7059], ["29700", 8.2353], ["29900", 1.1765], ["30000", 4.7059], ["30100", 7.0588], ["30200", 10.5882], ["30300", 4.7059], ["30400", 2.3529], ["30500", 1.1765], ["30600", 1.1765], ["30700", 2.3529], ["31500", 2.3529], ["32299", 1.1765...
[ { "code": "class Solution:\n def maxPower(self, stations: List[int], r: int, k: int) -> int:\n n = len(stations)\n e = sum(stations[i] for i in range(r+1))\n available_power = [e]\n for i in range(1, n):\n if i+r < n:\n e += stations[i+r]\n if ...
74
[ { "code": "def is_ok(val, stations, r, k):\n total = 0\n n = len(stations)\n for i in range(n):\n lo = max(0, i-r)\n hi = min(n-1, i+r)\n if i == 0:\n total += sum(stations[lo:hi+1])\n else:\n if lo > prev_lo:\n total -= stations[prev_lo]...
38
2,642
time-to-cross-a-bridge
<p>There are <code>k</code> workers who want to move <code>n</code> boxes from the right (old) warehouse to the left (new) warehouse. You are given the two integers <code>n</code> and <code>k</code>, and a 2D integer array <code>time</code> of size <code>k x 4</code> where <code>time[i] = [right<sub>i</sub>, pick<sub>i...
44.033425
Hard
[ "array", "heap-priority-queue", "simulation" ]
{"lang": "python3", "distribution": [["323", 1.2821], ["325", 1.2821], ["328", 1.2821], ["336", 1.2821], ["338", 1.2821], ["355", 1.2821], ["360", 1.2821], ["365", 1.2821], ["366", 2.5641], ["369", 1.2821], ["370", 1.2821], ["371", 1.2821], ["372", 3.8462], ["373", 1.2821], ["374", 5.1282], ["375", 3.8462], ["376", 1.2...
{"lang": "python3", "distribution": [["23100", 2.5641], ["23200", 21.7949], ["23300", 41.0256], ["23400", 21.7949], ["23500", 3.8462], ["23700", 2.5641], ["23800", 1.2821]]}
[ { "code": "class Solution:\n def findCrossingTime(self, n: int, k: int, time: List[List[int]]) -> int:\n time.sort(key=lambda x: x[0] + x[2])\n cur = 0\n wait_in_left, wait_in_right = [], []\n work_in_left, work_in_right = [], []\n for i in range(k):\n heappush(w...
52
[ { "code": "class Solution:\n def findCrossingTime(self, n: int, k: int, time: List[List[int]]) -> int:\n\n right = []\n picking = []\n putting = []\n left = []\n # each worker will be separated by their efficiency\n for i,j in enumerate(time):\n heapq.heap...
7