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/beautiful-arrangement-ii/discuss/1574163/Python3-Solution-or-100-faster-or-4-line-code | class Solution:
def constructArray(self, n: int, k: int) -> List[int]:
result = list(range(1,n+1))
result[0 : k+1 : 2] = list(range(1,(k+2)//2 + 1))
result[1 : k+1 : 2] = list(range(k+1,(k+2)//2,-1))
return result | beautiful-arrangement-ii | Python3 Solution | 100% faster | 4-line code | satyam2001 | 0 | 85 | beautiful arrangement ii | 667 | 0.597 | Medium | 11,100 |
https://leetcode.com/problems/beautiful-arrangement-ii/discuss/1447565/Python3-solution | class Solution:
def constructArray(self, n: int, k: int) -> List[int]:
l,h = 1,n
res = [l]
l += 1
while k > 1:
res.append(h)
h -= 1
k -= 1
if k > 1:
res.append(l)
l += 1
k -= 1
if ... | beautiful-arrangement-ii | Python3 solution | EklavyaJoshi | 0 | 41 | beautiful arrangement ii | 667 | 0.597 | Medium | 11,101 |
https://leetcode.com/problems/beautiful-arrangement-ii/discuss/1154660/Python3-Simple-to-understand-Greedy-Approach | class Solution:
def constructArray(self, n: int, k: int) -> List[int]:
solution = [1]
offset = k
currentSign = 1
while abs(offset) >= 1:
if currentSign == 1:
solution.append(solution[-1] + offset)
currentSign = 0
... | beautiful-arrangement-ii | [Python3] Simple to understand, Greedy Approach | karanpitt | 0 | 70 | beautiful arrangement ii | 667 | 0.597 | Medium | 11,102 |
https://leetcode.com/problems/beautiful-arrangement-ii/discuss/388331/Simon's-Note-Python3 | class Solution:
def constructArray(self, n: int, k: int) -> List[int]:
t=[i for i in range(1,n+1)]
for j in range(1,k):
t[j:]=t[j:][::-1]
return t | beautiful-arrangement-ii | [🎈Simon's Note🎈] Python3 | SunTX | 0 | 112 | beautiful arrangement ii | 667 | 0.597 | Medium | 11,103 |
https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/discuss/1581461/Binary-Search-Solution-with-detailed-explanation-beating-98-in-time-and-86-in-space | class Solution:
def findKthNumber(self, m: int, n: int, k: int) -> int:
# special cases: k == 1, k == m * n
if k == 1:
return 1
if k == m * n:
return m * n
# make the matrix a tall one - height >= width
# because later I will loop along the width. This will reduce t... | kth-smallest-number-in-multiplication-table | Binary Search Solution with detailed explanation, beating 98% in time and 86% in space | leonine9 | 1 | 83 | kth smallest number in multiplication table | 668 | 0.515 | Hard | 11,104 |
https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/discuss/1342158/Python3-binary-search | class Solution:
def findKthNumber(self, m: int, n: int, k: int) -> int:
lo, hi = 1, m*n
while lo < hi:
mid = lo + hi >> 1
cnt = sum(min(n, mid//(i+1)) for i in range(m))
if cnt < k: lo = mid + 1
else: hi = mid
return lo | kth-smallest-number-in-multiplication-table | [Python3] binary search | ye15 | 1 | 99 | kth smallest number in multiplication table | 668 | 0.515 | Hard | 11,105 |
https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/discuss/1580515/TLE-Python3-Dijkstra's-algorithm | class Solution:
def findKthNumber(self, m: int, n: int, k: int) -> int:
pq = [(1, 1, 1)] # Priority queue (table[i][j], i, j)
seen = set((1,1)) # Visited nodes
# Traverse the first k-1 nodes
for i in range(1, k):
# Pop ith smallest node
cur, a, b... | kth-smallest-number-in-multiplication-table | TLE [Python3] Dijkstra's algorithm | vscala | 0 | 118 | kth smallest number in multiplication table | 668 | 0.515 | Hard | 11,106 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1046286/Python.-faster-than-98.05.-recursive.-6-lines.-DFS. | class Solution:
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
if not root: return root
if root.val < low: return self.trimBST(root.right, low, high)
if root.val > high: return self.trimBST(root.left, low, high)
root.left = self.trimBST(root.left, low, high)
root.right = self.trimBST(roo... | trim-a-binary-search-tree | Python. faster than 98.05%. recursive. 6 lines. DFS. | m-d-f | 24 | 1,000 | trim a binary search tree | 669 | 0.663 | Medium | 11,107 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1951579/python3-oror-10-line-self-recursive | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
if not root: return None
if root.val > high:
return self.trimBST(root.left, low, high)
if root.val < low:
return self.trimBST(root.right, low, high)
root.le... | trim-a-binary-search-tree | python3 || 10-line self recursive | gulugulugulugulu | 1 | 25 | trim a binary search tree | 669 | 0.663 | Medium | 11,108 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1949450/Python-Easy-Python-Solution-Using-Recursion | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
def TrimBST(node):
if node == None:
return None
elif node.val > high :
return TrimBST(node.left)
elif node.val < low :
return TrimBST(node.right)
else:
node.left = TrimBST(node.l... | trim-a-binary-search-tree | [ Python ] ✅✅ Easy Python Solution Using Recursion ✌🥳 | ASHOK_KUMAR_MEGHVANSHI | 1 | 38 | trim a binary search tree | 669 | 0.663 | Medium | 11,109 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1947870/python3-recursion-easy-to-read | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
if root is None:
return None
if low <= root.val <= high:
root.left = self.trimBST(root.left, low, high)
root.right = self.trimBST(root.right, low, high... | trim-a-binary-search-tree | python3 recursion easy-to-read | user2613C | 1 | 59 | trim a binary search tree | 669 | 0.663 | Medium | 11,110 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1924346/Python-Elegant-Clean-Recursive-Solution-or-with-Comments-or-Time-and-Space-Complexity%3A-O(n) | class Solution:
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
# base case
if not root:
return
# trim left and right subtree
root.left = self.trimBST(root.left, low, high)
root.right = self.trimBST(root.right, low, high)
# if node'... | trim-a-binary-search-tree | [Python] Elegant Clean Recursive Solution | with Comments | Time & Space Complexity: O(n) | ysingla | 1 | 43 | trim a binary search tree | 669 | 0.663 | Medium | 11,111 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1411985/Python-Solution-Beats-100-time-w-Comments | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
def recur(root):
if root is None:
return None
#if root is larger than boundary, then we must return the next child that's within the boundary
#in this case, only children to the left have a chance of... | trim-a-binary-search-tree | Python Solution Beats 100% time w/ Comments | mguthrie45 | 1 | 89 | trim a binary search tree | 669 | 0.663 | Medium | 11,112 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1110051/Python-or-Recursion-or-Faster-99 | class Solution:
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
def rec(root):
if root:
if not(low<=root.val<=high):
if root.val<low:
return rec(root.right)
return rec(root.left)
e... | trim-a-binary-search-tree | Python | Recursion | Faster 99% | rajatrai1206 | 1 | 117 | trim a binary search tree | 669 | 0.663 | Medium | 11,113 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1046702/Python-easy-solution-or-93-faster-or-Recursion | class Solution:
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
def helper_trim(node):
if node is None: return node
elif node.val > high:
return self.trimBST(node.left)
elif node.val < low:
return self.trimB... | trim-a-binary-search-tree | Python easy solution | 93% faster | Recursion | vsahoo | 1 | 93 | trim a binary search tree | 669 | 0.663 | Medium | 11,114 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/2492442/Python-runtime-64.38-memory-42.23-with-explanation | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
if not root:
return None
elif low <= root.val <= high:
root.left = self.trimBST(root.left, low, high)
root.right = self.trimBST(root.right, low, high)
... | trim-a-binary-search-tree | Python, runtime 64.38%, memory 42.23% with explanation | tsai00150 | 0 | 28 | trim a binary search tree | 669 | 0.663 | Medium | 11,115 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1958095/Python-solution-Pre-order%2B-Construct-BST | class Solution:
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
nodes = []
def preorder(node):
if node is None:
return
nodes.append(node)
preorder(node.left)
preorder(node.right)
preorder(root)
hea... | trim-a-binary-search-tree | Python solution, Pre order+ Construct BST | pradeep288 | 0 | 14 | trim a binary search tree | 669 | 0.663 | Medium | 11,116 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1952643/Explained-Python-Simple-Solution-with-comments-O(n)-time-O(1)-space-80-time-80-memory | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
return trim(root, low, high)
def trim(node, low, high):
# node is empty
if node is None:
return None
# node is a leaf (no children)
if node.left is None and node.right is None:
if low <= nod... | trim-a-binary-search-tree | [Explained] [Python] Simple Solution with comments O(n) time O(1) space [80% time 80% memory] | akhileshravi | 0 | 12 | trim a binary search tree | 669 | 0.663 | Medium | 11,117 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1951321/Recursion-and-Iterative-or-Python3 | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int,
high: int) -> Optional[TreeNode]:
def trim(root: Optional[TreeNode]):
if root is None:
return None
if root.val < low:
return trim(root.right)
elif root.... | trim-a-binary-search-tree | Recursion and Iterative | Python3 | qihangtian | 0 | 7 | trim a binary search tree | 669 | 0.663 | Medium | 11,118 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1951321/Recursion-and-Iterative-or-Python3 | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int,
high: int) -> Optional[TreeNode]:
# trim to root
while root is not None:
if root.val > high:
root = root.left
elif root.val < low:
root = root.right
... | trim-a-binary-search-tree | Recursion and Iterative | Python3 | qihangtian | 0 | 7 | trim a binary search tree | 669 | 0.663 | Medium | 11,119 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1949721/Python3-Simple-DFS-solution-or-40ms-faster-than-99.61-or-18-MB-less-than-83.03 | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
def dfs(node):
if not node:
return
if low <= node.val <= high:
node.left = dfs(node.left)
node.right = dfs(node.right)
... | trim-a-binary-search-tree | [Python3] Simple DFS solution | 40ms faster than 99.61% | 18 MB, less than 83.03% | nandhakiran366 | 0 | 8 | trim a binary search tree | 669 | 0.663 | Medium | 11,120 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1949538/Python-Solution | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
if root is None: return root
root.left = self.trimBST(root.left, low, high)
root.right = self.trimBST(root.right, low, high)
if low <= root.val <= high: return root
if root... | trim-a-binary-search-tree | ✅ Python Solution | dhananjay79 | 0 | 19 | trim a binary search tree | 669 | 0.663 | Medium | 11,121 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1948813/Python-or-Recursion-or-Beats-98 | class Solution(object):
def trimBST(self, root, low, high):
"""
:type root: TreeNode
:type low: int
:type high: int
:rtype: TreeNode
"""
if not root:
return
elif root.val < low:
return self.trimBST(root.right, low, high)
... | trim-a-binary-search-tree | Python | Recursion | Beats 98% | prajyotgurav | 0 | 19 | trim a binary search tree | 669 | 0.663 | Medium | 11,122 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1948372/669.-Trim-a-Binary-Search-Tree | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
if(root is None):
return None
elif(root.val > high):
return self.trimBST(root.left, low, high)
elif(root.val < low):
ret... | trim-a-binary-search-tree | 669. Trim a Binary Search Tree | shubham_pcs2012 | 0 | 6 | trim a binary search tree | 669 | 0.663 | Medium | 11,123 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1948239/Python3-simple-recursion(Beats-~96) | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
if not root:
return None
if low<=root.val<=high:
root.left = self.trimBST(root.left, low, high)
root.right = self.trimBST(root.right, low, high) ... | trim-a-binary-search-tree | ✔️Python3-simple recursion(Beats ~96%) | constantine786 | 0 | 14 | trim a binary search tree | 669 | 0.663 | Medium | 11,124 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1948092/Fastest-python-solution-with-comments.-99.61-faster | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
if root == None:
return None
'''
Invalid node
Ignore entire left subtree of this node
and return the root of trimmed right subtree
'''
... | trim-a-binary-search-tree | Fastest python solution with comments. 99.61% faster | souravrane_ | 0 | 14 | trim a binary search tree | 669 | 0.663 | Medium | 11,125 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1948031/Python-Double-beats-100-solution | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
if not root : return None
if root.val < low : root = self.trimBST(root.right, low, high)
elif root.val > high : root = self.trimBST(root.left, low, high)
else:
... | trim-a-binary-search-tree | [Python] Double beats 100% solution | crazypuppy | 0 | 7 | trim a binary search tree | 669 | 0.663 | Medium | 11,126 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1948027/Clean-recursive-DFS-in-Python-and-C | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
def helper(n: TreeNode = root) -> TreeNode:
if not n:
return None
if n.val < low:
return helper(n.right)
if n.val > high:
... | trim-a-binary-search-tree | Clean recursive DFS in Python and C | mousun224 | 0 | 9 | trim a binary search tree | 669 | 0.663 | Medium | 11,127 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1547637/Python3 | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
def trim(root):
if not root:
return None
if root.val < low:
return trim(root.right)
if root.val > high:
... | trim-a-binary-search-tree | Python3 | immutable_23 | 0 | 32 | trim a binary search tree | 669 | 0.663 | Medium | 11,128 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1481715/Python-Clean-Postorder-DFS | class Solution:
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
def trim(node = root):
if not node:
return None
L, R = trim(node.left), trim(node.right)
if node.val < low:
return R
... | trim-a-binary-search-tree | [Python] Clean Postorder DFS | soma28 | 0 | 48 | trim a binary search tree | 669 | 0.663 | Medium | 11,129 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1429430/Python3-Recursive-DFS-two-possible-solutions | class Solution:
def traversal(self, node, low, high):
if not node:
return (False, None)
l_is_out, l = self.traversal(node.left, low, high)
r_is_out, r = self.traversal(node.right, low, high)
if l_is_out:
node.left = l
if r_is... | trim-a-binary-search-tree | [Python3] Recursive DFS two possible solutions | maosipov11 | 0 | 43 | trim a binary search tree | 669 | 0.663 | Medium | 11,130 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1429430/Python3-Recursive-DFS-two-possible-solutions | class Solution:
def traversal(self, node, low, high):
if not node:
return None
if node.val < low:
return self.traversal(node.right, low, high)
elif node.val > high:
return self.traversal(node.left, low, high)
node.left = self.trav... | trim-a-binary-search-tree | [Python3] Recursive DFS two possible solutions | maosipov11 | 0 | 43 | trim a binary search tree | 669 | 0.663 | Medium | 11,131 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1249170/Simple-Recursive-Solution-or-Python | class Solution:
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
if not root:
return root
# Trim right sub-tree
if root.val < low:
return self.trimBST(root.right, low, high)
# Trim left sub-tree
if root.val > high:
... | trim-a-binary-search-tree | Simple Recursive Solution | Python | shubh_027 | 0 | 64 | trim a binary search tree | 669 | 0.663 | Medium | 11,132 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1052079/Python-Solution | class Solution:
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
###### 1##############
def inrange(val):
if val<low or val>high:
return False
return True
def rec(node):
if node == None:
return None
... | trim-a-binary-search-tree | Python Solution | SaSha59 | 0 | 105 | trim a binary search tree | 669 | 0.663 | Medium | 11,133 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1047130/Iteration-Version-O-(log(N))-Trimming-while-Doing-Binary-Search-Without-Stack | class Solution:
def trimBST(self, root: TreeNode, low: int, high: int) -> TreeNode:
head = TreeNode(left=root) # Guard Node
pre, cur = head, head.left
# Binary search for low
while cur:
if cur.val > low:
pre = cur
cur = cur.left
elif... | trim-a-binary-search-tree | Iteration Version O (log(N))- Trimming while Doing Binary Search - Without Stack | milileetcode | 0 | 74 | trim a binary search tree | 669 | 0.663 | Medium | 11,134 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/1046589/Python-DFS-Post-Order-4-lines | class Solution:
def trimBST(self, root, low, high):
if not root: return None
root.left = self.trimBST(root.left, low, high)
root.right = self.trimBST(root.right, low, high)
return root if low <= root.val <= high else root.left or root.right | trim-a-binary-search-tree | [Python] DFS Post-Order 4 lines | reeestandard | 0 | 49 | trim a binary search tree | 669 | 0.663 | Medium | 11,135 |
https://leetcode.com/problems/trim-a-binary-search-tree/discuss/487298/Python-Iterative-solution | class Solution:
def trimBST(self, root: TreeNode, L: int, R: int) -> TreeNode:
# iterative
if root is None: return None
while root and (root.val < L or root.val > R):
if root.val < L: root = root.right
else: root = root.left
lnode = rnode = r... | trim-a-binary-search-tree | Python Iterative solution | Kingfisher | 0 | 141 | trim a binary search tree | 669 | 0.663 | Medium | 11,136 |
https://leetcode.com/problems/maximum-swap/discuss/846837/Python-3-or-Greedy-Math-or-Explanations | class Solution:
def maximumSwap(self, num: int) -> int:
s = list(str(num))
n = len(s)
for i in range(n-1): # find index where s[i] < s[i+1], meaning a chance to flip
if s[i] < s[i+1]: break
else: return num ... | maximum-swap | Python 3 | Greedy, Math | Explanations | idontknoooo | 43 | 3,600 | maximum swap | 670 | 0.479 | Medium | 11,137 |
https://leetcode.com/problems/maximum-swap/discuss/1623820/Python-3-O(n)-time-O(1)-space-without-using-strings-with-comments | class Solution:
def maximumSwap(self, num: int) -> int:
# larger digit to swap, digit position of this digit
high_digit = high_pos = 0
# smaller digit to swap, digit position of this digit
low_digit = low_pos = 0
# greatest digit seen so far, digit postion o... | maximum-swap | Python 3 O(n) time, O(1) space, without using strings, with comments | dereky4 | 7 | 889 | maximum swap | 670 | 0.479 | Medium | 11,138 |
https://leetcode.com/problems/maximum-swap/discuss/1548210/Python3-O(n)-time-solution | class Solution:
def maximumSwap(self, num: int) -> int:
nums = [int(x) for x in str(num)]
_max = -1
_max_idx = 0
last_max_idx, last_min_idx = 0, 0
for idx in range(len(nums) - 1, -1, -1):
if nums[idx] > _max:
_max = nums[idx]
... | maximum-swap | [Python3] O(n) time solution | maosipov11 | 2 | 383 | maximum swap | 670 | 0.479 | Medium | 11,139 |
https://leetcode.com/problems/maximum-swap/discuss/2695571/Python-Hash-table-O(n)-or-O(1) | class Solution:
def maximumSwap(self, num: int) -> int:
num = [int(i) for i in str(num)]
dit = defaultdict(int)
for i, n in enumerate(num):
dit[n] = max(dit[n], i)
flag = False
for i, n in enumerate(num):
for x in range(9, n, -1):
... | maximum-swap | Python Hash table O(n) | O(1) | scr112 | 1 | 119 | maximum swap | 670 | 0.479 | Medium | 11,140 |
https://leetcode.com/problems/maximum-swap/discuss/1452761/PyPy3-A-bit-slower-but-simple-solution-w-comments | class Solution:
def maximumSwap(self, num: int) -> int:
# Init
max_num = num
# Conver num to string
s = list(str(num))
n = len(s)
# Loop for each condition
for i in range(n):
for j in range(i+1, n):
... | maximum-swap | [Py/Py3] A bit slower but simple solution w/ comments | ssshukla26 | 1 | 153 | maximum swap | 670 | 0.479 | Medium | 11,141 |
https://leetcode.com/problems/maximum-swap/discuss/1419957/Simple-Python-Beats-95 | class Solution:
def maximumSwap(self, num: int) -> int:
s = str(num)
iss = {n:i for i, n in enumerate(s)}
heap = [-int(x) for x in s]
heapq.heapify(heap)
idx = 0
while idx < len(s) and heap:
if int(s[idx]) < -heap[0]:
res = [x for... | maximum-swap | Simple Python Beats 95% | Pythagoras_the_3rd | 1 | 443 | maximum swap | 670 | 0.479 | Medium | 11,142 |
https://leetcode.com/problems/maximum-swap/discuss/1419957/Simple-Python-Beats-95 | class Solution:
def maximumSwap(self, num: int) -> int:
s = str(num)
iss = {n:i for i, n in enumerate(s)}
q = collections.deque(sorted([int(x) for x in s], key = lambda x:-x))
idx = 0
while idx < len(s) and q:
if int(s[idx]) < q[0]:
res =... | maximum-swap | Simple Python Beats 95% | Pythagoras_the_3rd | 1 | 443 | maximum swap | 670 | 0.479 | Medium | 11,143 |
https://leetcode.com/problems/maximum-swap/discuss/2808082/using-hashmap | class Solution:
def maximumSwap(self, num: int) -> int:
# O(N), O(N)
"""
2736 -> 7236
9973 -> 9973
9978 -> 9987
# { 2 : 0, 7 : 1, 3 : 2, 6 : 3}
# { 9 : 1, 7 : 2, 8 : 3}
"""
# this num will convert into list [2, 7, 3, 6]
num = list(map(... | maximum-swap | using hashmap | sahilkumar158 | 0 | 7 | maximum swap | 670 | 0.479 | Medium | 11,144 |
https://leetcode.com/problems/maximum-swap/discuss/2678671/Python3-Easy-O(N)-Solution | class Solution:
def maximumSwap(self, num: int) -> int:
num_arr = list(str(num)) # array with the number elements: 2736 -> ['2','7','3','6']
hashmap = {} # dictionary to store the elements of num and their indices
for index, number in enumerate(num_arr): # O(N)
hashmap[number]... | maximum-swap | Python3 Easy O(N) Solution | ramyh | 0 | 26 | maximum swap | 670 | 0.479 | Medium | 11,145 |
https://leetcode.com/problems/maximum-swap/discuss/2666297/8-Line-Simple-Python-Solution | class Solution:
def maximumSwap(self, num: int) -> int:
pre, after, maxs, string = -1, -1, 0, str(num)[::-1]
for index, char in enumerate(string):
if int(char) > int(string[maxs]):
maxs = index
if int(char) < int(string[maxs]):
pre, after = max... | maximum-swap | 8-Line Simple Python Solution | ParkerMW | 0 | 10 | maximum swap | 670 | 0.479 | Medium | 11,146 |
https://leetcode.com/problems/maximum-swap/discuss/2509210/Python-easy-to-read-and-understand-or-greedy | class Solution:
def maximumSwap(self, num: int) -> int:
nums = list(str(num))
n = len(nums)
suff = [0]*n
suff[n-1] = int(nums[n-1])
d = {}
for i, num in enumerate(nums):
d[int(num)] = i
for i in range(n-2, -1, -1):
suf... | maximum-swap | Python easy to read and understand | greedy | sanial2001 | 0 | 105 | maximum swap | 670 | 0.479 | Medium | 11,147 |
https://leetcode.com/problems/maximum-swap/discuss/2252541/Elegant-Solution | class Solution:
def maximumSwap(self, num: int) -> int:
s = str(num)
total = []
for i in range(len(s)):
for j in range(i+1,len(s)):
num1 = s[:i] + s[j] + s[i+1:j] + s[i] + s[j+1:]
num2 = int(num1)
if num2 > num:
... | maximum-swap | Elegant Solution | nth19912000 | 0 | 63 | maximum swap | 670 | 0.479 | Medium | 11,148 |
https://leetcode.com/problems/maximum-swap/discuss/1937752/Python3-92.42-or-Greedy-and-Recursion-or-Easy-Implementation | class Solution:
def maximumSwap(self, num: int) -> int:
n_l = list(str(num))
n_l = list(map(int, n_l))
ans = []
def process(n_l=n_l):
nonlocal ans
if len(n_l) == 1:
ans.append(n_l[0])
return
m_v... | maximum-swap | Python3 92.42% | Greedy & Recursion | Easy Implementation | doneowth | 0 | 160 | maximum swap | 670 | 0.479 | Medium | 11,149 |
https://leetcode.com/problems/maximum-swap/discuss/1885891/Python-Rolling-Max-Solution | class Solution:
def maximumSwap(self, num: int) -> int:
ans = []
origin = num
res = num
while num != 0:
res = num % 10
ans.append(res)
num //= 10
running_max = []
anchor_points = []
for index, a in enumerate(ans)... | maximum-swap | Python Rolling Max Solution | Vayne1994 | 0 | 208 | maximum swap | 670 | 0.479 | Medium | 11,150 |
https://leetcode.com/problems/maximum-swap/discuss/1826233/Python-3-with-comments | class Solution:
def maximumSwap(self, num: int) -> int:
num=list(str(num))
mx='-1'
mi=0
#find the largest number once the subsequent numbers start increasing
for i in range(1,len(num)):
if num[i]>num[i-1]:
... | maximum-swap | Python 3 with comments | nabeelar | 0 | 204 | maximum swap | 670 | 0.479 | Medium | 11,151 |
https://leetcode.com/problems/maximum-swap/discuss/1657371/the-easiest-to-understand-so-far(i-guess)-python | class Solution:
def maximumSwap(self, num: int) -> int:
nums = list(int(c) for c in str(num))
Sorted = sorted(nums, reverse = True)
i, j = 0, 0
while i < len(nums):
if Sorted[i] > nums[i]:
# if find the first char bigger than original one, then swap these ... | maximum-swap | the easiest to understand so far(i guess) python | barry0828 | 0 | 242 | maximum swap | 670 | 0.479 | Medium | 11,152 |
https://leetcode.com/problems/maximum-swap/discuss/1494701/Python3-stack | class Solution:
def maximumSwap(self, num: int) -> int:
str_num = list(str(num))
stack = []
l = sys.maxsize
for i in range(len(str_num)):
while stack and str_num[stack[-1]] < str_num[i]:
l = min(stack.pop(), l)
stack.append(i)
if l == s... | maximum-swap | Python3 stack | BichengWang | 0 | 205 | maximum swap | 670 | 0.479 | Medium | 11,153 |
https://leetcode.com/problems/maximum-swap/discuss/1385807/Python-or-Simple-and-Easy-Solution | class Solution:
def maximumSwap(self, num: int) -> int:
numa=[]
s=str(num)
for i in s:
numa.append(int(i))
samp=numa[:]
numa.sort(reverse=True)
ind=0
for i in range(len(s)):
if samp[i]!=numa[i]:
ind=i
bre... | maximum-swap | Python | Simple and Easy Solution | Hemanth1720 | 0 | 244 | maximum swap | 670 | 0.479 | Medium | 11,154 |
https://leetcode.com/problems/maximum-swap/discuss/1385285/Python-or-Easy-Solution | class Solution(object):
def maximumSwap(self, num):
"""
:type num: int
:rtype: int
"""
a=str(num)
l=list(a.strip())
print(l)
p=l[:]
p.sort(reverse=True)
fl=0
## looping in descending - if swap is possible then we will swap and b... | maximum-swap | Python | Easy Solution | nmk0462 | 0 | 175 | maximum swap | 670 | 0.479 | Medium | 11,155 |
https://leetcode.com/problems/maximum-swap/discuss/1263041/Easy-to-understand-Python | class Solution:
def maximumSwap(self, num: int) -> int:
helper = [
[]
for _ in range(10)
]
num = str(num)
for i, digit in enumerate(num):
helper[int(digit)].append(i)
index = 0
for i in range(9, -1, -1):
for po... | maximum-swap | Easy to understand Python | 1354924411 | 0 | 89 | maximum swap | 670 | 0.479 | Medium | 11,156 |
https://leetcode.com/problems/maximum-swap/discuss/1049005/Python-Easy-to-understand-with-Explanation | class Solution:
def maximumSwap(self, num: int) -> int:
x = list(map(int, list(str(num))))
n = len(x)
# Stores GREATER OR EQUAL element than self
# To current idx's right side
greater_idx = [ i for i in range(n) ]
for i in reversed(range(n-1)):
# IMP: '<... | maximum-swap | Python, Easy to understand with Explanation | Raj214 | 0 | 113 | maximum swap | 670 | 0.479 | Medium | 11,157 |
https://leetcode.com/problems/maximum-swap/discuss/1006889/Linear-python-solution-with-short-explanation | class Solution:
def maximumSwap(self, num: int) -> int:
'''
Find earliest digit that has a higher digit come after it. Swap those two.
dp[i] = idx for highest number after idx i.
'''
if num < 10:
return num
nums = [int(d) for d i... | maximum-swap | Linear python solution with short explanation | gins1 | 0 | 160 | maximum swap | 670 | 0.479 | Medium | 11,158 |
https://leetcode.com/problems/maximum-swap/discuss/895899/Python3-linear-scan-O(N) | class Solution:
def maximumSwap(self, num: int) -> int:
nums = list(map(int, str(num))) # array of digits
ii = m = mm = None
for i in reversed(range(len(nums))):
if not m or nums[m] < nums[i]: m = i # max digit
if nums[i] < nums[m]: ii, mm = i, m
if mm: num... | maximum-swap | [Python3] linear scan O(N) | ye15 | 0 | 94 | maximum swap | 670 | 0.479 | Medium | 11,159 |
https://leetcode.com/problems/maximum-swap/discuss/401798/Python-beats-70-simple-and-concise | class Solution:
def maximumSwap(self, num: int) -> int:
n = [int(x) for x in list(str(num))]
m = []
for i in range(len(n)):
if max(n[i:])!=n[i]:
t = n[i:][::-1]
ind = len(t) - t.index(max(t)) -1
n[i], n[i+ind] = n[i+ind], n[i]
return ''.join([str(x) for x in n])
return num | maximum-swap | Python beats 70% `simple and concise | saffi | 0 | 292 | maximum swap | 670 | 0.479 | Medium | 11,160 |
https://leetcode.com/problems/maximum-swap/discuss/1689186/Python-or-Simple-and-Easy | class Solution:
def maximumSwap(self, num: int) -> int:
num = str(num)
for i in range(len(num)):
max_number = max([k for k in num[i:]])
index = max([k for k in range(i, len(num)) if num[k]==max_number])
if int(num[i])<int(max_number):
... | maximum-swap | Python | Simple and Easy | Aman_24 | -2 | 333 | maximum swap | 670 | 0.479 | Medium | 11,161 |
https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/discuss/405721/Beats-100-Python-Solution | class Solution:
def findSecondMinimumValue(self, root: TreeNode) -> int:
def inorderTraversal(root):
if not root:
return []
else:
return inorderTraversal(root.left) + [root.val] + inorderTraversal(root.right)
r = set(inorderTraversal(root))
if len(r)>=2:
return sorted(list(r))[1]
else:
retu... | second-minimum-node-in-a-binary-tree | Beats 100% Python Solution | saffi | 2 | 424 | second minimum node in a binary tree | 671 | 0.44 | Easy | 11,162 |
https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/discuss/2525527/Simple-easy-to-understand-solution-for-python-beginners | class Solution:
def findSecondMinimumValue(self, root: Optional[TreeNode]) -> int:
if not root:
return None
r, l = [root], []
while r:
n = r.pop(0)
l.append(n.val)
if n.right: r.append(n.right)
if n.left: r.append... | second-minimum-node-in-a-binary-tree | Simple easy to understand solution for python beginners | pro6igy | 0 | 18 | second minimum node in a binary tree | 671 | 0.44 | Easy | 11,163 |
https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/discuss/1912418/Python3-Step-By-Step-Recursive-Solution-Faster-Than-86.87 | class Solution:
def findSecondMinimumValue(self, root: Optional[TreeNode]) -> int:
v = []
def dfs(tree):
if not tree:
return
v.append(tree.val)
dfs(tree.left)
dfs(tree.right)
... | second-minimum-node-in-a-binary-tree | Python3, Step By Step Recursive Solution, Faster Than 86.87% | Hejita | 0 | 28 | second minimum node in a binary tree | 671 | 0.44 | Easy | 11,164 |
https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/discuss/1739890/Super-Easy-Solution-(Beats-100)-O(N) | class Solution:
def findSecondMinimumValue(self, root: Optional[TreeNode]) -> int:
stack = [root]
my_set = set()
while len(stack) > 0:
node = stack.pop()
my_set.add(node.val)
if node.right:
stack.append(node.right)
... | second-minimum-node-in-a-binary-tree | Super Easy Solution (Beats 100%) O(N) | flanqueue | 0 | 83 | second minimum node in a binary tree | 671 | 0.44 | Easy | 11,165 |
https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/discuss/1172632/WEEB-DOES-PYTHON-BFS | class Solution:
def findSecondMinimumValue(self, root: TreeNode) -> int:
queue, result = deque([root]), []
while queue:
curNode = queue.popleft()
result.append(curNode.val)
if curNode.left:
queue.append(curNode.left)
if curNode.right:
queue.append(curNode.right)
result.sort()
if resul... | second-minimum-node-in-a-binary-tree | WEEB DOES PYTHON BFS | Skywalker5423 | 0 | 79 | second minimum node in a binary tree | 671 | 0.44 | Easy | 11,166 |
https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/discuss/1097041/Python-recursion-beats-80 | class Solution:
def findSecondMinimumValue(self, root: TreeNode) -> int:
if not root.left:
return -1
if not root:
return -1
left=self.findSecondMinimumValue(root.left)
right = self.findSecondMinimumValue(root.right)
if root.right.val>root.val:
... | second-minimum-node-in-a-binary-tree | Python recursion, beats 80% | kyswn | 0 | 119 | second minimum node in a binary tree | 671 | 0.44 | Easy | 11,167 |
https://leetcode.com/problems/bulb-switcher-ii/discuss/897976/Python3-O(1) | class Solution:
def flipLights(self, n: int, m: int) -> int:
def fn(n, m):
"""Return number of different status."""
if m * n == 0: return 1
return fn(n-1, m-1) + fn(n-1, m)
return fn(min(n, 3), min(m, 3)) | bulb-switcher-ii | [Python3] O(1) | ye15 | 4 | 462 | bulb switcher ii | 672 | 0.51 | Medium | 11,168 |
https://leetcode.com/problems/bulb-switcher-ii/discuss/1535818/Python-3-or-Math-O(1) | class Solution:
def flipLights(self, n: int, presses: int) -> int:
if not presses:
return 1
elif n < 3:
if n == 1:
return 2
elif presses >= 2:
return 4
else:
return 3
else:
if presse... | bulb-switcher-ii | Python 3 | Math, O(1) | idontknoooo | 1 | 400 | bulb switcher ii | 672 | 0.51 | Medium | 11,169 |
https://leetcode.com/problems/bulb-switcher-ii/discuss/699368/Python3-bit-manipulate-first-3-bits-Bulb-Switcher-II | class Solution:
# Operations: O(flip odds), E(flip evens), A(flip all), T(flip 3k + 1), N(flip nothing)
states = {
'N': 0b000,
'A': 0b111,
'O': 0b101,
'E': 0b010,
'T': 0b001,
'AT': 0b111 ^ 0b001,
'OT': 0b101 ^ 0b001,
'ET': 0b010 ^ 0b001,
}
... | bulb-switcher-ii | Python3 bit manipulate first 3 bits - Bulb Switcher II | r0bertz | 1 | 313 | bulb switcher ii | 672 | 0.51 | Medium | 11,170 |
https://leetcode.com/problems/number-of-longest-increasing-subsequence/discuss/1881188/Python-DP-solution-explained | class Solution:
def findNumberOfLIS(self, nums: List[int]) -> int:
dp = [1] * len(nums)
ct = [1] * len(nums)
maxLen, maxCt = 0, 0
# same as the LIS code, iterate
# over all the elements once and then
# from 0 -> i again to compute LISs
for i in range(... | number-of-longest-increasing-subsequence | [Python] DP solution explained | buccatini | 2 | 182 | number of longest increasing subsequence | 673 | 0.423 | Medium | 11,171 |
https://leetcode.com/problems/number-of-longest-increasing-subsequence/discuss/2800806/Python-(Simple-Dynamic-Programming) | class Solution:
def findNumberOfLIS(self, nums):
n = len(nums)
dp, cnt, max_val = [1]*n, [1]*n, 1
for i in range(1,n):
for j in range(i):
if nums[i] > nums[j]:
if dp[j] + 1 > dp[i]:
dp[i], cnt[i] = 1 + dp[j], cnt[j]
... | number-of-longest-increasing-subsequence | Python (Simple Dynamic Programming) | rnotappl | 0 | 4 | number of longest increasing subsequence | 673 | 0.423 | Medium | 11,172 |
https://leetcode.com/problems/number-of-longest-increasing-subsequence/discuss/2775249/n2-Complexity-Easy-to-Understand | class Solution:
def findNumberOfLIS(self, nums: List[int]) -> int:
longestDict = {}
max_len = 1
max_cnt = 1
total_cnt = 0
for i in range(len(nums)):
# length of LIS and count of LIS
longestDict[i] = [1, 1]
for i in range(len(nums)-1,-1,-1):
... | number-of-longest-increasing-subsequence | n^2 Complexity - Easy to Understand | shubhamsasane | 0 | 4 | number of longest increasing subsequence | 673 | 0.423 | Medium | 11,173 |
https://leetcode.com/problems/number-of-longest-increasing-subsequence/discuss/2402159/Python-DP-Solution | class Solution:
def findNumberOfLIS(self, nums: List[int]) -> int:
n = len(nums)
dp = [1 for _ in range(n)]
freq = [1 for _ in range(n)]
for i in range(n):
for j in range(i):
if nums[j] < nums[i] and 1 + dp[j] >= dp[i]:
if dp[i] == 1 + ... | number-of-longest-increasing-subsequence | Python DP Solution | gouthampoloth | 0 | 65 | number of longest increasing subsequence | 673 | 0.423 | Medium | 11,174 |
https://leetcode.com/problems/number-of-longest-increasing-subsequence/discuss/2317167/Python3-Solution-with-using-dp | class Solution:
def findNumberOfLIS(self, nums: List[int]) -> int:
dp = [0] * len(nums) # longest subseq
cdp = [0] * len(nums) # count of longest subseq
longest = 1
for i in range(len(nums)):
dp[i] = 1
cdp[i] = 1
for ... | number-of-longest-increasing-subsequence | [Python3] Solution with using dp | maosipov11 | 0 | 32 | number of longest increasing subsequence | 673 | 0.423 | Medium | 11,175 |
https://leetcode.com/problems/number-of-longest-increasing-subsequence/discuss/2297652/simple-python-dp | class Solution:
def findNumberOfLIS(self, nums: List[int]) -> int:
if not nums: return 0
n = len(nums)
m, dp, cnt = 0, [1] * n, [1] * n
for i in range(n):
for j in range(i):
if nums[j] < nums[i]:
if dp[i] < dp[j]+1: dp[i], cnt[i] = dp[j... | number-of-longest-increasing-subsequence | simple python dp | gasohel336 | 0 | 84 | number of longest increasing subsequence | 673 | 0.423 | Medium | 11,176 |
https://leetcode.com/problems/number-of-longest-increasing-subsequence/discuss/896111/Python3-bottom-up-dp-O(N2) | class Solution:
def findNumberOfLIS(self, nums: List[int]) -> int:
lis = [[1, 1] for _ in nums] # longest increasing subsequence (length & count)
for i, x in enumerate(nums):
for ii in range(i):
if nums[ii] < x:
if lis[ii][0] + 1 > lis[i][0]: li... | number-of-longest-increasing-subsequence | [Python3] bottom-up dp O(N^2) | ye15 | 0 | 91 | number of longest increasing subsequence | 673 | 0.423 | Medium | 11,177 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/2445685/Two-python-solutions-using-dp-and-a-straightforward-soln | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
counter=1
temp=1
for i in range(0,len(nums)-1):
if nums[i]<nums[i+1]:
temp+=1
if temp>counter:
counter=temp
else:
temp=1
ret... | longest-continuous-increasing-subsequence | Two python solutions using dp and a straightforward soln | guneet100 | 3 | 182 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,178 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/2053719/O(N)-solution | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
maxLen = count = 1
for i in range(len(nums) - 1):
if nums[i] < nums[i + 1]:
count += 1
else:
count = 1
maxLen = max(count, maxLen)
... | longest-continuous-increasing-subsequence | O(N) solution | andrewnerdimo | 2 | 54 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,179 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/1470624/Python3-solution | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
msf = 0 # maxim so far
meh = 1 # maxim ending here
n = len(nums)
if n == 1: return 1
last = nums[0]
for i in range(1, n):
if nums[i] > last:
last = nums[i]
... | longest-continuous-increasing-subsequence | Python3 solution | FlorinnC1 | 1 | 174 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,180 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/1469705/python3-stack-fast-easy-solution | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
stack = [nums[0]]
ret = 1
for i in range(1, len(nums)):
if stack and stack[-1] >= nums[i]: stack.clear()
stack.append(nums[i])
ret = max(ret, len(stack))
return ret | longest-continuous-increasing-subsequence | python3 stack fast easy solution | BichengWang | 1 | 93 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,181 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/2787076/Python-longest-continuous-increasing-subsequence. | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
if len(nums) == 1:
return 1
longest = 0
starting, ending = 0, 0
for i in range(len(nums)-1):
if nums[i] < nums[i+1]:
ending += 1
else:
ending += 1... | longest-continuous-increasing-subsequence | Python longest continuous increasing subsequence. | said_codes | 0 | 3 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,182 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/2749857/Easy-Python-Solution | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
count = 1
ans = 1
for i in range(1,len(nums)):
if nums[i] > nums[i-1]:
count += 1
ans = max(ans,count)
if nums[i] <= nums[i-1]:
count = 1
return... | longest-continuous-increasing-subsequence | Easy Python Solution | imkprakash | 0 | 2 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,183 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/2749221/Is-my-solution-slow | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
ans = -math.inf
i,j = 0,1
cnt = 1
if len(nums) == 1:
return 1
while i in range(len(nums)) and j in range(len(nums)):
if nums[j] > nums[i]:
cnt+=1
else:
... | longest-continuous-increasing-subsequence | Is my solution slow | narutoright152 | 0 | 2 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,184 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/2650953/Easy-and-Faster-solution | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
md=[1]
dx=1
for i in range(1,len(nums)):
if(nums[i]>nums[i-1]):
dx+=1
else:
md.append(dx)
dx=1
md.append(dx)
return max(md) | longest-continuous-increasing-subsequence | Easy and Faster solution | Raghunath_Reddy | 0 | 1 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,185 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/2476144/Two-pointers-approach-98.72-memory-efficient-and-97.86-faster-in-Python | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
l, r = 0, 1
res = 0
ct = 1
while r < len(nums):
if nums[l] < nums[r]:
ct += 1
l += 1
r += 1
else:
ct = 1
l = r
... | longest-continuous-increasing-subsequence | Two pointers approach 98.72 % memory efficient and 97.86 % faster in Python | ankurbhambri | 0 | 35 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,186 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/2311566/Easy-Python-Solution-or-O(n2)-or-O(n)-or-Sliding-Window | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
count=0
for i in range(len(nums)):
a=nums[i]
c=1
for j in range(i+1, len(nums)):
if nums[j]>a:
a=nums[j]
c+=1
else:
... | longest-continuous-increasing-subsequence | Easy Python Solution | O(n2) | O(n) | Sliding Window | Siddharth_singh | 0 | 49 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,187 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/2311566/Easy-Python-Solution-or-O(n2)-or-O(n)-or-Sliding-Window | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
max_overall, max_current=0, 1
a=nums[0]
for i in range(1, len(nums)):
if nums[i]>a:
a=nums[i]
max_current+=1
else:
a=nums[i]
max_overall... | longest-continuous-increasing-subsequence | Easy Python Solution | O(n2) | O(n) | Sliding Window | Siddharth_singh | 0 | 49 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,188 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/2060203/Python-solution | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
ans = []
subs = 0
last = -10**9-1
for i in nums:
if i > last:
subs += 1
last = i
else:
ans += [subs]
subs = 1
la... | longest-continuous-increasing-subsequence | Python solution | StikS32 | 0 | 58 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,189 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/1950369/Python-easy-solution | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
dp=[0]*len(nums);dp[0]=1;i=1
while i!=len(nums):
if nums[i-1]<nums[i]:
dp[i]=dp[i-1]+1
elif nums[i-1]>=nums[i]:
dp[i]=1
i+=1
return max(dp) | longest-continuous-increasing-subsequence | Python easy solution | Whitedevil07 | 0 | 43 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,190 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/1847738/PYTHON-ONE-PASS-solution-step-by-step-(76ms) | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
#Get the length
LENGTH = len ( nums );
#Keep track of the max and the current streak
maxStreak = 1;
streak = 1;
#Point at the first index
pointer = 1;
... | longest-continuous-increasing-subsequence | PYTHON ONE PASS solution step-by-step (76ms) | greg_savage | 0 | 62 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,191 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/1833571/7-Lines-Python-Solution-oror-92-Faster-oror-Memory-less-than-87 | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
cnt=1 ; ans=0
for i in range(len(nums)-1):
if nums[i+1]<=nums[i]:
if cnt>ans: ans=cnt
cnt=0
cnt+=1
return max(cnt,ans) | longest-continuous-increasing-subsequence | 7-Lines Python Solution || 92% Faster || Memory less than 87% | Taha-C | 0 | 67 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,192 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/1520680/One-pass-96-speed | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
count = max_count = 1
for i in range(1, len(nums)):
if nums[i] > nums[i - 1]:
count += 1
else:
max_count = max(max_count, count)
count = 1
return max(ma... | longest-continuous-increasing-subsequence | One pass, 96% speed | EvgenySH | 0 | 122 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,193 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/1414053/Python-oror-Simple-and-Easy-Approach | class Solution:
def findLengthOfLCIS(self, lst: List[int]) -> int:
temp = lst[0]
maxi = 1
count = 1
for i in range(1, len(lst)):
if temp < lst[i]:
count += 1
else:
count = 1
temp = lst[i]
if count > maxi:
maxi = count
return (maxi) | longest-continuous-increasing-subsequence | Python || Simple and Easy Approach | naveenrathore | 0 | 73 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,194 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/1374179/Simple-Python-solution-space%3AO(1)-Time%3AO(N) | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
longest = 1
continuous_long = 1 # considered first element
for i in range(1,len(nums)):
if nums[i-1]<nums[i]:
# chained to continuous
continuous_long+=1
else:... | longest-continuous-increasing-subsequence | Simple Python solution space:O(1) Time:O(N) | SathvikPN | 0 | 42 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,195 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/1238617/Python3-simple-solution-beats-90-users | class Solution:
def findLengthOfLCIS(self, nums):
z = 1
res = 1
for i in range(1,len(nums)):
if nums[i-1] < nums[i]:
z += 1
if i == len(nums)-1:
res = max(res,z)
else:
res = max(res, z)
... | longest-continuous-increasing-subsequence | Python3 simple solution beats 90% users | EklavyaJoshi | 0 | 42 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,196 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/352926/Solution-in-Python-3-(beats-100)-(-O(n)-time-)-(-O(1)-space-) | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
L, M, s = len(nums), 0, 0
if L == 0: return 0
for i in range(L-1):
s += 1
if nums[i+1] <= nums[i]:
if s > M: M = s
s = 0
return max(M,s+1)
- Junaid Mansuri
(LeetCode ID)@hotmail.com | longest-continuous-increasing-subsequence | Solution in Python 3 (beats 100%) ( O(n) time ) ( O(1) space ) | junaidmansuri | 0 | 170 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,197 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/1308581/Python3-dollarolution | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
mx, x = 0, 1
for i in range(1,len(nums)):
if nums[i] > nums[i-1]:
x += 1
else:
if x > mx:
mx = x
x = 1
m = nums[i]
if x ... | longest-continuous-increasing-subsequence | Python3 $olution | AakRay | -1 | 29 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,198 |
https://leetcode.com/problems/longest-continuous-increasing-subsequence/discuss/1206337/Faster-than-93-solutions | class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
curlen = 0
maxlen = 0
if not nums:
return 0
for i in nums:
if nums[i] < nums[i+1]:
curlen +=1
maxlen = curlen
return maxlen | longest-continuous-increasing-subsequence | Faster than 93% solutions | devanshsolani30 | -2 | 67 | longest continuous increasing subsequence | 674 | 0.491 | Easy | 11,199 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.