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/find-all-people-with-secret/discuss/1599870/Python3-BFS-or-DFS-by-group | class Solution:
def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
can = {0, firstPerson}
for _, grp in groupby(sorted(meetings, key=lambda x: x[2]), key=lambda x: x[2]):
queue = set()
graph = defaultdict(list)
for x, y, _ ... | find-all-people-with-secret | [Python3] BFS or DFS by group | ye15 | 29 | 3,100 | find all people with secret | 2,092 | 0.342 | Hard | 28,900 |
https://leetcode.com/problems/find-all-people-with-secret/discuss/1599870/Python3-BFS-or-DFS-by-group | class Solution:
def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
can = {0, firstPerson}
for _, grp in groupby(sorted(meetings, key=lambda x: x[2]), key=lambda x: x[2]):
stack = set()
graph = defaultdict(list)
for x, y, _ ... | find-all-people-with-secret | [Python3] BFS or DFS by group | ye15 | 29 | 3,100 | find all people with secret | 2,092 | 0.342 | Hard | 28,901 |
https://leetcode.com/problems/find-all-people-with-secret/discuss/1600419/Really-the-Simplest-answer-I-came-through-oror-BFS-or-DFS | class Solution:
def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
meetings.sort(key=lambda x:x[2])
groups = itertools.groupby(meetings,key = lambda x:x[2])
sh = {0,firstPerson}
for key,grp in groups:
seen = set()
graph = defaultdict(list... | find-all-people-with-secret | ππ Really the Simplest answer I came through || BFS or DFS π | abhi9Rai | 12 | 576 | find all people with secret | 2,092 | 0.342 | Hard | 28,902 |
https://leetcode.com/problems/find-all-people-with-secret/discuss/1600419/Really-the-Simplest-answer-I-came-through-oror-BFS-or-DFS | class Solution:
def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
meetings.sort(key=lambda x:x[2])
groups = itertools.groupby(meetings,key = lambda x:x[2])
sh = {0,firstPerson}
for key,grp in groups:
seen = set()
graph = defaultdict(list)
for a,b,t in grp:
... | find-all-people-with-secret | ππ Really the Simplest answer I came through || BFS or DFS π | abhi9Rai | 12 | 576 | find all people with secret | 2,092 | 0.342 | Hard | 28,903 |
https://leetcode.com/problems/find-all-people-with-secret/discuss/1599997/Python-BFS | class Solution:
def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
g = defaultdict(dict)
for p1, p2, t in meetings:
g[t][p1] = g[t].get(p1, [])
g[t][p1].append(p2)
g[t][p2] = g[t].get(p2, [])
g[t][p2].append(p1)
... | find-all-people-with-secret | Python BFS | BetterLeetCoder | 6 | 486 | find all people with secret | 2,092 | 0.342 | Hard | 28,904 |
https://leetcode.com/problems/find-all-people-with-secret/discuss/1605880/Python-solution-based-on-connected-components | class Solution(object):
def findAllPeople(self, n, meetings, firstPerson):
"""
:type n: int
:type meetings: List[List[int]]
:type firstPerson: int
:rtype: List[int]
"""
times = dict()
known = set([firstPerson,0])
for x,y,time in meetin... | find-all-people-with-secret | Python solution based on connected components | gajrajgchouhan | 1 | 62 | find all people with secret | 2,092 | 0.342 | Hard | 28,905 |
https://leetcode.com/problems/find-all-people-with-secret/discuss/2530437/Python-Simple-DFS-Solution-w-Explanation-or-O(V%2BE)-Time-O(V%2BE)-Space-or-No-DSU | class Solution:
def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
# Build the graph & store time of the meeting as weights of the edges
adj = defaultdict(list)
for x, y, time in meetings:
adj[x].append((y, time))
adj[y].app... | find-all-people-with-secret | [Python] Simple DFS Solution w/ Explanation | O(V+E) Time, O(V+E) Space | No DSU | namanr17 | 0 | 32 | find all people with secret | 2,092 | 0.342 | Hard | 28,906 |
https://leetcode.com/problems/find-all-people-with-secret/discuss/2524038/python3-bfs-with-pq-solution-for-reference | class Solution:
def findAllPeople(self, n: int, meetings, firstPerson: int):
g = defaultdict(list)
for s,e,m in meetings:
g[s].append((e, m))
g[e].append((s, m))
st = [(0,0), (0,firstPerson)]
ans = set([])
while st:
... | find-all-people-with-secret | [python3] bfs with pq, solution for reference | vadhri_venkat | 0 | 11 | find all people with secret | 2,092 | 0.342 | Hard | 28,907 |
https://leetcode.com/problems/find-all-people-with-secret/discuss/1629797/python3-using-groupby-and-deque-to-solve-it | class Solution:
def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
from itertools import groupby
from collections import deque
ans = {0, firstPerson}
frames = {}
for k, g in groupby(sorted(meetings, key=lambda x: x[2])... | find-all-people-with-secret | [python3] using groupby and deque to solve it | etu7912a482 | 0 | 32 | find all people with secret | 2,092 | 0.342 | Hard | 28,908 |
https://leetcode.com/problems/find-all-people-with-secret/discuss/1600725/python-simple-to-understand | class Solution:
def findAllPeople(self, n: int, meetings: List[List[int]], fP: int) -> List[int]:
# these two person already know the answer initially
seen = {0,fP}
# set of all the times for the meeting
time = set()
# collections of all the meetin... | find-all-people-with-secret | python simple to understand | Anshuman2043 | 0 | 46 | find all people with secret | 2,092 | 0.342 | Hard | 28,909 |
https://leetcode.com/problems/find-all-people-with-secret/discuss/1600061/Python3-or-Sort-by-Time-and-BFS-or-Explanation-Included | class Solution:
def findAllPeople(self, n: int, meetings: List[List[int]], firstPerson: int) -> List[int]:
secretHolders = set()
secretHolders.add(0)
secretHolders.add(firstPerson)
tupleMeetings = {}
for meeting in meetings:
time = meeting[2]
... | find-all-people-with-secret | Python3 | Sort by Time and BFS | Explanation Included | RoshanNaik | 0 | 72 | find all people with secret | 2,092 | 0.342 | Hard | 28,910 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/1611987/Python3-brute-force | class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
ans = set()
for x, y, z in permutations(digits, 3):
if x != 0 and z & 1 == 0:
ans.add(100*x + 10*y + z)
return sorted(ans) | finding-3-digit-even-numbers | [Python3] brute-force | ye15 | 29 | 1,900 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,911 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/1611987/Python3-brute-force | class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
ans = []
freq = Counter(digits)
for x in range(100, 1000, 2):
if not Counter(int(d) for d in str(x)) - freq: ans.append(x)
return ans | finding-3-digit-even-numbers | [Python3] brute-force | ye15 | 29 | 1,900 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,912 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/1615242/Python-a-short-Counter-based-solution | class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
digits = Counter(digits)
result = []
for d1 in range(1, 10):
for d2 in range(10):
for d3 in range(0, 10, 2):
if not Counter([d1, d2, d3]) - ... | finding-3-digit-even-numbers | Python, a short Counter-based solution | blue_sky5 | 2 | 125 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,913 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/2156932/Python-one-line-solution | class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
return sorted({i*100 + j*10 + k for i,j,k in permutations(digits,3) if i!=0 and k%2==0}) | finding-3-digit-even-numbers | Python one line solution | writemeom | 1 | 154 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,914 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/1857712/1-Line-Python-Solution-oror-40-Faster-oror-Memory-less-than-70 | class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
return [''.join([str(i) for i in x]) for x in sorted(set(permutations(digits,3))) if x[2]%2==0 and x[0]!=0] | finding-3-digit-even-numbers | 1-Line Python Solution || 40% Faster || Memory less than 70% | Taha-C | 1 | 150 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,915 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/2474364/Clean-and-well-structured-Python3-implementation-(Top-96.9) | class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
hmap, res = defaultdict(int), []
for num in digits:
hmap[num] += 1 #counting frequency of digits of digits array
for num in range(100, 999, 2): #step 2 because we need even numbers
... | finding-3-digit-even-numbers | βοΈ Clean and well structured Python3 implementation (Top 96.9%) | Kagoot | 0 | 46 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,916 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/2240506/Python-simple-solution | class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
nums = {str(x) for x in range(100,1000,2)}
ans = []
for i in nums:
for d in i:
if i.count(d) > digits.count(int(d)):
break
else:
ans.append(i... | finding-3-digit-even-numbers | Python simple solution | StikS32 | 0 | 145 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,917 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/2124344/Python-3-Loops-1-to-10-0-to-10 | class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
c, res = Counter(digits), set()
for i in range(1, 10):
c[i] -= 1
for j in range(0, 10):
c[j] -= 1
for k in range(0, 10):
c[k] -= 1
... | finding-3-digit-even-numbers | Python 3 Loops, 1 to 10, 0 to 10 | Hejita | 0 | 108 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,918 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/2108273/Easy-Understandable-python-code | class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
l = set()
s = ""
for i in itertools.permutations(digits,3): # Making all the 3-digits Even numbers permutations
if i[0]==0:
continue
elif i[-1]%2!=0:
continu... | finding-3-digit-even-numbers | Easy Understandable python code | M3Sachin | 0 | 96 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,919 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/2103398/Python-oneliner-using-Counter | class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
c = Counter(digits)
return sorted({i for i in range(100, 1000 , 2) if c >= Counter(map(int , str(i)))}) | finding-3-digit-even-numbers | Python oneliner using Counter | Vigneswar_A | 0 | 50 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,920 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/1917970/Python-Clean-and-Simple!-Counter-and-Permutations | class Solution:
def findEvenNumbers(self, digits):
c, numbers = Counter(digits), []
for i in range(100,1000,2):
ci = Counter([int(x) for x in str(i)])
if all(k in c and c[k]>=v for k,v in ci.items()):
numbers.append(i)
return numbers | finding-3-digit-even-numbers | Python - Clean and Simple! Counter and Permutations | domthedeveloper | 0 | 177 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,921 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/1917970/Python-Clean-and-Simple!-Counter-and-Permutations | class Solution:
def findEvenNumbers(self, digits):
c, numbers = Counter(digits), []
for i in range(100,1000,2):
if c >= Counter([int(x) for x in str(i)]):
numbers.append(i)
return numbers | finding-3-digit-even-numbers | Python - Clean and Simple! Counter and Permutations | domthedeveloper | 0 | 177 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,922 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/1917970/Python-Clean-and-Simple!-Counter-and-Permutations | class Solution:
def findEvenNumbers(self, digits):
return (lambda c:[i for i in range(100,1000,2) if c >= Counter([int(x) for x in str(i)])])(Counter(digits)) | finding-3-digit-even-numbers | Python - Clean and Simple! Counter and Permutations | domthedeveloper | 0 | 177 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,923 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/1917970/Python-Clean-and-Simple!-Counter-and-Permutations | class Solution:
def findEvenNumbers(self, digits):
arr = []
for a, b, c in set(permutations(digits, 3)):
if a and not c:
arr.append(100*a+10*b+c)
return sorted(arr) | finding-3-digit-even-numbers | Python - Clean and Simple! Counter and Permutations | domthedeveloper | 0 | 177 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,924 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/1917970/Python-Clean-and-Simple!-Counter-and-Permutations | class Solution:
def findEvenNumbers(self, digits):
return sorted(a*100+b*10+c for a, b, c in set(permutations(digits, 3)) if a and not c&1) | finding-3-digit-even-numbers | Python - Clean and Simple! Counter and Permutations | domthedeveloper | 0 | 177 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,925 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/1646906/Python-using-Counter-and-permutations | class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
count = Counter(digits)
for v, n in count.items():
count[v] = min(n, 3)
l = set()
for t in permutations(count.elements(), 3):
v = int(''.join(map(str, t)))
if v % 2... | finding-3-digit-even-numbers | Python, using Counter and permutations | emwalker | 0 | 143 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,926 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/1618254/Counters-and-list-filtering-89-speed | class Solution:
nums = {n: Counter(map(int, str(n))) for n in range(100, 999, 2)}
def findEvenNumbers(self, digits: List[int]) -> List[int]:
cnt_digits = Counter(digits)
return [n for n, cnt in Solution.nums.items()
if all(count <= cnt_digits[d] for d, count in cnt.items())] | finding-3-digit-even-numbers | Counters and list filtering, 89% speed | EvgenySH | 0 | 157 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,927 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/1612000/Python3-itertools | class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
output = set()
for n in itertools.permutations(digits, 3):
if n[0] != 0 and n[2] % 2 == 0:
output.add(n[0] * 100 + n[1] * 10 + n[2])
output = list(output)
output.sort()
... | finding-3-digit-even-numbers | Python3 itertools | michaelb072 | 0 | 71 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,928 |
https://leetcode.com/problems/finding-3-digit-even-numbers/discuss/1611990/python-brute-force-solution | class Solution:
def findEvenNumbers(self, digits: List[int]) -> List[int]:
n = len(digits)
res = []
for i in range(n):
if digits[i] != 0:
for j in range(n):
if j != i:
for k in range(n):
... | finding-3-digit-even-numbers | python brute force solution | abkc1221 | 0 | 112 | finding 3 digit even numbers | 2,094 | 0.574 | Easy | 28,929 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2700533/Fastest-python-solution-TC%3A-O(N)SC%3A-O(1) | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow,fast,prev=head,head,None
while fast and fast.next:
prev=slow
slow=slow.next
fast=fast.next.next
if prev==None:
return None
prev.next=slow.next
... | delete-the-middle-node-of-a-linked-list | Fastest python solution TC: O(N),SC: O(1) | shubham_1307 | 12 | 1,200 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,930 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/1617620/Python-oror-Easy-Solution | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head.next == None:
return
slow, fast = head, head
prev = None
while fast and fast.next:
prev = slow
slow = slow.next
fast = fast.next.next
prev.next = slow.next
return head | delete-the-middle-node-of-a-linked-list | Python || Easy Solution | naveenrathore | 3 | 130 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,931 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2234244/Python3-solution-using-2-pointer-slow-and-fast | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return
fast = head.next
slow = head
while slow and fast and fast.next:
if fast.next.next is None:
break
... | delete-the-middle-node-of-a-linked-list | Python3 solution using 2 pointer slow and fast | ShaangriLa | 1 | 26 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,932 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/1980213/python-3-oror-two-pointers-oror-O(n)O(1) | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = prevSlow = ListNode(0, head)
slow = fast = head
while fast and fast.next:
prevSlow = slow
slow = slow.next
fast = fast.next.next
prevSlow.next ... | delete-the-middle-node-of-a-linked-list | python 3 || two pointers || O(n)/O(1) | dereky4 | 1 | 114 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,933 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/1807644/Python-slow-and-fast-pointer-solution | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head.next is None:
head=head.next
return head
cur = head
prev = None
double_cur = head
while double_cur and double_cur.next:
prev = cur
c... | delete-the-middle-node-of-a-linked-list | Python slow and fast pointer solution | adiljay05 | 1 | 58 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,934 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/1612192/Python3-2-pointers | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev = None
fast = slow = head
while fast and fast.next:
fast = fast.next.next
prev = slow
slow = slow.next
if not prev: return None
prev.next = pre... | delete-the-middle-node-of-a-linked-list | [Python3] 2 pointers | ye15 | 1 | 47 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,935 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/1612192/Python3-2-pointers | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = fast = slow = ListNode(next = head)
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
slow.next = slow.next.next
return dummy.next | delete-the-middle-node-of-a-linked-list | [Python3] 2 pointers | ye15 | 1 | 47 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,936 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/1612009/python-Floyd's-Algo | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head.next:
return
slow = fast = head
prev = None
while fast and fast.next:
prev = slow
slow = slow.next
fast = fast.next.ne... | delete-the-middle-node-of-a-linked-list | python Floyd's Algo | abkc1221 | 1 | 85 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,937 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2847208/Easiest-Python-Solution-2095.-Delete-the-Middle-Node-of-a-Linked-List | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
current = head
nums = []
while current:
nums.append(current.val)
current = current.next
mid = len(nums)//2
del nums[int(mid)]
dummy = current = ListNode(0)
... | delete-the-middle-node-of-a-linked-list | β
Easiest Python Solution - 2095. Delete the Middle Node of a Linked List | Brian_Daniel_Thomas | 0 | 1 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,938 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2781907/Delete-the-Middle-Node-of-a-Linked-List-Python-easy-solution | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
n=0
node = head
while node:
n+=1
node = node.next
mid = n//2
if mid==0:
head = None
return head
... | delete-the-middle-node-of-a-linked-list | Delete the Middle Node of a Linked List - Python easy solution | Apucs | 0 | 4 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,939 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2710187/python-easy-solution-oror-simple-traversal | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head.next:return None
cnt=0
cur=head
while cur and cur.next: #program to find length
cnt+=1
cur=cur.next
length=(cnt+1)
middle=length//2 #midd... | delete-the-middle-node-of-a-linked-list | python easy solution || simple traversal | tush18 | 0 | 2 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,940 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2704086/Python3!-1-Pass.-As-short-as-it-gets. | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
def recursive(node:ListNode, length:int = 0) -> int:
if node is None:
return length
total = recursive(node.next, length+1)
if length + 1 == total // 2:
... | delete-the-middle-node-of-a-linked-list | πPython3! 1 Pass. As short as it gets. | aminjun | 0 | 4 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,941 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2702974/Python3-or-Faster-than-99-or-Two-Pointers | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head.next == None:
return None
elif head.next.next == None:
head.next = None
return head
else:
slow = head
fast = head.next.next
while(fast != Non... | delete-the-middle-node-of-a-linked-list | Python3 | Faster than 99% | Two Pointers | TheSnappl | 0 | 10 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,942 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2702425/PYTHON-Simple-python-solution | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head.next == None:
head = None
else:
tmp = head
length = 0
while tmp:
length += 1
tmp = tmp.next
... | delete-the-middle-node-of-a-linked-list | [PYTHON] Simple python solution | valera_grishko | 0 | 4 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,943 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2702378/Python-Simple-Python-Solution-Using-Iterative-Approach | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head.next == None:
return None
node = head
length = 0
while node != None:
length = length + 1
node = node.next
mid_point = length // 2
new_node = head
while mid_point > 1:
new_node = new_node.next... | delete-the-middle-node-of-a-linked-list | [ Python ] β
β
Simple Python Solution Using Iterative Approach π₯³βπ | ASHOK_KUMAR_MEGHVANSHI | 0 | 15 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,944 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2701991/Python-Accepted | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head.next: return None
slow, fast = head, head.next.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
slow.next = slow.next.next
return h... | delete-the-middle-node-of-a-linked-list | Python Accepted β
| Khacker | 0 | 4 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,945 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2701867/Python-(Faster-than-98)-with-a-brief-explanationor-Two-pointers | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(0, head)
slow = dummy
fast = dummy.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
slow.next = slow.next.next
re... | delete-the-middle-node-of-a-linked-list | Python (Faster than 98%) with a brief explanation| Two-pointers | KevinJM17 | 0 | 2 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,946 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2701352/tortoise-and-hare-beating-89-in-time-and-79-in-memory | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
# tortoise and hare
if not head.next:
return None
hare = head
tortoise = head
# the previous node for tortoise
tortoise_ = head
while hare and hare.next:
... | delete-the-middle-node-of-a-linked-list | tortoise and hare, beating 89% in time and 79% in memory | leonine9 | 0 | 1 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,947 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2701176/Simple-1-pass-Python3-Solution | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head.next is None:
return None
prev = temp1x = temp2x = head
while True:
temp1x = temp1x.next
temp2x = temp2x.next.next
if... | delete-the-middle-node-of-a-linked-list | Simple π₯1-passπ₯ Python3 Solution | mediocre-coder | 0 | 2 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,948 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2701168/Python-2-Pointers-or-O(n)-or-Simple-Intuitive | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head.next:
return None
slow, fast = head, head.next.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
slow.next = slow... | delete-the-middle-node-of-a-linked-list | Python 2 Pointers | O(n) | Simple Intuitive | Nibba2018 | 0 | 4 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,949 |
https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/discuss/2701062/Python-3-oror-Simple-Solution-oror-Easy-Understanding-oror-O(N)-complexity | class Solution:
def deleteMiddle(self, head: Optional[ListNode]) -> Optional[ListNode]:
if head == None:
return head
if head.next == None:
return head.next
node = head
n = 0
while(node != None):
n+=1
node = node.next
mid... | delete-the-middle-node-of-a-linked-list | Python 3 || Simple Solution || Easy Understanding || O(N) complexity | ahamedmusadiq_-12 | 0 | 3 | delete the middle node of a linked list | 2,095 | 0.605 | Medium | 28,950 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/1612179/Python3-lca | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
def lca(node):
"""Return lowest common ancestor of start and dest nodes."""
if not node or node.val in (startValue , destValue): return node
left, right =... | step-by-step-directions-from-a-binary-tree-node-to-another | [Python3] lca | ye15 | 108 | 8,100 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,951 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/1728875/2-Python-solutions%3A-Graph-based-LCA | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
graph=defaultdict(list)
stack=[(root)]
#Step1: Build Graph
while stack:
node=stack.pop()
if node.left:
graph[node.val].ap... | step-by-step-directions-from-a-binary-tree-node-to-another | 2 Python π solutions: Graph-based, LCA | InjySarhan | 15 | 1,300 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,952 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/1728875/2-Python-solutions%3A-Graph-based-LCA | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
def getLCA(root,p,q):
if not root: return None
if root.val==p or root.val==q:
return root
L=getLCA(root.left,p,q)
R=getLCA(root.... | step-by-step-directions-from-a-binary-tree-node-to-another | 2 Python π solutions: Graph-based, LCA | InjySarhan | 15 | 1,300 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,953 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/1728875/2-Python-solutions%3A-Graph-based-LCA | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
#soltion 2 LCA
def getLCA(root,p,q):
if not root: return None
if root.val==p or root.val==q:
return root
L=getLCA(root.left,p,q)
... | step-by-step-directions-from-a-binary-tree-node-to-another | 2 Python π solutions: Graph-based, LCA | InjySarhan | 15 | 1,300 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,954 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/2224238/Simple-iterative-solution-pre-order-with-explanation-and-comments | class Solution:
def find_path(self, node, src_val, dest_val):
# To handle pre-order iterative
stack = []
stack.append((node, ""))
# Tracking the path to both start and destination
src_path = dest_path = ""
while stack:
node, path = stack.pop()
... | step-by-step-directions-from-a-binary-tree-node-to-another | Simple iterative solution pre-order with explanation and comments | massaker | 2 | 128 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,955 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/2065266/Python-two-DFS-traversals | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
def buildAdjacencyList(node):
if not node:
return
if node.left:
adj[node.val].append((node.left.val, 'L'))
adj[node.... | step-by-step-directions-from-a-binary-tree-node-to-another | Python, two DFS traversals | blue_sky5 | 2 | 237 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,956 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/1964602/BFS-Python-straightforward-solution | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
dq = deque([[root, ""]])
sourceDirections = ""
destDirections = ""
while len(dq) > 0:
curr = dq.popleft()
if curr[0] is None:
continue
... | step-by-step-directions-from-a-binary-tree-node-to-another | BFS Python straightforward solution | user6397p | 2 | 213 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,957 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/2103945/python3-solution%3A-90-faster-solution | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
final_paths = [[], []]
def path_to_node(root, cur_path, direction):
nonlocal final_paths, startValue, destValue
if not root:
... | step-by-step-directions-from-a-binary-tree-node-to-another | python3 solution: 90% faster solution | subrahmanyajoshi123 | 1 | 201 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,958 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/1996946/Clean-simple-Python-LCA-with-parents-hashmap | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
# Build a parent map with which we can move upward in the tree.
# For this specific problem, we also mark whether it's a left or right child of the parent.
parents = dict()
... | step-by-step-directions-from-a-binary-tree-node-to-another | Clean, simple Python, LCA with parents hashmap | boris17 | 1 | 105 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,959 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/1879154/python-easy-dfs-solution-(no-LCA) | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
direction = defaultdict(dict)
stack = [root]
while stack:
node = stack.pop()
if node.left:
stack.append(node.left)
... | step-by-step-directions-from-a-binary-tree-node-to-another | python easy dfs solution (no LCA) | byuns9334 | 1 | 251 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,960 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/2562873/Python-O(n)-LCA-without-copying-path-each-time | class Solution:
def getDirections(self, root: Optional[TreeNode], s: int, t: int) -> str:
sPath, tPath, stack, path = None, None, [], []
def dfs(n):
nonlocal sPath
nonlocal tPath
if n is None or (sPath is not None and tPath is not None):
return
if n.val == s:
sPa... | step-by-step-directions-from-a-binary-tree-node-to-another | [Python] O(n) LCA without copying path each time | ambient8 | 0 | 80 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,961 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/2311045/Python-BackTrack | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
def findRootToNode(node, curStr, dest, res):
if node.val == dest:
res[0] = ''.join(curStr)
return
if node.left:
curSt... | step-by-step-directions-from-a-binary-tree-node-to-another | Python, BackTrack | Kewl777 | 0 | 65 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,962 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/2060164/Python3-BacktrackLCA-path-construction | class Solution:
def getDirections(self, root: Optional[TreeNode], start_value: int, dest_value: int) -> str:
def dirs_from_root(root: TreeNode, t: int, dirs) -> list[str]:
if root is None:
return False
elif root.val == t:
return True
... | step-by-step-directions-from-a-binary-tree-node-to-another | [Python3] Backtrack/LCA path construction | taborlin | 0 | 118 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,963 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/2005656/Python-DFS-easy-solution | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
p_s, p_d = [], []
p_s = self.find(root, startValue, p_s)
p_d = self.find(root, destValue, p_d)
overlap_index = -1
# find common ancestor
for i in range(len(p_s)... | step-by-step-directions-from-a-binary-tree-node-to-another | Python DFS easy solution | EthanLi0203 | 0 | 210 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,964 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/1709138/Without-LCA-ignore-common-part-of-paths | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
stack, start, end = [(root, "")], "", ""
while stack:
node, path = stack.pop()
stack = stack + [(node.left, path + "L")] if node.left else stack
stack = s... | step-by-step-directions-from-a-binary-tree-node-to-another | Without LCA; ignore common part of paths | abhie | 0 | 95 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,965 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/1616418/Find-both-with-while-loop | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
row = [("", root)]
start_path, dest_path = "", ""
start_found = dest_found = False
while row:
new_row = []
for path, node in row:
if node... | step-by-step-directions-from-a-binary-tree-node-to-another | Find both with while loop | EvgenySH | 0 | 70 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,966 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/1613267/Python-3-BFS-%2B-Djikstra's-algoirthm | class Solution:
def getDirections(self, root: Optional[TreeNode], s: int, d: int) -> str:
par = {}
left, right = {}, {}
def dfs(cur, parent, d):
if not cur:
return
if parent:
par[cur.val] = parent.val
if d == 'l':
... | step-by-step-directions-from-a-binary-tree-node-to-another | [Python 3] BFS + Djikstra's algoirthm | chestnut890123 | 0 | 91 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,967 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/1612693/Simple-Python-solution.(with-comments) | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
def path(root,val,lst,ast):
if root:
lst.append(ast)
if root.val==val:
return True
if path(root.left,val,lst,"L") or path(root.rig... | step-by-step-directions-from-a-binary-tree-node-to-another | Simple Python solution.(with comments) | NagatoBeast | 0 | 59 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,968 |
https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/discuss/1612246/easy-to-understand%3A-bfs-twice | class Solution:
def getDirections(self, root: Optional[TreeNode], startValue: int, destValue: int) -> str:
graph = defaultdict(set) # from.val -> (dest_node, 'L')
que = deque([root])
while que:
size = len(que)
for _ in range(size):
node = que... | step-by-step-directions-from-a-binary-tree-node-to-another | easy to understand: bfs twice | mtx2d | 0 | 61 | step by step directions from a binary tree node to another | 2,096 | 0.488 | Medium | 28,969 |
https://leetcode.com/problems/valid-arrangement-of-pairs/discuss/1612010/Python3-Hierholzer's-algo | class Solution:
def validArrangement(self, pairs: List[List[int]]) -> List[List[int]]:
graph = defaultdict(list)
degree = defaultdict(int) # net out degree
for x, y in pairs:
graph[x].append(y)
degree[x] += 1
degree[y] -= 1
for k... | valid-arrangement-of-pairs | [Python3] Hierholzer's algo | ye15 | 32 | 1,400 | valid arrangement of pairs | 2,097 | 0.41 | Hard | 28,970 |
https://leetcode.com/problems/valid-arrangement-of-pairs/discuss/1612010/Python3-Hierholzer's-algo | class Solution:
def validArrangement(self, pairs: List[List[int]]) -> List[List[int]]:
graph = defaultdict(list)
degree = defaultdict(int) # net out degree
for x, y in pairs:
graph[x].append(y)
degree[x] += 1
degree[y] -= 1
for k... | valid-arrangement-of-pairs | [Python3] Hierholzer's algo | ye15 | 32 | 1,400 | valid arrangement of pairs | 2,097 | 0.41 | Hard | 28,971 |
https://leetcode.com/problems/valid-arrangement-of-pairs/discuss/1616288/Python-O(V%2BE)-by-Euler-path-w-Visualization | class Solution:
def validArrangement(self, pairs: List[List[int]]) -> List[List[int]]:
# in degree table for each node
in_degree = defaultdict(int)
# out degree table for each node
out_degree = defaultdict(int)
# adjacency matrix for each node
... | valid-arrangement-of-pairs | Python O(V+E) by Euler path [w/ Visualization] | brianchiang_tw | 1 | 116 | valid arrangement of pairs | 2,097 | 0.41 | Hard | 28,972 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1705383/Python-Simple-Solution-or-100-Time | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
tuple_heap = [] # Stores (value, index) as min heap
for i, val in enumerate(nums):
if len(tuple_heap) == k:
heappushpop(tuple_heap, (val, i)) # To prevent size of heap growing larger than k
... | find-subsequence-of-length-k-with-the-largest-sum | Python Simple Solution | 100% Time | anCoderr | 10 | 806 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,973 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1635474/Python-optimized-solution-with-heap | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
import heapq
h = []
n = len(nums)
for i in range(n):
heapq.heappush(h, (-nums[i], i))
res = []
for _ in range(k):
v, idx = heapq.heappop(h)
... | find-subsequence-of-length-k-with-the-largest-sum | Python optimized solution with heap | byuns9334 | 2 | 263 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,974 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1626859/Easy-python3-solution | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
valindex=sorted([(num,i) for i,num in enumerate(nums)],reverse=True)
return [num for num,i in sorted(valindex[:k],key=lambda x:x[1])] | find-subsequence-of-length-k-with-the-largest-sum | Easy python3 solution | Karna61814 | 2 | 157 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,975 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/2779624/python-heap | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
min_heap = []
for i, n in enumerate(nums):
heappush(min_heap, (n, i))
if len(min_heap) > k:
heappop(min_heap)
min_heap.sort(key = lambda x: x[1])
return [i[0] for i... | find-subsequence-of-length-k-with-the-largest-sum | python heap | JasonDecode | 1 | 22 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,976 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/2390383/Python-all-solutions-using-sort-and-heap-or-sort-or-heap-or-one-liner | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
arr = [(nums[i], i) for i in range(len(nums))]
arr.sort(reverse=True)
arr = arr[:k]
arr.sort(key=lambda k: k[1])
return [val[0] for val in arr] | find-subsequence-of-length-k-with-the-largest-sum | Python all solutions using sort and heap | sort | heap | one-liner | wilspi | 1 | 155 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,977 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/2390383/Python-all-solutions-using-sort-and-heap-or-sort-or-heap-or-one-liner | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
return [
nums[x]
for x in sorted(
sorted(range(len(nums)), key=lambda k: nums[k], reverse=True)[:k]
)
] | find-subsequence-of-length-k-with-the-largest-sum | Python all solutions using sort and heap | sort | heap | one-liner | wilspi | 1 | 155 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,978 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/2390383/Python-all-solutions-using-sort-and-heap-or-sort-or-heap-or-one-liner | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
arr = [(-nums[i], i) for i in range(len(nums))]
heapq.heapify(arr)
ans = []
while k > 0:
ans.append(heapq.heappop(arr))
k -= 1
ans.sort(key=lambda k: k[1])
return... | find-subsequence-of-length-k-with-the-largest-sum | Python all solutions using sort and heap | sort | heap | one-liner | wilspi | 1 | 155 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,979 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1732750/Python-3-using-Heap | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
nums = [[-nums[i],i] for i in range(len(nums))]
heapq.heapify(nums)
ans,fin = [],[]
for i in range(k):
ans.append(heapq.heappop(nums)[::-1])
heapq.heapify(ans)
for i in range(l... | find-subsequence-of-length-k-with-the-largest-sum | Python 3 using Heap | akshaiy_14 | 1 | 161 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,980 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1624081/Intuitive-approach-by-two-sorting. | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
# transform nums into nums_with_index and sort it according to value.
# decreasingly
# . e.g.:
# (3, 1, 2) -> ((0, 3), (1, 1), (2, 2))
# -> ((0, 3), (2, 2), (1, 1))
nums_with... | find-subsequence-of-length-k-with-the-largest-sum | Intuitive approach by two sorting. | puremonkey2001 | 1 | 82 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,981 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1623315/Python3-O(N)-quick-select | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
temp = nums[:]
shuffle(temp)
def part(lo, hi):
"""Return partition of nums[lo:hi]."""
i, j = lo+1, hi-1
while i <= j:
if temp[i] < temp[lo]: i += 1
... | find-subsequence-of-length-k-with-the-largest-sum | [Python3] O(N) quick select | ye15 | 1 | 242 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,982 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/2847367/Python3-solution | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
if (len(nums) == k):
return nums
for i in range(len(nums) - k):
nums.remove(min(nums))
return nums | find-subsequence-of-length-k-with-the-largest-sum | Python3 solution | SupriyaArali | 0 | 1 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,983 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/2712078/python-99-faster-2-lines-of-code-easy | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
for i in range(len(nums)-k):
nums.remove(min(nums))
return nums | find-subsequence-of-length-k-with-the-largest-sum | python 99% faster 2 lines of code easy | Raghunath_Reddy | 0 | 14 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,984 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/2669224/Easy-python-solution-using-sorting | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
a=Counter(sorted(nums,reverse=True)[:k])
ans=[]
for i in nums:
if i in a:
ans.append(i)
a[i]-=1
if a[i]==0:
del a[i]
return ... | find-subsequence-of-length-k-with-the-largest-sum | Easy python solution using sorting | prateek4463 | 0 | 5 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,985 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/2363395/Easy-python-heap | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
val_idx_heap = []
for i,val in enumerate(nums):
if len(val_idx_heap)==k:
heappushpop(val_idx_heap,(val,i))
else:
heappush(val_idx_heap,(val,i))
val_idx_heap... | find-subsequence-of-length-k-with-the-largest-sum | Easy python heap | sunakshi132 | 0 | 68 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,986 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/2254289/A-hidden-power-of-sort()-faster-than-99.91 | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
val_n_index = [(i, n) for i, n in enumerate(nums)]
val_n_index.sort(reverse=True, key=lambda t: t[1])
val_n_index = val_n_index[:k]
val_n_index.sort(key=lambda t: t[0])
return [i[1] for i in val_... | find-subsequence-of-length-k-with-the-largest-sum | A hidden power of sort(π΄), faster than 99.91% | amaargiru | 0 | 112 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,987 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/2223187/Python-Solution | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
nums2=sorted(nums,reverse=True)[:k]
li=[]
for i in nums:
if i in nums2:
li.append(i)
nums2.remove(i)
return li | find-subsequence-of-length-k-with-the-largest-sum | Python Solution | SakshiMore22 | 0 | 76 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,988 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/2134162/Memory-Usage%3A-14.1-MB-less-than-95.39-of-Python3 | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
res, max_k = [], sorted(nums, reverse=True)[:k]
for num in nums:
if num in max_k:
res.append(num)
max_k.remove(num)
if len(max_k) == 0:
... | find-subsequence-of-length-k-with-the-largest-sum | Memory Usage: 14.1 MB, less than 95.39% of Python3 | writemeom | 0 | 101 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,989 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/2047183/Python-Solution | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
arr = sorted(nums)
for i in range(len(nums) - k):
nums.remove(arr[i])
return nums | find-subsequence-of-length-k-with-the-largest-sum | Python Solution | hgalytoby | 0 | 139 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,990 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1988282/Python-3-line-Easy-Solution-with-Sorting-beats-98 | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
# sort by value in descending order, keep track of original indices
nums = sorted(enumerate(nums), key=lambda x: x[1], reverse=True)
# sort k large numbers by original indices (as it's supposed to be a sequence)
s... | find-subsequence-of-length-k-with-the-largest-sum | Python 3-line Easy Solution with Sorting [beats 98%] | back2square1 | 0 | 79 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,991 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1969139/Python-Easy-Soluction-or-Beats-99-submits | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
ans = sorted(nums)[-k:]
for n in nums :
if ans :
if n in ans :
ans.remove(n)
yield n
else :
break | find-subsequence-of-length-k-with-the-largest-sum | [ Python ] Easy Soluction | Beats 99% submits | crazypuppy | 0 | 124 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,992 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1947107/Python-Fast-and-Efficient-Solution-Using-Sorting-%2B-heapq | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
if len(nums) == k:
return nums
num_idx = []
for i, e in enumerate(nums):
num_idx.append((e, i))
heapq.heapify(num_idx)
res = heapq.nlargest(k, nu... | find-subsequence-of-length-k-with-the-largest-sum | Python Fast & Efficient Solution Using Sorting + heapq | Hejita | 0 | 69 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,993 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1810435/1-Line-Python-Solution-oror-82-Faster-oror-Memory-less-than-85 | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
return [val for idx,val in sorted(sorted(enumerate(nums), key=lambda x: -x[1])[:k], key=lambda x: x[0])] | find-subsequence-of-length-k-with-the-largest-sum | 1-Line Python Solution || 82% Faster || Memory less than 85% | Taha-C | 0 | 134 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,994 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1721023/Python3-accepted-solution | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
ans = []
li = sorted(nums)[len(nums)-k:]
for i in range(len(nums)):
if(nums[i] in li and ans.count(nums[i])<li.count(nums[i])):
ans.append(nums[i])
return ans | find-subsequence-of-length-k-with-the-largest-sum | Python3 accepted solution | sreeleetcode19 | 0 | 116 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,995 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1691009/Python-clean-noob-solution-with-99.9 | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
result = []
for i,j in enumerate(nums):
if len(result)==k:
heappushpop(result,(j,i))
else:
heappush(result,(j,i))
# print(result)
ans = []
r... | find-subsequence-of-length-k-with-the-largest-sum | Python clean noob solution with 99.9% | Brillianttyagi | 0 | 139 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,996 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1646945/Python-2-lines-not-very-clear-%3A) | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
A = sorted(enumerate(nums), key=lambda t: (-t[1], t[0]))
return [v for i, v in sorted(A[:k], key=lambda t: t[0])] | find-subsequence-of-length-k-with-the-largest-sum | Python, 2 lines, not very clear :) | emwalker | 0 | 86 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,997 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1641278/one-line-python | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
return next(zip(*sorted(sorted([(v, i) for i, v in enumerate(nums)], reverse=True)[:k], key=lambda x: x[1]))) | find-subsequence-of-length-k-with-the-largest-sum | one-line python | Hoke_luo | 0 | 103 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,998 |
https://leetcode.com/problems/find-subsequence-of-length-k-with-the-largest-sum/discuss/1627346/Two-times-sorting-100-speed | class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
return [n for n, i in sorted(sorted((n, i) for i, n in
enumerate(nums))[-k:],
key=lambda tpl: tpl[1])] | find-subsequence-of-length-k-with-the-largest-sum | Two times sorting, 100% speed | EvgenySH | 0 | 108 | find subsequence of length k with the largest sum | 2,099 | 0.425 | Easy | 28,999 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.