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/populating-next-right-pointers-in-each-node-ii/discuss/1661195/Python-Simple-O(1)-and-O(n)-space-iterative-BFS-explained
class Solution: def connect(self, root: 'Node') -> 'Node': if not root: return root queue = deque([root]) while queue: size = len(queue) # iterate over all the nodes # in the current level hence the # following for loop rather than a # continuous while loop. for i in range(size): node = queue.popleft() # if it's the last node in this level # it's next pointer should point to None # else, the next pointer would point to # the first element in the queue since # we're adding elements to the queue # in the required order (left -> right) if i == (size-1): node.next = None else: node.next = queue[0] if node.left: queue.append(node.left) if node.right: queue.append(node.right) return root
populating-next-right-pointers-in-each-node-ii
[Python] Simple O(1) and O(n) space iterative BFS explained
buccatini
2
109
populating next right pointers in each node ii
117
0.498
Medium
1,000
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/1661195/Python-Simple-O(1)-and-O(n)-space-iterative-BFS-explained
class Solution: def connect(self, root: 'Node') -> 'Node': if not root: return root dummy = Node(-1) tmp = dummy # pointer to the original # root since that's what we # need to return res = root # we're going to traverse using # root so loop until root is None while root: # do a level order traversal # here while manipulating the # required pointers while root: # at this point, if there's a left # child, set tmp.next (dummy.next) to # the first node in the next level # we can then move tmp fwd to this node if root.left: tmp.next = root.left tmp = tmp.next # now if there's a right child, # set tmp.next to it since tmp, after # the previous conditional is pointing # to the previous node in the current level if root.right: tmp.next = root.right tmp = tmp.next # now since we're updating our next pointer # for nodes, we can simply move to the next # node by moving to the next node root = root.next # now remember how dummy.next is still # pointing to the left child of the first node # in this current level which means it's the first # node of the next level, so we set root to this root = dummy.next # reset both tmp and dummy to how they were # before we starting the while loop to repeat # the whole flow we just discussed until # we've visited all the nodes and updated them tmp = dummy dummy.next = None return res
populating-next-right-pointers-in-each-node-ii
[Python] Simple O(1) and O(n) space iterative BFS explained
buccatini
2
109
populating next right pointers in each node ii
117
0.498
Medium
1,001
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2338058/Python-Simple-Iterative-3-Pointers-Space-O(1)-oror-Documented
class Solution: def connect(self, root: 'Node') -> 'Node': # it will contain the left most node of tree at the current level leftMost = root # start from the first top-left-most node of the tree while leftMost: cur = leftMost leftMost = None curLeftNode = None # update pointers for the current level of tree while cur: if cur.left: # set the left most node at the current level if not leftMost: leftMost = cur.left # update the next pointer to right side node if curLeftNode: curLeftNode.next = cur.left # update curLeftNode to next right side node curLeftNode = cur.left if cur.right: # set the left most node at the current level if not leftMost: leftMost = cur.right # update the next pointer to right side node if curLeftNode: curLeftNode.next = cur.right # update curLeftNode to next right side node curLeftNode = cur.right # move rightward at that level cur = cur.next return root
populating-next-right-pointers-in-each-node-ii
[Python] Simple Iterative 3-Pointers - Space O(1) || Documented
Buntynara
1
19
populating next right pointers in each node ii
117
0.498
Medium
1,002
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2033456/Python3-solution
class Solution: def connect(self, root: 'Node') -> 'Node': if not root: return root q = collections.deque([root]) while q: l = len(q) for i in range(l): c = q.popleft() if i < l-1: c.next = q[0] if c.left: q.append(c.left) if c.right: q.append(c.right) return root
populating-next-right-pointers-in-each-node-ii
Python3 solution
dalechoi
1
28
populating next right pointers in each node ii
117
0.498
Medium
1,003
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/726414/Python-Easy-understanding-solution-with-explanation
class Solution: def connect(self, root: 'Node') -> 'Node': if not root: return # use queue to store nodes in every level, if queue[0] is not the last node, then let it point to the next node in the rest queue q = collections.deque() q.append(root) while q: size = len(q) for i in range(size): node = q.popleft() if i < size - 1: node.next = q[0] if node.left: q.append(node.left) if node.right: q.append(node.right) return root
populating-next-right-pointers-in-each-node-ii
Python -- Easy understanding solution with explanation
nbismoi
1
203
populating next right pointers in each node ii
117
0.498
Medium
1,004
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/694591/Python3-9-line
class Solution: def connect(self, root: 'Node') -> 'Node': parent = root while parent: child = dummy = Node() while parent: if parent.left: child.next = child = parent.left if parent.right: child.next = child = parent.right parent = parent.next parent = dummy.next return root
populating-next-right-pointers-in-each-node-ii
[Python3] 9-line
ye15
1
78
populating next right pointers in each node ii
117
0.498
Medium
1,005
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/694591/Python3-9-line
class Solution: def connect(self, root: 'Node') -> 'Node': if root: queue = deque([root]) while queue: prev = None for _ in range(len(queue)): node = queue.popleft() node.next = prev prev = node if node.right: queue.append(node.right) if node.left: queue.append(node.left) return root
populating-next-right-pointers-in-each-node-ii
[Python3] 9-line
ye15
1
78
populating next right pointers in each node ii
117
0.498
Medium
1,006
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/694591/Python3-9-line
class Solution: def connect(self, root: 'Node') -> 'Node': head = root while head and head.left: node = head while node: node.left.next = node.right if node.next: node.right.next = node.next.left node = node.next head = head.left return root
populating-next-right-pointers-in-each-node-ii
[Python3] 9-line
ye15
1
78
populating next right pointers in each node ii
117
0.498
Medium
1,007
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2835778/Python-O(V-%2B-E-maybe)-solution-Accepted
class Solution: def connect(self, root: 'Node') -> 'Node': if not root: return None queue = collections.deque() queue.append(root) while queue: qsize = len(queue) for i in range(qsize): node = queue.popleft() if i != qsize - 1: node.next = queue[0] if node.left: queue.append(node.left) if node.right: queue.append(node.right) return root
populating-next-right-pointers-in-each-node-ii
Python O(V + E `maybe`) solution [Accepted]
lllchak
0
2
populating next right pointers in each node ii
117
0.498
Medium
1,008
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2794850/python-solution
class Solution: def connect(self, root: 'Node') -> 'Node': levels = [] def levelorder(node, level): if level >= len(levels): levels.append([]) if node: levels[level].append(node) if node.left: levelorder(node.left, level + 1) if node.right: levelorder(node.right, level + 1) if not root: return None levelorder(root, 0) for i in range(len(levels)): for j in range(len(levels[i]) - 1): levels[i][j].next = levels[i][j+1] return levels[0][0]
populating-next-right-pointers-in-each-node-ii
python solution
maomao1010
0
5
populating next right pointers in each node ii
117
0.498
Medium
1,009
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2769504/Easy-to-Understand-oror-Beginner's-Level-oror-Python
class Solution(object): def connect(self, root): if not root: return None q = deque() q.append(root) while q: p = q.popleft() for i in range(len(q)): p2 = q.popleft() p.next = p2 if p.left: q.append(p.left) if p.right: q.append(p.right) p = p2 p.next = None if p.left: q.append(p.left) if p.right: q.append(p.right) return root
populating-next-right-pointers-in-each-node-ii
✅Easy to Understand || Beginner's Level || Python
its_krish_here
0
3
populating next right pointers in each node ii
117
0.498
Medium
1,010
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2609286/Python-Solution-or-Recursion-or-Easy
class Solution: def connect(self, root: 'Node') -> 'Node': def populate(root, level): if root is None: return None if level in drr: drr[level].append(root) else: drr[level]=[root] populate(root.left, level+1) populate(root.right, level+1) return def connectRight(root, level): if root is None: return None if root.val==drr[level][0].val: if len(drr[level])>1: root.next=drr[level][1] drr[level].pop(0) connectRight(root.left, level+1) connectRight(root.right, level+1) return drr={} populate(root, 0) #store nodes level wise # print(drr) connectRight(root, 0) #point node to right node return root
populating-next-right-pointers-in-each-node-ii
Python Solution | Recursion | Easy
Siddharth_singh
0
24
populating next right pointers in each node ii
117
0.498
Medium
1,011
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2559401/Constant-space-with-out-stack-call
class Solution: def connect(self, root: 'Node') -> 'Node': cur = root while cur: temp = Node() start_of_level = temp while cur: if cur.left: temp.next = cur.left temp = temp.next if cur.right: temp.next = cur.right temp = temp.next cur = cur.next cur = start_of_level.next return root
populating-next-right-pointers-in-each-node-ii
Constant space with out stack call
imanhn
0
29
populating next right pointers in each node ii
117
0.498
Medium
1,012
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2426065/python-BFS-short-and-easy-solution
class Solution: def connect(self, root: 'Node') -> 'Node': if not root: return root qu=deque([root,None]) while len(qu)>1: r=len(qu)-1 for i in range(r): temp=qu.popleft() temp.next=qu[0] if temp.left: qu.append(temp.left) if temp.right: qu.append(temp.right) qu.append(qu.popleft()) return root
populating-next-right-pointers-in-each-node-ii
python BFS short and easy solution
benon
0
21
populating next right pointers in each node ii
117
0.498
Medium
1,013
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2340298/python-simple-dfs-solution
class Solution: def connect(self, root: 'Node') -> 'Node': d = defaultdict(list) stack = [(root, 0)] while stack: node, depth = stack.pop() if node: d[depth].append(node) if node.right: stack.append((node.right, depth + 1)) if node.left: stack.append((node.left, depth + 1)) for k in d: arr = d[k] for i in range(len(arr)-1): arr[i].next = arr[i+1] arr[-1].next = None return root
populating-next-right-pointers-in-each-node-ii
python simple dfs solution
byuns9334
0
64
populating next right pointers in each node ii
117
0.498
Medium
1,014
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2327073/O(n)timeBEATS-99.97-MEMORYSPEED-0ms-JULY-2022
class Solution: def connect(self, root: 'Node') -> 'Node': parents = [root] kids = [] prev = None while len(parents) > 0: p = parents.pop(0) if prev: prev.next = p prev = p if p: if p.left: kids.append(p.left) if p.right: kids.append(p.right) if len(parents) == 0: parents = kids kids = [] prev = None return root
populating-next-right-pointers-in-each-node-ii
O(n)time/BEATS 99.97% MEMORY/SPEED 0ms JULY 2022
cucerdariancatalin
0
133
populating next right pointers in each node ii
117
0.498
Medium
1,015
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2220169/Very-Easy-HashMap-Python-Solution-level-order-traversal
class Solution: def connect(self, root: 'Node') -> 'Node': d = {} def dfs(node, h): if not node: return if h in d: n1 = d[h].pop() n1.next = node d[h].append(node) else: d[h] = [node] dfs(node.left, h+1) dfs(node.right, h+1) # print(d) dfs(root, 0) return root
populating-next-right-pointers-in-each-node-ii
Very Easy HashMap Python Solution -- level order traversal
saqibmubarak
0
28
populating next right pointers in each node ii
117
0.498
Medium
1,016
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2042144/Python3-Solution-with-using-two-pointers
class Solution: def connect(self, root: 'Node') -> 'Node': if not root: return None dummy = Node() head = root while head: curr = head prev = dummy while curr: if curr.left: prev.next = curr.left prev = prev.next if curr.right: prev.next = curr.right prev = prev.next curr = curr.next head = dummy.next dummy.next = None return root
populating-next-right-pointers-in-each-node-ii
[Python3] Solution with using two-pointers
maosipov11
0
15
populating next right pointers in each node ii
117
0.498
Medium
1,017
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2041603/Python-or-BFS-no-recursion-or-Time-O(N)-or-Space-O(1)
class Solution: def connect(self, root: 'Node') -> 'Node': # Will be used to keep track of the head of the current level dummy = Node(-1) # Pointer to the head of the previous level prev_head = root while prev_head: # Pointer to the current node in the previous level prev_node = prev_head # Needs to be reset to avoid having leftovers from previous cycle dummy.next = None # Pointer to node on current level curr = dummy while prev_node: if prev_node.left: curr.next = prev_node.left curr = curr.next if prev_node.right: curr.next = prev_node.right curr = curr.next # go the next node in previous level prev_node = prev_node.next # Go to next level prev_head = dummy.next return root
populating-next-right-pointers-in-each-node-ii
Python | BFS no recursion | Time O(N) | Space O(1)
user3694B
0
37
populating next right pointers in each node ii
117
0.498
Medium
1,018
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2041473/Python-or-BFS-no-recursion-or-Time-O(N)-or-Space-O(L)
class Solution: def connect(self, root: 'Node') -> 'Node': if not root: return root queue = deque([(root, 0)]) prev_node, prev_level = None, -1 while queue: node, level = queue.pop() if prev_node and prev_level == level: prev_node.next = node if node.left: queue.appendleft((node.left, level + 1)) if node.right: queue.appendleft((node.right, level + 1)) prev_node, prev_level = node, level return root
populating-next-right-pointers-in-each-node-ii
Python | BFS no recursion | Time O(N) | Space O(L)
user3694B
0
10
populating next right pointers in each node ii
117
0.498
Medium
1,019
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2035804/Simple-Python-Solution-Level-Order-Traversal
class Solution(object): def connect(self, root): def findNext(curr): while curr.next: curr = curr.next if curr.left: return curr.left if curr.right: return curr.right return None if(root is None): return if root.left: if root.right: root.left.next = root.right else: root.left.next = findNext(root) if root.right: root.right.next = findNext(root) self.connect(root.right) self.connect(root.left) return root
populating-next-right-pointers-in-each-node-ii
Simple Python Solution Level Order Traversal
NathanPaceydev
0
29
populating next right pointers in each node ii
117
0.498
Medium
1,020
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2035555/Python-Recursive-solution!-10-lines-of-code
class Solution: def connect(self, root: 'Node') -> 'Node': arrayLevels={} def levels(node,level): if node: if level in arrayLevels: arrayLevels[level].next = node arrayLevels[level]= node levels(node.left,level+1) levels(node.right,level+1) levels(root,1) return root
populating-next-right-pointers-in-each-node-ii
[Python] Recursive solution! 10 lines of code
lipatino
0
33
populating next right pointers in each node ii
117
0.498
Medium
1,021
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2034927/Populating-Right-Pointer-in-Binary-Tree-oror-Level-Order-Traversal-oror-Easy-to-understand
class Solution: def connect(self, root: 'Node') -> 'Node': if root is None: return None ans =[] def kk(r,l,ans): if len(ans)<l: ans.append([r]) else: ans[l-1].append(r) if r.left is not None: kk(r.left,l+1,ans) if r.right is not None: kk(r.right,l+1,ans) kk(root,1,ans) for i in ans: for k in range(len(i)): if k==len(i)-1: i[k].next = None else: i[k].next = i[k+1] return root
populating-next-right-pointers-in-each-node-ii
Populating Right Pointer in Binary Tree || Level Order Traversal || Easy to understand
user8744WJ
0
14
populating next right pointers in each node ii
117
0.498
Medium
1,022
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2033579/Python3-or-BFS-or-Faster-than-97.7
class Solution: def connect(self, root: 'Node') -> 'Node': def bfs(nodes): if not nodes: return children = [] for i, n in enumerate(nodes): if n.left: children.append(n.left) if n.right: children.append(n.right) if i != len(nodes) - 1: n.next = nodes[i + 1] bfs(children) if root: bfs([root]) return root
populating-next-right-pointers-in-each-node-ii
Python3 | BFS | Faster than 97.7%
anels
0
9
populating next right pointers in each node ii
117
0.498
Medium
1,023
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2033374/Python-level-order-traversal
class Solution: def connect(self, root: 'Node') -> 'Node': if not root: return root q = deque([root]) while q: length = len(q) next_node = None for _ in range(length): node = q.popleft() node.next = next_node next_node = node if node.right: q.append(node.right) if node.left: q.append(node.left) return root
populating-next-right-pointers-in-each-node-ii
Python, level-order traversal
blue_sky5
0
13
populating next right pointers in each node ii
117
0.498
Medium
1,024
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/2033238/python3-bfs-simple
class Solution: def connect(self, root: 'Node') -> 'Node': if root is None: return None childs = [root] while childs: nchilds = [] prev = childs[0] for node in childs: if prev != node: prev.next = node prev = node if node.left: nchilds.append(node.left) if node.right: nchilds.append(node.right) childs = nchilds return root
populating-next-right-pointers-in-each-node-ii
python3 bfs simple
user2613C
0
15
populating next right pointers in each node ii
117
0.498
Medium
1,025
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/1534231/Python-or-BFS-with-deque
class Solution: def connect(self, root: 'Node') -> 'Node': queue = deque([root] if root else []) while queue: next_ = deque() for _ in range(len(queue)): front = queue.popleft() front.next = queue[0] if queue else None if front.left: next_.append(front.left) if front.right: next_.append(front.right) queue = next_ return root
populating-next-right-pointers-in-each-node-ii
Python | BFS with deque
leeteatsleep
0
51
populating next right pointers in each node ii
117
0.498
Medium
1,026
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/1467353/O(1)-space-O(n)-time-iterative-python
class Solution: def connect(self, root: 'Node') -> 'Node': def helper(head): leftmost = head while leftmost: prev = None root = leftmost leftmost = None while root: if root.left: if prev: prev.next = root.left prev = root.left if not leftmost: leftmost = root.left if root.right: if prev: prev.next = root.right prev = root.right if not leftmost: leftmost = root.right root = root.next helper(root) return root
populating-next-right-pointers-in-each-node-ii
O(1) space O(n) time , iterative python
deleted_user
0
57
populating next right pointers in each node ii
117
0.498
Medium
1,027
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/1448480/Row-by-row-75-speed
class Solution: def connect(self, root: 'Node') -> 'Node': if not root: return root row = [root] while row: new_row = [] if len(row) > 1: for a, b in zip(row, row[1:]): a.next = b for node in row: if node.left: new_row.append(node.left) if node.right: new_row.append(node.right) row = new_row return root
populating-next-right-pointers-in-each-node-ii
Row by row, 75% speed
EvgenySH
0
66
populating next right pointers in each node ii
117
0.498
Medium
1,028
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/1098014/Python-91-Time-and-97-Space-solution.-Simple-modification-of-BFS
class Solution: def connect(self, root: 'Node') -> 'Node': if not root: return root root.next = None q = [root] sub_q = [] while q: el = q.pop(0) if el.left: sub_q.append(el.left) if el.right: sub_q.append(el.right) if not q: ss = len(sub_q) for i in range (ss-1): sub_q[i].next = sub_q[i+1] if sub_q: sub_q[ss-1].next = None q = sub_q sub_q = [] return root
populating-next-right-pointers-in-each-node-ii
Python 91% Time and 97% Space solution. Simple modification of BFS
rooky1905
0
147
populating next right pointers in each node ii
117
0.498
Medium
1,029
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/712047/Python-BFS-solution-easy-to-understand-with-a-dummy-head
class ListNode: def __init__(self, x): self.val = x self.next = None def list_to_link(l): cur = dummy = ListNode(0) for e in l: cur.next = ListNode(e) cur = cur.next return dummy.next
populating-next-right-pointers-in-each-node-ii
Python BFS solution easy to understand with a dummy head
swwl1992
0
131
populating next right pointers in each node ii
117
0.498
Medium
1,030
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/254967/Python-Easy-to-Understand
class Solution: def connect(self, root: 'Node') -> 'Node': pre = dummy = TreeLinkNode(0) node = root while root: if root.left: pre.next = root.left pre = pre.next if root.right: pre.next = root.right pre = pre.next root = root.next if not root: pre = dummy root = dummy.next dummy.next = None return node
populating-next-right-pointers-in-each-node-ii
Python Easy to Understand
ccparamecium
0
272
populating next right pointers in each node ii
117
0.498
Medium
1,031
https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/discuss/812130/Python-easy-to-understand
class Solution: def connect(self, root: 'Node') -> 'Node': if root == None: return None q = collections.deque([root]) while len(q) > 0: size = len(q) prev = None for _ in range(size): node = q.popleft() if prev: prev.next = node if node.left: q.append(node.left) if node.right: q.append(node.right) prev = node prev.next = None return root
populating-next-right-pointers-in-each-node-ii
Python easy to understand
wakanapo
-2
145
populating next right pointers in each node ii
117
0.498
Medium
1,032
https://leetcode.com/problems/pascals-triangle/discuss/1490520/Python3-easy-code-faster-than-96.67
class Solution: def generate(self, numRows: int) -> List[List[int]]: l=[0]*numRows for i in range(numRows): l[i]=[0]*(i+1) l[i][0]=1 l[i][i]=1 for j in range(1,i): l[i][j]=l[i-1][j-1]+l[i-1][j] return l
pascals-triangle
Python3 easy code- faster than 96.67%
Rosh_65
44
3,600
pascals triangle
118
0.694
Easy
1,033
https://leetcode.com/problems/pascals-triangle/discuss/2490404/Easy-oror-0-ms-oror-100-oror-Fully-Explained-oror-Java-C%2B%2B-Python-JavaScript-Python3-oror-DP
class Solution(object): def generate(self, numRows): # Create an array list to store the output result... output = [] for i in range(numRows): if(i == 0): # Create a list to store the prev triangle value for further addition... # Inserting for the first row &amp; store the prev array to the output array... prev = [1] output.append(prev) else: curr = [1] j = 1 # Calculate for each of the next values... while(j < i): # Inserting the addition of the prev arry two values... curr.append(prev[j-1] + prev[j]) j+=1 # Store the number 1... curr.append(1) # Store the value in the Output array... output.append(curr) # Set prev is equal to curr... prev = curr return output # Return the output list of pascal values...
pascals-triangle
Easy || 0 ms || 100% || Fully Explained || Java, C++, Python, JavaScript, Python3 || DP
PratikSen07
27
1,800
pascals triangle
118
0.694
Easy
1,034
https://leetcode.com/problems/pascals-triangle/discuss/2040184/O(n)timeBEATS-99.97-MEMORYSPEED-0ms-MAY-2022
class Solution: def generate(self, numRows: int) -> List[List[int]]: ans = [None] * numRows for i in range(numRows): row, mid = [1] * (i + 1), (i >> 1) + 1 for j in range(1, mid): val = ans[i-1][j-1] + ans[i-1][j] row[j], row[len(row)-j-1] = val, val ans[i] = row return ans
pascals-triangle
O(n)time/BEATS 99.97% MEMORY/SPEED 0ms MAY 2022
cucerdariancatalin
20
2,300
pascals triangle
118
0.694
Easy
1,035
https://leetcode.com/problems/pascals-triangle/discuss/1287157/Well-Commented-Python-Solution
class Solution: def generate(self, numRows: int) -> List[List[int]]: triangle = [] #initiating the triangle which we will output for row_num in range(numRows): # for loop for adding rows to the final triangle row = [None for _ in range(row_num + 1)] #intiating each row with None, notice the no. of elements in the row row[0], row[-1] = 1, 1 #first and last element of each row is 1 for j in range(1, row_num): # nested for loop to fill up each row row[j] = triangle[row_num - 1][j-1] + triangle[row_num - 1][j] #as seen in question animation, each element is the sum of the elements directly above it triangle.append(row) #After filling up the row - append it to the main traingle return triangle #return the triangle
pascals-triangle
Well Commented Python Solution
yasirm
12
1,000
pascals triangle
118
0.694
Easy
1,036
https://leetcode.com/problems/pascals-triangle/discuss/2661799/Python-Simple-With-Comments
class Solution: def generate(self, numRows: int) -> List[List[int]]: a = [] for x in range(numRows): a.append([1]) # creating the 2d array # appending the starting 1: [1, ...] if x > 1: # skipping the first two rows for c in range(x-1): # for loop used to find info about previous row a[x].append(a[x-1][c]+a[x-1][c+1]) # math for adding the c and the c+1 number from the previous row if x > 0: # checking if its not the first row a[x].append(1) # appending the ending 1: [1, ..., 1] return a
pascals-triangle
Python Simple - With Comments
omkarxpatel
11
1,300
pascals triangle
118
0.694
Easy
1,037
https://leetcode.com/problems/pascals-triangle/discuss/912002/python3-just-1-line-using-Binomial-Theorem
class Solution: def generate(self, numRows: int) -> List[List[int]]: return [[comb(i,j) for j in range(i+1)] for i in range(numRows)]
pascals-triangle
python3 just 1 line using Binomial Theorem
dqdwsdlws
8
411
pascals triangle
118
0.694
Easy
1,038
https://leetcode.com/problems/pascals-triangle/discuss/2319783/PYTHON-99.95-or-97.62-or-Comments
class Solution: def generate(self, numRows: int) -> List[List[int]]: a = [] for x in range(numRows): a.append([1]) # creating the 2d array # appending the starting 1: [1, ...] if x > 1: # skipping the first two rows for c in range(x-1): # for loop used to find info about previous row a[x].append(a[x-1][c]+a[x-1][c+1]) # math for adding the c and the c+1 number from the previous row if x > 0: # checking if its not the first row a[x].append(1) # appending the ending 1: [1, ..., 1] return a
pascals-triangle
[PYTHON] 99.95% | 97.62% | Comments
omkarxpatel
7
207
pascals triangle
118
0.694
Easy
1,039
https://leetcode.com/problems/pascals-triangle/discuss/1742176/Python-3-(40ms)-or-Iterative-Easy-Approach
class Solution: def generate(self, numRows: int) -> List[List[int]]: res=[] for i in range(numRows): res.append([]) for j in range(i+1): if j == 0 or j == i: res[i].append(1) else: res[i].append(res[i - 1][j - 1] + res[i - 1][j]) return res
pascals-triangle
Python 3 (40ms) | Iterative Easy Approach
MrShobhit
6
471
pascals triangle
118
0.694
Easy
1,040
https://leetcode.com/problems/pascals-triangle/discuss/2301780/Python3-oror-85.23-up-efficiency-will-come...
class Solution: def generate(self, numRows: int) -> List[List[int]]: dp = [[1]] for r in range(numRows-1): newDp = [1] for c in range(1, r+1): newDp.append(dp[r][c-1]+dp[r][c]) newDp.append(1) dp.append(newDp) return dp
pascals-triangle
Python3 || 85.23% up efficiency will come...
sagarhasan273
4
854
pascals triangle
118
0.694
Easy
1,041
https://leetcode.com/problems/pascals-triangle/discuss/2119175/Python3-6-liner-easy-to-read-solution-99-memory
class Solution: def generate(self, numRows: int) -> List[List[int]]: rows = [[1]] for n in range(1, numRows): last = [0, *rows[n-1], 0] row = [last[i-1]+last[i] for i in range(1, len(last))] rows.append(row) return rows
pascals-triangle
Python3 6 liner easy to read solution - 99% memory
zhenyulin
4
163
pascals triangle
118
0.694
Easy
1,042
https://leetcode.com/problems/pascals-triangle/discuss/1467410/Easy-Python-Solution
class Solution: def generate(self, numRows: int) -> List[List[int]]: # first lets generate an empty triangle pascal = [[1]*i for i in range(1, numRows+1)] # the trick here is that each cell/element is the sum of cell/element in same column of the previous row and previous # column of the previous row for i in range(2, numRows): for j in range(1,i): pascal[i][j] = pascal[i-1][j] + pascal[i-1][j-1] return pascal
pascals-triangle
Easy Python Solution
ajinkya2021
4
293
pascals triangle
118
0.694
Easy
1,043
https://leetcode.com/problems/pascals-triangle/discuss/1369526/WEEB-DOES-PYTHON
class Solution: def generate(self, numRows: int) -> List[List[int]]: pascal, prev = [], [] for i in range(1,numRows+1): row = [1] * i if len(row) > 2: for j in range(len(prev)-1): row[j+1] = prev[j] + prev[j+1] prev = row pascal.append(row) return pascal
pascals-triangle
WEEB DOES PYTHON
Skywalker5423
4
283
pascals triangle
118
0.694
Easy
1,044
https://leetcode.com/problems/pascals-triangle/discuss/2312936/Python3-oror-97.08-Time-Efficient
class Solution: def generate(self, numRows: int) -> List[List[int]]: l=[[1]*(i+1) for i in range(numRows)] if(numRows==1): l[0]=[1] else: l[0]=[1] l[1]=[1,1] for i in range(2,len(l)): for j in range(0,len(l[i])): if(j==0 or j==len(l[i])+1): continue else: l[i][j]=sum(l[i-1][j-1:j+1]) return l
pascals-triangle
Python3 || 97.08% Time Efficient
KaushikDhola
3
236
pascals triangle
118
0.694
Easy
1,045
https://leetcode.com/problems/pascals-triangle/discuss/2309159/PYTHON-FAST-or-LOW-MEMORY-or-EASY
class Solution: def generate(self, numRows: int) -> List[List[int]]: a = [] for x in range(numRows): a.append([1]) # creating the 2d array # appending the starting 1: [1, ...] if x > 1: # skipping the first two rows for c in range(x-1): # for loop used to find info about previous row a[x].append(a[x-1][c]+a[x-1][c+1]) # math for adding the c and the c+1 number from the previous row if x > 0: # checking if its not the first row a[x].append(1) # appending the ending 1: [1, ..., 1] return a
pascals-triangle
[PYTHON] FAST | LOW MEMORY | EASY
omkarxpatel
3
33
pascals triangle
118
0.694
Easy
1,046
https://leetcode.com/problems/pascals-triangle/discuss/1787872/Python-Simple-Python-Solution-oror-Runtime%3A-24-ms-faster-than-98.24-of-Python3
class Solution: def generate(self, numRows: int) -> List[List[int]]: result = [[1],[1,1]] for i in range(2,numRows): new_row = [1] last_row = result[-1] for j in range(len(last_row)-1): new_row.append(last_row[j]+last_row[j+1]) new_row.append(1) result.append(new_row) return result[:numRows]
pascals-triangle
[ Python ] ✅✅ Simple Python Solution || Runtime: 24 ms, faster than 98.24% of Python3 🥳👍🔥✌
ASHOK_KUMAR_MEGHVANSHI
3
322
pascals triangle
118
0.694
Easy
1,047
https://leetcode.com/problems/pascals-triangle/discuss/1288946/Python-Solution
class Solution: def generate(self, numRows: int) -> List[List[int]]: if numRows == 1: return [[1]] rows = [[1], [1, 1]] for i in range(2, numRows): row = [1] for j in range(1, i): row.append(rows[-1][j] + rows[-1][j - 1]) row.append(1) rows.append(row) return rows
pascals-triangle
Python Solution
mariandanaila01
3
470
pascals triangle
118
0.694
Easy
1,048
https://leetcode.com/problems/pascals-triangle/discuss/1276574/Generate-Parentheses-ororor-Python-Solution-based-on-backtracking
class Solution: def generateParenthesis(self, n: int) -> List[str]: if not n: return [] ans=[] self.backtrack(n,0,0,"",ans) return ans def backtrack(self,n,openb,closeb,curr_set,ans): if len(curr_set)==2*n: ans.append(curr_set) return if openb<n: self.backtrack(n,openb+1,closeb,curr_set+"(",ans) if closeb<openb: self.backtrack(n,openb,closeb+1,curr_set+")",ans)
pascals-triangle
Generate Parentheses ||| Python Solution based on backtracking
jaipoo
3
208
pascals triangle
118
0.694
Easy
1,049
https://leetcode.com/problems/pascals-triangle/discuss/1132468/Python3-simple-solution
class Solution: def generate(self, numRows: int) -> List[List[int]]: l = [[1],[1,1]] if numRows <= 2: return l[:numRows] i = 3 while i <= numRows: x = l[i-2] y = [1] for j in range(len(x)-1): y.append(x[j] + x[j+1]) y.append(1) l.append(y) i += 1 return l
pascals-triangle
Python3 simple solution
EklavyaJoshi
3
201
pascals triangle
118
0.694
Easy
1,050
https://leetcode.com/problems/pascals-triangle/discuss/2592747/Python3-easy-solution-with-explanation
class Solution: def generate(self, numRows: int) -> List[List[int]]: res = [[1]] for i in range(numRows - 1): temp = [0] + res[-1] + [0] row = [] for j in range(len(res[-1])+1): row.append(temp[j] + temp[j+1]) res.append(row) return res
pascals-triangle
Python3 easy solution with explanation
khushie45
2
313
pascals triangle
118
0.694
Easy
1,051
https://leetcode.com/problems/pascals-triangle/discuss/2367464/O(n)timeBEATS-99.97-MEMORYSPEED-0ms-AUG-2022
class Solution: def generate(self, numRows: int) -> List[List[int]]: ans = [None] * numRows for i in range(numRows): row, mid = [1] * (i + 1), (i >> 1) + 1 for j in range(1, mid): val = ans[i-1][j-1] + ans[i-1][j] row[j], row[len(row)-j-1] = val, val ans[i] = row return ans
pascals-triangle
O(n)time/BEATS 99.97% MEMORY/SPEED 0ms AUG 2022
cucerdariancatalin
2
416
pascals triangle
118
0.694
Easy
1,052
https://leetcode.com/problems/pascals-triangle/discuss/2303775/Python-or-faster-than-100-or-Memory-less-than-98-or-Begginer-Friendly-or-Easy-Understanding
class Solution: def generate(self, numRows: int) -> List[List[int]]: pascal_triangle = [[1 for _ in range(row + 1)] for row in range(numRows)] for row in range(numRows): for col in range(1, row): pascal_triangle[row][col] = pascal_triangle[row - 1][col] + pascal_triangle[row - 1][col - 1] return pascal_triangle
pascals-triangle
Python | faster than 100% | Memory less than 98% | Begginer-Friendly | Easy-Understanding
Saiko15
2
166
pascals triangle
118
0.694
Easy
1,053
https://leetcode.com/problems/pascals-triangle/discuss/2071093/Python-Simple-concise-solution-(beats-99)
class Solution: def generate(self, numRows: int) -> List[List[int]]: result = [[1 for j in range(0, i)] for i in range(1, numRows + 1)] for i in range(2, numRows): for j in range(1, i): result[i][j] = result[i - 1][j - 1] + result[i - 1][j] return result
pascals-triangle
[Python] Simple, concise solution (beats 99%)
FedMartinez
2
160
pascals triangle
118
0.694
Easy
1,054
https://leetcode.com/problems/pascals-triangle/discuss/1984935/Python3-Solution-for-Beginners-Simple-Solution-O(n2)
class Solution: def generate(self, numRows: int) -> List[List[int]]: ans = [[1], [1,1]] if numRows == 1: return [[1]] elif numRows == 2: return ans for i in range(2, numRows): row = [] for j in range(i+1): if j == 0 or j == i: row.append(1) else: ele = ans[i-1][j-1] + ans[i-1][j] row.append(ele) ans.append(row) return ans
pascals-triangle
Python3 Solution for Beginners Simple Solution O(n^2)
alreadyselfish
2
144
pascals triangle
118
0.694
Easy
1,055
https://leetcode.com/problems/pascals-triangle/discuss/1462883/Python-Clean-DP
class Solution: def generate(self, numRows: int) -> List[List[int]]: triangle = [[1]*i for i in range(1, numRows+1)] for r in range(2, numRows): for c in range(1, r): triangle[r][c] = triangle[r-1][c-1] + triangle[r-1][c] return triangle
pascals-triangle
[Python] Clean DP
soma28
2
155
pascals triangle
118
0.694
Easy
1,056
https://leetcode.com/problems/pascals-triangle/discuss/1041769/Python-faster-than-95
class Solution: def generate(self, numRows: int) -> List[List[int]]: if numRows == 0: return [] triangle = [[1]] for i in range(1, numRows): prev = triangle[i - 1] current = [1] for i in range(1, len(prev)): current.append(prev[i] + prev[i-1]) current.append(1) triangle.append(current) return triangle
pascals-triangle
Python faster than 95%
borodayev
2
149
pascals triangle
118
0.694
Easy
1,057
https://leetcode.com/problems/pascals-triangle/discuss/467088/PythonJS-O(-n2-)-DP-w-Comment
class Solution: def generate(self, numRows: int) -> List[List[int]]: pascal_tri = [] # build each row for i in range( numRows ): cur_row = [ 1 ] * (i+1) # update each element on current row i for j in range( len(cur_row) ): if j != 0 and j != i: cur_row[j] = pascal_tri[i-1][j-1] + pascal_tri[i-1][j] pascal_tri.append( cur_row ) return pascal_tri
pascals-triangle
Python/JS O( n^2 ) DP [w/ Comment]
brianchiang_tw
2
200
pascals triangle
118
0.694
Easy
1,058
https://leetcode.com/problems/pascals-triangle/discuss/467088/PythonJS-O(-n2-)-DP-w-Comment
class Solution: def generate(self, numRows: int) -> List[List[int]]: def maker( level ): if level == 1: # base case: cur = [1] result.append(cur) return cur else: # general cases: prev = maker(level-1) cur = [1] + [ (prev[i] + prev[i+1]) for i in range(level-2) ] +[1] result.append(cur) return cur # ----------------------------------------------------------------------------------- result = [] maker(level=numRows) return result
pascals-triangle
Python/JS O( n^2 ) DP [w/ Comment]
brianchiang_tw
2
200
pascals triangle
118
0.694
Easy
1,059
https://leetcode.com/problems/pascals-triangle/discuss/467088/PythonJS-O(-n2-)-DP-w-Comment
class Solution: def generate(self, numRows: int) -> List[List[int]]: def dp( i ): if i == 1: ## Base case return [[1]] else: ## General cases: # Previous already built pascal triangle prev_tri = dp(i-1) last_row = prev_tri[-1] # Current row for row i cur_row = [1] + [last_row[k]+last_row[k+1] for k in range(i-2)] + [1] # New pascal triangle = Previous pascal triangle + current row return prev_tri + [cur_row] # --------------------------------------- return dp(numRows)
pascals-triangle
Python/JS O( n^2 ) DP [w/ Comment]
brianchiang_tw
2
200
pascals triangle
118
0.694
Easy
1,060
https://leetcode.com/problems/pascals-triangle/discuss/2783511/Python-Easy-Solution-with-Explanation
class Solution(object): def generate(self, numRows): """ :type numRows: int :rtype: List[List[int]] """ pascal = [[1]*(i+1) for i in range(numRows)] for i in range(2, numRows): for j in range(1,i): pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j] return pascal
pascals-triangle
Python Easy Solution with Explanation
the_coder5311
1
26
pascals triangle
118
0.694
Easy
1,061
https://leetcode.com/problems/pascals-triangle/discuss/2414631/python
class Solution: def generate(self, numRows: int) -> List[List[int]]: ans = [] for i in range(0 , numRows): inner = [] for j in range(0 , i + 1): if i == j or j == 0: inner.append(1) else: inner.append(ans[i-1][j] + ans[i-1][j-1]) ans.append(inner) return ans
pascals-triangle
python
akashp2001
1
90
pascals triangle
118
0.694
Easy
1,062
https://leetcode.com/problems/pascals-triangle/discuss/2410859/Simple-Solution-using-for-loop
class Solution: def generate(self, numRows: int) -> List[List[int]]: res = [[1]] for i in range(numRows-1): temp = [1] for j in range(1, i+1): temp.append(res[i][j-1] + res[i][j]) temp.append(1) res.append(temp) return res
pascals-triangle
Simple Solution using for loop
Abhi_-_-
1
92
pascals triangle
118
0.694
Easy
1,063
https://leetcode.com/problems/pascals-triangle/discuss/2301505/Python3-straightforward-solution
class Solution: def generate(self, numRows: int) -> List[List[int]]: result = [] for i in range(numRows): curr = [] for j in range(i+1): if j == 0 or j == i: curr.append(1) else: curr.append(result[i-1][j-1] + result[i-1][j]) result.append(curr) return result
pascals-triangle
📌 Python3 straightforward solution
Dark_wolf_jss
1
24
pascals triangle
118
0.694
Easy
1,064
https://leetcode.com/problems/pascals-triangle/discuss/2227364/python-solution-oror-easy-to-understand-oror-brute-force
class Solution: def generate(self, numRows: int) -> List[List[int]]: if numRows == 1: return [[1]] if numRows == 2: return [[1], [1,1]] ans = [[1], [1,1]] for n in range(2, numRows): ans.append([1]) for i in range(len(ans[-2]) - 1): ans[-1].append(ans[-2][i] + ans[-2][i+1]) ans[-1].append(1) return ans
pascals-triangle
python solution || easy to understand || brute force
lamricky11
1
82
pascals triangle
118
0.694
Easy
1,065
https://leetcode.com/problems/pascals-triangle/discuss/2162547/Simplest-Approach-oror-Faster-than-89-oror-TC-%3A-O(N2)-oror-SC-%3A-O(1)
class Solution: def generate(self, numRows: int) -> List[List[int]]: pascalTriangle = [] for i in range(numRows): currentRow = [1]*(i+1) if i > 1: for j in range(1,i): currentRow[j] = pascalTriangle[i-1][j] + pascalTriangle[i-1][j-1] pascalTriangle.append(currentRow) return pascalTriangle
pascals-triangle
Simplest Approach || Faster than 89% || TC :- O(N^2) || SC :- O(1)
Vaibhav7860
1
127
pascals triangle
118
0.694
Easy
1,066
https://leetcode.com/problems/pascals-triangle/discuss/2125638/Python-or-Neat-Solution-85-faster-than-other-submissions
class Solution: def generate(self, numRows: int) -> List[List[int]]: # creating a pascal triangle of required siz pstri = [[1] * r for r in range(1,numRows+1)] for i in range(2,numRows): for j in range(1,i): pstri[i][j] = pstri[i-1][j] + pstri[i-1][j-1] return pstri
pascals-triangle
Python | Neat Solution 85% faster than other submissions
__Asrar
1
129
pascals triangle
118
0.694
Easy
1,067
https://leetcode.com/problems/pascals-triangle/discuss/1909072/Eaisest-python-solution-with-intution-and-complexity-analysis
class Solution: def generate(self, numRows: int) -> List[List[int]]: ans=[] for i in range(numRows): ans.append([0]*(i+1)) for j in range(i+1): if j==0 or j==i: ans[i][j]=1 else: ans[i][j]=ans[i-1][j]+ans[i-1][j-1] return(ans)
pascals-triangle
Eaisest python solution with intution and complexity analysis
tkdhimanshusingh
1
102
pascals triangle
118
0.694
Easy
1,068
https://leetcode.com/problems/pascals-triangle/discuss/1896757/python3-easy-solution-for-beginners-with-lots-of-comments
class Solution: def generate(self, numRows: int) -> List[List[int]]: for i in range(1, numRows+1): if numRows == 1: #creating the first two rows of the triangle return [[1]] if numRows == 2: return [[1],[1,1]] sol = [[1],[1,1]] for i in range(1, numRows-1): #i references the previous row in the triangle my_list = [1] #creating a list for the row we are going to add for j in range(0, len(sol[i])): #j handles adding adjacent numbers in row i temp = [] #create a temp variable to add the adjacent numbers into if j+1 < len(sol[i]): #check to see we are not at the end of the row i temp.append(sol[i][j]) temp.append(sol[i][j+1]) Sum = sum(temp) #sum of the two adjacent numbers my_list.append(Sum) #add the sum to the list we are about to add to the triangl else: my_list.append(1) #add 1 to the end of the sol.append(my_list) #adding a new row into the triangle #print(sol) return sol #return the full triangle 2D list # Runtime: 72 ms, faster than 5.11% of Python3 online submissions for Pascal's Triangle. # Memory Usage: 13.8 MB, less than 97.72% of Python3 online submissions for Pascal's Triangle. # This is the brute force method but easiest way to do it for beginners. Good Luck!!!!
pascals-triangle
python3 easy solution for beginners with lots of comments
vnimmag2
1
87
pascals triangle
118
0.694
Easy
1,069
https://leetcode.com/problems/pascals-triangle/discuss/1877082/Python-or-Intuitive-and-Concise-code
class Solution: def generate(self, numRows: int) -> List[List[int]]: res = [[1]] for i in range(1,numRows): temp = [1] * (i+1) for j in range(1,len(temp)-1): temp[j] = res[-1][j-1] + res[-1][j] res.append(temp) return res
pascals-triangle
Python | Intuitive and Concise code
iamskd03
1
77
pascals triangle
118
0.694
Easy
1,070
https://leetcode.com/problems/pascals-triangle/discuss/1841174/Python-or-DP-Solution-or-Easy-Understanding
class Solution: def generate(self, numRows: int) -> List[List[int]]: dp = [[1] * i for i in range(1, numRows+1)] if numRows > 2: for i in range(2, numRows): n = len(dp[i]) for j in range(n): if j == 0 or j == n-1: continue dp[i][j] = dp[i-1][j] + dp[i-1][j-1] return dp
pascals-triangle
Python | DP Solution | Easy Understanding
Mikey98
1
202
pascals triangle
118
0.694
Easy
1,071
https://leetcode.com/problems/pascals-triangle/discuss/1784489/Can-anyone-explain-how-this-even-happened
class Solution: def generate(self, numRows: int) -> List[List[int]]: result = [[1]] past = [1] count = 1 while count != numRows: temp = past #temp is [1] temp.append(0) #temp is [1,0] pmet = temp[::-1] #pmet is [0,1] past = [] #clear the past save to make a new one for i in range(len(temp)): #combine the two list of the same len to make one that is also the same len past.append(temp[i] + pmet[i]) #adds [0,1] and [0,1] to make [1,1] result.append(past)#adds to results count=count+1 #loops back to while return result #Don't know why it adds a 0 for the lists in between the first and the last one (example:[[1],[1,1,0],[1,2,1,0],[1,3,3,1]])
pascals-triangle
Can anyone explain how this even happened?
fox-of-snow
1
45
pascals triangle
118
0.694
Easy
1,072
https://leetcode.com/problems/pascals-triangle/discuss/1706623/easy-Pythonic-solution
class Solution: def choose(self,n,r): return math.factorial(n)//(math.factorial(r)*math.factorial(n-r)) def generate(self, numRows: int) -> List[List[int]]: lst=[] for i in range(numRows): temp=[] for j in range(i+1): temp.append(self.choose(i,j)) lst.append(temp) return lst
pascals-triangle
easy Pythonic solution
amannarayansingh10
1
138
pascals triangle
118
0.694
Easy
1,073
https://leetcode.com/problems/pascals-triangle/discuss/1699657/Simple-dp-python-solution-97-faster
class Solution: def generate(self, numRows: int) -> List[List[int]]: l =[] for i in range(numRows): sub_l = [] for j in range(i+1): if j == 0 or j == i: sub_l.append(1) else: sub_l.append(l[i-1][j-1]+ l[i-1][j]) l.append(sub_l) return l
pascals-triangle
Simple dp python solution 97% faster
arjuhooda
1
115
pascals triangle
118
0.694
Easy
1,074
https://leetcode.com/problems/pascals-triangle/discuss/1619741/Python3-DP-or-Bottom-up-Approach-or-O(n2)-Time-or-O(n2)-Space
class Solution: def generate(self, numRows: int) -> List[List[int]]: res = [[1]] for _ in range(1, numRows): lastRow = res[-1] newRow = [1] for i in range(1, len(lastRow)): newRow.append(lastRow[i - 1] + lastRow[i]) newRow.append(1) res.append(newRow) return res
pascals-triangle
[Python3] DP | Bottom-up Approach | O(n^2) Time | O(n^2) Space
PatrickOweijane
1
140
pascals triangle
118
0.694
Easy
1,075
https://leetcode.com/problems/pascals-triangle/discuss/1453695/Python3C%2B%2B-Solution-with-Explanation
class Solution: def generate(self, numRows: int) -> List[List[int]]: ans = [[1]] for i in range(1,numRows): a = ans[-1] + [0] b = [0] + ans[-1] sum_ab = [x+y for (x,y) in zip(a,b)] ans.append(sum_ab) print(ans) return ans
pascals-triangle
[Python3/C++] Solution with Explanation
light_1
1
82
pascals triangle
118
0.694
Easy
1,076
https://leetcode.com/problems/pascals-triangle/discuss/1447506/Python-easy-dynamic-programming-solution
class Solution: def generate(self, numRows: int) -> List[List[int]]: if numRows == 1: return [[1]] if numRows == 2: return [[1], [1,1]] dp = [[1], [1,1]] for idx in range(2, numRows): curRow = [] for i in range(idx+1): if i == 0: curRow.append(1) elif i == idx: curRow.append(1) else: curRow.append(dp[idx-1][i-1] + dp[idx-1][i]) dp.append(curRow) return dp
pascals-triangle
Python easy dynamic programming solution
freetochoose
1
215
pascals triangle
118
0.694
Easy
1,077
https://leetcode.com/problems/pascals-triangle/discuss/1341932/python3-solution-sliding-window
class Solution: def generate(self, numRows: int) -> List[List[int]]: val = [[1],[1,1]] if numRows==2: return val if numRows==1: return [[1]] for i in range(numRows-2): num = [1] num.extend(self.sum_two(val[-1])) num.append(1) val.append(num) return val def sum_two(self,arr): i = 0 j= 0 k = 2 arr1 = [] while j<len(arr): if j-i+1<k: j+=1 elif j-i+1==k: arr1.append(sum(arr[i:j+1])) i+=1 j+=1 return arr1
pascals-triangle
python3 solution, sliding window
pheraram
1
73
pascals triangle
118
0.694
Easy
1,078
https://leetcode.com/problems/pascals-triangle/discuss/1287685/Python-oror-Simple-level-by-level-iterative-approach
class Solution: def generate(self, numRows: int) -> List[List[int]]: if numRows == 1: return [[1]] pasc_tri = [[1], [1,1]] while numRows-2 > 0: last = pasc_tri[-1] res = [] for i in range(len(last)-1): res.append(last[i] + last[i+1]) pasc_tri.append([1] + res + [1]) numRows -= 1 return pasc_tri
pascals-triangle
Python || Simple level by level iterative approach
airksh
1
25
pascals triangle
118
0.694
Easy
1,079
https://leetcode.com/problems/pascals-triangle/discuss/693045/Python3-dp
class Solution: def generate(self, numRows: int) -> List[List[int]]: ans, row = [], [] for i in range(numRows): row.append(1) for j in reversed(range(1, i)): row[j] += row[j-1] ans.append(row.copy()) return ans
pascals-triangle
[Python3] dp
ye15
1
134
pascals triangle
118
0.694
Easy
1,080
https://leetcode.com/problems/pascals-triangle/discuss/537268/Simple-Python-3-beats-97
class Solution: def generate(self, numRows: int) -> List[List[int]]: res = [] for ri in range(numRows): if ri == 0: res.append([1]) else: row = [1] for i in range(1, len(res[ri-1])): row.append(res[ri-1][i-1] + res[ri-1][i]) res.append(row + [1]) return res
pascals-triangle
Simple Python 3, beats 97%
kstanski
1
308
pascals triangle
118
0.694
Easy
1,081
https://leetcode.com/problems/pascals-triangle/discuss/332153/Two-Short-Solutions-in-Python-3-(DP-and-Factorial)-(beats-~99)
class Solution: def generate(self, N: int) -> List[List[int]]: A = [[1]]*min(N,1)+[[1]+[0]*(i-2)+[1] for i in range(2,N+1)] for i in range(2,N): for j in range(1,i): A[i][j] = A[i-1][j-1] + A[i-1][j] return A
pascals-triangle
Two Short Solutions in Python 3 (DP and Factorial) (beats ~99%)
junaidmansuri
1
314
pascals triangle
118
0.694
Easy
1,082
https://leetcode.com/problems/pascals-triangle/discuss/291129/Runtime%3A-8-ms-faster-than-99.95-of-Python-online-submissions-for-Pascal's-Triangle.
class Solution(object): def generate(self, numRows): """ :type numRows: int :rtype: List[List[int]] """ response = [[1]*i for i in range(1, numRows+1)] for i in range(2, numRows): for j in range(1,i): response[i][j] = response[i-1][j] + response[i-1][j-1] return response
pascals-triangle
Runtime: 8 ms, faster than 99.95% of Python online submissions for Pascal's Triangle.
kala725
1
200
pascals triangle
118
0.694
Easy
1,083
https://leetcode.com/problems/pascals-triangle-ii/discuss/467945/Pythonic-O(-k-)-space-sol.-based-on-math-formula-90%2B
class Solution: def getRow(self, rowIndex: int) -> List[int]: if rowIndex == 0: # Base case return [1] elif rowIndex == 1: # Base case return [1, 1] else: # General case: last_row = self.getRow( rowIndex-1 ) size = len(last_row) return [ last_row[0] ] + [ last_row[idx] + last_row[idx+1] for idx in range( size-1) ] + [ last_row[-1] ]
pascals-triangle-ii
Pythonic O( k ) space sol. based on math formula, 90%+
brianchiang_tw
15
1,800
pascals triangle ii
119
0.598
Easy
1,084
https://leetcode.com/problems/pascals-triangle-ii/discuss/2040210/O(n)timeBEATS-99.97-MEMORYSPEED-0ms-MAY-2022
class Solution: def getRow(self, rowIndex: int) -> List[int]: res=[] for i in range(rowIndex+1): res.append([]) for j in range(i+1): if j == 0 or j == i: res[i].append(1) else: res[i].append(res[i - 1][j - 1] + res[i - 1][j]) return res[rowIndex]
pascals-triangle-ii
O(n)time/BEATS 99.97% MEMORY/SPEED 0ms MAY 2022
cucerdariancatalin
11
917
pascals triangle ii
119
0.598
Easy
1,085
https://leetcode.com/problems/pascals-triangle-ii/discuss/912234/O(1)
class Solution: def getRow(self, rowIndex: int) -> List[int]: return [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1], [1, 6, 15, 20, 15, 6, 1], [1, 7, 21, 35, 35, 21, 7, 1], [1, 8, 28, 56, 70, 56, 28, 8, 1], [1, 9, 36, 84, 126, 126, 84, 36, 9, 1], [1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1], [1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1], [1, 12, 66, 220, 495, 792, 924, 792, 495, 220, 66, 12, 1], [1, 13, 78, 286, 715, 1287, 1716, 1716, 1287, 715, 286, 78, 13, 1], [1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1], [1, 15, 105, 455, 1365, 3003, 5005, 6435, 6435, 5005, 3003, 1365, 455, 105, 15, 1], [1, 16, 120, 560, 1820, 4368, 8008, 11440, 12870, 11440, 8008, 4368, 1820, 560, 120, 16, 1], [1, 17, 136, 680, 2380, 6188, 12376, 19448, 24310, 24310, 19448, 12376, 6188, 2380, 680, 136, 17, 1], [1, 18, 153, 816, 3060, 8568, 18564, 31824, 43758, 48620, 43758, 31824, 18564, 8568, 3060, 816, 153, 18, 1], [1, 19, 171, 969, 3876, 11628, 27132, 50388, 75582, 92378, 92378, 75582, 50388, 27132, 11628, 3876, 969, 171, 19, 1], [1, 20, 190, 1140, 4845, 15504, 38760, 77520, 125970, 167960, 184756, 167960, 125970, 77520, 38760, 15504, 4845, 1140, 190, 20, 1], [1, 21, 210, 1330, 5985, 20349, 54264, 116280, 203490, 293930, 352716, 352716, 293930, 203490, 116280, 54264, 20349, 5985, 1330, 210, 21, 1], [1, 22, 231, 1540, 7315, 26334, 74613, 170544, 319770, 497420, 646646, 705432, 646646, 497420, 319770, 170544, 74613, 26334, 7315, 1540, 231, 22, 1], [1, 23, 253, 1771, 8855, 33649, 100947, 245157, 490314, 817190, 1144066, 1352078, 1352078, 1144066, 817190, 490314, 245157, 100947, 33649, 8855, 1771, 253, 23, 1], [1, 24, 276, 2024, 10626, 42504, 134596, 346104, 735471, 1307504, 1961256, 2496144, 2704156, 2496144, 1961256, 1307504, 735471, 346104, 134596, 42504, 10626, 2024, 276, 24, 1], [1, 25, 300, 2300, 12650, 53130, 177100, 480700, 1081575, 2042975, 3268760, 4457400, 5200300, 5200300, 4457400, 3268760, 2042975, 1081575, 480700, 177100, 53130, 12650, 2300, 300, 25, 1], [1, 26, 325, 2600, 14950, 65780, 230230, 657800, 1562275, 3124550, 5311735, 7726160, 9657700, 10400600, 9657700, 7726160, 5311735, 3124550, 1562275, 657800, 230230, 65780, 14950, 2600, 325, 26, 1], [1, 27, 351, 2925, 17550, 80730, 296010, 888030, 2220075, 4686825, 8436285, 13037895, 17383860, 20058300, 20058300, 17383860, 13037895, 8436285, 4686825, 2220075, 888030, 296010, 80730, 17550, 2925, 351, 27, 1], [1, 28, 378, 3276, 20475, 98280, 376740, 1184040, 3108105, 6906900, 13123110, 21474180, 30421755, 37442160, 40116600, 37442160, 30421755, 21474180, 13123110, 6906900, 3108105, 1184040, 376740, 98280, 20475, 3276, 378, 28, 1], [1, 29, 406, 3654, 23751, 118755, 475020, 1560780, 4292145, 10015005, 20030010, 34597290, 51895935, 67863915, 77558760, 77558760, 67863915, 51895935, 34597290, 20030010, 10015005, 4292145, 1560780, 475020, 118755, 23751, 3654, 406, 29, 1], [1, 30, 435, 4060, 27405, 142506, 593775, 2035800, 5852925, 14307150, 30045015, 54627300, 86493225, 119759850, 145422675, 155117520, 145422675, 119759850, 86493225, 54627300, 30045015, 14307150, 5852925, 2035800, 593775, 142506, 27405, 4060, 435, 30, 1], [1, 31, 465, 4495, 31465, 169911, 736281, 2629575, 7888725, 20160075, 44352165, 84672315, 141120525, 206253075, 265182525, 300540195, 300540195, 265182525, 206253075, 141120525, 84672315, 44352165, 20160075, 7888725, 2629575, 736281, 169911, 31465, 4495, 465, 31, 1], [1, 32, 496, 4960, 35960, 201376, 906192, 3365856, 10518300, 28048800, 64512240, 129024480, 225792840, 347373600, 471435600, 565722720, 601080390, 565722720, 471435600, 347373600, 225792840, 129024480, 64512240, 28048800, 10518300, 3365856, 906192, 201376, 35960, 4960, 496, 32, 1], [1, 33, 528, 5456, 40920, 237336, 1107568, 4272048, 13884156, 38567100, 92561040, 193536720, 354817320, 573166440, 818809200, 1037158320, 1166803110, 1166803110, 1037158320, 818809200, 573166440, 354817320, 193536720, 92561040, 38567100, 13884156, 4272048, 1107568, 237336, 40920, 5456, 528, 33, 1]][rowIndex]
pascals-triangle-ii
O(1)
dqdwsdlws
11
327
pascals triangle ii
119
0.598
Easy
1,086
https://leetcode.com/problems/pascals-triangle-ii/discuss/2654390/Python-or-Clean-Recursion-or-O(rowIndex)-Space
class Solution: def getRow(self, rowIndex: int) -> List[int]: if rowIndex == 0: return [1] prevRow = self.getRow(rowIndex - 1) currentRow = [1] for i in range(len(prevRow) - 1): currentRow.append(prevRow[i] + prevRow[i + 1]) currentRow.append(1) return currentRow
pascals-triangle-ii
Python | Clean Recursion | O(rowIndex) Space
Aymenua
5
586
pascals triangle ii
119
0.598
Easy
1,087
https://leetcode.com/problems/pascals-triangle-ii/discuss/2612506/SIMPLE-PYTHON3-SOLUTION-easy-approach
class Solution: def getRow(self, rowIndex: int) -> List[int]: list1 = [] for i in range(rowIndex+1): temp_list = [] for j in range(i+1): if j==0 or j==i: temp_list.append(1) else: temp_list.append(list1[i-1][j-1] + list1[i-1][j]) list1.append(temp_list) return list1[rowIndex]
pascals-triangle-ii
✅✔ SIMPLE PYTHON3 SOLUTION ✅✔easy-approach
rajukommula
4
226
pascals triangle ii
119
0.598
Easy
1,088
https://leetcode.com/problems/pascals-triangle-ii/discuss/1742190/Python-3-(40ms)-or-Iterative-Easy-Approach
class Solution: def getRow(self, rowIndex: int) -> List[int]: res=[] for i in range(rowIndex+1): res.append([]) for j in range(i+1): if j == 0 or j == i: res[i].append(1) else: res[i].append(res[i - 1][j - 1] + res[i - 1][j]) return res[rowIndex]
pascals-triangle-ii
Python 3 (40ms) | Iterative Easy Approach
MrShobhit
4
286
pascals triangle ii
119
0.598
Easy
1,089
https://leetcode.com/problems/pascals-triangle-ii/discuss/913200/python3-just-1-line-using-Binomial-Theorem
class Solution: def getRow(self, rowIndex: int) -> List[int]: return [comb(rowIndex,j) for j in range(rowIndex+1)]
pascals-triangle-ii
python3 just 1 line using Binomial Theorem
dqdwsdlws
4
185
pascals triangle ii
119
0.598
Easy
1,090
https://leetcode.com/problems/pascals-triangle-ii/discuss/789241/Python-3-solution-faster-than-95.36
class Solution: def getRow(self, rowIndex: int) -> List[int]: tri=[[1], [1,1]] for i in range(2, rowIndex+1): row=[None]*(i+1) row[0]=1 row[i]=1 for j in range(1,i): row[j]=tri[i-1][j]+tri[i-1][j-1] tri.append(row) return tri[rowIndex]
pascals-triangle-ii
Python 3 solution faster than 95.36%
Mradul_005
3
795
pascals triangle ii
119
0.598
Easy
1,091
https://leetcode.com/problems/pascals-triangle-ii/discuss/2586299/Python3-Solution-Faster-than-94.4-using-Binomial-Theorem
class Solution: def getRow(self, rowIndex: int) -> List[int]: arr = [] # Factorial function (e.g. 4! would be 4 x 3 x 2 x 1 and 6! would be 6 x 5 x 4 x 3 x 2 x 1 and 0! would be 1) def factorial(num): ans = 1 for x in range(1, num+1): ans *= x return ans # This is just the nCr formula from above def nCr(n, r): return int(factorial(n) / ((factorial(r)) * factorial(n - r))) for y in range(0, rowIndex + 1): arr.append(int(factorial(rowIndex) / ((factorial(y)) * factorial(rowIndex - y)))) # rowIndex represents n in the nCr formula and y represents r
pascals-triangle-ii
Python3 Solution Faster than 94.4% using Binomial Theorem
James-Kosinar
2
111
pascals triangle ii
119
0.598
Easy
1,092
https://leetcode.com/problems/pascals-triangle-ii/discuss/788442/Python-Solution
class Solution: def getRow(self, rowIndex: int) -> List[int]: kth_index_row = [] kth_index_row.append(1) for i in range(rowIndex): for j in range(i, 0, -1): kth_index_row[j] = kth_index_row[j - 1] + kth_index_row[j] kth_index_row.append(1) return kth_index_row
pascals-triangle-ii
Python Solution
DebbieAlter
2
166
pascals triangle ii
119
0.598
Easy
1,093
https://leetcode.com/problems/pascals-triangle-ii/discuss/787245/Pascal-Triangle-2-or-Python3-or-explained-or-QUESTION
class Solution: def getRow(self, rowIndex: int) -> List[int]: # deal with edge cases explicitely if rowIndex == 0: return [1] if rowIndex == 1: return [1, 1] # there will be two arrays: prev and cur # representing the previous row from which we construct the current one prev = [1, 1] for i in range(1, rowIndex+1): # initialise current with 1 as seen in the animation as well cur = [1] # looping through the interior points and build them by summing neighbours for j in range(1,i): cur.append(prev[j-1]+prev[j]) # finishing off this row with another one cur.append(1) prev = cur[:] return prev
pascals-triangle-ii
Pascal Triangle 2 | Python3 | explained | QUESTION
Matthias_Pilz
2
114
pascals triangle ii
119
0.598
Easy
1,094
https://leetcode.com/problems/pascals-triangle-ii/discuss/2448914/Python3-solution-recursion-with-memoization-93-faster
class Solution: def getRow(self, rowIndex: int) -> List[int]: #Cache dict for storing computed values #Map format -> tuple: int -> (i-th row, j-th column): value cache = {} def getNum(i: int, j: int) -> int: #Base case if j == 0 or i == j: return 1 if (i - 1, j - 1) in cache: #If the value for i-th row and j-th column was already computed, fetch it from cache first = cache[(i - 1, j - 1)] else: #Compute the value and then store it in cache first = getNum(i - 1, j - 1) cache[(i - 1, j - 1)] = first if (i - 1, j) in cache: #If the value for i-th row and j-th column was already computed, fetch it from cache second = cache[(i - 1, j)] else: #Compute the value and then store it in cache second = getNum(i - 1, j) cache[(i - 1, j)] = second return first + second #For every cell in the row compute the value answer = [getNum(rowIndex, j) for j in range(rowIndex + 1)] return answer
pascals-triangle-ii
Python3 solution, recursion with memoization, 93% faster
matteogianferrari
1
153
pascals triangle ii
119
0.598
Easy
1,095
https://leetcode.com/problems/pascals-triangle-ii/discuss/2340863/Simple-Python-implementation-using-Factorial-and-Combinations
class Solution: # creating a factorial function def factorial(self, n): if n == 1 or n == 0: return 1 else: return n * factorial(n - 1) # method to find the combination of two numbers def comb(self, n, r): return factorial(n)/(factorial(n) * factorial(n - r)) # method to find the main answer def getRow(self, rowIndex: int) -> List[int]: res = [] for i in range(rowIndex + 1): for j in range(0, i + 1): res.append(comb(i, j)) return res[-(rowIndex + 1):]
pascals-triangle-ii
Simple Python implementation using Factorial and Combinations
abinvarghese90
1
40
pascals triangle ii
119
0.598
Easy
1,096
https://leetcode.com/problems/pascals-triangle-ii/discuss/2336701/Python3-simple-solution
class Solution: def getRow(self, rowIndex: int) -> List[int]: l = [] l.append(1) for i in range(1,rowIndex+1): l.append(0) l.append(1) for j in range(i): l.append(l[0]+l[1]) del l[0] del l[0] return l
pascals-triangle-ii
📌 Python3 simple solution
Dark_wolf_jss
1
36
pascals triangle ii
119
0.598
Easy
1,097
https://leetcode.com/problems/pascals-triangle-ii/discuss/2327071/O(n)timeBEATS-99.97-MEMORYSPEED-0ms-JULY-2022
class Solution: def getRow(self, rowIndex: int) -> List[int]: res=[] for i in range(rowIndex+1): res.append([]) for j in range(i+1): if j == 0 or j == i: res[i].append(1) else: res[i].append(res[i - 1][j - 1] + res[i - 1][j]) return res[rowIndex]
pascals-triangle-ii
O(n)time/BEATS 99.97% MEMORY/SPEED 0ms JULY 2022
cucerdariancatalin
1
156
pascals triangle ii
119
0.598
Easy
1,098
https://leetcode.com/problems/pascals-triangle-ii/discuss/2166464/Python-Easy-minimal-extra-space
class Solution: def getRow(self, rowIndex: int) -> List[int]: row = [1] for _ in range(rowIndex): for i in range(len(row)-1, 0, -1): row[i] = row[i] + row[i-1] row.append(1) return row
pascals-triangle-ii
Python Easy minimal extra space
hcks
1
60
pascals triangle ii
119
0.598
Easy
1,099