post_href stringlengths 57 213 | python_solutions stringlengths 71 22.3k | slug stringlengths 3 77 | post_title stringlengths 1 100 | user stringlengths 3 29 | upvotes int64 -20 1.2k | views int64 0 60.9k | problem_title stringlengths 3 77 | number int64 1 2.48k | acceptance float64 0.14 0.91 | difficulty stringclasses 3 values | __index_level_0__ int64 0 34k |
|---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/minimum-suffix-flips/discuss/1185638/Python3-simple-solution-using-three-approaches | class Solution:
def minFlips(self, target: str) -> int:
i = 1
count = 0
if target[0] == '1':
count += 1
while i < len(target):
if target[i] != target[i-1]:
count += 1
i += 1
return count | minimum-suffix-flips | Python3 simple solution using three approaches | EklavyaJoshi | 0 | 42 | minimum suffix flips | 1,529 | 0.724 | Medium | 22,700 |
https://leetcode.com/problems/minimum-suffix-flips/discuss/1185638/Python3-simple-solution-using-three-approaches | class Solution:
def minFlips(self, target: str) -> int:
count = 0
x = '0'
for i in target:
if i != x:
count += 1
x = i
return count | minimum-suffix-flips | Python3 simple solution using three approaches | EklavyaJoshi | 0 | 42 | minimum suffix flips | 1,529 | 0.724 | Medium | 22,701 |
https://leetcode.com/problems/minimum-suffix-flips/discuss/1122509/Python-3-easy-solution-94 | class Solution:
def minFlips(self, target: str) -> int:
count = 0
history = "0"
for c in target:
if c != history:
history = c
count += 1
return count | minimum-suffix-flips | Python 3 easy solution [94%] | arnav3 | 0 | 68 | minimum suffix flips | 1,529 | 0.724 | Medium | 22,702 |
https://leetcode.com/problems/minimum-suffix-flips/discuss/755866/Python-Easy-to-understand-solution | class Solution:
def minFlips(self, target: str) -> int:
if set(target) == set('0'):
return 0
if set(target) == set('1'):
return 1
ans, cur = 0, '0'
for bulb in target:
if bulb != cur:
cur = bulb
ans += 1
return ans | minimum-suffix-flips | Python Easy to understand solution | sexylol | 0 | 27 | minimum suffix flips | 1,529 | 0.724 | Medium | 22,703 |
https://leetcode.com/problems/minimum-suffix-flips/discuss/755806/Python3-Simple | class Solution:
def minFlips(self, target: str) -> int:
count = 0
checkfor = 1
for i in range(len(target)):
if int(target[i]) == checkfor:
count += 1
checkfor ^= 1
return count | minimum-suffix-flips | Python3 Simple | arowshan | 0 | 34 | minimum suffix flips | 1,529 | 0.724 | Medium | 22,704 |
https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/discuss/755979/Python3-recursive-postorder-dfs | class Solution:
def countPairs(self, root: TreeNode, distance: int) -> int:
def dfs(node):
"""Return (a list of) distances to leaves of sub-tree rooted at node."""
nonlocal ans
if not node: return []
if node.left is node.right is None: return [0]
left,right = dfs(node.left), dfs(node.right)
ans += sum(2 + x + y <= distance for x in left for y in right)
return [1 + x for x in left + right]
ans = 0
dfs(root)
return ans | number-of-good-leaf-nodes-pairs | [Python3] recursive postorder dfs | ye15 | 3 | 264 | number of good leaf nodes pairs | 1,530 | 0.607 | Medium | 22,705 |
https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/discuss/755979/Python3-recursive-postorder-dfs | class Solution:
def countPairs(self, root: TreeNode, distance: int) -> int:
ans = 0
def fn(node):
"""Return distances of leaves of sub-tree rooted at node."""
nonlocal ans
if not node: return []
if node.left is node.right is None: return [0]
left, right = fn(node.left), fn(node.right)
for x in right:
k = bisect_right(left, distance - x - 2) # binary search
ans += k
# merge
out = []
i = j = 0
while i < len(left) or j < len(right):
if j == len(right) or i < len(left) and left[i] < right[j]:
out.append(1 + left[i])
i += 1
else:
out.append(1 + right[j])
j += 1
return out
fn(root)
return ans | number-of-good-leaf-nodes-pairs | [Python3] recursive postorder dfs | ye15 | 3 | 264 | number of good leaf nodes pairs | 1,530 | 0.607 | Medium | 22,706 |
https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/discuss/952312/Python-dfs-clear-and-simple-solution | class Solution:
def __init__(self):
self.sol = 0
def countPairs(self, root: TreeNode, distance: int) -> int:
def solve(node):
if not node:
return []
if not node.left and not node.right:
# node, depth
return [[0, 0]]
else:
cur = []
l = solve(node.left)
r = solve(node.right)
for n in l: n[1] += 1
for n in r: n[1] += 1
for n in r:
for n1 in l:
if n[1] + n1[1] <= distance: self.sol += 1
return l+r
solve(root)
return self.sol | number-of-good-leaf-nodes-pairs | Python dfs clear and simple solution | modusV | 2 | 155 | number of good leaf nodes pairs | 1,530 | 0.607 | Medium | 22,707 |
https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/discuss/2743745/Clean | class Solution:
def countPairs(self, root: TreeNode, distance: int) -> int:
self.distance = distance
self.ans = 0
self.dfs(root)
return self.ans
def dfs(self, root):
if not root:
return []
if not root.left and not root.right:
return [1]
l, r = self.dfs(root.left), self.dfs(root.right)
for i in l:
for j in r:
if i + j <= self.distance:
self.ans += 1
l = [i+1 for i in l]
r = [j+1 for j in r]
return l+r | number-of-good-leaf-nodes-pairs | Clean | hacktheirlives | 0 | 3 | number of good leaf nodes pairs | 1,530 | 0.607 | Medium | 22,708 |
https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/discuss/2733076/JavaPython3-or-Single-Traversal | class Solution:
def __init__(self):
self.ans=0
def countPairs(self, root: TreeNode, distance: int) -> int:
self.solve(root,distance)
return self.ans
def solve(self,root,distance):
if not root:
return []
if not root.left and not root.right:
return [1]
leftNodes=self.solve(root.left,distance)
rightNodes=self.solve(root.right,distance)
for ln in leftNodes:
for rn in rightNodes:
if ln+rn<=distance:
self.ans+=1
leftNodes=list(map(lambda x:x+1,leftNodes))
rightNodes=list(map(lambda x:x+1,rightNodes))
return leftNodes+rightNodes | number-of-good-leaf-nodes-pairs | [Java/Python3] | Single Traversal | swapnilsingh421 | 0 | 8 | number of good leaf nodes pairs | 1,530 | 0.607 | Medium | 22,709 |
https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/discuss/2087831/Python3-Solution-with-using-dfs | class Solution:
def __init__(self):
self.res = 0
def traversal(self, node, dist):
if not node:
return []
if not node.left and not node.right:
return [1]
dists_from_left = self.traversal(node.left, dist)
dists_from_right = self.traversal(node.right, dist)
for ld in dists_from_left:
for rd in dists_from_right:
if ld + rd <= dist:
self.res += 1
return [d + 1 for d in dists_from_left + dists_from_right if d < dist]
def countPairs(self, root: TreeNode, distance: int) -> int:
self.traversal(root, distance)
return self.res | number-of-good-leaf-nodes-pairs | [Python3] Solution with using dfs | maosipov11 | 0 | 75 | number of good leaf nodes pairs | 1,530 | 0.607 | Medium | 22,710 |
https://leetcode.com/problems/string-compression-ii/discuss/1203398/Python3-top-down-dp | class Solution:
def getLengthOfOptimalCompression(self, s: str, k: int) -> int:
rle = lambda x: x if x <= 1 else int(log10(x)) + 2 # rle length of a char repeated x times
@cache
def fn(i, k, prev, cnt):
"""Return length of rle of s[i:] with k chars to be deleted."""
if k < 0: return inf
if i == len(s): return 0
ans = fn(i+1, k-1, prev, cnt) # delete current character
if prev == s[i]:
ans = min(ans, fn(i+1, k, s[i], cnt+1) + rle(cnt+1) - rle(cnt))
else:
ans = min(ans, fn(i+1, k, s[i], 1) + 1)
return ans
return fn(0, k, "", 0) | string-compression-ii | [Python3] top-down dp | ye15 | 12 | 1,300 | string compression ii | 1,531 | 0.499 | Hard | 22,711 |
https://leetcode.com/problems/string-compression-ii/discuss/2704634/Python-with-Comments | class Solution:
def getLengthOfOptimalCompression(self, s: str, k: int) -> int:
#traverse the string
#keep track of the status of delete or not delete current character
#the status includes current index, number of delete, the previous character, and the runing length of previous character
#return the minium length of compresed between delete or not delete
#O(n^2*26*k) = O(n^2*k) time and space
memo = {}
return self.dfs(s, 0, k, None, 0, memo)
def dfs(self, s, i, k, prev, l, memo):
if i == len(s):
return 0
if (i, k, prev, l) in memo:
return memo[(i, k, prev, l)]
if k > 0:
delete = self.dfs(s, i + 1, k - 1, prev, l, memo)
else:
#in this case, we cannot delete, set it as INF to choose skip in the end
delete = float("inf")
if s[i] == prev:
#need one more digit for the count
carry = 1 if l == 1 or len(str(l + 1)) > len(str(l)) else 0
skip = carry + self.dfs(s, i + 1, k, s[i], l + 1, memo)
else:
skip = 1 + self.dfs(s, i + 1, k, s[i], 1, memo)
memo[(i, k, prev, l)] = min(delete, skip)
return memo[(i, k, prev, l)] | string-compression-ii | Python with Comments 💚 | Khacker | 4 | 667 | string compression ii | 1,531 | 0.499 | Hard | 22,712 |
https://leetcode.com/problems/string-compression-ii/discuss/2704720/python3 | class Solution:
def getLengthOfOptimalCompression(self, s: str, k: int) -> int:
# We define f(i, curr_run_ch, run_length, nb_dels_remain) to return
# the minimum, additional, number of characters it will cost to run-length
# compress the substring s[i..n-1].
# `curr_run_ch` is the character we have in the current "run", or the same
# contiguous block of characters.
# `run_length` is the length of the current "run", or the length of the
# contiguous block of identical characters.
# e.g. if we just encoded "aaaaa", `curr_run_ch` is "a" and `run_length` = 5
# `nb_dels_remain` is the number of delete operations we have available to us,
# should we choose to use them
memo = {}
def f(i, curr_run_ch, run_length, nb_dels_remain):
if i == len(s):
return 0
key = (i, curr_run_ch, run_length, nb_dels_remain)
if key in memo:
return memo[key]
# At character i, we have two possible options, we could choose to either
# delete this character or keep this character. Each choice we make will
# incurr some additional run-length encoding length for s[i..n-1]. We return
# the minimum of the two.
# Delete s[i]
del_ch_cost = float('inf')
if nb_dels_remain > 0:
# Deleting s[i] means the latest character we kept stays the same AND
# the current run-length of characters stays the same as well
del_ch_cost = f(i + 1, curr_run_ch, run_length, nb_dels_remain - 1)
# Keep s[i]
keep_ch_cost = 0
if s[i] == curr_run_ch:
# The new character at s[i] we are about to encode is the same as the character in the
# current "run", we could choose to include it into the current run of course.
# Be careful that if we started with run-length of 1, 9, 99, 999 and etc, encoding another
# character same as `curr_run_ch` into the same "run" will require an extra digit.
# e.g. 'a' => '2a' '9a' => '10a', '99a' => '100a'
extra_digit_cost = 0
if run_length == 1 or len(str(run_length + 1)) > len(str(run_length)):
extra_digit_cost = 1
keep_ch_cost = extra_digit_cost + f(i + 1, curr_run_ch, run_length + 1, nb_dels_remain)
else:
# s[i] != curr_run_ch, we are going to need to run-length encode at least
# one instance of s[i] which would cost 1, plus whatever the cost to encode
# the rest. Of course that also means the current "run" will "reset" and start anew with
# a single character s[i]
keep_ch_cost = 1 + f(i + 1, s[i], 1, nb_dels_remain)
memo[key] = min(keep_ch_cost, del_ch_cost)
return memo[key]
return f(0, '', 0, k) | string-compression-ii | python3 | rupamkarmakarcr7 | 1 | 113 | string compression ii | 1,531 | 0.499 | Hard | 22,713 |
https://leetcode.com/problems/string-compression-ii/discuss/2790904/Fast-recursive-Python-solution-beats-97-for-speed-and-92-for-memory | class Solution:
def getLengthOfOptimalCompression(self, s: str, k: int) -> int:
def cl(l0, k):
if k>=l0:
return 0, {(0, "", 0)}
if not k:
ct=1
while ct<=l0 and s[l0-ct]==s[l0-1]:
ct+=1
el=encoded_len(s[:l0])
return el, {(el, s[l0-1], ct-1)}
l1, min_lst1=table[k-1]
l2, min_lst2=table[k]
end_char=s[l0-1]
lans=l1
for l, min_ending, min_ct in min_lst2:
if min_ending==end_char and min_ct not in[1, 9, 99]:
if l<lans:
lans=l
else:
if l+1<lans:
lans=l+1
min_lstans={it for it in min_lst1 if it[0]<=lans+1}
for l, min_ending, min_ct in min_lst2:
if min_ending==end_char:
if min_ct in [1, 9, 99]:
l+=1
min_ct+=1
else:
l+=1
min_ct=1
if l<=lans+1:
min_lstans.add((l, s[l0-1], min_ct))
return lans, min_lstans
def cl2(l0, k):
if k>=l0:
return 0, {(0, "", 0)}
if not k:
ct=1
while ct<=l0 and s[l0-ct]==s[l0-1]:
ct+=1
el=encoded_len(s[:l0])
return el, {(el, s[l0-1], ct-1)}
l1, min_lst1=table[k-1]
l2, min_lst2=table[k]
end_char=s[l0-1]
lans=l1
for l, min_ending, min_ct in min_lst2:
if min_ending==end_char and min_ct not in[1, 9, 99]:
if l<lans:
lans=l
else:
if l+1<lans:
lans=l+1
min_lstans={it for it in min_lst1 if it[0]<=lans+2}
for l, min_ending, min_ct in min_lst2:
if min_ending==end_char:
if min_ct in [1, 9, 99]:
l+=1
min_ct+=1
else:
l+=1
min_ct=1
if l<=lans+2:
min_lstans.add((l, s[l0-1], min_ct))
return lans, min_lstans
def encoded_len(s):
l=len(s)
ans=0
i=0
while i<l:
ct=0
c0=s[i]
while i<l and s[i]==c0:
i+=1
ct+=1
ans+=((4 if ct==100 else 3) if ct>=10 else 2) if ct>=2 else 1
return ans
l0=len(s)
if l0<50:
for i in range(1, l0+1):
table=[cl(i, j) for j in range(k+1)]
else:
for i in range(1, l0+1):
table=[cl2(i, j) for j in range(k+1)]
return table[k][0] | string-compression-ii | Fast recursive Python solution, beats 97% for speed and 92% for memory | mbeceanu | 0 | 4 | string compression ii | 1,531 | 0.499 | Hard | 22,714 |
https://leetcode.com/problems/string-compression-ii/discuss/2705013/Python-solution-beats-95-users-with-explanation | class Solution:
def getLengthOfOptimalCompression(self, s: str, k: int) -> int:
# Find min lenth of the code starting from group ind, if there are res_k characters to delete and
# group ind needs to be increased by carry_over additional characters
def FindMinLen(ind, res_k, carry_over=0):
# If we already found the min length - just retrieve it (-1 means we did not calculate it)
if carry_over == 0 and dynamic[ind][res_k] != -1:
return dynamic[ind][res_k]
# Number of character occurences that we need to code. Includes carry-over.
cur_count = carry_over + frequency[ind]
# Min code length if the group ind stays intact. The code accounts for single-character "s0" vs. "s" situation.
min_len = 1 + min(len(str(cur_count)), cur_count - 1) + FindMinLen(ind+1,res_k)
# Min length if we keep only 0, 1, 9, or 99 characters in the group - delete the rest, if feasible
for leave_count, code_count in [(0,0), (1, 1), (9, 2), (99, 3)]:
if cur_count > leave_count and res_k >= cur_count - leave_count:
min_len = min(min_len, code_count + FindMinLen(ind + 1,res_k - (cur_count - leave_count)))
# If we drop characters between this character group and next group, like drop "a" in "bbbabb"
next_ind = chars.find(chars[ind], ind + 1)
delete_count = sum(frequency[ind+1:next_ind])
if next_ind > 0 and res_k >= delete_count:
min_len = min(min_len, FindMinLen(next_ind, res_k - delete_count, carry_over = cur_count))
# If there was no carry-over, store the result
if carry_over == 0: dynamic[ind][res_k] = min_len
return min_len
# Two auxiliary lists - character groups (drop repeated) and number of characters in the group
frequency, chars = [], ""
for char in s:
if len(frequency)==0 or char != chars[-1]:
frequency.append(0)
chars = chars + char
frequency[-1] += 1
# Table with the results. Number of character groups by number of available deletions.
dynamic = [[-1] * (k + 1) for i in range(len(frequency))] + [[0]*(k + 1)]
return FindMinLen(0, k) | string-compression-ii | Python solution beats 95% users with explanation | mritunjayyy | 0 | 93 | string compression ii | 1,531 | 0.499 | Hard | 22,715 |
https://leetcode.com/problems/count-good-triplets/discuss/767942/Python3-1-line | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
return sum(abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c for i in range(len(arr)) for j in range(i+1, len(arr)) for k in range(j+1, len(arr))) | count-good-triplets | [Python3] 1-line | ye15 | 4 | 1,800 | count good triplets | 1,534 | 0.808 | Easy | 22,716 |
https://leetcode.com/problems/count-good-triplets/discuss/767942/Python3-1-line | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
ans = 0
for i in range(len(arr)):
for j in range(i+1, len(arr)):
for k in range(j+1, len(arr)):
if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c: ans += 1
return ans | count-good-triplets | [Python3] 1-line | ye15 | 4 | 1,800 | count good triplets | 1,534 | 0.808 | Easy | 22,717 |
https://leetcode.com/problems/count-good-triplets/discuss/840465/Python-3-360-ms-Solution-96 | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
size = len(arr)
result = 0
for i in range(0, size - 2):
for j in range(i + 1, size - 1):
if abs(arr[i] - arr[j]) <= a:
for k in range(j + 1, size):
if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
result += 1
return result | count-good-triplets | Python 3 360 ms Solution 96% | Skyfall2017 | 3 | 781 | count good triplets | 1,534 | 0.808 | Easy | 22,718 |
https://leetcode.com/problems/count-good-triplets/discuss/1223173/840ms-Python-(with-comments-and-detailed-walkthrough) | class Solution(object):
def countGoodTriplets(self, arr, a, b, c):
"""
:type arr: List[int]
:type a: int
:type b: int
:type c: int
:rtype: int
"""
#initialise a variable to store the output
result = 0
#loop through each each index untill except the last 2 as we are looking for set of 3 numbers
for i in range(len(arr)-2):
#loop from 2nd element to the 2nd last element
for j in range(1,len(arr)-1):
#This condition is reduce excessive computation as we dont want to deal with j<=i
if j<=i:
continue
#loop from 3rd element to end of the list
for k in range(2,len(arr)):
#This condition is to remove excess computation as we dont want to deal with k<=j
if k<=j:
continue
#Checking whether the condition given in the question is being satisfied
if ((abs(arr[i] - arr[j]) <= a) and (abs(arr[j] - arr[k]) <= b) and abs(arr[i] - arr[k]) <= c):
#increment the output variable whenever we find a suitable pair (i,j,k) that satisfies all the conditions
result+=1
return result | count-good-triplets | 840ms, Python (with comments and detailed walkthrough) | Akshar-code | 2 | 403 | count good triplets | 1,534 | 0.808 | Easy | 22,719 |
https://leetcode.com/problems/count-good-triplets/discuss/2489890/Python-Roughly-O(n-log-n)-with-Fenwick-Tree | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
pre = [0] * 1001
post = Fenwick(1001)
pre[arr[0]] += 1
vi = 2
while vi < 0 + len(arr):
# post[arr[vi]] += 1
post.addSum(arr[vi], 1)
vi += 1
# pi = 1
# while pi < 0 + len(post):
# post[pi] += post[pi-1]
# pi += 1
i = 1
ans = 0
while i < 0 + len(arr) - 1:
middle = arr[i]
aupper = min(1000, middle + a)
alower = max(0, middle - a)
bupper = min(1000, middle + b)
blower = max(0, middle - b)
traversea = alower
while traversea <= aupper:
if pre[traversea]:
intersectlower = max(blower, traversea - c)
intersecupper = min(bupper, traversea + c)
if intersecupper >= intersectlower:
if not intersectlower:
# ans += pre[traversea] * post[intersecupper]
ans += pre[traversea] * post.getSum(intersecupper)
else:
# ans += pre[traversea] * (post[intersecupper] - post[intersectlower - 1])
ans += pre[traversea] * post.getCum(intersecupper, intersectlower)
traversea += 1
pre[middle] += 1
vaftermiddle = arr[i + 1]
# while vaftermiddle <= 1000:
# post[vaftermiddle] -= 1
# vaftermiddle += 1
post.addSum(vaftermiddle, -1)
i += 1
return ans | count-good-triplets | [Python] Roughly O(n log n) with Fenwick Tree | DG_stamper | 1 | 70 | count good triplets | 1,534 | 0.808 | Easy | 22,720 |
https://leetcode.com/problems/count-good-triplets/discuss/1756596/Python-dollarolution | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
x, count = len(arr), 0
for i in range(x):
for j in range(i+1,x):
if abs(arr[i] -arr[j]) < a+1:
for k in range(j+1,x):
if abs(arr[j] -arr[k]) < b+1 and abs(arr[i] -arr[k]) < c+1:
count += 1
return count | count-good-triplets | Python $olution | AakRay | 1 | 367 | count good triplets | 1,534 | 0.808 | Easy | 22,721 |
https://leetcode.com/problems/count-good-triplets/discuss/1080214/94-Python3-Solution | class Solution:
def countGoodTriplets(self, arr, a = 0, b = 0, c = 0):
# | a[i] - a[j] | <= a
# | a[j] - a[k] | <= b
# | a[i] - a[k] | <= c
output = 0
# store array length so it's not called every iteration
arrLen = len(arr)
for i in range(arrLen):
ai = arr[i]
for j in range(i + 1, arrLen):
aj = arr[j]
# no use in going down another level if this check is false
if abs(ai - aj) > a:
continue
for k in range(j + 1, arrLen):
ak = arr[k]
# no need to check if k < len(arr) or i >= 0, they will never be those values in range()
if (
abs(aj - ak) <= b and
abs(ai - ak) <= c and
i < j and
j < k
):
output += 1
return output
` `` | count-good-triplets | 94% Python3 Solution | bruzzi | 1 | 196 | count good triplets | 1,534 | 0.808 | Easy | 22,722 |
https://leetcode.com/problems/count-good-triplets/discuss/2761016/Python3-Solution | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
ans = 0
for i in range(0,len(arr)):
j = i + 1
for j in range(j,len(arr)):
k = j + 1
for k in range(k,len(arr)):
if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:
ans += 1
return ans | count-good-triplets | Python3 Solution | sipi09 | 0 | 4 | count good triplets | 1,534 | 0.808 | Easy | 22,723 |
https://leetcode.com/problems/count-good-triplets/discuss/2627032/Pyhton3-Solution-oror-Nested-For-Loops-oror-O(N3) | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
new = []
for i in range(len(arr)):
for j in range(i+1, len(arr)):
for k in range(j+1, len(arr)):
new.append([arr[i],arr[j],arr[k]])
#print(new)
count = 0
for i in new:
if abs(i[0] - i[1]) <= a:
if abs(i[1] - i[2]) <= b:
if abs(i[0] - i[2]) <= c:
count += 1
return count | count-good-triplets | Pyhton3 Solution || Nested For Loops || O(N^3) | shashank_shashi | 0 | 10 | count good triplets | 1,534 | 0.808 | Easy | 22,724 |
https://leetcode.com/problems/count-good-triplets/discuss/2554428/EASY-PYTHON3-SOLUTION | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
count = 0
for i in range(len(arr)):
for j in range(i+1, len(arr)):
for k in range(j+1, len(arr)):
if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:
count += 1
return count | count-good-triplets | ✅✔🔥 EASY PYTHON3 SOLUTION 🔥✅✔ | rajukommula | 0 | 50 | count good triplets | 1,534 | 0.808 | Easy | 22,725 |
https://leetcode.com/problems/count-good-triplets/discuss/2446678/Python3-or-Brute-Force-or-faster-than-95 | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
lenth = len(arr)
count = 0
for i in range(lenth - 2):
for j in range(i + 1, lenth - 1):
if abs(arr[i] - arr[j]) <= a:
for k in range(j + 1, lenth):
if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
count += 1
return count | count-good-triplets | Python3 | Brute Force | faster than 95% | Sergei_Gusev | 0 | 46 | count good triplets | 1,534 | 0.808 | Easy | 22,726 |
https://leetcode.com/problems/count-good-triplets/discuss/2309152/Python-Simplest | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
count = 0
for i in range(len(arr)):
for j in range(i+1,len(arr)):
for k in range(j+1,len(arr)):
if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[k]-arr[i])<=c:
count+=1
return count | count-good-triplets | Python Simplest | Abhi_009 | 0 | 229 | count good triplets | 1,534 | 0.808 | Easy | 22,727 |
https://leetcode.com/problems/count-good-triplets/discuss/2181993/Python-simple-solution | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
ans = 0
for i in range(len(arr)):
for j in range(i+1,len(arr)):
for k in range(j+1,len(arr)):
if abs(arr[i]-arr[j]) <= a and abs(arr[j]-arr[k]) <= b and abs(arr[i]-arr[k]) <= c:
ans += 1
return ans | count-good-triplets | Python simple solution | StikS32 | 0 | 94 | count good triplets | 1,534 | 0.808 | Easy | 22,728 |
https://leetcode.com/problems/count-good-triplets/discuss/2180593/Python-Solution-easy-(78-faster-than-online-submissions) | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
ct=0
for i in range(len(arr)):
for j in range(i+1,len(arr)):
for k in range(j+1,len(arr)):
if abs(arr[i] - arr[j]) <= a:
if abs(arr[j] - arr[k]) <= b:
if abs(arr[i] - arr[k]) <= c:
ct+=1
return ct | count-good-triplets | Python Solution - easy (78% faster than online submissions) | T1n1_B0x1 | 0 | 122 | count good triplets | 1,534 | 0.808 | Easy | 22,729 |
https://leetcode.com/problems/count-good-triplets/discuss/1867251/Python-brute-force-memory-less-than-90 | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
good = 0
for i in range(len(arr)):
for j in range(i+1, len(arr)):
for k in range(j+1, len(arr)):
if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
good += 1
return good | count-good-triplets | Python brute force, memory less than 90% | alishak1999 | 0 | 109 | count good triplets | 1,534 | 0.808 | Easy | 22,730 |
https://leetcode.com/problems/count-good-triplets/discuss/1813653/2-Lines-Python-Solution-oror-65-Faster-oror-Memory-less-than-70 | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
ans = 0
for i,j,k in combinations(arr,3):
if abs(i-j)>a or abs(j-k)>b or abs(i-k)>c: continue
ans += 1
return ans | count-good-triplets | 2-Lines Python Solution || 65% Faster || Memory less than 70% | Taha-C | 0 | 161 | count good triplets | 1,534 | 0.808 | Easy | 22,731 |
https://leetcode.com/problems/count-good-triplets/discuss/1275315/Python3-or-768-ms-faster-than-32.20 | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
count = 0
for combination in combinations(arr, 3):
if abs(combination[0] - combination[1]) <= a:
if abs(combination[1] - combination[2]) <= b:
if abs(combination[0] - combination[2]) <= c:
count += 1
return count | count-good-triplets | Python3 | 768 ms, faster than 32.20% | ZoS | 0 | 132 | count good triplets | 1,534 | 0.808 | Easy | 22,732 |
https://leetcode.com/problems/count-good-triplets/discuss/1243752/Python-Simple-Nested-For-loop-(92.74-faster) | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
count=0
for i in range(len(arr)):
for j in range(i+1,len(arr)):
if abs(arr[i]-arr[j])<=a:
for k in range(j+1,len(arr)):
if abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:
count+=1
return count | count-good-triplets | Python Simple Nested For loop (92.74% faster) | dhrumilg699 | 0 | 219 | count good triplets | 1,534 | 0.808 | Easy | 22,733 |
https://leetcode.com/problems/count-good-triplets/discuss/1206578/python3-easy-and-simple | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
n=len(arr)
cout=0
for i in range(n):
for j in range(i+1,n):
for k in range(j+1,n):
if abs(arr[i]-arr[j])<=a and abs(arr[j]-arr[k])<=b and abs(arr[i]-arr[k])<=c:
cout+=1
return cout | count-good-triplets | python3 easy and simple | akashmaurya001 | 0 | 112 | count good triplets | 1,534 | 0.808 | Easy | 22,734 |
https://leetcode.com/problems/count-good-triplets/discuss/991043/Python3-%3A-Optimized-Code | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
ans = 0
for i in range(len(arr)):
for j in range(i+1,len(arr)):
if(abs(arr[i] - arr[j]) > a):
continue
for k in range(j+1,len(arr)):
if(abs(arr[j] - arr[k]) > b):
continue
if(abs(arr[i] - arr[k]) <= c):
ans+=1
return ans | count-good-triplets | Python3 : Optimized Code | abhijeetmallick29 | 0 | 323 | count good triplets | 1,534 | 0.808 | Easy | 22,735 |
https://leetcode.com/problems/count-good-triplets/discuss/962364/Runtime%3A-356-ms-faster-than-94.42-andand-Memory-Usage%3A-14.2-MB-less-than-53.13-of-Python3 | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
count = 0
for i in range(len(arr)):
for j in range(i+1,len(arr)):
if abs(arr[i]-arr[j]) <= a :
for k in range(j+1,len(arr)):
if abs(arr[j]-arr[k]) <= b and abs(arr[i]-arr[k]) <= c :
count += 1
return count | count-good-triplets | Runtime: 356 ms, faster than 94.42% && Memory Usage: 14.2 MB, less than 53.13% of Python3 | ankur1801 | 0 | 135 | count good triplets | 1,534 | 0.808 | Easy | 22,736 |
https://leetcode.com/problems/count-good-triplets/discuss/945039/Python-and-Go-clean-code-with-some-optimisation | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
cnt = 0
for i in range(len(arr)-2):
for j in range(i+1, len(arr)-1):
if abs(arr[i]-arr[j]) <= a:
for k in range(j+1, len(arr)):
if abs(arr[j]-arr[k]) <= b and abs(arr[i]-arr[k]) <= c:
cnt+=1
return cnt | count-good-triplets | Python and Go clean code with some optimisation | modusV | 0 | 166 | count good triplets | 1,534 | 0.808 | Easy | 22,737 |
https://leetcode.com/problems/count-good-triplets/discuss/781379/Runtime%3A-372-ms-faster-than-95.52-of-Python3 | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
length = len(arr)
count = 0
for i in range(length-2):
for j in range(i+1, length-1):
if abs(arr[i]-arr[j]) > a:
continue
for k in range(j+1, length):
if abs(arr[j]-arr[k]) > b:
continue
if abs(arr[i] - arr[k]) > c:
continue
count +=1
return count | count-good-triplets | Runtime: 372 ms, faster than 95.52% of Python3 | Anonyknight | 0 | 202 | count good triplets | 1,534 | 0.808 | Easy | 22,738 |
https://leetcode.com/problems/count-good-triplets/discuss/774611/Intuitive-approach-by-triple-for-loop | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
ans = 0
for i in range(0, len(arr)-2):
for j in range(i+1, len(arr)-1):
if abs(arr[i] - arr[j]) <= a:
for k in range(j+1, len(arr)):
if abs(arr[k]-arr[j]) <= b and abs(arr[i]-arr[k]) <= c:
# print(f"Found ({arr[i]}, {arr[j]}, {arr[k]})")
ans += 1
return ans | count-good-triplets | Intuitive approach by triple for loop | puremonkey2001 | 0 | 53 | count good triplets | 1,534 | 0.808 | Easy | 22,739 |
https://leetcode.com/problems/count-good-triplets/discuss/768396/Simplest-code-Python3-100faster | class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
length=len(arr)
count=0
for i in range(length):
for j in range(i+1,length):
for k in range(j+1,length):
if abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
count+=1
else:
continue
return count | count-good-triplets | Simplest code Python3 100%faster | Geeky-star | -1 | 82 | count good triplets | 1,534 | 0.808 | Easy | 22,740 |
https://leetcode.com/problems/find-the-winner-of-an-array-game/discuss/767983/Python3-6-line-O(N) | class Solution:
def getWinner(self, arr: List[int], k: int) -> int:
win = cnt = 0 #winner & count
for i, x in enumerate(arr):
if win < x: win, cnt = x, 0 #new winner in town
if i: cnt += 1 #when initializing (i.e. i == 0) count is 0
if cnt == k: break #early break
return win | find-the-winner-of-an-array-game | [Python3] 6-line O(N) | ye15 | 7 | 267 | find the winner of an array game | 1,535 | 0.488 | Medium | 22,741 |
https://leetcode.com/problems/find-the-winner-of-an-array-game/discuss/2247507/python-3-or-simple-solution-or-O(n)O(1) | class Solution:
def getWinner(self, arr: List[int], k: int) -> int:
if k == 1:
return max(arr[0], arr[1])
i = wins = 0
for j in range(1, len(arr)):
if arr[i] > arr[j]:
wins += 1
if wins == k:
return arr[i]
else:
i = j
wins = 1
return max(arr) | find-the-winner-of-an-array-game | python 3 | simple solution | O(n)/O(1) | dereky4 | 0 | 55 | find the winner of an array game | 1,535 | 0.488 | Medium | 22,742 |
https://leetcode.com/problems/find-the-winner-of-an-array-game/discuss/2191413/Simple-Intuitive-Solution | class Solution:
def getWinner(self, arr: List[int], k: int) -> int:
n = len(arr)
winningInteger = arr[0]
winCount = 0
for i in range(1, n):
if winningInteger > arr[i]:
winCount += 1
else:
winningInteger = arr[i]
winCount = 1
if winCount == k:
return winningInteger
return winningInteger | find-the-winner-of-an-array-game | Simple Intuitive Solution | Vaibhav7860 | 0 | 30 | find the winner of an array game | 1,535 | 0.488 | Medium | 22,743 |
https://leetcode.com/problems/minimum-swaps-to-arrange-a-binary-grid/discuss/768030/Python3-bubble-ish-sort | class Solution:
def minSwaps(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
#summarizing row into number
row = [0]*m
for i in range(m):
row[i] = next((j for j in reversed(range(n)) if grid[i][j]), 0)
ans = 0
#sequentially looking for row to fill in
for k in range(m):
for i, v in enumerate(row):
if v <= k: #enough trailing zeros
ans += i
row.pop(i) #value used
break
else: return -1 #cannot find such row
return ans | minimum-swaps-to-arrange-a-binary-grid | [Python3] bubble-ish sort | ye15 | 13 | 750 | minimum swaps to arrange a binary grid | 1,536 | 0.465 | Medium | 22,744 |
https://leetcode.com/problems/minimum-swaps-to-arrange-a-binary-grid/discuss/2811787/python-leverage-syntactic-sugar | class Solution:
def minSwaps(self, grid: List[List[int]]) -> int:
A = [sum(int(x == 0) for x in accumulate(row[::-1])) for row in grid]
n = len(grid)
res = 0
for i in range(n):
for j in range(i, n):
if A[j] >= n - 1 - i:
A = A[:i] + A[j:j+1] + A[i:j] + A[j+1:]
res += j - i
break
else:
return -1
return res | minimum-swaps-to-arrange-a-binary-grid | [python] leverage syntactic sugar | pukras | 0 | 1 | minimum swaps to arrange a binary grid | 1,536 | 0.465 | Medium | 22,745 |
https://leetcode.com/problems/get-the-maximum-score/discuss/768050/Python3-range-sum-with-two-pointers-O(M%2BN) | class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
ans = i = ii = s = ss = 0
while i < len(nums1) and ii < len(nums2):
#update range sum & move pointer
if nums1[i] < nums2[ii]:
s += nums1[i]
i += 1
elif nums1[i] > nums2[ii]:
ss += nums2[ii]
ii += 1
#add larger range sum to ans
#add common value & move pointers
else:
ans += max(s, ss) + nums1[i]
s = ss = 0
i, ii = i+1, ii+1
#complete the range sum & update ans
ans += max(s + sum(nums1[i:]), ss + sum(nums2[ii:]))
return ans % 1_000_000_007 | get-the-maximum-score | [Python3] range sum with two pointers O(M+N) | ye15 | 1 | 73 | get the maximum score | 1,537 | 0.393 | Hard | 22,746 |
https://leetcode.com/problems/get-the-maximum-score/discuss/2624497/DP-as-merging-short-solution-in-Python-O(n) | class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
scores = defaultdict(int)
i, j = 0, 0
while i < len(nums1) and j < len(nums2):
if nums1[i] < nums2[j]:
scores[nums1[i]] = nums1[i] if i == 0 else scores[nums1[i-1]] + nums1[i]
i += 1
elif nums1[i] > nums2[j]:
scores[nums2[j]] = nums2[j] if j == 0 else scores[nums2[j-1]] + nums2[j]
j += 1
else:
scores[nums1[i]] = max(0 if i == 0 else scores[nums1[i-1]],
0 if j == 0 else scores[nums2[j-1]]) + nums1[i]
i, j = i + 1, j + 1
while i < len(nums1):
scores[nums1[i]] = nums1[i] if i == 0 else scores[nums1[i-1]] + nums1[i]
i += 1
while j < len(nums2):
scores[nums2[j]] = nums2[j] if j == 0 else scores[nums2[j-1]] + nums2[j]
j += 1
return max(scores[nums1[-1]], scores[nums2[-1]]) % 1000000007 | get-the-maximum-score | DP as merging, short solution in Python, O(n) | metaphysicalist | 0 | 12 | get the maximum score | 1,537 | 0.393 | Hard | 22,747 |
https://leetcode.com/problems/get-the-maximum-score/discuss/2043833/python-3-oror-two-pointers-oror-O(m-%2B-n)O(1) | class Solution:
def maxSum(self, nums1: List[int], nums2: List[int]) -> int:
i = j = score1 = score2 = maxScore = 0
m, n = len(nums1), len(nums2)
MOD = 10 ** 9 + 7
while i < m and j < n:
if nums1[i] < nums2[j]:
score1 += nums1[i]
i += 1
elif nums1[i] > nums2[j]:
score2 += nums2[j]
j += 1
else:
maxScore += nums1[i] + max(score1, score2)
maxScore %= MOD
score1 = score2 = 0
i += 1
j += 1
while i < m:
score1 += nums1[i]
i += 1
while j < n:
score2 += nums2[j]
j += 1
maxScore += max(score1, score2)
return maxScore % MOD | get-the-maximum-score | python 3 || two-pointers || O(m + n)/O(1) | dereky4 | 0 | 64 | get the maximum score | 1,537 | 0.393 | Hard | 22,748 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/784720/Python3-O(N)-and-O(logN)-solutions | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
ss, x = set(arr), 1
while True:
if x not in ss: k -= 1
if not k: return x
x += 1 | kth-missing-positive-number | [Python3] O(N) and O(logN) solutions | ye15 | 5 | 240 | kth missing positive number | 1,539 | 0.56 | Easy | 22,749 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/784720/Python3-O(N)-and-O(logN)-solutions | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
lo, hi = 0, len(arr)
while lo < hi:
mid = (lo + hi)//2
if arr[mid] - (mid + 1) < k: lo = mid + 1
else: hi = mid
return lo + k | kth-missing-positive-number | [Python3] O(N) and O(logN) solutions | ye15 | 5 | 240 | kth missing positive number | 1,539 | 0.56 | Easy | 22,750 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1461718/Simple-Python-O(logn)-binary-search-beats-100-solution | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
left, right = 0, len(arr)
while left < right:
mid = left+(right-left)//2
n_missing = arr[mid]-mid-1
if n_missing >= k:
right = mid
else:
left = mid+1
# if more n_missing at the last index than k
if left < len(arr):
n_missing = arr[left]-left
return arr[left]-(n_missing-k)
# if fewer n_missing at the last index than k
return arr[-1]+(k-arr[-1]+len(arr)) | kth-missing-positive-number | Simple Python O(logn) binary search beats 100% solution | Charlesl0129 | 4 | 356 | kth missing positive number | 1,539 | 0.56 | Easy | 22,751 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1461718/Simple-Python-O(logn)-binary-search-beats-100-solution | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
n_missing = lambda i: arr[i]-i-1
left, right = 0, len(arr)
while left < right:
mid = left+(right-left)//2
if n_missing(mid) >= k:
right = mid
else:
left = mid+1
# if more n_missing at the last index than k
if left < len(arr):
return arr[left]-(n_missing(left)-k+1)
# if fewer n_missing at the last index than k
return arr[len(arr)-1]+(k-n_missing(len(arr)-1)) | kth-missing-positive-number | Simple Python O(logn) binary search beats 100% solution | Charlesl0129 | 4 | 356 | kth missing positive number | 1,539 | 0.56 | Easy | 22,752 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2420855/Beats-94-Easy-Understanding-Python | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
curr = k
for i in arr:
if i<=curr:
curr +=1
return curr | kth-missing-positive-number | Beats 94% Easy Understanding Python | adahuang | 3 | 138 | kth missing positive number | 1,539 | 0.56 | Easy | 22,753 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1004706/O(N)-time-O(1)-space-(N%3A-number-of-elements-in-arr)-48ms-(84.10-faster) | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
if not arr: return k
arr = [0] + arr
for i in range(1, len(arr)):
k -= (arr[i] - arr[i-1] - 1)
if k <= 0: return arr[i] + k - 1
return arr[-1] + k | kth-missing-positive-number | O(N) time, O(1) space (N: number of elements in arr), 48ms (84.10% faster) | cyshih | 3 | 186 | kth missing positive number | 1,539 | 0.56 | Easy | 22,754 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2436065/C%2B%2BPython-Best-Optimized-Solution-using-Binary-Search | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
s = 0
e = len(arr)-1
ans = 0
while(s<=e):
mid = (s+e)//2
if(arr[mid]-(mid+1)<k):
ans = mid+1
s = mid+1
else:
e = mid-1
return k+ans | kth-missing-positive-number | C++/Python Best Optimized Solution using Binary Search | arpit3043 | 2 | 123 | kth missing positive number | 1,539 | 0.56 | Easy | 22,755 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1170732/Python3-Simple-And-Two-Line-Solution | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
se = set(range(1 , max(arr) + k + 2))
return sorted(list(se.difference(set(arr))))[k - 1] | kth-missing-positive-number | [Python3] Simple And Two Line Solution | VoidCupboard | 2 | 144 | kth missing positive number | 1,539 | 0.56 | Easy | 22,756 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2371064/Python-Simple-Faster-Solution-Binary-Search-oror-Documented | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
low, high = 0, len(arr)-1 # two pointers low to high
# Repeat until the pointers low and high meet each other
while low <= high:
mid = (low + high) // 2 # middle point - pivot
missingK = arr[mid]-(mid+1)
if missingK < k:
low = mid + 1 # go right side
else:
high = mid - 1 # go left side
return low + k | kth-missing-positive-number | [Python] Simple Faster Solution - Binary Search || Documented | Buntynara | 1 | 85 | kth missing positive number | 1,539 | 0.56 | Easy | 22,757 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2339748/easy-python-code-or-O(n-logn)-or-binary-search | class Solution:
def binarysearch(self,arr,n):
l,r = 0,len(arr)
while(l<=r and l < len(arr) and r>=0):
m = (l+r)//2
if arr[m] > n:
r = m-1
elif arr[m] < n:
l = m+1
else:
return True
return False
def findKthPositive(self, arr: List[int], k: int) -> int:
newarr = []
i = 1
while(len(newarr)<k):
if self.binarysearch(arr,i):
i+=1
continue
else:
newarr.append(i)
i+=1
return newarr[-1] | kth-missing-positive-number | easy python code | O(n logn) | binary search | dakash682 | 1 | 112 | kth missing positive number | 1,539 | 0.56 | Easy | 22,758 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2147095/Easy-to-understand-and-one-linear-solution-python | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
return sorted(set(range(1, arr[-1]+k+1)) - set(arr))[k-1] | kth-missing-positive-number | Easy to understand & one linear solution python | writemeom | 1 | 92 | kth missing positive number | 1,539 | 0.56 | Easy | 22,759 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1812911/Simple-Python-Solution-or-75-lesser-Memory | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
lst = []
i=1
while len(lst)<k:
if i not in arr:
lst.append(i)
i += 1
return lst[-1] | kth-missing-positive-number | ✔Simple Python Solution | 75% lesser Memory | Coding_Tan3 | 1 | 91 | kth missing positive number | 1,539 | 0.56 | Easy | 22,760 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1647124/3-lines-python-solution | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
arr_set=set(range(1,3000))
res=list(arr_set-arr_set.intersection(set(arr)))
return res[k-1] | kth-missing-positive-number | 3 lines python solution | amannarayansingh10 | 1 | 123 | kth missing positive number | 1,539 | 0.56 | Easy | 22,761 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1004429/Three-line-python-solution | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
for i, num in enumerate(arr+[9999]):
if num - (i+1) >= k:
return k + i | kth-missing-positive-number | Three line python solution | 173734014 | 1 | 96 | kth missing positive number | 1,539 | 0.56 | Easy | 22,762 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/961673/Python-Simple-Solution | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
c=0
for i in range(1,arr[-1]+1):
if i not in arr:
c+=1
if c==k:
return i
return arr[-1]+(k-c) | kth-missing-positive-number | Python Simple Solution | lokeshsenthilkumar | 1 | 269 | kth missing positive number | 1,539 | 0.56 | Easy | 22,763 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/784107/Python-Brute-Force-Easily-Understandable | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
pool = list(range(1,max(arr)+k+1))
res = []
while len(res) <= k:
for i in pool:
if i not in arr:
res.append(i)
return res[k-1] | kth-missing-positive-number | Python Brute Force Easily Understandable | Venezsia1573 | 1 | 73 | kth missing positive number | 1,539 | 0.56 | Easy | 22,764 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2849320/Pen-n-Paper-Solution-oror-Binary-Search-oror-Easy | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
low=0
high=len(arr)-1
while low<=high:
mid=(low+high)//2
if arr[mid]-mid-1<k:
low=mid+1
else:
high=mid-1
return low+k | kth-missing-positive-number | Pen n Paper Solution || Binary Search || Easy | user9516zM | 0 | 2 | kth missing positive number | 1,539 | 0.56 | Easy | 22,765 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2829501/Kth-Missing-Positive-Numbers-or-Python | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
low, high = 0, len(arr)
while low < high:
mid = (low + high) // 2
if arr[mid] - mid - 1 < k:
low = mid + 1
else:
high = mid
return high + k | kth-missing-positive-number | Kth Missing Positive Numbers | Python | jashii96 | 0 | 3 | kth missing positive number | 1,539 | 0.56 | Easy | 22,766 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2813145/simple-python-binary-search | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
low = 0
high = len(arr)
while low < high:
mid = (low + high) // 2
if arr[mid] - mid -1 < k:
low = mid + 1
else:
high = mid
return low + k | kth-missing-positive-number | simple python binary search | sudharsan1000m | 0 | 3 | kth missing positive number | 1,539 | 0.56 | Easy | 22,767 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2804032/Python-simple-binary-search | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
first,last = 0, len(arr)-1
while first<= last:
mid = (first+last)//2
miss_num = arr[mid] - mid- 1
if miss_num >= k:
last = mid-1
elif miss_num < k and miss_num >=0 :
first = mid+1
else:
return k
return arr[last] +k - (arr[last]-(last+1))
"""
Brute force approach: Complexity O(N)
# newarr = []
# num = 1
# count = 0
# while count <= k:
# if num not in arr:
# newarr.append(num)
# count += 1
# num += 1
# return newarr[k-1]
"""
# | kth-missing-positive-number | Python simple binary search | BhavyaBusireddy | 0 | 4 | kth missing positive number | 1,539 | 0.56 | Easy | 22,768 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2765316/python-binary-search | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
arr.append(arr[-1] + k)
left, right = 0, len(arr) - 1
while left < right:
mid = left + (right - left) // 2
if arr[mid] - mid - 1 < k:
left = mid + 1
else:
right = mid
return arr[right-1] + k - (arr[right-1] - right) | kth-missing-positive-number | python binary search | michaelniki | 0 | 4 | kth missing positive number | 1,539 | 0.56 | Easy | 22,769 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2710820/Py3-Binary-Search-intuition-step-by-step-explained. | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
left, right = 0, len(arr)
while left < right:
mid = left + (right-left) // 2
if arr[mid] - mid - 1 >= k:
right = mid
else:
left = mid + 1
return left + k | kth-missing-positive-number | ⭐[Py3] Binary Search intuition step by step explained. | bromalone | 0 | 13 | kth missing positive number | 1,539 | 0.56 | Easy | 22,770 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2691516/Python-solution | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
nums = dict()
count =0
m =max(arr) + k
for i in range(1,m+1):
nums[i] =0
for a in arr:
nums[a] =1
for key, val in nums.items():
if val == 0:
count+=1
if count == k:
return key
return None | kth-missing-positive-number | Python solution | Sheeza | 0 | 6 | kth missing positive number | 1,539 | 0.56 | Easy | 22,771 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2670035/Python-with-explanation-beats-97-O(1)-space | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
interval_start = 0
for num in arr:
if num - interval_start - 1 >= k:
return interval_start + k
k -= (num - interval_start - 1)
interval_start = num
return interval_start + k | kth-missing-positive-number | Python with explanation, beats 97% O(1) space | linger_baruch | 0 | 4 | kth missing positive number | 1,539 | 0.56 | Easy | 22,772 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2663114/Kth-Missing-Positive-Number-or-simple-binary-search-explained | class Solution:
def findKthPositive(self, arr, k: int) -> int:
missing_num_count = 0
looking_num = 1
i = 0
while i< len(arr):
while looking_num != arr[i]:
missing_num_count += 1
if missing_num_count == k:
return looking_num
looking_num += 1
looking_num += 1
i += 1
print(Solution().findKthPositive([2,3,4,7,11], 5)) | kth-missing-positive-number | Kth Missing Positive Number | simple binary search explained | Ninjaac | 0 | 27 | kth missing positive number | 1,539 | 0.56 | Easy | 22,773 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2663114/Kth-Missing-Positive-Number-or-simple-binary-search-explained | class Solution:
def findKthPositive(self, arr, k: int) -> int:
low = 0
high = len(arr)-1
while(low<=high):
mid = (low+high)//2
# if the number of missing values < k -- we need increase the index
if(arr[mid]-(mid+1)<k):
low = mid+1
else:
high = mid-1
return k+low | kth-missing-positive-number | Kth Missing Positive Number | simple binary search explained | Ninjaac | 0 | 27 | kth missing positive number | 1,539 | 0.56 | Easy | 22,774 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2531344/Python3-or-Solved-Using-Binary-Search-and-Keeping-Track-of-Latest-Missing-Positive-Integer | class Solution:
#Time-Complexity: O(n + klog(n)) -> O(n)
#Space-Complexity: O(1)
def findKthPositive(self, arr: List[int], k: int) -> int:
#Approach: Start from 1 and decrement k by 1 every time current number we are on
#is not in input array arr! When k hits 0, we know the latest missing positive
#integer we recorded is the kth missing positive number!
#sort the input array beforehand in-place!
arr.sort()
#define binary search helper!
def helper(e, a):
L, R = 0, len(a) - 1
while L <= R:
mid = (L + R) // 2
middle = a[mid]
if(middle == e):
return True
elif(middle < e):
L = mid + 1
continue
else:
R = mid - 1
continue
return False
#start will keep track of current positive number we are on!
start = 1
# as long as k does not equal 0...
while True:
if(helper(start, arr)):
start += 1
continue
else:
k -= 1
if(k == 0):
break
start += 1
continue
return start | kth-missing-positive-number | Python3 | Solved Using Binary Search and Keeping Track of Latest Missing Positive Integer | JOON1234 | 0 | 22 | kth missing positive number | 1,539 | 0.56 | Easy | 22,775 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2435953/C%2B%2BPython-best-optimized-approach-with-O(log(N))-approach | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
s = 0
e = len(arr)-1
ans = 0
while(s<=e):
mid = (s+e)//2
if(arr[mid]-(mid+1)<k):
ans = mid+1
s = mid+1
else:
e = mid-1
return k+ans | kth-missing-positive-number | C++/Python best optimized approach with O(log(N)) approach | arpit3043 | 0 | 34 | kth missing positive number | 1,539 | 0.56 | Easy | 22,776 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2339953/Python-or-Brute-Force-or-Basic-maths | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
arr = [0] + arr
i = 0
diff = arr[i + 1] - arr[i] - 1
while k > diff:
k -= diff
i += 1
if i + 1 >= len(arr):
break
diff = arr[i + 1] - arr[i] - 1
return arr[i] + k | kth-missing-positive-number | Python | Brute Force | Basic maths | pcdean2000 | 0 | 60 | kth missing positive number | 1,539 | 0.56 | Easy | 22,777 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2313602/Kth-Missing-Positive-Number | class Solution:
def bs(self,arr,x):
l = 0
r = len(arr)-1
while l<=r:
m=(l+r)//2
if arr[m] == x:
return True
if arr[m]<x:
l=m+1
else:
r = m-1
return False
def findKthPositive(self, arr: List[int], k: int) -> int:
out = [0]*1001
idx = 0
for i in range(1,arr[-1]+k+1):
x = self.bs(arr,i)
if idx ==k :
break
if not x:
out[idx]=i
idx+=1
return out[idx-1] | kth-missing-positive-number | Kth Missing Positive Number | dhananjayaduttmishra | 0 | 18 | kth missing positive number | 1,539 | 0.56 | Easy | 22,778 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2248241/Python-2-solutions-Clean-and-Concise | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
# arr = [2, 3, 4, 7, 11]
def cntMissingElements(i):
return arr[i] - (i+1)
n = len(arr)
for i in range(n):
if cntMissingElements(i) >= k:
return arr[i] - 1 - (cntMissingElements(i) - k)
return arr[-1] + k - cntMissingElements(n-1) | kth-missing-positive-number | [Python] 2 solutions - Clean & Concise | hiepit | 0 | 73 | kth missing positive number | 1,539 | 0.56 | Easy | 22,779 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2248241/Python-2-solutions-Clean-and-Concise | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
# arr = [2, 3, 4, 7, 11]
def cntMissingElements(i):
return arr[i] - (i+1)
# Find first index such that cntMissingElements >= k
n = len(arr)
left, right = 0, n - 1
ans = -1
while left <= right:
mid = (left + right) // 2
if cntMissingElements(mid) >= k:
ans = mid
right = mid - 1
else:
left = mid + 1
if ans != -1:
return arr[ans] + k - cntMissingElements(ans) - 1
return arr[-1] + k - cntMissingElements(n-1) | kth-missing-positive-number | [Python] 2 solutions - Clean & Concise | hiepit | 0 | 73 | kth missing positive number | 1,539 | 0.56 | Easy | 22,780 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2165400/Python-Fastest-O(logN)-99-Faster-Solution | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
left = 0
right = len(arr)-1
n = len(arr)
if arr[0]>k:
return k
while left<right:
mid = (left+right)//2
if (arr[mid]-mid)<=k and (mid==(len(arr)-1) or k<(arr[mid+1]-mid-1)):
return arr[mid]+k-(arr[mid]-mid-1)
elif (arr[mid]-mid)<=k:
left = mid+1
else:
right = mid
return arr[left]+k-(arr[left]-left-1) | kth-missing-positive-number | [Python] Fastest O(logN) 99% Faster Solution | RedHeadphone | 0 | 190 | kth missing positive number | 1,539 | 0.56 | Easy | 22,781 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2127101/Python-Solution-Very-Simple-Binary-Search | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
'''
[2,3,4,7,11], k = 5
[8,25,30] k =26
7,23,27
arr[(low-1)] + k-arr[low-1] + (i+1)
25+1+2 = 28
or
k + low + 1
1. First find out after which number the kth missing positive number would lie.
2. once we find out that index , next step is to simply return the kth number + offset .
offset is basically the number of positive integers which are already included.
'''
low = 0
high = len(arr)
while low < high:
mid = low + (high - low)//2
missing_nums = arr[mid] - (mid+1)
if missing_nums < k:
low = mid + 1
else:
high = mid
# kth + offset
return k + low | kth-missing-positive-number | Python Solution - Very Simple Binary Search | SaSha59 | 0 | 182 | kth missing positive number | 1,539 | 0.56 | Easy | 22,782 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2123287/Simple-Python-Solution-Using-Counters | class Solution(object):
def findKthPositive(self, arr, k):
i, j, missing = 0, 1, []
while i<len(arr):
if arr[i]!=j:
missing.append(j)
i-=1
j+=1
i+=1
if len(missing)>=k:
return missing[k-1]
return arr[-1]+k-len(missing) | kth-missing-positive-number | Simple Python Solution Using Counters | NathanPaceydev | 0 | 65 | kth missing positive number | 1,539 | 0.56 | Easy | 22,783 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2097968/Simple-O(N)-python-solution-faster-than-90 | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
last=1
for n in arr:
k-=n-last
if k<=0:
return n+k-1
last=n+1
return arr[-1]+k | kth-missing-positive-number | Simple O(N) python solution, faster than 90% | xsank | 0 | 42 | kth missing positive number | 1,539 | 0.56 | Easy | 22,784 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2038277/python-easy-binary-search | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
if arr[0] > k : return k
if arr[-1] <= k : return k + len(arr)
l = 0
r = len(arr) - 1
while l <= r :
m = (l + r)>>1
if arr[m] - m - 1 < k : l = m + 1
else : r = m - 1
return k + l | kth-missing-positive-number | python - easy binary search | ZX007java | 0 | 141 | kth missing positive number | 1,539 | 0.56 | Easy | 22,785 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2008759/Python-or-C%2B%2B-Simple-Intuitive | class Solution:
from bisect import bisect_left
def findKthPositive(self, arr: List[int], k: int) -> int:
count = 0
for i in range(1,2001):
if not self.binary_search(arr,i): count = count + 1
if count == k: return i
def binary_search(self, arr, x):
i = bisect_left(arr, x)
if i != len(arr) and arr[i] == x:
return True
else:
return False | kth-missing-positive-number | Python | C++ Simple Intuitive | aakash94 | 0 | 71 | kth missing positive number | 1,539 | 0.56 | Easy | 22,786 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2008759/Python-or-C%2B%2B-Simple-Intuitive | class Solution(object):
from bisect import bisect_left
def findKthPositive(self, arr, k):
count = 0
for i in range(1,2001):
if not self.binary_search(arr,i): count = count + 1
if count == k: return i
def binary_search(self, arr, x):
i = bisect_left(arr, x)
if i != len(arr) and arr[i] == x:
return True
else:
return False | kth-missing-positive-number | Python | C++ Simple Intuitive | aakash94 | 0 | 71 | kth missing positive number | 1,539 | 0.56 | Easy | 22,787 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/2003379/Python-3-Solution-Using-Dictionary-I-don't-know-if-this-is-a-good-solution-or-not. | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
total_list = {x: 1 for x in range(1, 2001)}
for i in arr:
total_list[i] -= 1
missing_numbers = []
for c, v in total_list.items():
if v == 1:
missing_numbers.append(c)
return missing_numbers[k-1] | kth-missing-positive-number | Python 3 Solution, Using Dictionary, I don't know if this is a good solution or not. | AprDev2011 | 0 | 42 | kth missing positive number | 1,539 | 0.56 | Easy | 22,788 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1923053/Python-solution-with-memory-less-than-99 | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
missing = []
for i in range(1, max(arr)+1):
if i not in arr:
missing.append(i)
if len(missing) < k:
for i in range(1, k-len(missing)+1):
missing.append(max(arr)+i)
return missing[k-1] | kth-missing-positive-number | Python solution with memory less than 99% | alishak1999 | 0 | 108 | kth missing positive number | 1,539 | 0.56 | Easy | 22,789 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1870487/Python-soln | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
i=1
arr=set(arr)
while True:
if i not in arr:
k-=1
if k==0:
return i
i+=1 | kth-missing-positive-number | Python soln | heckt27 | 0 | 43 | kth missing positive number | 1,539 | 0.56 | Easy | 22,790 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1858122/Python-(263mscomment-if-faster-method-known) | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
count=0
for i in range(1,arr[-1]+k+1):
if i not in arr:
count+=1
if k==count:
return i | kth-missing-positive-number | Python (263ms/comment if faster method known) | sharma_nayaab | 0 | 30 | kth missing positive number | 1,539 | 0.56 | Easy | 22,791 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1594980/Python-3-O(n)-faster-than-95 | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
missing = prev = 0
for num in arr:
missing += num - prev - 1
if missing >= k:
return num + k - missing - 1
prev = num
return prev + k - missing | kth-missing-positive-number | Python 3 O(n) faster than 95% | dereky4 | 0 | 118 | kth missing positive number | 1,539 | 0.56 | Easy | 22,792 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1529487/python-3-solution | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
length = len(arr)
if arr[-1] == length:
return arr[-1] + k
else:
count = 0
j = 0
i = 1
while True:
if j < length and i == arr[j]:
j += 1
else:
missingElement = i
count += 1
if count == k:
return missingElement
i += 1 | kth-missing-positive-number | python 3 solution | khageshwor | 0 | 66 | kth missing positive number | 1,539 | 0.56 | Easy | 22,793 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1174412/Python-solution-using-sets | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
n = len(arr)
return sorted(list(set(range(n+k+1))-set(arr)))[k] | kth-missing-positive-number | Python solution using sets | BijanRegmi | 0 | 87 | kth missing positive number | 1,539 | 0.56 | Easy | 22,794 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1006518/Python-One-Line-Solution-(Beginners) | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
return(list(set(range(1,2001))-set(arr))[k-1]) | kth-missing-positive-number | Python - One Line Solution (Beginners) | okeoke | 0 | 129 | kth missing positive number | 1,539 | 0.56 | Easy | 22,795 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/1005678/Python3-O(n)-time-and-O(1)-space-solution | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
max_num = max(arr)
j = 0
for i in range(1,max_num):
if i == arr[j]:
j += 1
else:
if k !=1:
k -= 1
else:
return i
if k:
return max_num + k | kth-missing-positive-number | [Python3] O(n) time and O(1) space solution | pratushah | 0 | 86 | kth missing positive number | 1,539 | 0.56 | Easy | 22,796 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/915901/Memory-100-but-how-to-increase-decrease-at-15 | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
counter = i = 0
while i < k :
counter += 1
if counter not in arr:
i += 1
return counter | kth-missing-positive-number | Memory 100% but how to increase decrease at 15%? | moshahin | 0 | 45 | kth missing positive number | 1,539 | 0.56 | Easy | 22,797 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/783311/Python3-ez-logic | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
prev=0
temp=[]
for a in arr:
if k==len(temp):break
if a-prev!=1:
for i in range(prev+1,a):
if k==len(temp):break
temp.append(i)
prev=a
if len(temp)<k:
for i in range(arr[-1]+1,arr[-1]+(k-len(temp)+1)):
temp.append(i)
return temp[-1] | kth-missing-positive-number | Python3 ez logic | harshitCode13 | 0 | 42 | kth missing positive number | 1,539 | 0.56 | Easy | 22,798 |
https://leetcode.com/problems/kth-missing-positive-number/discuss/780090/Solution-with-video-explanation-(thinking-out-loud-solving) | class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
check = [True]*2001
for e in arr:
check[e] = False
ind = 1
for i in range(k):
while check[ind]==False:
ind+=1
ind+=1
return ind-1 | kth-missing-positive-number | Solution with video explanation (thinking out loud solving) | dakshah | 0 | 117 | kth missing positive number | 1,539 | 0.56 | Easy | 22,799 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.