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/shortest-palindrome/discuss/739903/Python3-2-line-brute-force-and-9-line-KMP | class Solution:
def shortestPalindrome(self, s: str) -> str:
ss = s + "#" + s[::-1]
lps = [0]*len(ss) #longest prefix suffix array
k = 0
for i in range(1, len(ss)):
while k and ss[k] != ss[i]:
k = lps[k-1]
if ss[k] == ss[i]: k += 1
... | shortest-palindrome | [Python3] 2-line brute force & 9-line KMP | ye15 | 1 | 92 | shortest palindrome | 214 | 0.322 | Hard | 3,700 |
https://leetcode.com/problems/shortest-palindrome/discuss/2847682/python | class Solution:
def shortestPalindrome(self, s: str) -> str:
if len(s) in [0,1]:
return s
n = len(s)//2
if len(s)%2==0:
n-=1
for i in range(n,0,-1):
if 2*(i+1)<= len(s) and s[:i+1][::-1] == s[i+1:2*(i+1)]:
return s[2*(i+1) :][::-1] ... | shortest-palindrome | python | ABDRAHIMHA2001 | 0 | 1 | shortest palindrome | 214 | 0.322 | Hard | 3,701 |
https://leetcode.com/problems/shortest-palindrome/discuss/2704009/sasta-python-code-oror | class Solution:
def shortestPalindrome(self, s: str) -> str:
def palin(st,j):
i=0
while i<j:
if st[i]!=st[j]:
return False
i+=1
j-=1
return True
if len(s)==0:
return ""
... | shortest-palindrome | sasta python code || | narendra_036 | 0 | 9 | shortest palindrome | 214 | 0.322 | Hard | 3,702 |
https://leetcode.com/problems/shortest-palindrome/discuss/2677831/Brute-force-in-Python-but-faster-than-half | class Solution:
def shortestPalindrome(self, s: str) -> str:
n = len(s)
if n == 1:
return s
for pivot in range((n + 1) // 2, -1, -1):
pre = s[:pivot]
suf1 = s[pivot:pivot+pivot][::-1]
suf2 = s[pivot-1:pivot+pivot-1][::-1]
if pre... | shortest-palindrome | Brute force in Python, but faster than half | metaphysicalist | 0 | 28 | shortest palindrome | 214 | 0.322 | Hard | 3,703 |
https://leetcode.com/problems/shortest-palindrome/discuss/2564711/Simple-python-solution-and-easy-to-understand-explaination | class Solution:
def shortestPalindrome(self, s: str) -> str:
end = len(s)-1
while(end>0):
if s[0] == s[end]:
#Check if s[0:end+1] is palindrome
if end % 2 == 0: # s[0:end+1] has odd digits
if s[0:end//2] == s[end:end//2:-1]:
... | shortest-palindrome | Simple python solution and easy-to-understand explaination | guojunfeng1998 | 0 | 77 | shortest palindrome | 214 | 0.322 | Hard | 3,704 |
https://leetcode.com/problems/shortest-palindrome/discuss/2177355/Simple-Iterative-Solution-O(n)-Python3 | class Solution:
def shortestPalindrome(self, s: str) -> str:
# filter out edge cases for strings we need to check
if len(s) <= 1:
return s
elif len(s) == 2:
if s[0] == s[1]:
return s
else:
return str(s[1]) + s
... | shortest-palindrome | Simple Iterative Solution O(n) - Python3 | ginomcfino | 0 | 73 | shortest palindrome | 214 | 0.322 | Hard | 3,705 |
https://leetcode.com/problems/shortest-palindrome/discuss/1727335/python3-easy-solution-but-should-I-use-more-complicated-method-like-KMP | class Solution:
def shortestPalindrome(self, s: str) -> str:
if s=="":
return ""
reverse_s = ''.join(list(reversed(s)))
for i in range(len(s)):
if reverse_s[i:] == s[0:len(s)-i]:
return reverse_s[0:i]+s | shortest-palindrome | python3 easy solution but, should I use more complicated method like KMP? | njuasxzg | 0 | 122 | shortest palindrome | 214 | 0.322 | Hard | 3,706 |
https://leetcode.com/problems/shortest-palindrome/discuss/1683203/Python-3-Rabin-Karp-%2B-custom-hash-function-constant-space-and-average-liniar-time | class Solution:
def shortestPalindrome(self, s: str) -> str:
n = len(s)
# The idea is to find the longest prefix in string S that is a palindrome. To solve this, apply Rabin-Karp
# algorithm with a custom rolling hash function. Once the longest palindrome prefix has been found, just add the... | shortest-palindrome | [Python 3] Rabin-Karp + custom hash function, constant space and average liniar time | corcoja | 0 | 135 | shortest palindrome | 214 | 0.322 | Hard | 3,707 |
https://leetcode.com/problems/shortest-palindrome/discuss/1590060/Python-one-liner | class Solution:
def shortestPalindrome(self, s: str) -> str:
return s[[i for i in range(len(s) - 1, -1, -1) if s[:i] == s[:i][::-1]][0]:][::-1] + s if s != s[::-1] else s | shortest-palindrome | Python one liner | shiv-sj | 0 | 173 | shortest palindrome | 214 | 0.322 | Hard | 3,708 |
https://leetcode.com/problems/shortest-palindrome/discuss/1255373/Python3-solution-using-KMP-algorithm | class Solution:
def shortestPalindrome(self, s: str) -> str:
"""
catacb
catacb#bcatac
0000100012345
basically if we observe catacb, if we just add 1 b in the beginning, it becomes a palindrome.
Easiest way to find that being, what is the prefix, which is also... | shortest-palindrome | Python3 solution using KMP algorithm | amol1729 | 0 | 108 | shortest palindrome | 214 | 0.322 | Hard | 3,709 |
https://leetcode.com/problems/shortest-palindrome/discuss/483134/Python3-short-simple-recursive-solution-faster-than-92.15 | class Solution:
def shortestPalindrome(self, s: str) -> str:
i,l = 0,len(s)
for j in range(l-1,-1,-1):
if s[i]==s[j]: i+=1
if i==l: return s
return s[i:][::-1] + self.shortestPalindrome(s[:i]) + s[i:] | shortest-palindrome | Python3 short simple recursive solution, faster than 92.15% | jb07 | 0 | 183 | shortest palindrome | 214 | 0.322 | Hard | 3,710 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2180509/Python-Easy-O(logn)-Space-approach-or-One-liner | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
n = len(nums)
def partition(l, r, pivot):
pivot_elem=nums[pivot]
nums[r],nums[pivot]=nums[pivot],nums[r]
index=l
for i in range(l, r):
... | kth-largest-element-in-an-array | Python Easy O(logn) Space approach | One liner | constantine786 | 24 | 3,300 | kth largest element in an array | 215 | 0.658 | Medium | 3,711 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2180509/Python-Easy-O(logn)-Space-approach-or-One-liner | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
return sorted(nums)[-k] | kth-largest-element-in-an-array | Python Easy O(logn) Space approach | One liner | constantine786 | 24 | 3,300 | kth largest element in an array | 215 | 0.658 | Medium | 3,712 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2182564/Two-Approaches-or-Sorting-or-Min-Heap | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums.sort(reverse=True)
return nums[k-1] | kth-largest-element-in-an-array | Two Approaches | Sorting | Min-Heap | zeus-salazar | 13 | 587 | kth largest element in an array | 215 | 0.658 | Medium | 3,713 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2182564/Two-Approaches-or-Sorting-or-Min-Heap | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
minheap=[]
count=0
for num in nums:
heappush(minheap,num)
count+=1
if count>k:
heappop(minheap)
count-=1
return minheap[0] | kth-largest-element-in-an-array | Two Approaches | Sorting | Min-Heap | zeus-salazar | 13 | 587 | kth largest element in an array | 215 | 0.658 | Medium | 3,714 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2134244/Python-2-best-Solutions-using-Heap-and-Quick-Select | class Solution:
def findKthLargest(self, nums, k):
pivot = nums[0]
left = [num for num in nums if num < pivot]
equal = [num for num in nums if num == pivot]
right = [num for num in nums if num > pivot]
if k <= len(right): return self.findKthLargest(right, k)... | kth-largest-element-in-an-array | Python 2 best Solutions using Heap and Quick-Select | samirpaul1 | 7 | 491 | kth largest element in an array | 215 | 0.658 | Medium | 3,715 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/553723/Several-Python-solution-sharing.-w-Study-resource | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums.sort( reverse = True )
return nums[k-1] | kth-largest-element-in-an-array | Several Python solution sharing. [w/ Study resource] | brianchiang_tw | 7 | 1,400 | kth largest element in an array | 215 | 0.658 | Medium | 3,716 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/553723/Several-Python-solution-sharing.-w-Study-resource | class Solution:
def partition(self, nums, left, right):
pivot = nums[left]
l, r = left+1, right
while l <= r:
if nums[l] < pivot and nums[r] > pivot:
nums[l], nums[r] = nums[r], nums[l]
l, r... | kth-largest-element-in-an-array | Several Python solution sharing. [w/ Study resource] | brianchiang_tw | 7 | 1,400 | kth largest element in an array | 215 | 0.658 | Medium | 3,717 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2759090/Python-using-heap | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums=list(map(lambda x:-x,nums))
heapq.heapify(nums)
for i in range(k-1):
heapq.heappop(nums) #Pop minimum from the list
return -heapq.heappop(nums) | kth-largest-element-in-an-array | Python using heap | Jebdeju | 4 | 517 | kth largest element in an array | 215 | 0.658 | Medium | 3,718 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/1371234/Python-Quickselect-template | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
def partition(left, right, pivot_index):
pivot = nums[pivot_index]
nums[pivot_index], nums[right] = nums[right], nums[pivot_index]
index = left
for i in range(left, right):
... | kth-largest-element-in-an-array | [Python] Quickselect template | soma28 | 4 | 372 | kth largest element in an array | 215 | 0.658 | Medium | 3,719 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2183958/Python-one-liner-solution-oror-Easy-To-Understand | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
return sorted(nums,reverse=True)[k-1] | kth-largest-element-in-an-array | ✅Python one-liner solution || Easy-To-Understand | Shivam_Raj_Sharma | 3 | 164 | kth largest element in an array | 215 | 0.658 | Medium | 3,720 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2180503/Python-Two-Line-Solution | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums.sort()
return nums[-k] | kth-largest-element-in-an-array | Python Two Line Solution | Ritaj_Zamel | 3 | 368 | kth largest element in an array | 215 | 0.658 | Medium | 3,721 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/1633253/Simple-and-straightforward-Python3-implementation-using-heap-with-explanation | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
# 1. Initialize minheap/priority queue to empty list. (in Python3 all heaps are minheaps by default)
pq = []
# 2. loop through every number in nums
for n in nums:
# 3. push each number n onto our heap pq
... | kth-largest-element-in-an-array | Simple and straightforward Python3 implementation using heap with explanation | drdill | 3 | 251 | kth largest element in an array | 215 | 0.658 | Medium | 3,722 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/491649/Python-One-Liner-Heap | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
return min(heapq.nlargest(k, nums)) | kth-largest-element-in-an-array | Python - One Liner - Heap | mmbhatk | 3 | 945 | kth largest element in an array | 215 | 0.658 | Medium | 3,723 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2403885/Quick-Select-algorithm-easy-to-understand | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
pivot = random.choice(nums)
bigger = [x for x in nums if x > pivot]
eq = [x for x in nums if x == pivot]
less = [x for x in nums if x < pivot]
if len(bigger) >= k: # all k elem... | kth-largest-element-in-an-array | Quick Select algorithm easy to understand | imanhn | 2 | 166 | kth largest element in an array | 215 | 0.658 | Medium | 3,724 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2484681/Python-(Simple-Approach-and-Beginner-Friendly) | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums = sorted(nums)
return nums[len(nums)-k] | kth-largest-element-in-an-array | Python (Simple Approach and Beginner-Friendly) | vishvavariya | 1 | 191 | kth largest element in an array | 215 | 0.658 | Medium | 3,725 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2465988/Python-1-line-solution-O(N-log-N) | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
return sorted(nums)[-k] | kth-largest-element-in-an-array | Python 1 line solution O(N log N) | sezanhaque | 1 | 101 | kth largest element in an array | 215 | 0.658 | Medium | 3,726 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2181271/Python-59-ms-run-time-or-99.17-faster-one-liner-97-memory-efficient | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
return sorted(nums,reverse=True)[k-1] | kth-largest-element-in-an-array | Python 59 ms run time | 99.17% faster one liner 97% memory efficient | anuvabtest | 1 | 134 | kth largest element in an array | 215 | 0.658 | Medium | 3,727 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2180861/Python3-or-very-easy-or-heap-oreasy-to-understand | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
heap = []
for e in nums:
heappush(heap, -e)
ans= -1
while k:
k-=1
ans = -heappop(heap)
return ans | kth-largest-element-in-an-array | Python3 | very easy | heap |easy to understand | H-R-S | 1 | 53 | kth largest element in an array | 215 | 0.658 | Medium | 3,728 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2180564/Quickselect-solution-in-Python-and-C%2B%2B | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
def swap(i, j):
nums[i], nums[j] = nums[j], nums[i]
def find_pivot_idx(l, r):
pivot_idx = (l + r) // 2
swap(pivot_idx, r)
pivot_val = nums[r]
end_of_larger_i... | kth-largest-element-in-an-array | Quickselect solution in Python and C++ | kryuki | 1 | 48 | kth largest element in an array | 215 | 0.658 | Medium | 3,729 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2133311/minHeap-solution | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
if not nums:
return 0
heap = []
for i in nums:
heapq.heappush(heap, i)
if len(heap) > k:
heapq.heappop(heap)
return heapq.heappop(heap) | kth-largest-element-in-an-array | minHeap solution | writemeom | 1 | 88 | kth largest element in an array | 215 | 0.658 | Medium | 3,730 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/1927426/2-lines-of-code-and-97.16-less-memory-usage | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums.sort(reverse=True)
return nums[k - 1] | kth-largest-element-in-an-array | 2 lines of code and 97.16 less memory usage | ankurbhambri | 1 | 64 | kth largest element in an array | 215 | 0.658 | Medium | 3,731 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/1869155/Python-one-line-solution | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
return sorted(nums, reverse=True)[k-1] | kth-largest-element-in-an-array | Python one line solution | alishak1999 | 1 | 164 | kth largest element in an array | 215 | 0.658 | Medium | 3,732 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/1827506/99.83-Faster-or-Simple-1-line-python-solution | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
return sorted(nums)[-k] | kth-largest-element-in-an-array | ✔99.83 Faster | Simple 1 line python solution | Coding_Tan3 | 1 | 113 | kth largest element in an array | 215 | 0.658 | Medium | 3,733 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/1822851/best-solution-ever-python-O(Nlogk)-time-O(k)-space-complexity | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
import heapq
heap = []
for num in nums:
if len(heap)<k:
heapq.heappush(heap,num)
elif num>heap[0]:
heapq.heappushpop(heap,num)
return heapq.heapp... | kth-largest-element-in-an-array | best solution ever , python, O(Nlogk) time, O(k) space complexity | aaronat | 1 | 78 | kth largest element in an array | 215 | 0.658 | Medium | 3,734 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/1645758/One-liner-Python3-solution-Easy-faster-than-96.61-and-memory-usage-less-than-93.31 | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
return sorted(nums)[len(nums) - k] | kth-largest-element-in-an-array | One-liner Python3 solution - Easy, faster than 96.61% and memory usage less than 93.31% | y-arjun-y | 1 | 175 | kth largest element in an array | 215 | 0.658 | Medium | 3,735 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/1339497/Python-Max-Heap-Solution-Without-Using-a-Library-Function-(93-less-memory) | class Solution:
def __init__(self):
self.heap=[]
self.capacity=0
self.length=0
def findKthLargest(self, nums: List[int], k: int) -> int:
self.capacity=len(nums)
self.heap=[0]*self.capacity
for i in nums:
self.heap[self.length] = i
... | kth-largest-element-in-an-array | Python Max Heap Solution Without Using a Library Function (93% less memory) | prajwal_vs | 1 | 177 | kth largest element in an array | 215 | 0.658 | Medium | 3,736 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/1147682/Python3-Solution-(99.8) | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
h = [] # use min heap with size k
for n in nums[:k]:
heappush(h, n)
for n in nums[k:]:
if n > h[0]:
heapreplace(h, n)
return heappop(h)
``` | kth-largest-element-in-an-array | Python3 Solution (99.8%) | dalechoi | 1 | 432 | kth largest element in an array | 215 | 0.658 | Medium | 3,737 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/736733/Python3-3-solutions-from-O(NlogN)-to-O(NlogK)-to-O(N) | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
return sorted(nums)[-k] | kth-largest-element-in-an-array | [Python3] 3 solutions from O(NlogN) to O(NlogK) to O(N) | ye15 | 1 | 126 | kth largest element in an array | 215 | 0.658 | Medium | 3,738 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/736733/Python3-3-solutions-from-O(NlogN)-to-O(NlogK)-to-O(N) | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
pq = [] #min heap of size k
for x in nums:
heappush(pq, x)
if len(pq) > k: heappop(pq)
return pq[0] | kth-largest-element-in-an-array | [Python3] 3 solutions from O(NlogN) to O(NlogK) to O(N) | ye15 | 1 | 126 | kth largest element in an array | 215 | 0.658 | Medium | 3,739 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/736733/Python3-3-solutions-from-O(NlogN)-to-O(NlogK)-to-O(N) | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
return nlargest(k, nums)[-1] | kth-largest-element-in-an-array | [Python3] 3 solutions from O(NlogN) to O(NlogK) to O(N) | ye15 | 1 | 126 | kth largest element in an array | 215 | 0.658 | Medium | 3,740 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/736733/Python3-3-solutions-from-O(NlogN)-to-O(NlogK)-to-O(N) | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
"""Hoare's selection algo"""
def partition(lo, hi):
"""Return partition of nums[lo:hi]."""
i, j = lo+1, hi-1
while i <= j:
if nums[i] < nums[lo]: i += 1
... | kth-largest-element-in-an-array | [Python3] 3 solutions from O(NlogN) to O(NlogK) to O(N) | ye15 | 1 | 126 | kth largest element in an array | 215 | 0.658 | Medium | 3,741 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/736733/Python3-3-solutions-from-O(NlogN)-to-O(NlogK)-to-O(N) | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
def fn(lo, hi):
"""Partition nums[lo:hi+1] into two parts"""
p = randint(lo, hi) #random pivot
nums[hi], nums[p] = nums[p], nums[hi] #relocate to rear
i = lo
while ... | kth-largest-element-in-an-array | [Python3] 3 solutions from O(NlogN) to O(NlogK) to O(N) | ye15 | 1 | 126 | kth largest element in an array | 215 | 0.658 | Medium | 3,742 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/701254/Python-3Kth-Largest-Element-in-an-Array.-Beats-95. | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
heapq.heapify(nums)
return nlargest(len(nums), nums)[k-1] | kth-largest-element-in-an-array | [Python 3]Kth Largest Element in an Array. Beats 95%. | tilak_ | 1 | 378 | kth largest element in an array | 215 | 0.658 | Medium | 3,743 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2847975/One-line-Python-Solution | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums.sort()
return nums[-k] | kth-largest-element-in-an-array | One line Python Solution | abdulrahmanphy64 | 0 | 2 | kth largest element in an array | 215 | 0.658 | Medium | 3,744 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2840650/Python-Easy-Solution | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
s = sorted(nums)
for i in range(k-1):
s.pop(len(s)-1)
return s[-1] | kth-largest-element-in-an-array | Python Easy Solution | Jashan6 | 0 | 3 | kth largest element in an array | 215 | 0.658 | Medium | 3,745 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2840216/Find-K-largest-values-python-O(N*log(N)) | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
kheap = []
for num in nums:
if len(kheap) < k:
heapq.heappush(kheap, num)
elif kheap[0]<num:
heapq.heappop(kheap)
heapq.heappush(kheap, num)
retur... | kth-largest-element-in-an-array | Find K largest values - python - O(N*log(N)) | DavidCastillo | 0 | 1 | kth largest element in an array | 215 | 0.658 | Medium | 3,746 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2840215/Find-K-largest-values-python-O(N*log(k)) | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
kheap = []
for num in nums:
if len(kheap) < k:
heapq.heappush(kheap, num)
elif kheap[0]<num:
heapq.heappop(kheap)
heapq.heappush(kheap, num)
retur... | kth-largest-element-in-an-array | Find K largest values - python - O(N*log(k)) | DavidCastillo | 0 | 2 | kth largest element in an array | 215 | 0.658 | Medium | 3,747 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2834044/Simplest-Python-Solution | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums.sort()
nums=nums[::-1]
return nums[(k-1)] | kth-largest-element-in-an-array | Simplest Python Solution | Ashil_3108 | 0 | 5 | kth largest element in an array | 215 | 0.658 | Medium | 3,748 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2824521/Easiest-Solution | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums.sort()
return nums[-k] | kth-largest-element-in-an-array | Easiest Solution | khanismail_1 | 0 | 4 | kth largest element in an array | 215 | 0.658 | Medium | 3,749 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2822275/Python-solution-or-heap | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
heap = []
n = len(nums)
for i in range(n):
heapq.heappush(heap,nums[i])
for i in range(n - k):
heapq.heappop(heap)
return heapq.heappop(heap) | kth-largest-element-in-an-array | Python solution | heap | maomao1010 | 0 | 4 | kth largest element in an array | 215 | 0.658 | Medium | 3,750 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2814381/Python-easy-code-solution. | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums.sort()
n = nums[::-1]
s = n[k-1:k]
ans = 0
for i in s:
ans += i
return int(ans) | kth-largest-element-in-an-array | Python easy code solution. | anshu71 | 0 | 5 | kth largest element in an array | 215 | 0.658 | Medium | 3,751 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2808326/Python-quick-select | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
k = len(nums) - k
def quickSelect(l, r):
pivot, p = nums[r], l
for i in range(l, r):
if nums[i] <= pivot:
nums[p], nums[i] = nums[i], nums[p]
p +... | kth-largest-element-in-an-array | Python quick select | zananpech9 | 0 | 5 | kth largest element in an array | 215 | 0.658 | Medium | 3,752 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2806451/one-line-python-solution | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums.sort()
return (nums[-k])
# result = []
# for i in range(k):
# result.append(max(nums))
# nums.remove(max(nums))
# return result[-1] | kth-largest-element-in-an-array | one line python solution | rahul_1234598 | 0 | 5 | kth largest element in an array | 215 | 0.658 | Medium | 3,753 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2803919/Daily-LeetCode-Practice-Day-22 | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
if not nums:
return
pivot = random.choice(nums)
left = [x for x in nums if x > pivot]
mid = [x for x in nums if x == pivot]
right = [x for x in nums if x < pivot]
L = len(... | kth-largest-element-in-an-array | Daily LeetCode Practice, Day 22 | yluo3421 | 0 | 3 | kth largest element in an array | 215 | 0.658 | Medium | 3,754 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2785616/ONE-LINE-SOLUTION-PYTHON-O(n) | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
return sorted(nums)[::-1][k-1] | kth-largest-element-in-an-array | ONE LINE SOLUTION PYTHON O(n) | m_ujeeb | 0 | 1 | kth largest element in an array | 215 | 0.658 | Medium | 3,755 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2780439/Two-line-solution-using-python! | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
nums.sort(reverse=True)
return nums[k-1] | kth-largest-element-in-an-array | Two line solution using python! | secret_hell | 0 | 3 | kth largest element in an array | 215 | 0.658 | Medium | 3,756 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2780426/Bucket-sort.-Easy-O(n) | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
arr = [0 for i in range(2*10001)]
for i in nums:
arr[i+10000] += 1
for j in range(len(arr)-1,-1,-1):
if(arr[j]>=k):
return j-10000
else:
k -= arr[j] | kth-largest-element-in-an-array | Bucket sort. Easy O(n) | rkgupta1298 | 0 | 5 | kth largest element in an array | 215 | 0.658 | Medium | 3,757 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2779121/heap-approach-python | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
# O(n*log(k)), O(k)
heapq.heapify(nums)
while len(nums) > k:
heapq.heappop(nums)
return nums[0]
# or
# return heapq.nlargest(k, nums)[-1] | kth-largest-element-in-an-array | heap approach python | sahilkumar158 | 0 | 7 | kth largest element in an array | 215 | 0.658 | Medium | 3,758 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2757951/Python3-Priority-Queue | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
h = []
for num in nums:
if len(h) < k:
heapq.heappush(h, num)
else:
if num > h[0]:
heapq.heappushpop(h, num)
return h[0] | kth-largest-element-in-an-array | Python3 Priority Queue | jonathanbrophy47 | 0 | 4 | kth largest element in an array | 215 | 0.658 | Medium | 3,759 |
https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/2727344/Quick-Select-Algorithm | class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
n = len(nums)
k = n - k
def quickSort(l=0, r=n-1):
p, pivot = l, nums[r]
for i in range(l, r):
if nums[i] <= pivot:
nums[i], nums[p] = nums[p], nums[i]
... | kth-largest-element-in-an-array | Quick Select Algorithm | andrewnerdimo | 0 | 9 | kth largest element in an array | 215 | 0.658 | Medium | 3,760 |
https://leetcode.com/problems/combination-sum-iii/discuss/1312030/Elegant-Python-Iterative-or-Recursive | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
current_combination, combinations = [], []
integer, combination_sum = 1, 0
queue = [(integer, current_combination, combination_sum)]
while queue:
integer, current_combination, combinati... | combination-sum-iii | Elegant Python Iterative | Recursive | soma28 | 5 | 175 | combination sum iii | 216 | 0.672 | Medium | 3,761 |
https://leetcode.com/problems/combination-sum-iii/discuss/1312030/Elegant-Python-Iterative-or-Recursive | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
combinations = []
counter = [(integer, 1) for integer in range(1, 10)]
def recursion(integer = 0, current_combination = [], combination_sum = 0):
if combination_sum > n or len(current_... | combination-sum-iii | Elegant Python Iterative | Recursive | soma28 | 5 | 175 | combination sum iii | 216 | 0.672 | Medium | 3,762 |
https://leetcode.com/problems/combination-sum-iii/discuss/2677557/Python-oror-Backtracking-(with-comments) | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
if k > n: # if number of elements greater than sum no point in checking
return []
if n > 45: # we can have max sum 45 for [1,2....,9]
return []
lst = range(1,10)
ans=[]
... | combination-sum-iii | Python || Backtracking (with comments) | Graviel77 | 2 | 124 | combination sum iii | 216 | 0.672 | Medium | 3,763 |
https://leetcode.com/problems/combination-sum-iii/discuss/842624/Python-3-or-Backtracking-DFS-or-Explanations | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
def dfs(digit, start_num, cur, cur_sum):
if cur_sum == n and digit == k: ans.append(cur[:])
elif digit >= k or cur_sum > n: return
else:
for i in range(start_num+1, 10):
... | combination-sum-iii | Python 3 | Backtracking, DFS | Explanations | idontknoooo | 2 | 351 | combination sum iii | 216 | 0.672 | Medium | 3,764 |
https://leetcode.com/problems/combination-sum-iii/discuss/2382303/Python-or-Easy-solution | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
answer = []
path = []
def dp(idx, total):
if len(path) > k:
return
if total > n:
return
if total == n and len(path) == k:
... | combination-sum-iii | Python | Easy solution | pivovar3al | 1 | 26 | combination sum iii | 216 | 0.672 | Medium | 3,765 |
https://leetcode.com/problems/combination-sum-iii/discuss/2025095/Python-Simple-and-Easy-Solution-using-Recursion-and-Backtracking | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
options = [1,2,3,4,5,6,7,8,9]
ans = []
comb = []
score = 0
self.GenerateValid(options, 0, k, n, score, comb, ans)
return ans
def GenerateValid(self, options, currentIndex, k, n, score, comb, ans):
if(score == n and k == 0... | combination-sum-iii | Python Simple and Easy Solution using Recursion and Backtracking | shubhamgoel90 | 1 | 52 | combination sum iii | 216 | 0.672 | Medium | 3,766 |
https://leetcode.com/problems/combination-sum-iii/discuss/2023986/Python-or-Standard-Backtrack-or-Easy-to-Understand | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
result = []
candidate = []
def backtrack(startIndex, curSum):
if curSum == n and len(candidate) == k:
result.append(candidate[:])
return
... | combination-sum-iii | Python | Standard Backtrack | Easy to Understand | Mikey98 | 1 | 36 | combination sum iii | 216 | 0.672 | Medium | 3,767 |
https://leetcode.com/problems/combination-sum-iii/discuss/1197561/Python-simple-DFS-faster-than-85%2B | class Solution:
def combinationSum3(self, k, n):
nums = list(range(1, 10))
ans = []
def dfs(cur, pos, target, d):
if d == k:
if target == 0:
ans.append(cur.copy())
return
for i in range(pos, 9):... | combination-sum-iii | Python simple DFS, faster than 85+% | dustlihy | 1 | 102 | combination sum iii | 216 | 0.672 | Medium | 3,768 |
https://leetcode.com/problems/combination-sum-iii/discuss/2789873/Intuitive-solution | class Solution:
def combinationSum3(self, k: int, target: int) -> List[List[int]]:
ans = []
def help(i,n,s,tot):
if len(s)==k and tot==target:
ans.append(s.copy())
return
if i>n:
return
if l... | combination-sum-iii | Intuitive solution | Rtriders | 0 | 1 | combination sum iii | 216 | 0.672 | Medium | 3,769 |
https://leetcode.com/problems/combination-sum-iii/discuss/2772111/Python-solution-or-backtrack | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
def backtrack(nums, index, target, path, res):
if len(path) == k and target == 0:
res.append(path)
return
if target < nums[index]:
... | combination-sum-iii | Python solution | backtrack | maomao1010 | 0 | 3 | combination sum iii | 216 | 0.672 | Medium | 3,770 |
https://leetcode.com/problems/combination-sum-iii/discuss/2751342/PYTHON-Solution-using-Combination-Sum-II | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
candidates = [num for num in range(1,10)] # build candidates
result = []
# combination sum II
def backtrack(idx,curr,target):
if target == 0 and len(curr) == k:
result.append(... | combination-sum-iii | PYTHON Solution using Combination Sum II | iamrahultanwar | 0 | 2 | combination sum iii | 216 | 0.672 | Medium | 3,771 |
https://leetcode.com/problems/combination-sum-iii/discuss/2748503/Python | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
if k > n:
return []
if n > 45:
return []
lst = range(1,10)
ans=[]
def solve(ind, ds, target):
if target == 0 and len(ds) == k:
ans.... | combination-sum-iii | Python | lucy_sea | 0 | 3 | combination sum iii | 216 | 0.672 | Medium | 3,772 |
https://leetcode.com/problems/combination-sum-iii/discuss/2744142/Simple-Python-Backtrack-Solution | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
visited = [False for _ in range(n)]
result = []
def backtrack(index, path):
if sum(path) == n and len(path)==k:
result.append(list(path))
return
... | combination-sum-iii | Simple Python Backtrack Solution | Rui_Liu_Rachel | 0 | 5 | combination sum iii | 216 | 0.672 | Medium | 3,773 |
https://leetcode.com/problems/combination-sum-iii/discuss/2720185/Python-itertools-SolutionEasy-To-Understand | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
result = []
for comb in itertools.combinations(range(1, 10), k):
if sum(comb) == n:
result.append(list(comb))
return result | combination-sum-iii | Python itertools Solution[Easy To Understand] | namashin | 0 | 4 | combination sum iii | 216 | 0.672 | Medium | 3,774 |
https://leetcode.com/problems/combination-sum-iii/discuss/2693654/Simple-backtracking | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
arr = [1,2,3,4,5,6,7,8,9]
res = []
ans = []
def backtrack(i, k, curSum):
if k == 0 and curSum == n:
res.append(ans.copy())
return
if k <... | combination-sum-iii | Simple backtracking | mukundjha | 0 | 2 | combination sum iii | 216 | 0.672 | Medium | 3,775 |
https://leetcode.com/problems/combination-sum-iii/discuss/2498746/Easy-Python-Solution-or-Recursion | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
arr=[1,2,3,4,5,6,7,8,9]
ans=[]
def helper(ind, count, curr):
if ind==len(arr):
if count==k and sum(curr)==n:
return ans.append(curr[:])
return
... | combination-sum-iii | Easy Python Solution | Recursion | Siddharth_singh | 0 | 24 | combination sum iii | 216 | 0.672 | Medium | 3,776 |
https://leetcode.com/problems/combination-sum-iii/discuss/2456201/Python3-or-Simple-Backtracking-%2B-Recursion-Solution | class Solution:
#Time-Complexity : O(9^k), since branching factor of rec. tree is at worst 9 and the height of tree
#is at most k!
#Space-Complexity:O(9 + k) -> O(k), due to max call stack depth of recursion!
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
#arr will have numbers from 1 through... | combination-sum-iii | Python3 | Simple Backtracking + Recursion Solution | JOON1234 | 0 | 11 | combination sum iii | 216 | 0.672 | Medium | 3,777 |
https://leetcode.com/problems/combination-sum-iii/discuss/2357095/python3-solution-backtrack | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
if n >= 10:
candidates = [i for i in range(1, 10)]
else:
candidates = [i for i in range(1, n+1)]
res, track = [], []
def backtrack(first, track):
if sum... | combination-sum-iii | python3 solution backtrack | codeSheep_01 | 0 | 16 | combination sum iii | 216 | 0.672 | Medium | 3,778 |
https://leetcode.com/problems/combination-sum-iii/discuss/2104023/Python3-DFS%3A-Easy-to-understand | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
candidates = list(range(1,10,1)) #List from 1-9
return self.helper(candidates, n, [], [], k-1)
def helper(self, candidates: List[int], target: int, path: List[int], output: List[List[int]], noOfItems:int) -> List[L... | combination-sum-iii | Python3 DFS: Easy to understand | abrarjahin | 0 | 23 | combination sum iii | 216 | 0.672 | Medium | 3,779 |
https://leetcode.com/problems/combination-sum-iii/discuss/2028530/Python-Solution-or-One-Liner-or-Over-99-Faster-or-Itertools | class Solution:
def __init__(self):
self.l = [1,2,3,4,5,6,7,8,9]
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
return [item for item in itertools.combinations(self.l,r=k) if sum(item) == n] | combination-sum-iii | Python Solution | One Liner | Over 99% Faster | Itertools | Gautam_ProMax | 0 | 21 | combination sum iii | 216 | 0.672 | Medium | 3,780 |
https://leetcode.com/problems/combination-sum-iii/discuss/2027134/Python-One-Line! | class Solution:
def combinationSum3(self, k, n):
return [x for x in combinations(range(1,10), k) if sum(x) == n] | combination-sum-iii | Python - One-Line! | domthedeveloper | 0 | 14 | combination sum iii | 216 | 0.672 | Medium | 3,781 |
https://leetcode.com/problems/combination-sum-iii/discuss/2025973/Python3-simple-backtracking-oror-dfs | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
self.ans = set()
def dfs(total=0, arr=[]):
if total==n and len(arr) == k:
self.ans.add(tuple(arr))
return
if len(arr) > k:
return
for i in... | combination-sum-iii | Python3 simple backtracking || dfs | rjnkokre | 0 | 11 | combination sum iii | 216 | 0.672 | Medium | 3,782 |
https://leetcode.com/problems/combination-sum-iii/discuss/2025477/Python-solution-using-Backtracking-99-faster | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
res = []
def backtrack(val, stack, target):
if len(stack) == k:
if target == 0:
res.append(stack)
else:
return
for x in rang... | combination-sum-iii | Python solution using Backtracking 99% faster | pradeep288 | 0 | 10 | combination sum iii | 216 | 0.672 | Medium | 3,783 |
https://leetcode.com/problems/combination-sum-iii/discuss/2025295/Python-or-Backtracking-%2B-recursion-or-Easy-to-understand | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
res = []
def backtrack (num, stack, target):
if len(stack) == k:
if target == 0:
res.append(stack)
return
for currN... | combination-sum-iii | Python | Backtracking + recursion | Easy to understand | Patil_Pratik | 0 | 27 | combination sum iii | 216 | 0.672 | Medium | 3,784 |
https://leetcode.com/problems/combination-sum-iii/discuss/2024948/python-java-DFS-(recursia) | class Solution:
def __init__(self):
self.answer = []
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
def DFS(table, sum, start, digits, total):
if digits == 0 :
if total == sum :
self.answer.append([])
for n in table : self.answer[-1].append(n)
... | combination-sum-iii | python, java - DFS (recursia) | ZX007java | 0 | 12 | combination sum iii | 216 | 0.672 | Medium | 3,785 |
https://leetcode.com/problems/combination-sum-iii/discuss/2024897/Python3-or-recursion-or-simple-solution | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
res=[]
def sum3(st,N,rem,ans):
if(N==0):
if(rem==0):
res.append(ans)
return
for i in range(st,10):
sum3(i+1,N-1,rem-i,ans+[i])
... | combination-sum-iii | Python3 | recursion | simple solution | kalyan63 | 0 | 25 | combination sum iii | 216 | 0.672 | Medium | 3,786 |
https://leetcode.com/problems/combination-sum-iii/discuss/2024868/python-3-oror-backtracking | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
def helper(k, n, low):
if k == 1:
return [[n]] if low <= n <= 9 else []
res = []
for num in range(low, 10):
for combo in helper(k - 1, n - num, num + 1):
... | combination-sum-iii | python 3 || backtracking | dereky4 | 0 | 22 | combination sum iii | 216 | 0.672 | Medium | 3,787 |
https://leetcode.com/problems/combination-sum-iii/discuss/2024573/Python3-Backtracking-or-Run-time-Beats-93.11 | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
result, curr = [], []
def backtrack(curr, index):
if len(curr) == k and sum(curr) == n:
result.append(curr[:])
return
elif len(curr) >= k or index > 9:
... | combination-sum-iii | Python3 Backtracking | Run time Beats 93.11% | elainexma4 | 0 | 6 | combination sum iii | 216 | 0.672 | Medium | 3,788 |
https://leetcode.com/problems/combination-sum-iii/discuss/2024485/Python3-87.92-or-DFS-Solution-O(C(9-k))-or-Easy-Implementation | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
results = []
def dfs(remain, comb, next_start):
if remain == 0 and len(comb) == k:
# make a copy of current combination
# Otherwise the combination would be rev... | combination-sum-iii | Python3 87.92% | DFS Solution O(C(9, k)) | Easy Implementation | doneowth | 0 | 11 | combination sum iii | 216 | 0.672 | Medium | 3,789 |
https://leetcode.com/problems/combination-sum-iii/discuss/2024382/python3-simple-backtracking-explained | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
# k = length limit, n = target
def dfs(num, total): # return None
# num = current checking, total = sum of tmp list
nonlocal k, n
if len(tmp) == k:
if t... | combination-sum-iii | [python3] simple backtracking, explained | sshinyy | 0 | 10 | combination sum iii | 216 | 0.672 | Medium | 3,790 |
https://leetcode.com/problems/combination-sum-iii/discuss/2024249/Explained-Backtracking-(Python) | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
res = []
def get_path(min_num, path, sm):
"""
min_num : minimum number from which we check further numbers. It is higher
than the last number inserted into the path
... | combination-sum-iii | Explained Backtracking (Python) | kaustav43 | 0 | 10 | combination sum iii | 216 | 0.672 | Medium | 3,791 |
https://leetcode.com/problems/combination-sum-iii/discuss/2024021/Python-Recursion-with-Time-Complexity | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
res = []
def combinationSum3Inner(i, target, l):
if target == 0 and len(l) == k:
res.append(l)
return
if target < 0 or len(l)==k:
return ... | combination-sum-iii | ✅ Python Recursion with Time Complexity | constantine786 | 0 | 145 | combination sum iii | 216 | 0.672 | Medium | 3,792 |
https://leetcode.com/problems/combination-sum-iii/discuss/1714052/Python3-oror-Backtracking-solution-for-beginners-oror-Recursion | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
res = []
def backtrack(curr,i,k):
# base match: a match
if k==0 and sum(curr)==n:
res.append(curr.copy())
return
if sum(curr)>n or k<0:
... | combination-sum-iii | Python3 || Backtracking solution for beginners || Recursion | tahsin_alamin | 0 | 35 | combination sum iii | 216 | 0.672 | Medium | 3,793 |
https://leetcode.com/problems/combination-sum-iii/discuss/1625694/Python-Backtracking-Solution-oror-greater-97-Time | class Solution:
def backtracker(self, lst, target, partial, targetSubsetL, ans):
if len(partial) > targetSubsetL:
return
elif len(partial) == targetSubsetL and sum(partial) == target:
ans.append(partial)
else:
for i in range(len(lst)):
num ... | combination-sum-iii | Python Backtracking Solution || > 97% Time | henriducard | 0 | 53 | combination sum iii | 216 | 0.672 | Medium | 3,794 |
https://leetcode.com/problems/combination-sum-iii/discuss/1528944/WEEB-DOES-PYTHON-(3-METHODS) | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
result = []
nums = [i for i in range(1, 10)]
def dfs(path = [], idx = 0):
if sum(path) == n and len(path) == k:
result.append(path)
return
if sum(path) > n or len(path) > k or idx == len(nums): return
dfs(path + [nu... | combination-sum-iii | WEEB DOES PYTHON (3 METHODS) | Skywalker5423 | 0 | 63 | combination sum iii | 216 | 0.672 | Medium | 3,795 |
https://leetcode.com/problems/combination-sum-iii/discuss/1528944/WEEB-DOES-PYTHON-(3-METHODS) | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
nums = [i for i in range(1, 10)]
queue = deque([])
for i in range(len(nums)):
queue.append(([nums[i]], i, nums[i]))
return self.bfs(queue, nums, k, n)
def bfs(self, queue, nums, k, n):
result = []
while queue:
curPath,... | combination-sum-iii | WEEB DOES PYTHON (3 METHODS) | Skywalker5423 | 0 | 63 | combination sum iii | 216 | 0.672 | Medium | 3,796 |
https://leetcode.com/problems/combination-sum-iii/discuss/1528944/WEEB-DOES-PYTHON-(3-METHODS) | class Solution:
def combinationSum3(self, k: int, n: int) -> List[List[int]]:
return [val for val in itertools.combinations([i for i in range(1,10)], k) if sum(val) == n] | combination-sum-iii | WEEB DOES PYTHON (3 METHODS) | Skywalker5423 | 0 | 63 | combination sum iii | 216 | 0.672 | Medium | 3,797 |
https://leetcode.com/problems/combination-sum-iii/discuss/1429952/Python-backtracking-solution-for-Combination-Sum-I-II-and-III | class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
res = []
path = []
index = 0
total = 0
candidates.sort()
self.backtrack(candidates, target, res, path, index, total)
return res
def backtrack(self, candid... | combination-sum-iii | Python backtracking solution for Combination Sum I, II and III | treksis | 0 | 80 | combination sum iii | 216 | 0.672 | Medium | 3,798 |
https://leetcode.com/problems/combination-sum-iii/discuss/1429952/Python-backtracking-solution-for-Combination-Sum-I-II-and-III | class Solution:
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
res = []
path = []
index = 0
total = 0
self.backtrack(candidates, target, res, path, index, total)
return res
def backtrack(self, candi... | combination-sum-iii | Python backtracking solution for Combination Sum I, II and III | treksis | 0 | 80 | combination sum iii | 216 | 0.672 | Medium | 3,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.