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
retu... | 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]
... | 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]... | 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]... | 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]
... | 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:
retu... | 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 =... | 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."""
... | 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
... | 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 ... | 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... | 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):
... | 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 a... | 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):
... | 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... | 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
... | 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... | 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... | 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]... | 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)
coun... | 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]-ar... | 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):
... | 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... | 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... | 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] -... | 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 ... | 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(combinatio... | 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]-ar... | 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... | 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 i... | 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[... | 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)):
... | 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(... | 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 ... | 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] - ar... | 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 #earl... | 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]
... | 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]
... | 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 lo... | 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[... | 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
... | 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]
... | 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... | 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 = mi... | 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:
le... | 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
... | 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
... | 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
... | 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... | 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 >... | 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:
... | 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
... | 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:
... | 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_st... | 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_... | 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:
... | 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 po... | 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
... | 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
retur... | 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... | 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 - (cntMissingE... | 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
w... | 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) o... | 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 a... | 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]
... | 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
... | 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):
... | 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_lef... | 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)
... | 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... | 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... | 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:
re... | 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)
... | 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.