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/remove-covered-intervals/discuss/1785047/Python-Simple-Python-Solution-By-Sorting-the-List
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals=sorted(intervals) i=0 while i<len(intervals)-1: a,b = intervals[i] p,q = intervals[i+1] if a <= p and q <= b: intervals.remove(intervals[i+1]) i=i-1 elif p <= a and b <= q: intervals.remove(intervals[i]) i=i-1 i=i+1 return len(intervals)
remove-covered-intervals
[ Python ] ✔✔ Simple Python Solution By Sorting the List 🔥✌
ASHOK_KUMAR_MEGHVANSHI
9
677
remove covered intervals
1,288
0.572
Medium
19,100
https://leetcode.com/problems/remove-covered-intervals/discuss/1786906/Python3-oror-brute-force-40-Faster
class Solution: def removeCoveredIntervals(self, new: List[List[int]]) -> int: arr=[] for i in range(len(new)): for j in range(len(new)): if i!=j and new[j][0] <= new[i][0] and new[i][1] <= new[j][1]: arr.append(new[i]) break return len(new)-len(arr)
remove-covered-intervals
Python3 || brute force - 40% Faster
Anilchouhan181
3
60
remove covered intervals
1,288
0.572
Medium
19,101
https://leetcode.com/problems/remove-covered-intervals/discuss/1786026/Python-3-(90ms)-or-Sorted-Matrix-Solution-or-Easy-to-Understand
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals = sorted(intervals, key = lambda x : (x[0], -x[1])) res = 0 ending = 0 for _, end in intervals: if end > ending: res += 1 ending = end return res
remove-covered-intervals
Python 3 (90ms) | Sorted Matrix Solution | Easy to Understand
MrShobhit
3
62
remove covered intervals
1,288
0.572
Medium
19,102
https://leetcode.com/problems/remove-covered-intervals/discuss/1786655/Python-easy-to-read-and-understand
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals.sort(key= lambda x: (x[0], -x[1])) ans, right = 0, 0 for u, v in intervals: if v > right: ans += 1 right = max(right, v) return ans
remove-covered-intervals
Python easy to read and understand
sanial2001
1
38
remove covered intervals
1,288
0.572
Medium
19,103
https://leetcode.com/problems/remove-covered-intervals/discuss/1785717/Python-or-Sort-or-O(NlogN)-or-with-comments
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: # sort the interval by its 0th index intervals.sort() ans = 1 i = 1 curr = intervals[0] while i < len(intervals): # if [1,3] [1,5] [1,7] exists we should ideally count them as 1 as 1,7 will cover both 1,3 and 1,5 if curr[0]==intervals[i][0]: curr = [curr[0],max(curr[1],intervals[i][1])] # when a previous interval couldn't cover current interval, # add 1 to ans and update curr elif curr[1] < intervals[i][1]: ans+=1 curr = intervals[i] i+=1 return ans
remove-covered-intervals
Python | Sort | O(NlogN) | with comments
vishyarjun1991
1
43
remove covered intervals
1,288
0.572
Medium
19,104
https://leetcode.com/problems/remove-covered-intervals/discuss/1785678/Python-oror-Sorting-oror-98-Faster-oror-96-Space-Efficient
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: c = len(intervals) intervals.sort(key=lambda x: x[0]) x, y = intervals[0] for i in range(1, len(intervals)): if x <= intervals[i][0] and intervals[i][1] <= y: c -= 1 continue if intervals[i][0] <= x and y <= intervals[i][1]: c -= 1 x, y = intervals[i][0], intervals[i][1] return c
remove-covered-intervals
Python || Sorting || 98% Faster || 96% Space Efficient
cherrysri1997
1
71
remove covered intervals
1,288
0.572
Medium
19,105
https://leetcode.com/problems/remove-covered-intervals/discuss/2749987/intuitive-solution-using-python
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals.sort() prevStart, prevEnd = intervals[0][0], intervals[0][1] count = 0 for start, end in intervals[1:]: if prevEnd >= end or (start == prevStart and end >= prevEnd): prevEnd = max(end, prevEnd) count = count + 1 else: prevStart = start prevEnd = end return len(intervals) - count # intervals.sort() # prevStart, prevEnd = intervals[0] # count = 0 # for start, end in intervals[1:]: # if end <= prevEnd or (start == prevStart and end >= prevEnd): # prevEnd = max(end, prevEnd) # count = count + 1 # else: # prevEnd = end # return len(intervals) - count
remove-covered-intervals
intuitive solution using python
nagarkarjayesh4all
0
3
remove covered intervals
1,288
0.572
Medium
19,106
https://leetcode.com/problems/remove-covered-intervals/discuss/2283574/Python-sort
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals.sort(key=lambda interval: (interval[0], -interval[1])) removed = 0 interval = intervals[0] for ni in intervals[1:]: if ni[0] >= interval[0] and ni[1] <= interval[1]: removed += 1 else: interval = ni return len(intervals) - removed
remove-covered-intervals
Python, sort
blue_sky5
0
14
remove covered intervals
1,288
0.572
Medium
19,107
https://leetcode.com/problems/remove-covered-intervals/discuss/1792078/Python3-similar-to-Merge-intervals-sorting
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: # seems like merge intervals? no, kind of but # with merge its any overlap, here covered is # c <= a and b <= d intervals.sort() # NlogN time for the sort ans = len(intervals) # at worst x, y = intervals[0][0], intervals[0][1] for x2, y2 in intervals[1:]: # case where next is covered by prev if x2 >= x and y2 <= y: # there was overlap so you can "remove" aka merge ans -= 1 # x and y stay the same, it "covers" elif x >= x2 and y <= y2: # case where current is covered by next ans -= 1 # new thing to look at is range doing the "covering" x = x2 y = y2 else: # otherwise set to cur to become prev to look at # on next iteration x = x2 y = y2 return ans
remove-covered-intervals
🛳 Python3, similar to Merge intervals, sorting
normalpersontryingtopayrent
0
31
remove covered intervals
1,288
0.572
Medium
19,108
https://leetcode.com/problems/remove-covered-intervals/discuss/1787021/Python-Solution-using-iteration
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: uncovered_internals = len(intervals) for i in range(len(intervals)): for j in range(len(intervals)): if i == j: continue if intervals[i][0] >= intervals[j][0] and intervals[i][ 1] <= intervals[j][1]: uncovered_internals -= 1 break return uncovered_internals
remove-covered-intervals
Python Solution using iteration
pradeep288
0
29
remove covered intervals
1,288
0.572
Medium
19,109
https://leetcode.com/problems/remove-covered-intervals/discuss/1786579/Python-oror-Sort
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals.sort(key = lambda x:(x[0], -x[1])) pos = 0 count = 1 for i in range(1, len(intervals)): if intervals[i][0] < intervals[pos][0] or intervals[i][1] > intervals[pos][1]: pos = i count += 1 return count
remove-covered-intervals
Python || Sort
kalyan_yadav
0
20
remove covered intervals
1,288
0.572
Medium
19,110
https://leetcode.com/problems/remove-covered-intervals/discuss/1785881/fast-python-solution
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: #Time complexity: O(nlogn), Space complexity:O(1), beats 98.7% intervals.sort() res = 0 bound, pre = 0, -1 for i in intervals: if i[1]>bound: bound = i[1] if i[0]>pre: pre = i[0] res +=1 return res
remove-covered-intervals
fast python solution
verySmallPotato
0
7
remove covered intervals
1,288
0.572
Medium
19,111
https://leetcode.com/problems/remove-covered-intervals/discuss/1785875/Python3-Solution-with-using-sorting
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals.sort(key=lambda l: (l[0],-l[1])) max_right = intervals[0][1] covered_cnt = 0 for i in range(1, len(intervals)): if intervals[i][1] <= max_right: covered_cnt += 1 else: max_right = intervals[i][1] return len(intervals) - covered_cnt
remove-covered-intervals
[Python3] Solution with using sorting
maosipov11
0
7
remove covered intervals
1,288
0.572
Medium
19,112
https://leetcode.com/problems/remove-covered-intervals/discuss/1785793/Python-or-Very-Easy-Solution
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals.sort() index = 1 result = 0 length = len(intervals) while index < length: prev_start, prev_end = intervals[index-1][0], intervals[index-1][1] cur_start, cur_end = intervals[index][0], intervals[index][1] # This is the additional new condition if cur_start == prev_start: intervals[index-1][1] = max(prev_end, cur_end) intervals.pop(index) length -= 1 elif cur_start >= prev_start and cur_end <= prev_end: intervals[index-1][0] = min(prev_start, cur_start) intervals[index-1][1] = max(prev_end, cur_end) intervals.pop(index) length -= 1 else: index += 1 return length
remove-covered-intervals
Python | Very Easy Solution
Call-Me-AJ
0
40
remove covered intervals
1,288
0.572
Medium
19,113
https://leetcode.com/problems/remove-covered-intervals/discuss/1784692/Python3-oror-Sorting-and-Set-Theory-oror-Easy-to-understand
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: n = len(intervals) ans = n intervals.sort() a = intervals[0][0] b = intervals[0][1] for i in range(1, n): if intervals[i][1] <= b: ans -= 1 else: if a == intervals[i][0]: ans -= 1 a = intervals[i][0] b = intervals[i][1] return ans
remove-covered-intervals
Python3 || Sorting & Set Theory || Easy to understand
sr_vrd
0
39
remove covered intervals
1,288
0.572
Medium
19,114
https://leetcode.com/problems/remove-covered-intervals/discuss/1784594/Python-3-EASY-Intuitive-Solution
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals.sort(key=lambda x: (x[0], ~x[1])) count = 1 start, end = intervals[0] for i, j in intervals[1:]: if i >= start and j <= end: continue count += 1 start, end = i, j return count
remove-covered-intervals
✅ [Python 3] EASY Intuitive Solution
JawadNoor
0
25
remove covered intervals
1,288
0.572
Medium
19,115
https://leetcode.com/problems/remove-covered-intervals/discuss/1784490/Python-or-sort-and-compare
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: n = len(intervals) if n == 1: return 1 intervals.sort(key = lambda x:(x[0], -x[1])) result = 1 maxRight = intervals[0][1] for i in range(1, n): if intervals[i][1] <= maxRight: continue else: result += 1 maxRight = intervals[i][1] return result
remove-covered-intervals
Python | sort and compare
Mikey98
0
34
remove covered intervals
1,288
0.572
Medium
19,116
https://leetcode.com/problems/remove-covered-intervals/discuss/880466/Remove-Covered-Intervals-Python3-Solution
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: toDelete=set() intervals = sorted(intervals,key=lambda x:(x[0],-x[1])) n=len(intervals) for i in range(n-1): if i in toDelete: continue for j in range(i+1,n): if j in toDelete: continue if intervals[j][1]<=intervals[i][1]: toDelete.add(j) return n-len(toDelete)
remove-covered-intervals
Remove Covered Intervals Python3 Solution
harshitCode13
0
44
remove covered intervals
1,288
0.572
Medium
19,117
https://leetcode.com/problems/remove-covered-intervals/discuss/879446/Python-Solution
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: count = 0 prev_interval = None # Sorting ascendingly by start value and then descendingly by end value to cover all cases for curr_interval in sorted(intervals, key=lambda x: (x[0], -x[1])): # Check if previous interval exists and previous interval includes current interval if prev_interval and prev_interval[0] <= curr_interval[0] and prev_interval[1] >= curr_interval[1]: continue prev_interval = curr_interval count += 1 return count
remove-covered-intervals
Python Solution
ehdwn1212
0
51
remove covered intervals
1,288
0.572
Medium
19,118
https://leetcode.com/problems/remove-covered-intervals/discuss/878893/Python3-O(NlogN)-Greedy-Approach-Sort-and-count-or-Easy-to-understand
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals.sort(key=lambda x:(x[0], -x[1])) init=intervals[0] ans=1 for i in range(1,len(intervals)): if init[1]<intervals[i][1]: ans+=1 init=[min(init[0],intervals[i][0]), intervals[i][1]] return ans
remove-covered-intervals
[Python3] O(NlogN) Greedy Approach Sort and count | Easy to understand
_vaishalijain
0
33
remove covered intervals
1,288
0.572
Medium
19,119
https://leetcode.com/problems/remove-covered-intervals/discuss/482261/Python3-Solve-with-sort
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: intervals.sort() cur_left, cur_right = intervals[0] count = 1 for interval in intervals: if cur_left == interval[0]: cur_right = interval[1] else: if cur_right >= interval[1]: continue else: cur_left, cur_right = interval count += 1 return count ```
remove-covered-intervals
Python3 Solve with sort
alanzhu_xiaozhi
0
82
remove covered intervals
1,288
0.572
Medium
19,120
https://leetcode.com/problems/remove-covered-intervals/discuss/452956/Python-3-(six-lines)
class Solution: def removeCoveredIntervals(self, I: List[List[int]]) -> int: t = 0 for (X,Y) in I: for (x,y) in I: if x<=X and Y<=y and (X,Y) != (x,y): break else: t += 1 return t - Junaid Mansuri - Chicago, IL
remove-covered-intervals
Python 3 (six lines)
junaidmansuri
0
142
remove covered intervals
1,288
0.572
Medium
19,121
https://leetcode.com/problems/remove-covered-intervals/discuss/451349/Python3-greedy
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: ans = 0 lo = hi = -1 #set anchor for x, y in sorted(intervals): if y > hi: #test for coverage if x > lo: ans += 1 lo, hi = x, y #set new anchor return ans
remove-covered-intervals
[Python3] greedy
ye15
0
69
remove covered intervals
1,288
0.572
Medium
19,122
https://leetcode.com/problems/remove-covered-intervals/discuss/451349/Python3-greedy
class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -> int: ans, prev = 0, -inf for _, y in sorted(intervals, key=lambda x: (x[0], -x[1])): if y > prev: ans, prev = ans+1, y return ans
remove-covered-intervals
[Python3] greedy
ye15
0
69
remove covered intervals
1,288
0.572
Medium
19,123
https://leetcode.com/problems/minimum-falling-path-sum-ii/discuss/1998001/Python-DP-Solution-or-Min-and-Second-min-or-Faster-than-79.77
class Solution: def minFallingPathSum(self, grid: List[List[int]]) -> int: rows = len(grid) cols = len(grid[0]) min1 = min11 = float('inf') # min1 -> minimum , min11 -> second minimum in even indexed row min2 = min22 = float('inf') # min2 -> minimum , min22 -> second minimum in odd indexed row for i in range(rows): for j in range(cols): if i==0: if grid[i][j]<=min1: # Logic to find minimum and second minimum min11 = min1 min1 = grid[i][j] elif grid[i][j]<min11: min11 = grid[i][j] else: if i%2: if grid[i-1][j]==min1: # If adjacent -> then add the second minimum value grid[i][j] += min11 else: # Else -> add the minimum value grid[i][j] += min1 if grid[i][j]<min2: # Logic to find minimum and second minimum min22 = min2 min2 = grid[i][j] elif grid[i][j]<min22: min22 = grid[i][j] else: if grid[i-1][j]==min2: grid[i][j] += min22 else: grid[i][j] += min2 if grid[i][j]<min1: # Logic to find minimum and second minimum min11 = min1 min1 = grid[i][j] elif grid[i][j]<min11: min11 = grid[i][j] if i%2: # Reset the minimum and second minimum values accordingly min1 = min11 = float('inf') else: min2 = min22 = float('inf') return min(grid[-1]) # Return the minimum element in last row
minimum-falling-path-sum-ii
Python DP Solution | Min and Second min | Faster than 79.77%
lin_lance_07
2
148
minimum falling path sum ii
1,289
0.593
Hard
19,124
https://leetcode.com/problems/minimum-falling-path-sum-ii/discuss/1577762/Simple-Python-DP-Solution-or-Time%3A-O(N2)-or-Space%3A-O(N)
class Solution: def minFallingPathSum(self, grid: List[List[int]]) -> int: # Time : O(N^2) # Space : O(N) rows = len(grid) cols = len(grid[0]) if cols == 1: return grid[0][0] dp = [float('inf') for i in range(cols + 2) ] min_in_left = [float('inf') if i in {0, cols +1} else 0 for i in range(cols + 2) ] min_in_right = [float('inf') if i in {0, cols +1} else 0 for i in range(cols + 2) ] for i in range(rows-1,-1,-1): for j in range(1,cols+1): dp[j] = grid[i][j-1] + min(min_in_left[j-1], min_in_right[j+1]) for j in range(1, cols+1): min_in_left[j] = min(dp[j], min_in_left[j-1]) for j in range(cols, 0, -1): min_in_right[j] = min(dp[j], min_in_right[j+1]) return min(dp)
minimum-falling-path-sum-ii
Simple Python DP Solution | Time: O(N^2) | Space: O(N)
gangasingh1807
1
151
minimum falling path sum ii
1,289
0.593
Hard
19,125
https://leetcode.com/problems/minimum-falling-path-sum-ii/discuss/451374/Python3-Bottom-up-DP
class Solution: def minFallingPathSum(self, arr: List[List[int]]) -> int: for i in range(1, len(arr)): #find 1st &amp; 2nd mininum m1 = m2 = float("inf") for x in arr[i-1]: if x < m1: m1, m2 = x, m1 elif x < m2: m2 = x #update min falling path sum as of row i for j in range(len(arr[0])): arr[i][j] += (m1 if arr[i-1][j] != m1 else m2) return min(arr[-1])
minimum-falling-path-sum-ii
[Python3] Bottom-up DP
ye15
1
64
minimum falling path sum ii
1,289
0.593
Hard
19,126
https://leetcode.com/problems/minimum-falling-path-sum-ii/discuss/2080468/python-3-oror-dp-oror-O(n2)O(1)
class Solution: def minFallingPathSum(self, grid: List[List[int]]) -> int: n = len(grid) if n == 1: return grid[0][0] if grid[0][0] <= grid[0][1]: prevMin1, prevMin2 = 0, 1 else: prevMin1, prevMin2 = 1, 0 for j in range(2, n): if grid[0][j] <= grid[0][prevMin1]: prevMin1, prevMin2 = j, prevMin1 elif grid[0][j] < grid[0][prevMin2]: prevMin2 = j for i in range(1, n): min1, min2 = -1, -1 min1Val = min2Val = math.inf for j in range(n): if j != prevMin1: grid[i][j] += grid[i - 1][prevMin1] else: grid[i][j] += grid[i - 1][prevMin2] if grid[i][j] <= min1Val: min1, min2 = j, min1 min1Val, min2Val = grid[i][j], min1Val elif grid[i][j] < min2Val: min2 = j min2Val = grid[i][j] prevMin1, prevMin2 = min1, min2 return min(grid[-1])
minimum-falling-path-sum-ii
python 3 || dp || O(n^2)/O(1)
dereky4
0
40
minimum falling path sum ii
1,289
0.593
Hard
19,127
https://leetcode.com/problems/minimum-falling-path-sum-ii/discuss/2066792/Python-or-DP
class Solution: def minFallingPathSum(self, ma: List[List[int]]) -> int: n = len(ma) m = len(ma[0]) l = [] for i in range(m): l.append([ma[n-1][i],i]) l.sort() for i in range(n-2,-1,-1): t = [] for j in range(len(ma[0])): if l[0][1] == j: ma[i][j] += l[1][0] else: ma[i][j] += l[0][0] t.append([ma[i][j],j]) t.sort() l = t.copy() return min(ma[0])
minimum-falling-path-sum-ii
Python | DP
Shivamk09
0
31
minimum falling path sum ii
1,289
0.593
Hard
19,128
https://leetcode.com/problems/minimum-falling-path-sum-ii/discuss/1812696/Python3-oror-DP-oror-TC%3A-O(N*N)-SC%3A-O(N*N)
class Solution: def minFallingPathSum(self, grid: List[List[int]]) -> int: n= len(grid) dp = [[0 for c in range(n)]for r in range(n)] for c in range(n): dp[0][c] = grid[0][c] for r in range(1,n): for c in range(n): #find the minimum in the previous row, except that particular column if c == 0: dp[r][c] = grid[r][c] + min(dp[r-1][1:]) elif c == n - 1: dp[r][c] = grid[r][c] + min(dp[r-1][:n-1]) else: min_left = min(dp[r-1][:c]) min_right = min(dp[r-1][c+1:]) dp[r][c] = grid[r][c] + min(min_left,min_right) return min(dp[-1])
minimum-falling-path-sum-ii
Python3 || DP || TC: O(N*N), SC: O(N*N)
s_m_d_29
0
55
minimum falling path sum ii
1,289
0.593
Hard
19,129
https://leetcode.com/problems/minimum-falling-path-sum-ii/discuss/1771087/Python-top-down-DP-with-time-complexity-O(N**2*log(N))-and-space-complexity-O(N)
class Solution: def minFallingPathSum(self, grid: List[List[int]]) -> int: nr = len(grid) nc = len(grid[0]) dp = grid[0] # only store the previous row # sort last row so we could choose from the minimum value directly sidx = [i[0] for i in sorted(enumerate(dp), key=lambda x: x[1])] for r in range(1, nr): # initialize a new row to store new results new_dp = [0] * nc for c in range(nc): # get minimum possible value from previous row pc = sidx[0] if sidx[0] != c else sidx[1] new_dp[c] = dp[pc] + grid[r][c] # when finish another row, update dp and its order dp = new_dp sidx = [i[0] for i in sorted(enumerate(dp), key=lambda x: x[1])] return dp[sidx[0]]
minimum-falling-path-sum-ii
Python top down DP with time complexity O(N**2*log(N)) and space complexity O(N)
rikku1983
0
39
minimum falling path sum ii
1,289
0.593
Hard
19,130
https://leetcode.com/problems/minimum-falling-path-sum-ii/discuss/1557862/Python-and-GO-or-DP-Faster-100-or-O(1)-space-or-O(N)-time
class Solution: def minFallingPathSum(self, grid: List[List[int]]) -> int: if len(grid) == 1: return grid[0][0] minVal = 0 minPos = -1 prevMin = 0 for row in grid: # Since the maximum path sum would be 99 * 200 = 19800, set initital min values to max + 1 newMin = 19801 newMinPos = 0 newPrevMin = 19801 for i, val in enumerate(row): if i == minPos: val += prevMin else: val += minVal if val < newMin: newPrevMin = newMin newMin = val newMinPos = i elif val < newPrevMin: newPrevMin = val minVal = newMin minPos = newMinPos prevMin = newPrevMin return minVal
minimum-falling-path-sum-ii
Python & GO | DP Faster 100% | O(1) space | O(N) time
dartkron
0
87
minimum falling path sum ii
1,289
0.593
Hard
19,131
https://leetcode.com/problems/minimum-falling-path-sum-ii/discuss/1357587/Python-3-DP-solutions-with-time-O(mn)-and-space%3A-O(n)O(1)
class Solution: def minFallingPathSum(self, arr: List[List[int]]) -> int: if not arr or not arr[0]: return 0 m, n = len(arr), len(arr[0]) if n == 1: return arr[0][0] if m == 1 else 0 dp = arr[0] prev = [x for x in dp] min1, min2 = self.find_two_smallest(arr[0]) for i in range(1, m): for j in range(n): dp[j] = prev[min1] if j != min1 else prev[min2] dp[j] += arr[i][j] min1, min2 = self.find_two_smallest(dp) dp, prev = prev, dp return min(prev) def find_two_smallest(self, arr): n = len(arr) min1, min2 = 0, 1 for i in range(1, n): if arr[i] < arr[min1]: min1, min2 = i, min1 elif arr[i] < arr[min2]: min2 = i return min1, min2
minimum-falling-path-sum-ii
Python 3 DP solutions with time O(mn) and space: O(n)/O(1)
FXSD
0
49
minimum falling path sum ii
1,289
0.593
Hard
19,132
https://leetcode.com/problems/minimum-falling-path-sum-ii/discuss/1357587/Python-3-DP-solutions-with-time-O(mn)-and-space%3A-O(n)O(1)
class Solution: def minFallingPathSum(self, arr: List[List[int]]) -> int: if not arr or not arr[0]: return 0 m, n = len(arr), len(arr[0]) if n == 1: return arr[0][0] if m == 1 else 0 dp = [[-1, 0], [-1, 0]] for i in range(m): min1, min2 = [[-1, float("inf")], [-1, float("inf")]] for j in range(n): min_idx = 0 if dp[0][0] != j else 1 cur_val = dp[min_idx][1] + arr[i][j] if cur_val < min1[1]: min1, min2 = [j, cur_val], min1 elif cur_val < min2[1]: min2 = [j, cur_val] dp = [min1, min2] return dp[0][1]
minimum-falling-path-sum-ii
Python 3 DP solutions with time O(mn) and space: O(n)/O(1)
FXSD
0
49
minimum falling path sum ii
1,289
0.593
Hard
19,133
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/455239/Python-Simple.-20ms.
class Solution: def getDecimalValue(self, head: ListNode) -> int: answer = 0 while head: answer = 2*answer + head.val head = head.next return answer
convert-binary-number-in-a-linked-list-to-integer
[Python] Simple. 20ms.
rohin7
200
7,800
convert binary number in a linked list to integer
1,290
0.825
Easy
19,134
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/451811/Python3-concise-solution
class Solution: def getDecimalValue(self, head: ListNode) -> int: ans = 0 while head: ans = 2*ans + head.val head = head.next return ans
convert-binary-number-in-a-linked-list-to-integer
[Python3] concise solution
ye15
35
4,400
convert binary number in a linked list to integer
1,290
0.825
Easy
19,135
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1616498/PYTHON.-easy-and-clear-solution.
class Solution: def getDecimalValue(self, head: ListNode) -> int: res = 0 while head: res = 2*res + head.val head = head.next return res
convert-binary-number-in-a-linked-list-to-integer
PYTHON. easy & clear solution.
m-d-f
7
417
convert binary number in a linked list to integer
1,290
0.825
Easy
19,136
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1615736/Simple-5-line-solution-(Python)-time%3A-O(N)-space%3A-O(1)
class Solution: def getDecimalValue(self, head: ListNode) -> int: res = 0 while head: res = res * 2 + head.val head = head.next return res
convert-binary-number-in-a-linked-list-to-integer
Simple 5 line solution (Python) time: O(N), space: O(1)
kryuki
5
291
convert binary number in a linked list to integer
1,290
0.825
Easy
19,137
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/691292/PYTHON-EASY-28-ms-using-int(numberbase-2)
class Solution: def getDecimalValue(self, head: ListNode) -> int: binaryNumberString = "" while head: binaryNumberString += str(head.val) head = head.next return int(binaryNumberString,2)
convert-binary-number-in-a-linked-list-to-integer
[PYTHON]- EASY - 28 ms using int(number,base = 2)
amanpathak2909
4
313
convert binary number in a linked list to integer
1,290
0.825
Easy
19,138
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1369627/Python-or-Simple-or-99.54-faster
class Solution: def getDecimalValue(self, head: ListNode) -> int: ans = 0 while head: ans = 2*ans + head.val head = head.next return ans
convert-binary-number-in-a-linked-list-to-integer
Python | Simple | 99.54% faster
shraddhapp
2
170
convert binary number in a linked list to integer
1,290
0.825
Easy
19,139
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/2246470/Python3-O(N)-solution
class Solution: def getDecimalValue(self, head: ListNode) -> int: result = 0 while head: result = result * 2 + head.val head = head.next return result
convert-binary-number-in-a-linked-list-to-integer
📌 Python3 O(N) solution
Dark_wolf_jss
1
19
convert binary number in a linked list to integer
1,290
0.825
Easy
19,140
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1881639/Python-3-or-2-easy-solution-or-both-are-fastest
class Solution: def getDecimalValue(self, head: ListNode) -> int: s="" while head: s+=str(head.val) head=head.next return int(s,2)
convert-binary-number-in-a-linked-list-to-integer
Python 3 | 2 easy solution | both are fastest
Anilchouhan181
1
91
convert binary number in a linked list to integer
1,290
0.825
Easy
19,141
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1881639/Python-3-or-2-easy-solution-or-both-are-fastest
class Solution: def getDecimalValue(self, head: ListNode) -> int: ans=0 while head: ans=ans*2+head.val head=head.next return ans
convert-binary-number-in-a-linked-list-to-integer
Python 3 | 2 easy solution | both are fastest
Anilchouhan181
1
91
convert binary number in a linked list to integer
1,290
0.825
Easy
19,142
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/918976/3-Python-Solutions-Explained-(video-%2B-code)
class Solution: def getDecimalValue(self, head: ListNode) -> int: binary = "" while head: binary += str(head.val) head = head.next return int(binary, 2)
convert-binary-number-in-a-linked-list-to-integer
3 Python Solutions Explained (video + code)
spec_he123
1
84
convert binary number in a linked list to integer
1,290
0.825
Easy
19,143
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/918976/3-Python-Solutions-Explained-(video-%2B-code)
class Solution: def getDecimalValue(self, head: ListNode) -> int: res = 0 while head: res = (res << 1) | head.val head = head.next return res
convert-binary-number-in-a-linked-list-to-integer
3 Python Solutions Explained (video + code)
spec_he123
1
84
convert binary number in a linked list to integer
1,290
0.825
Easy
19,144
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/918976/3-Python-Solutions-Explained-(video-%2B-code)
class Solution: def getDecimalValue(self, head: ListNode) -> int: res = 0 while head: res = 2 * res + head.val head = head.next return res
convert-binary-number-in-a-linked-list-to-integer
3 Python Solutions Explained (video + code)
spec_he123
1
84
convert binary number in a linked list to integer
1,290
0.825
Easy
19,145
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/861521/Python3%3A-24-ms-faster-than-94.51-13.8-MB-less-than-50.89.-bit-manipulation
class Solution: def getDecimalValue(self, head: ListNode) -> int: result = 0 while(head): result <<= 1 result |= head.val head = head.next return result
convert-binary-number-in-a-linked-list-to-integer
Python3: 24 ms, faster than 94.51%, 13.8 MB, less than 50.89%. bit manipulation
Anonyknight
1
69
convert binary number in a linked list to integer
1,290
0.825
Easy
19,146
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/2456959/easy-python-code-or-linked-list
class Solution: def getDecimalValue(self, head: ListNode) -> int: x = [] output = 0 temp = head while(not temp.next==None): x.append(temp.val) temp = temp.next x.append(temp.val) j = len(x)-1 for i in x: output += i*(2**j) j-=1 return output
convert-binary-number-in-a-linked-list-to-integer
easy python code | linked list
dakash682
0
30
convert binary number in a linked list to integer
1,290
0.825
Easy
19,147
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/2203140/Python-Simple-Python-Solution
class Solution: def getDecimalValue(self, head: ListNode) -> int: number = '' while head != None: number = number + str(head.val) head = head.next result = int(number,2) return result
convert-binary-number-in-a-linked-list-to-integer
[ Python ] ✅✅ Simple Python Solution 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
87
convert binary number in a linked list to integer
1,290
0.825
Easy
19,148
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/2171951/Python-Linked-List-Simple-Solution
class Solution: def getDecimalValue(self, head: ListNode) -> int: decimal = 0 valArr = list() temp = head while(temp): valArr.insert(0, temp.val) temp = temp.next for i in range(len(valArr)): decimal += (2**i)*valArr[i] return (decimal)
convert-binary-number-in-a-linked-list-to-integer
Python Linked List Simple Solution
kaus_rai
0
45
convert binary number in a linked list to integer
1,290
0.825
Easy
19,149
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/2155310/Faster-than-97.22-Percent-Python
class Solution: def getDecimalValue(self, head: ListNode) -> int: res = 0 while head: res = 2*res + head.val head = head.next return res
convert-binary-number-in-a-linked-list-to-integer
Faster than 97.22 Percent-Python
jayeshvarma
0
37
convert binary number in a linked list to integer
1,290
0.825
Easy
19,150
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1999081/Python-Easy-Solution-or-Optimized-or-Faster
class Solution: def getDecimalValue(self, head: ListNode) -> int: a = '' while head is not None: a += str(head.val) head = head.next return int(a, 2)
convert-binary-number-in-a-linked-list-to-integer
[Python] Easy Solution | Optimized | Faster
jamil117
0
99
convert binary number in a linked list to integer
1,290
0.825
Easy
19,151
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1986969/O(N)-or-Single-loop-or-Using-Doubling-Technique
class Solution: def getDecimalValue(self, head: ListNode) -> int: ans = 0 while head!=None: ans = (2*ans) + head.val head=head.next return ans
convert-binary-number-in-a-linked-list-to-integer
O(N) | Single loop | Using Doubling Technique
divyamohan123
0
47
convert binary number in a linked list to integer
1,290
0.825
Easy
19,152
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1978470/Python-Easy-Solution
class Solution: def getDecimalValue(self, head: ListNode) -> int: if head.val == 1 and head.next == None: return int(1) else: s = '' pt = head while pt: s += str(pt.val) pt = pt.next b = s[::-1] b = b[1:] add = 0 count = 1 for i in b: m = (2**count) * int(i) add += m count += 1 last = int(s[len(s)-1]) add += last return add
convert-binary-number-in-a-linked-list-to-integer
[Python] Easy Solution
natscripts
0
49
convert binary number in a linked list to integer
1,290
0.825
Easy
19,153
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1953047/java-python-simple-solution-(Time-On-space-O1)
class Solution: def getDecimalValue(self, head: ListNode) -> int: length = -1 num = 0 w = head while w != None : w = w.next length += 1 mask = 1 << length w = head while w != None : if w.val == 1 : num += mask w = w.next mask >>= 1 return num
convert-binary-number-in-a-linked-list-to-integer
java, python - simple solution (Time On, space O1)
ZX007java
0
32
convert binary number in a linked list to integer
1,290
0.825
Easy
19,154
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1943002/Easy-Python3-Beats-99-Speed
class Solution: def getDecimalValue(self, head: ListNode) -> int: current = head s = '0b' while current: s += str(current.val) current = current.next return int(s, 2) ```
convert-binary-number-in-a-linked-list-to-integer
Easy Python3 Beats 99% Speed
aves_90
0
23
convert binary number in a linked list to integer
1,290
0.825
Easy
19,155
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1896958/Ez-oror-3-Sols-oror-beats-~88
class Solution: def getDecimalValue(self, head: ListNode) -> int: v="" while head!=None: v+=str(head.val) head=head.next return int(v, 2)
convert-binary-number-in-a-linked-list-to-integer
Ez || 3 Sols || beats ~88 %
ashu_py22
0
10
convert binary number in a linked list to integer
1,290
0.825
Easy
19,156
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1896958/Ez-oror-3-Sols-oror-beats-~88
class Solution: def getDecimalValue(self, head: ListNode) -> int: v=head.val while head.next: v= v * 2 + head.next.val head=head.next return v
convert-binary-number-in-a-linked-list-to-integer
Ez || 3 Sols || beats ~88 %
ashu_py22
0
10
convert binary number in a linked list to integer
1,290
0.825
Easy
19,157
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1896958/Ez-oror-3-Sols-oror-beats-~88
class Solution: def getDecimalValue(self, head: ListNode) -> int: v=head.val while head.next: v= (v<<1) + head.next.val head=head.next return v
convert-binary-number-in-a-linked-list-to-integer
Ez || 3 Sols || beats ~88 %
ashu_py22
0
10
convert binary number in a linked list to integer
1,290
0.825
Easy
19,158
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1800521/Python-3-Very-simple-solution-using-math-(24ms-14MB)
class Solution: def getDecimalValue(self, head: ListNode) -> int: answer = 0 while head: bit = head.val answer = (2 * answer) + bit head = head.next return answer
convert-binary-number-in-a-linked-list-to-integer
[Python 3] Very simple solution using math (24ms, 14MB)
seankala
0
61
convert binary number in a linked list to integer
1,290
0.825
Easy
19,159
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1800521/Python-3-Very-simple-solution-using-math-(24ms-14MB)
class Solution: def reverse_list(self, head: ListNode) -> ListNode: prev = None current = head while current: next_ = curr.next curr.next = prev prev = current current = next_ head = prev return head def getDecimalValue(self, head: ListNode) -> int: head = self.reverse_list(head) answer = 0 power = 0 while head: bit = head.val decimal = bit * (2**power) answer += decimal power += 1 head = head.next return answer
convert-binary-number-in-a-linked-list-to-integer
[Python 3] Very simple solution using math (24ms, 14MB)
seankala
0
61
convert binary number in a linked list to integer
1,290
0.825
Easy
19,160
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1691267/python-solution
class Solution: def getDecimalValue(self, head: ListNode) -> int: curr = head l = [] while curr: l.append(str(curr.val)) curr = curr.next m = "".join(l) return int(m, 2)
convert-binary-number-in-a-linked-list-to-integer
python solution
Ashi_garg
0
100
convert binary number in a linked list to integer
1,290
0.825
Easy
19,161
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/discuss/1682929/python3-simple
class Solution: def getDecimalValue(self, head: ListNode) -> int: decn = 0 while head: decn = decn*2 + head.val head = head.next return decn
convert-binary-number-in-a-linked-list-to-integer
python3 simple
snagsbybalin
0
28
convert binary number in a linked list to integer
1,290
0.825
Easy
19,162
https://leetcode.com/problems/sequential-digits/discuss/1713379/Python-3-(20ms)-or-Faster-than-95-or-Generating-All-Sequential-Digits-within-Range
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: l=len(str(low)) h=len(str(high)) ans=[] a=[12,23,34,45,56,67,78,89] t=0 while l<=h: for i in a: for j in range(0,l-2): t=i%10 if i==9: break i=int(str(i)+str(t+1)) if i%10==0: break if i>=low and i<=high: ans.append(i) l+=1 return ans
sequential-digits
Python 3 (20ms) | Faster than 95% | Generating All Sequential Digits within Range
MrShobhit
3
64
sequential digits
1,291
0.613
Medium
19,163
https://leetcode.com/problems/sequential-digits/discuss/853522/Python-3-or-Backtracking-DFS-or-Explanation
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: ans = [] def dfs(cur, digit): if digit <= 10 and low <= cur <= high: ans.append(cur) # add number satisfy the condition elif cur < low: pass # continue check larger values else: return # stop condition cur = cur * 10 + digit # generating number with sequential digits dfs(cur, digit+1) # backtracking for i in range(1, 10): dfs(0, i) # start with each digit from 1 to 9, inclusive return sorted(ans) # sort the result and return
sequential-digits
Python 3 | Backtracking, DFS | Explanation
idontknoooo
3
314
sequential digits
1,291
0.613
Medium
19,164
https://leetcode.com/problems/sequential-digits/discuss/452351/Python3%3A-Faster-than-100-less-memory-than-100
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: num = [] for x in range(1,9): while x <= high: r = x % 10 if r == 0: break if x >= low: num.append(x) x = (x * 10) + r + 1 return sorted(num)
sequential-digits
Python3: Faster than 100%, less memory than 100%
whenakko
3
613
sequential digits
1,291
0.613
Medium
19,165
https://leetcode.com/problems/sequential-digits/discuss/1715565/Python-Simple-Python-Solution-!!-Using-String-Subarray
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: ans=[] s='123456789' for i in range(len(s)-1): j=2 while j<10: if int(s[i:i+j]) not in ans: if int(s[i:i+j]) in range(low,high+1): ans.append(int(s[i:i+j])) j=j+1 ans.sort() return ans
sequential-digits
[ Python ] Simple Python Solution !! Using String Subarray ✔✔
ASHOK_KUMAR_MEGHVANSHI
2
38
sequential digits
1,291
0.613
Medium
19,166
https://leetcode.com/problems/sequential-digits/discuss/1713098/Python3-simplest-and-fastest-solution-in-O(1)-with-explanation
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: # https://oeis.org/A138141 nums = (12, 23, 34, 45, 56, 67, 78, 89, 123, 234, 345, 456, 567, 678, 789, 1234, 2345, 3456, 4567, 5678, 6789, 12345, 23456, 34567, 45678, 56789, 123456, 234567, 345678, 456789, 1234567, 2345678, 3456789, 12345678, 23456789, 123456789) return filter(lambda n: low <= n <= high, nums)
sequential-digits
[Python3] simplest and fastest solution in O(1), with explanation
modem93
2
52
sequential digits
1,291
0.613
Medium
19,167
https://leetcode.com/problems/sequential-digits/discuss/1711812/Python-oror-Simple-Approach-oror-Recursion-DP-DFS-Backtrackingoror-Easy-to-understand
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: ans=[] for i in range(1,10): temp=i for j in range(i+1,10): temp=10*temp+j if temp in range(low,high+1): ans.append(temp) ans.sort() return ans
sequential-digits
Python || Simple Approach || ❌ Recursion ❌ DP ❌ DFS ❌ Backtracking|| Easy to understand
rushi_javiya
2
31
sequential digits
1,291
0.613
Medium
19,168
https://leetcode.com/problems/sequential-digits/discuss/503845/Python-Memory-Usage-Less-Than-100
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: s = "123456789" res = [] for k in range(1, len(s) + 1): for i in range(len(s) - k + 1): x = int(s[i:i+k]) if x >= low and x <= high: res.append(x) return res
sequential-digits
Python - Memory Usage Less Than 100%
mmbhatk
2
172
sequential digits
1,291
0.613
Medium
19,169
https://leetcode.com/problems/sequential-digits/discuss/1475845/Python3-solution
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: z = low res = [] while z <= high: x = str(z) if int(x[0]) <= 10 - len(x): c = x[0] for i in range(1,len(x)): c += str(int(x[0]) + i) if int(c) <= high and int(c) >= low: res.append(int(c)) z = int(c) + 10**(len(x)-1) elif int(c) <= low: z = int(c) + 10**(len(x)-1) else: break else: z = 10**len(x) return res
sequential-digits
Python3 solution
EklavyaJoshi
1
66
sequential digits
1,291
0.613
Medium
19,170
https://leetcode.com/problems/sequential-digits/discuss/1094935/Python3-Explanation-Runtime-And-Space-Analysis-O(1)-TimeSpace
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: output = [] low_str = str(low) for number_digits in range(len(low_str), len(str(high)) + 1): for start_digit in range(1, 10): number = [] if start_digit + number_digits <= 10: for digit in range(start_digit, start_digit + number_digits): number.append(str(digit)) number = int("".join(number)) if number >= low and number <= high: output.append(number) else: break return output
sequential-digits
[Python3] Explanation - Runtime And Space Analysis - O(1) Time/Space
numiek
1
47
sequential digits
1,291
0.613
Medium
19,171
https://leetcode.com/problems/sequential-digits/discuss/688826/Python-Simple-Hack
class Solution(object): def sequentialDigits(self, low, high): """ :type low: int :type high: int :rtype: List[int] """ res = [] nums = '123456789' min_l = len(str(low)) max_l = len(str(high)) for i in range(min_l, max_l+1): for j in range(9 - i + 1): x = int(nums[j:i+j]) if low <= x <= high: res.append(x) return res
sequential-digits
Python Simple Hack
Sibu0811
1
88
sequential digits
1,291
0.613
Medium
19,172
https://leetcode.com/problems/sequential-digits/discuss/451829/Python-3-(one-line)-(beats-100)
class Solution: def sequentialDigits(self, L: int, H: int) -> List[int]: return [int('123456789'[i:i+d]) for d in range(len(str(L)),len(str(H))+1) for i in range(10-d) if L <= int('123456789'[i:i+d]) <= H]
sequential-digits
Python 3 (one line) (beats 100%)
junaidmansuri
1
310
sequential digits
1,291
0.613
Medium
19,173
https://leetcode.com/problems/sequential-digits/discuss/451829/Python-3-(one-line)-(beats-100)
class Solution: def sequentialDigits(self, L: int, H: int) -> List[int]: N, DL, DH, A = '123456789', len(str(L)), len(str(H)), [] for d in range(DL,DH+1): for i in range(10-d): if L <= int(N[i:i+d]) <= H: A.append(int(N[i:i+d])) return A - Junaid Mansuri - Chicago, IL
sequential-digits
Python 3 (one line) (beats 100%)
junaidmansuri
1
310
sequential digits
1,291
0.613
Medium
19,174
https://leetcode.com/problems/sequential-digits/discuss/2601457/Python3-Straightforward-recursive-approach-w-comments
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: # This recursive approach will build sequential numbers, and check if they are in bound # The recursive function takes an input number # Checks if it is inbound (and not already included): and adds it to the output # E.g "123" is in bound, and uniqe. Add to `output` # Checks if the number is too high -> if it is, stop the recursion on this number # E.g. "12345" is too high, no sense trying "123456" # If the number is not too high -> try the function on the same number plus the next in the sequence # E.g "123" is below upper limit, try "1234" # # With this function built, we simply need to try running it on each possible starting digit (0,9] output = [] def recurse(num): # Found a working number! Add it if num >= low and num <= high and num not in output: output.append(num) # Keep going, as long as the number isn't greater than the upper bound if num <= high: str_num = str(num) next_digit = int(str_num[-1]) + 1 if next_digit is not 10: recurse(int(str_num + str(next_digit))) # Try the recursion for each possible starting digit for i in range(1,9): recurse(i) return sorted(output) ```
sequential-digits
[Python3] Straightforward recursive approach w comments
connorthecrowe
0
9
sequential digits
1,291
0.613
Medium
19,175
https://leetcode.com/problems/sequential-digits/discuss/2475472/Python-runtime-64.78-memory-22.38-in-O(1)
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: ans = [] digit_len_low = len(str(low)) digit_len_high = len(str(high)) for digits in range(digit_len_low, digit_len_high+1): for start_num in range(1, 11-digits): # creating the number cur = 0 for i in range(digits-1, -1, -1): cur += (10 ** i) * start_num start_num += 1 if low <= cur <= high: ans.append(cur) return ans
sequential-digits
Python, runtime 64.78%, memory 22.38% in O(1)
tsai00150
0
30
sequential digits
1,291
0.613
Medium
19,176
https://leetcode.com/problems/sequential-digits/discuss/2475472/Python-runtime-64.78-memory-22.38-in-O(1)
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: ans = [] nums = "123456789" digit_len_low = len(str(low)) digit_len_high = len(str(high)) for digits in range(digit_len_low, digit_len_high+1): start = 0 while start + digits-1 < 9: # creating the number cur = int(nums[start:start+digits]) if low <= cur <= high: ans.append(cur) print(cur) start += 1 return ans
sequential-digits
Python, runtime 64.78%, memory 22.38% in O(1)
tsai00150
0
30
sequential digits
1,291
0.613
Medium
19,177
https://leetcode.com/problems/sequential-digits/discuss/2475472/Python-runtime-64.78-memory-22.38-in-O(1)
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: ans = [] for length in range(2, 10): for start in range(1, 11-length): cur = 0 for i in range(length-1, -1, -1): cur += (10**i) * start start += 1 if low <= cur <= high: ans.append(cur) return ans
sequential-digits
Python, runtime 64.78%, memory 22.38% in O(1)
tsai00150
0
30
sequential digits
1,291
0.613
Medium
19,178
https://leetcode.com/problems/sequential-digits/discuss/2333234/Python-easy-to-read-and-understand-or-BFS
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: q = [i+1 for i in range(9)] ans = [] while q: val = q.pop(0) if low <= val <= high: ans.append(val) last = str(val)[-1] if int(last) < 9: new = str(val) + str(int(last)+1) if int(new) <= high: q.append(int(new)) return ans
sequential-digits
Python easy to read and understand | BFS
sanial2001
0
59
sequential digits
1,291
0.613
Medium
19,179
https://leetcode.com/problems/sequential-digits/discuss/1872824/86.06-faster-or-93.93-less-memory-or-O(10*n)-n-len(str(high))-or-sliding-window
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: #min_size is 3 for low = 100 min_size, max_size = len(str(low)), len(str(high)) res = [] seq = '123456789' for size in range(min_size, max_size + 1): for i in range(10-size): s = seq[i:size+i] if int(s) >= low and int(s) <= high: res.append(s) return res
sequential-digits
86.06% faster | 93.93% less memory | O(10*n), n =len(str(high)) | sliding window
aamir1412
0
35
sequential digits
1,291
0.613
Medium
19,180
https://leetcode.com/problems/sequential-digits/discuss/1727627/Python-3-easiest-solution
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: nums = [12, 23, 34, 45, 56, 67, 78, 89, 123, 234, 345, 456, 567, 678, 789, 1234, 2345, 3456, 4567, 5678, 6789, 12345, 23456, 34567, 45678, 56789, 123456, 234567, 345678, 456789, 1234567, 2345678, 3456789, 12345678, 23456789, 123456789] res = [] for num in nums: if num > high: break elif num >= low: res.append(num) return res
sequential-digits
Python 3, easiest solution
dereky4
0
76
sequential digits
1,291
0.613
Medium
19,181
https://leetcode.com/problems/sequential-digits/discuss/1713115/Python3-24ms-solution-using-iteration
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: # first find the minimum and maximum digits possible in each combination # find the leading digit of low and high and make a string of digits 1-9 # a substring of this string will be a viable combination minDig, maxDig = len(str(low)), min(9, len(str(high))) sMin, sMax, nums, ans = int(str(low)[0]), int(str(high)[0]), "123456789", [] # for all possible lengths of combinations for i in range(minDig, maxDig + 1): # we start first substring with nums[0] and keep generating a # combination until we cant get a substring of desired length start = 0 end = 9 - i # for a bit of optimization, # if this is the minimum possible length, we start from leading # digit of low and if it is maximum length, we stop at minimum # of previous end and leading digit of high (to make sure length # is achieved) if i == minDig: start = sMin - 1 if i == maxDig: end = min(end, sMax - 1) # Now that starting and ending index is defined, just get substrings while start <= end: ans.append(int(nums[start: start + i])) start += 1 # There is one more thing to check, whether the first and last combination # in our list are in the range. Eg: low = 473, the above iteration will consider # 456 to be valid which is not the case start, end = 0, len(ans) if end == 0: return [] if ans[0] < low: start = 1 if ans[end - 1] > high: end -= 1 return ans[start:end]
sequential-digits
[Python3] 24ms solution using iteration
dayeem
0
15
sequential digits
1,291
0.613
Medium
19,182
https://leetcode.com/problems/sequential-digits/discuss/1712772/Python-easy-to-understand
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: pre = '123456789' dic = set() st = len(str(low)) ed = len(str(high)) s = 0 while st<=ed: temp = int(pre[s:st+s]) s += 1 if temp>=low: if temp<=high: dic.add(temp) else: break if str(temp)[-1] == '9': s = 0 st+=1 return sorted(dic)
sequential-digits
Python easy to understand
hrithikhh86
0
32
sequential digits
1,291
0.613
Medium
19,183
https://leetcode.com/problems/sequential-digits/discuss/1712529/Python3-oror-FAST-Solution
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: l,h=len(str(low)),len(str(high)) seq=[] for i in range(l,h+1): begNum=low//10**(i-1) if i>(10-begNum): continue for j in range(begNum,10-i+1): num=0 for k in range(j,j+i): num=num*10+k if num in range(low,high+1) and num not in seq: seq.append(num) return seq ```
sequential-digits
[⭐️ Python3 || FAST Solution ]
alchimie54
0
8
sequential digits
1,291
0.613
Medium
19,184
https://leetcode.com/problems/sequential-digits/discuss/1712407/Python-Easy-Solution-using-Sliding-Window-Approach
class Solution: def sequentialDigits(self, l: int, h: int) -> List[int]: result = [] number = ['1','2','3','4','5','6','7','8','9'] low = len(str(l)) high = len(str(h)) while(low <= high): idx = 0 while(idx <= len(number)-low): sub = number[idx:idx+low] r = "".join(sub) if int(r) >= l and int(r) <= h: result.append(int(r)) idx += 1 #increment low += 1 #increment return result
sequential-digits
Python Easy Solution using Sliding Window Approach
Madhivarman
0
17
sequential digits
1,291
0.613
Medium
19,185
https://leetcode.com/problems/sequential-digits/discuss/1712253/Easy-Python3-Observation-based-approach-(Commented)(Explained)
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: ans = [] #Initialise the ans array for i in range(2, 10): #Run a for loop to generate all the possible numbers with given condition with lengths 2 to 9 t1 = str(1) #Initialise the first digit of the lowest number the in the range (10^i, 10^(i+1)) t2 = str(9) #Initialise the last digit of the highest number the in the range (10^i, 10^(i+1)) for i in range(i-1): #Run a for loop to generate the min and the max of the range t1 += str(int(t1[-1]) +1) #next digit is 1+ previous digit t2 = str(int(t2[0]) - 1) + t2 # previous digit is the 1 - current digit temp = [t1] # add the lowest nuber initially to the array while int(temp[-1]) < int(t2): # while the last number in the temp is less that the highest number in range arr = list(temp[-1]) #Split the digits arr = [str(int(i) +1) for i in arr] #Add 1 to each digit temp.append(''.join(arr)) #convert the list back into string for i in temp: #For every number in the range(10^i, 10^(i+1)) if low<=int(i) <=high: #Check if the number falls in the given range in question ans.append(i) #If it falls then add it to the ans return ans #At the end of the day return the ans array which is sorted
sequential-digits
Easy Python3 Observation based approach (Commented)(Explained)
sdasstriver9
0
15
sequential digits
1,291
0.613
Medium
19,186
https://leetcode.com/problems/sequential-digits/discuss/1711780/Python-Solution-using-BFS
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: q, res = [], [] for i in range(1, 10): q.append(i) while len(q): cur_val = q.pop() if low <= cur_val <= high: res.append(cur_val) new_val = cur_val * 10 + cur_val % 10 + 1 if new_val <= high and cur_val % 10!=9: q.append(new_val) return sorted(res)
sequential-digits
Python Solution, using BFS
pradeep288
0
18
sequential digits
1,291
0.613
Medium
19,187
https://leetcode.com/problems/sequential-digits/discuss/855427/Python3-bisect_left-Sequential-Digits
class Solution: arr = [int('123456789'[i:i+l]) for l in range(2, 10) for i in range(10-l)] def sequentialDigits(self, low: int, high: int) -> List[int]: return Solution.arr[bisect_left(Solution.arr, low): bisect_left(Solution.arr, high)]
sequential-digits
Python3 bisect_left - Sequential Digits
r0bertz
0
30
sequential digits
1,291
0.613
Medium
19,188
https://leetcode.com/problems/sequential-digits/discuss/853854/Python-Simple-solution-using-Python
class Solution(object): def sequentialDigits(self, low, high): """ :type low: int :type high: int :rtype: List[int] """ dig = '123456789' res = [] l = len(str(low)) while l <= len(str(high)): for i in range(10-l): cur = int(dig[i:i+l]) if low <= cur <= high: res.append(cur) l += 1 return res
sequential-digits
[Python] Simple solution using Python
mjgallag
0
63
sequential digits
1,291
0.613
Medium
19,189
https://leetcode.com/problems/sequential-digits/discuss/477006/Python3-96.72-(20-ms)100.00-(12.8-MB)
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: ret = [] if (low <= 123456789): high = high if high < 123456789 else 123456789 starting_length = len(str(low)) ending_length = len(str(high)) + 1 for length in range(starting_length, ending_length): positional_value = 10 ** (length - 1) number_string_list = [str(a) for a in range(1, length + 1)] number = int("".join(number_string_list)) while (1): if (number > high): return ret elif (number >= low): ret.append(number) else: pass if (number_string_list[-1] == "9"): break positional_value_ = positional_value for position in range(length): number_string_list[position] = chr(ord(number_string_list[position]) + 1) number += positional_value_ positional_value_ //= 10 return ret
sequential-digits
Python3 96.72% (20 ms)/100.00% (12.8 MB)
numiek_p
0
69
sequential digits
1,291
0.613
Medium
19,190
https://leetcode.com/problems/sequential-digits/discuss/452417/Python-3%3A-generate-and-bisect
class Solution: def sequentialDigits(self, low: int, high: int) -> list: from bisect import bisect_left, bisect_right d = [str(i) for i in range(1,10)] def seq(n) -> int: for j in range(10 - n): yield int(''.join(d[j:j+n])) def sequentialTotal(): for it in (seq(n) for n in range(1,10)): for i in it: yield i all = [i for i in sequentialTotal()] i = bisect_left(all,low) j = bisect_right(all,high) return all[i:j]
sequential-digits
Python 3: generate and bisect
deleted_user
0
45
sequential digits
1,291
0.613
Medium
19,191
https://leetcode.com/problems/sequential-digits/discuss/452378/Python3%3A-24ms-faster-than-100
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: res = [] length = len(str(low)) firstDigit = low // (10 ** (length - 1)) while True: num = digit = firstDigit for i in range(length - 1): digit += 1 if digit > 9: break num = num * 10 + digit if digit <= 9: if num > high: return res if num >= low: res.append(num) if digit >= 9: firstDigit = 1 length += 1 if length > 9: return res else: firstDigit +=1 return res
sequential-digits
Python3: 24ms, faster than 100%
andnik
0
103
sequential digits
1,291
0.613
Medium
19,192
https://leetcode.com/problems/sequential-digits/discuss/451968/Python-24ms-generate-next-sequential-integer-from-previous
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: res = [] digits = [int(x) for x in str(low)] first, length = digits[0], len(digits) if first > 10 - length: first = 1 length += 1 power = 10**(length - 1) # generate sequentual integer starting from specified digit def generateSeqNumber(f, k): digitsArray = [i for i in range(f, f + k)] return int("".join([str(d) for d in digitsArray])) x = generateSeqNumber(first, length) while x <= high: if x >= low: res.append(x) # if there is no valid sequential integer with specified length if first >= 10 - length: first = 1 length += 1 power *= 10 x = generateSeqNumber(first, length) else: x = (x - first*power)*10 + (first + length) first += 1 return res
sequential-digits
Python 24ms, generate next sequential integer from previous
shigatan
0
68
sequential digits
1,291
0.613
Medium
19,193
https://leetcode.com/problems/sequential-digits/discuss/451865/Python3-directly-generate-integers-with-sequential-digits
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: digits = "123456789" def fn(n): if n > 9: return [] #cap lo = int(digits[:n]) hi = int(digits[-n:]) return range(lo, hi+1, int("1"*n)) lo, hi = len(str(low)), len(str(high)) ans = [] for n in range(lo, hi+1): ans.extend([x for x in fn(n) if low <= x <= high]) return ans
sequential-digits
[Python3] directly generate integers with sequential digits
ye15
0
93
sequential digits
1,291
0.613
Medium
19,194
https://leetcode.com/problems/sequential-digits/discuss/451865/Python3-directly-generate-integers-with-sequential-digits
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: ans = [] for x in range(1, 10): val = 0 for d in range(x, 10): val = 10*val + d if low <= val <= high: ans.append(val) return sorted(ans)
sequential-digits
[Python3] directly generate integers with sequential digits
ye15
0
93
sequential digits
1,291
0.613
Medium
19,195
https://leetcode.com/problems/sequential-digits/discuss/1711650/Explained-Solution-Using-hints-Python3!
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: possibilities = [ 12, 23, 34, 45, 56, 67, 78, 89, 123, 234, 345, 456, 567, 678, 789, 1234, 2345, 3456, 4567, 5678, 6789, 12345, 23456, 34567, 45678, 56789, 123456, 234567, 345678, 456789, 1234567, 2345678, 3456789, 12345678, 23456789, 123456789, ] res = [] for i in range(len(possibilities)): if possibilities[i]<low: continue elif possibilities[i]>high: break res.append(possibilities[i]) return res
sequential-digits
Explained Solution Using hints - Python3!
kindahodor
-1
19
sequential digits
1,291
0.613
Medium
19,196
https://leetcode.com/problems/sequential-digits/discuss/1377826/Python-or-Recursive-or-Faster-Than-95-or-Beginner-Friendly
class Solution: def sequentialDigits(self, low: int, high: int) -> List[int]: def recur(number, ans): intNum = int(number) if intNum >= low and intNum <= high: ans.append(intNum) if number[-1] != '9': recur( number + str( int(number[-1])+1 ), ans) ans = [] for i in range(1, 9): recur(str(i), ans) ans.sort() return ans
sequential-digits
Python | Recursive | Faster Than 95% | Beginner Friendly
paramvs8
-1
95
sequential digits
1,291
0.613
Medium
19,197
https://leetcode.com/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/discuss/691648/Python3-binary-search-like-bisect_right-Maximum-Side-Length-of-a-Square-with-Sum-less-Threshold
class Solution: def maxSideLength(self, mat: List[List[int]], threshold: int) -> int: ans = 0 m = len(mat) n = len(mat[0]) presum = [[0] * (n+1) for _ in range(m+1)] for i in range(1, m+1): for j in range(1, n+1): presum[i][j] = mat[i-1][j-1] + presum[i][j-1] + presum[i-1][j] - presum[i-1][j-1] lo, hi = 1, min(i, j) + 1 while lo < hi: mid = (lo + hi)//2 cursum = presum[i][j] - presum[i-mid][j] - presum[i][j-mid] + presum[i-mid][j-mid] if cursum > threshold: hi = mid else: lo = mid + 1 ans = max(ans, lo-1) return ans
maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold
Python3 binary search like bisect_right - Maximum Side Length of a Square with Sum <= Threshold
r0bertz
1
165
maximum side length of a square with sum less than or equal to threshold
1,292
0.532
Medium
19,198
https://leetcode.com/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/discuss/691648/Python3-binary-search-like-bisect_right-Maximum-Side-Length-of-a-Square-with-Sum-less-Threshold
class Solution: def maxSideLength(self, mat: List[List[int]], threshold: int) -> int: m = len(mat) n = len(mat[0]) presum = [[0] * (n+1) for _ in range(m+1)] for i in range(1, m+1): for j in range(1, n+1): presum[i][j] = mat[i-1][j-1] + presum[i][j-1] + presum[i-1][j] - presum[i-1][j-1] ans = 0 for i in range(m+1): for j in range(n+1): length = ans + 1 while (i + length <= m and j + length <= n and presum[i+length][j+length] - presum[i+length][j] - presum[i][j+length] + presum[i][j] <= threshold): ans = length length += 1 return ans
maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold
Python3 binary search like bisect_right - Maximum Side Length of a Square with Sum <= Threshold
r0bertz
1
165
maximum side length of a square with sum less than or equal to threshold
1,292
0.532
Medium
19,199