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/product-of-array-except-self/discuss/2690293/Simple-python-Soln
class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: product = 1 zero_i = set() for i in range(len(nums)): if nums[i] != 0: product*= nums[i] else: zero_i.add(i) if len(zero_i) == 1: item = [0]*...
product-of-array-except-self
Simple python Soln
user1090g
0
5
product of array except self
238
0.648
Medium
4,500
https://leetcode.com/problems/product-of-array-except-self/discuss/2682318/Python-Solution-Constant-Space-89-Fast
class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: n=len(nums) ans=[1]*n lP=1 for i in range(n): ans[i]*=lP lP*=nums[i] rP=1 for i in range(len(ans)-1,-1,-1): ans[i]*=rP rP*=nums[i] return...
product-of-array-except-self
Python Solution Constant Space 89% Fast
pranjalmishra334
0
82
product of array except self
238
0.648
Medium
4,501
https://leetcode.com/problems/product-of-array-except-self/discuss/2674298/Efficient-way-of-finding-product-is-elements-in-a-list-excluding-self
class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: prod=1 flag=0 if len(nums)>=2: for i in range(len(nums)): if nums[i] == 0: flag+=1 else: prod*=nums[i] for i...
product-of-array-except-self
Efficient way of finding product is elements in a list excluding self
deepanjan234
0
5
product of array except self
238
0.648
Medium
4,502
https://leetcode.com/problems/product-of-array-except-self/discuss/2667706/Simple-and-Straight-Forward-Solution-using-Python
class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: n = len(nums) left = [1]*n right = [1]*n answer = [1]*n left[0]=1 right[n-1]=1 for i in range(1,n): left[i] = nums[i-1]*left[i-1] for j in...
product-of-array-except-self
Simple and Straight Forward Solution using Python
anusha_anil
0
43
product of array except self
238
0.648
Medium
4,503
https://leetcode.com/problems/product-of-array-except-self/discuss/2667521/Python-Easy-solution
class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: finlist = [] pre = 1 prelist = [1] post = 1 postlist = [1] for num in nums: pre *= num prelist.append(pre) for num in nums[::-1]: post *= num ...
product-of-array-except-self
[Python] Easy solution
natscripts
0
5
product of array except self
238
0.648
Medium
4,504
https://leetcode.com/problems/product-of-array-except-self/discuss/2656304/Python3-or-Prefix-Product-in-Both-Directions-(Use-2)-O(N)-TIME-AND-SPACE-SOL!
class Solution: #Time-Complexity: O(2n + n * O(1)) -> O(N) #Space-Complexity: O(2N) -> O(N) def productExceptSelf(self, nums: List[int]) -> List[int]: #Approach: Allocate two prefix sum, one starting from left and one starting from right. #For every index i, we can get the prefix and suffix...
product-of-array-except-self
Python3 | Prefix Product in Both Directions (Use 2) O(N) TIME AND SPACE SOL!
JOON1234
0
110
product of array except self
238
0.648
Medium
4,505
https://leetcode.com/problems/product-of-array-except-self/discuss/2641988/Simple-Approach
class Solution: def productExceptSelf(self, nums: List[int]) -> List[int]: import math n = len(nums) res = [1 for _ in range(n)] for i in range(1,n): res[i] = res[i-1] * nums[i-1] prod = nums[n-1] for i in range(n-2,-1,-1): res[i] = res[i] * p...
product-of-array-except-self
Simple Approach
vkmaurya
0
2
product of array except self
238
0.648
Medium
4,506
https://leetcode.com/problems/product-of-array-except-self/discuss/2551062/Python-simple-solution-using-prefix-and-suffix
class Solution: def productExceptSelf(self, nums): p, s, p_arr, s_arr = 1, 1, [], [] for i in range(len(nums)): p, s = p * nums[i], s * nums[-i - 1] p_arr.append(p) s_arr.append(s) for j in range(1, len(nums) - 1): nums[j] = p_arr[j - 1] * s_...
product-of-array-except-self
Python simple solution using prefix and suffix
Mark_computer
0
99
product of array except self
238
0.648
Medium
4,507
https://leetcode.com/problems/product-of-array-except-self/discuss/2551062/Python-simple-solution-using-prefix-and-suffix
class Solution: def productExceptSelf(self, nums): p, s, p_arr, s_arr = 1, 1, [], [] for i in range(len(nums)): p, s = p * nums[i], s * nums[-i - 1] # p is going from left to right; s -- from right to left p_arr.append(p) s_arr.append(s) for j in range(1,...
product-of-array-except-self
Python simple solution using prefix and suffix
Mark_computer
0
99
product of array except self
238
0.648
Medium
4,508
https://leetcode.com/problems/product-of-array-except-self/discuss/2540301/Python-It
class Solution: def productExceptSelf(self, n: List[int]) -> List[int]: ans = [1] * len(n) pre, pos = 1, 1 for i in range(len(n)): ans[i] = pre pre *= n[i] for i in range(len(n)-1, -1, -1): ans[i] *= pos p...
product-of-array-except-self
Python It ✅
Khacker
0
211
product of array except self
238
0.648
Medium
4,509
https://leetcode.com/problems/sliding-window-maximum/discuss/1624633/Python-3-O(n)
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: res = [] window = collections.deque() for i, num in enumerate(nums): while window and num >= nums[window[-1]]: window.pop() window.append(i) if i...
sliding-window-maximum
Python 3 O(n)
dereky4
3
772
sliding window maximum
239
0.466
Hard
4,510
https://leetcode.com/problems/sliding-window-maximum/discuss/755538/Python3-mono-queue
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: queue = deque() # decreasing queue ans = [] for i, x in enumerate(nums): while queue and queue[-1][1] <= x: queue.pop() queue.append((i, x)) if queue and queue[0][0] <= i-k...
sliding-window-maximum
[Python3] mono-queue
ye15
3
132
sliding window maximum
239
0.466
Hard
4,511
https://leetcode.com/problems/sliding-window-maximum/discuss/1738137/Python-Deque-solution-explained
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: # this deque will hold the # index of the max element # in a sliding window queue = deque() res = [] for i, curr_val in enumerate(nums): # remove all those elements...
sliding-window-maximum
[Python] Deque solution explained
buccatini
2
377
sliding window maximum
239
0.466
Hard
4,512
https://leetcode.com/problems/sliding-window-maximum/discuss/1505368/Py3Py-Solution-using-monotonically-decreasing-sequence-w-comment
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: # Init q = [] # This is queue of indexes not num queue of values n = len(nums) output = [] # Base Case: If window size is 1 if k == 1: return nums ...
sliding-window-maximum
[Py3/Py] Solution using monotonically decreasing sequence w/ comment
ssshukla26
2
230
sliding window maximum
239
0.466
Hard
4,513
https://leetcode.com/problems/sliding-window-maximum/discuss/536588/Python3-Easy-Solution.-Slicing
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: rlist = [] for i in range(len(nums)): if (i+k) > len(nums): break int_list = nums[i:(i+k)] max_element = max(int_list) rlist.append(max_element) r...
sliding-window-maximum
Python3 Easy Solution. Slicing
cppygod
2
186
sliding window maximum
239
0.466
Hard
4,514
https://leetcode.com/problems/sliding-window-maximum/discuss/2505411/Python-3-Monotonic-Deque-Approach-Explained
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: N = len(nums) mono_deque = collections.deque() result = [] for i, num in enumerate(nums): while mono_deque and mono_deque[0] < i - k + 1: mono_deque.popleft...
sliding-window-maximum
Python 3 Monotonic Deque Approach Explained
sebikawa
1
122
sliding window maximum
239
0.466
Hard
4,515
https://leetcode.com/problems/sliding-window-maximum/discuss/2505411/Python-3-Monotonic-Deque-Approach-Explained
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: N = len(nums) mono_deque = collections.deque() result = [] for i, num in enumerate(nums): while mono_deque and mono_deque[-1] < i - k + 1: mono_deque.pop() ...
sliding-window-maximum
Python 3 Monotonic Deque Approach Explained
sebikawa
1
122
sliding window maximum
239
0.466
Hard
4,516
https://leetcode.com/problems/sliding-window-maximum/discuss/2342547/GoPython-Devide-and-Conquer-Solution-and-Deque-Solution
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: res, dq = [], deque() for i, v in enumerate(nums): if i > k-1 and dq[0] < i - k + 1: dq.popleft() while dq and v > nums[dq[-1]]: dq.pop() dq += i, ...
sliding-window-maximum
[Go][Python] Devide & Conquer Solution and Deque Solution
larashion
1
68
sliding window maximum
239
0.466
Hard
4,517
https://leetcode.com/problems/sliding-window-maximum/discuss/2342547/GoPython-Devide-and-Conquer-Solution-and-Deque-Solution
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: res, left, right = [0 for _ in range(len(nums) - k + 1)], defaultdict(int), defaultdict(int) for i in range(len(nums)): if i % k == 0: left[i] = nums[i] else: lef...
sliding-window-maximum
[Go][Python] Devide & Conquer Solution and Deque Solution
larashion
1
68
sliding window maximum
239
0.466
Hard
4,518
https://leetcode.com/problems/sliding-window-maximum/discuss/2211348/Python-or-Easy-to-Understand
class Solution: def maxSlidingWindow(self, A: List[int], B: int) -> List[int]: # SO as we can imagine we have to find maximum as we slide through the window # and we can achieve by maintaining a maxx variable and compairing it with each addition of j # and when window reaches k w...
sliding-window-maximum
Python | Easy to Understand
varun21vaidya
1
178
sliding window maximum
239
0.466
Hard
4,519
https://leetcode.com/problems/sliding-window-maximum/discuss/2030080/Python-Simple-and-Easy-Solution
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: res = [] queue = collections.deque() l = r = 0 while r < len(nums): while queue and nums[queue[-1]] < nums[r]: queue.pop() queue.append(r) if...
sliding-window-maximum
Python - Simple and Easy Solution
dayaniravi123
1
123
sliding window maximum
239
0.466
Hard
4,520
https://leetcode.com/problems/sliding-window-maximum/discuss/1570913/Python-Simple-deque-solution
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: n = len(nums) from queue import deque deq = deque() ans = [] for i in range(n): while deq and i - deq[0][1] >= k: deq.popleft() while deq and deq[-1][0] <...
sliding-window-maximum
[Python] Simple deque solution
nomofika
1
338
sliding window maximum
239
0.466
Hard
4,521
https://leetcode.com/problems/sliding-window-maximum/discuss/1051342/Python3-faster-than-98-and-easy-understanding
class Solution: #monotonic queue def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: if len(nums) == 0: return [] res,mqueue=[],collections.deque() #deque much faster than list for i,e in enumerate(nums): #monotonic queue pu...
sliding-window-maximum
Python3 faster than 98% & easy understanding
695051665
1
154
sliding window maximum
239
0.466
Hard
4,522
https://leetcode.com/problems/sliding-window-maximum/discuss/247137/Python3-104-ms-beats-97.58.-O(nk)-O(n)-time-complexity.
class Solution: def maxSlidingWindow(self, nums: 'List[int]', k: 'int') -> 'List[int]': n = len(nums) if n * k == 0: return [] # O(nk) in the worst case of sorted descending array # O(n) in the best case of sorted ascending array output = [] max_idx = -1 ...
sliding-window-maximum
Python3, 104 ms, beats 97.58%. O(nk) / O(n) time complexity.
andvary
1
633
sliding window maximum
239
0.466
Hard
4,523
https://leetcode.com/problems/sliding-window-maximum/discuss/2833821/Python-Solution-using-deque-or-O(n)
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: output = [] q = collections.deque() # index l = r = 0 # O(n) O(n) while r < len(nums): # pop smaller values from q while q and nums[q[-1]] < nums[r]: q.pop() q.append(r) # remove left val from window if l >...
sliding-window-maximum
Python Solution using deque | O(n)
nikhitamore
0
9
sliding window maximum
239
0.466
Hard
4,524
https://leetcode.com/problems/sliding-window-maximum/discuss/2825356/Python-super-fast-easy-solution-linear-time-and-space.
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: arr, ans = deque([]), [] for i in range(len(nums)): if arr and arr[0] == i - k: arr.popleft() while arr and nums[ arr[-1] ] < nums[i]: arr.pop() arr.append(i) ...
sliding-window-maximum
Python super fast easy solution linear time and space.
Samuelabatneh
0
7
sliding window maximum
239
0.466
Hard
4,525
https://leetcode.com/problems/sliding-window-maximum/discuss/2807438/Faster-than-100-using-deque
class Solution: def maxSlidingWindow(self, arr: List[int], k: int) -> List[int]: ans=[] s = [] n=len(arr) maxi=-1e9 i=j=0 while j<n: while s and s[-1]<arr[j]: s.pop() s.append(arr[j]) if j-i+1<k: j+=1...
sliding-window-maximum
Faster than 100% using deque
sanskar_
0
5
sliding window maximum
239
0.466
Hard
4,526
https://leetcode.com/problems/sliding-window-maximum/discuss/2764058/Descending-Queue
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: q = [] res = [] # R points to the 'to-be-added' elem for R in range(len(nums)): # Pop nums[R-k] (if it is the head of the window !) if q and R >= k and q[0] == nums[R-k]: ...
sliding-window-maximum
Descending Queue
KKCrush
0
5
sliding window maximum
239
0.466
Hard
4,527
https://leetcode.com/problems/sliding-window-maximum/discuss/2764057/Descending-Queue
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: q = [] res = [] # R points to the 'to-be-added' elem for R in range(len(nums)): # Pop nums[R-k] (if it is the head of the window !) if q and R >= k and q[0] == nums[R-k]: ...
sliding-window-maximum
Descending Queue
KKCrush
0
2
sliding window maximum
239
0.466
Hard
4,528
https://leetcode.com/problems/sliding-window-maximum/discuss/2760193/Optimal-Solution-of-Sliding-Window-Maximum-with-python3.
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: output=[] left=right=0 q=collections.deque() while right<len(nums): while q and nums[q[-1]] <nums[right]: q.pop() q.append(right) # remove left valu...
sliding-window-maximum
Optimal Solution of Sliding Window Maximum with python3.
ossamarhayrhay2001
0
4
sliding window maximum
239
0.466
Hard
4,529
https://leetcode.com/problems/sliding-window-maximum/discuss/2741419/Python-solution-using-heap
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: heap = [] for i in range(k): heapq.heappush(heap, -nums[i]) dic = defaultdict(int) answer = [] left, right = 0, k-1 while right < len(nums): while -h...
sliding-window-maximum
Python solution using heap
tesfish
0
9
sliding window maximum
239
0.466
Hard
4,530
https://leetcode.com/problems/sliding-window-maximum/discuss/2696107/Monotonic-Queue-or
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: queue = collections.deque() for i in range(k-1): while queue and queue[-1] < nums[i]: queue.pop() queue.append(nums[i]) rst = [] for i in range(k-1,le...
sliding-window-maximum
Monotonic Queue | 单调队列
wenkii
0
2
sliding window maximum
239
0.466
Hard
4,531
https://leetcode.com/problems/sliding-window-maximum/discuss/2690415/Sliding-Window-Maximum
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: res = [] deque = [] for i in range(len(nums)): while len(deque) > 0 and nums[i] > nums[deque[len(deque) - 1]]: deque.pop() deque.append(i) if i - deque[0] >= ...
sliding-window-maximum
Sliding Window Maximum
Erika_v
0
4
sliding window maximum
239
0.466
Hard
4,532
https://leetcode.com/problems/sliding-window-maximum/discuss/2668740/Python-O(N)-O(N)
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: queue = deque() def clean(idx): while queue and queue[-1] < nums[idx]: queue.pop() queue.append(nums[idx]) for idx in range(k): clean(idx) ...
sliding-window-maximum
Python - O(N), O(N)
Teecha13
0
8
sliding window maximum
239
0.466
Hard
4,533
https://leetcode.com/problems/sliding-window-maximum/discuss/2667012/Heap-solution-or-Python3-or-Simple-and-intuitive
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: max_hp = [] answer = [] for i in range(k): heapq.heappush(max_hp,(-nums[i],i)) N = len(nums) for i in range(k,N): answer.append(-max_hp[0][0]) #...
sliding-window-maximum
Heap solution | Python3 | Simple and intuitive
abhi-now
0
3
sliding window maximum
239
0.466
Hard
4,534
https://leetcode.com/problems/sliding-window-maximum/discuss/2666463/python-or-deque-or-sliding-window
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: # use deque to handle this sliding window problem queue, n = collections.deque(), len(nums) # select kth elements into queue for i in range(k): # ensure the left elemt have the bigg...
sliding-window-maximum
python | deque | sliding window
MichelleZou
0
36
sliding window maximum
239
0.466
Hard
4,535
https://leetcode.com/problems/sliding-window-maximum/discuss/2664906/python-deque-solotion
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: # o(n), o(n) output = [] q = collections.deque() l = r = 0 while r < len(nums): # pop smaller values from q while q and nums[q[-1]] < nums[r]: q.pop() ...
sliding-window-maximum
python deque solotion
sahilkumar158
0
3
sliding window maximum
239
0.466
Hard
4,536
https://leetcode.com/problems/sliding-window-maximum/discuss/2656355/O(n)-or-Python
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: res = [] l = r = 0 q = collections.deque() while r < len(nums): while q and nums[q[-1]] < nums[r]: q.pop() q.append(r) if l > q[0]: ...
sliding-window-maximum
O(n) | Python
normal_crayon
0
11
sliding window maximum
239
0.466
Hard
4,537
https://leetcode.com/problems/sliding-window-maximum/discuss/2645070/take-a-note-to-learn
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: d = collections.deque() out = [] for i, n in enumerate(nums): while d and nums[d[-1]] < n: d.pop() d += i, if d[0] == i - k: d.popleft() ...
sliding-window-maximum
take a note to learn
lucy_sea
0
3
sliding window maximum
239
0.466
Hard
4,538
https://leetcode.com/problems/sliding-window-maximum/discuss/2643957/Python-using-monotonous-queue-or-O(N)
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: res = [] left = 0 q = collections.deque() # q is a monotonous queue, leftmost is the index of the max value for right in range(len(nums)): while q and nums[q[-1]]<nums[right]: # in d...
sliding-window-maximum
Python using monotonous queue | O(N)
gcheng81
0
4
sliding window maximum
239
0.466
Hard
4,539
https://leetcode.com/problems/sliding-window-maximum/discuss/2536352/Python3-%2B-Sliding-Window-%2B-Max-Heap
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: start = 0 heap = [] i = 0 while i < k: heappush(heap, (-1*nums[i], i)) i += 1 ans = [] for end in range(k, len(nums)): ans.append(-1*heap[0][0]) while heap and heap[0][1] <= start: heappop(heap) ...
sliding-window-maximum
Python3 + Sliding Window + Max Heap
leet_satyam
0
118
sliding window maximum
239
0.466
Hard
4,540
https://leetcode.com/problems/sliding-window-maximum/discuss/2509732/What's-wrong-in-this-code-4451-test-cases-passing
class Solution: def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: l,r = 0,0 q = [] while True: if not q: q.append(nums[l]) else: while q and nums[r]>=q[-1]: q.pop() q.append(n...
sliding-window-maximum
What's wrong in this code, 44/51 test cases passing
abhineetsingh192
0
41
sliding window maximum
239
0.466
Hard
4,541
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2324351/PYTHON-oror-EXPLAINED-oror
class Solution: def searchMatrix(self, mat: List[List[int]], target: int) -> bool: m=len(mat) n=len(mat[0]) i=m-1 j=0 while i>=0 and j<n: if mat[i][j]==target: return True elif mat[i][j]<target: ...
search-a-2d-matrix-ii
✔️ PYTHON || EXPLAINED || ; ]
karan_8082
48
1,700
search a 2d matrix ii
240
0.507
Medium
4,542
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2324351/PYTHON-oror-EXPLAINED-oror
class Solution: def searchMatrix(self, mat: List[List[int]], target: int) -> bool: m=len(mat) n=len(mat[0]) for i in range(m): if mat[i][0]<=target and mat[i][-1]>=target: lo=0 hi=n while (lo<hi): ...
search-a-2d-matrix-ii
✔️ PYTHON || EXPLAINED || ; ]
karan_8082
48
1,700
search a 2d matrix ii
240
0.507
Medium
4,543
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/1079178/Python.-Super-simple-O(m-%2B-n).-faster-than-93.65
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: row, col, num_row = 0, len(matrix[0]) - 1, len(matrix) while col >= 0 and row < num_row: if matrix[row][col] > target: col -= 1 elif matrix[row][col] < target: ...
search-a-2d-matrix-ii
Python. Super simple O(m + n). faster than 93.65%
m-d-f
11
504
search a 2d matrix ii
240
0.507
Medium
4,544
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/697281/Python3-O(n-%2B-m)-with-explanationproof
class Solution: def searchMatrix(self, matrix, target): if matrix == None or len(matrix) == 0 or len(matrix[0]) == 0: return False n, m = len(matrix), len(matrix[0]) i, j = 0, m - 1 while i < n and j >= 0: if matrix[i][j] == target: return Tru...
search-a-2d-matrix-ii
Python3 O(n + m) with explanation/proof
awitten1
9
591
search a 2d matrix ii
240
0.507
Medium
4,545
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2452328/Python-Very-clear-explanation-with-drawing-O(m%2Bn).
class Solution: def searchMatrix(self, matrix, target): rows, cols = len(matrix), len(matrix[0]) top = 0 right = cols-1 bottom = rows-1 left = 0 while bottom >= top and left <= right: if matrix[bottom][left] == target: re...
search-a-2d-matrix-ii
Python Very clear explanation with drawing O(m+n).
OsamaRakanAlMraikhat
3
68
search a 2d matrix ii
240
0.507
Medium
4,546
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/1419121/Easy-Python-Solution-or-Faster-than-98-Memory-less-93
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for r in matrix: if r[0] <= target and r[-1] >= target: l, h = 0, len(r)-1 while l <= h: m = (l+h)//2 if r[m] > target: ...
search-a-2d-matrix-ii
Easy Python Solution | Faster than 98%, Memory < 93%
the_sky_high
3
286
search a 2d matrix ii
240
0.507
Medium
4,547
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/415360/Python3-Brute-Force-Binary-Search-Search-Space-Reduction-Solutions
class Solution: def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ if len(matrix) == 0: return False row, col = len(matrix), len(matrix[0]) for i in range(row): for j in ...
search-a-2d-matrix-ii
Python3 Brute Force/ Binary Search/ Search Space Reduction Solutions
yanshengjia
3
302
search a 2d matrix ii
240
0.507
Medium
4,548
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/415360/Python3-Brute-Force-Binary-Search-Search-Space-Reduction-Solutions
class Solution: def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ if len(matrix) == 0: return False row, col = len(matrix), len(matrix[0]) # iterate over matrix diagonals, ...
search-a-2d-matrix-ii
Python3 Brute Force/ Binary Search/ Search Space Reduction Solutions
yanshengjia
3
302
search a 2d matrix ii
240
0.507
Medium
4,549
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2326170/Python-Beginner-Friendly-Solution-oror-Brute-Force
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: ans = False for i in matrix: if target in i: ans = True return ans
search-a-2d-matrix-ii
Python Beginner Friendly Solution || Brute Force
Shivam_Raj_Sharma
2
37
search a 2d matrix ii
240
0.507
Medium
4,550
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/1715636/Python-Simple-Python-Solution-Using-Binary-Search-and-For-Loop-!!-O(N-LogN)
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: def search(array,target): low = 0 high = len(array) while low<high: mid=(low+high)//2 if array[mid]==target: return mid elif array[mid]<target: low=mid+1 elif array[mid]>target: high=mi...
search-a-2d-matrix-ii
[ Python ] ✔🔥 Simple Python Solution Using Binary Search and For Loop !! O(N LogN)
ASHOK_KUMAR_MEGHVANSHI
2
127
search a 2d matrix ii
240
0.507
Medium
4,551
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/990886/10-line-python-O(n)-solution
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: m, n = len(matrix), len(matrix[0]) row, col = 0, n - 1 while row < m and col >= 0: if matrix[row][col] == target: return True elif matrix[row][col] > target: ...
search-a-2d-matrix-ii
10 line python O(n) solution
ChiCeline
2
281
search a 2d matrix ii
240
0.507
Medium
4,552
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2326574/Python3-Runtime%3A-175ms-94.99-oror-memory%3A-20.3mb-98.38
class Solution: # Runtime: 175ms 94.99% || memory: 20.3mb 98.38% def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: col = len(matrix[0])-1 for item in range(len(matrix)): while matrix[item][col] > target and col >= 0: col-=1 if matrix[it...
search-a-2d-matrix-ii
Python3 Runtime: 175ms 94.99% || memory: 20.3mb 98.38%
arshergon
1
20
search a 2d matrix ii
240
0.507
Medium
4,553
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2231277/2-pointers-approach-oror-Clean-Code
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: m, n = len(matrix), len(matrix[0]) i, j = 0, n - 1 while i < m and j >= 0: if matrix[i][j] == target: return True elif matrix[i][j] < target: ...
search-a-2d-matrix-ii
2 pointers approach || Clean Code
Vaibhav7860
1
42
search a 2d matrix ii
240
0.507
Medium
4,554
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/1761136/python-linear-time
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: col = 0 row = len(matrix)-1 while True: if row < 0: return False elif col > len(matrix[0])-1: return False elif matrix[row][col] == target...
search-a-2d-matrix-ii
python linear time
gasohel336
1
72
search a 2d matrix ii
240
0.507
Medium
4,555
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/1592201/Py3Py-Simple-traversal-w-comments
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: # Init m = len(matrix) n = len(matrix[0]) # Helper function tp check if row and col # are within limits def inLimits(r,c): return (0 <= r < m) and (0 <= ...
search-a-2d-matrix-ii
[Py3/Py] Simple traversal w/ comments
ssshukla26
1
85
search a 2d matrix ii
240
0.507
Medium
4,556
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/1079121/python-solution-O(row%2Bcol)-time
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: row=len(matrix) col=len(matrix[0]) i=row-1 j=0 while(i>=0 and j<col): if matrix[i][j]==target: return True elif matrix[i][j]<target: j...
search-a-2d-matrix-ii
python solution O(row+col) time
_Rehan12
1
32
search a 2d matrix ii
240
0.507
Medium
4,557
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/557533/Python3-binary-search-explained
class Solution: def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ if not matrix: return False # keep the current state of the search in a global variable state = {'found': False} memory...
search-a-2d-matrix-ii
Python3 binary search explained
zetinator
1
262
search a 2d matrix ii
240
0.507
Medium
4,558
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/417051/Can-anyone-tell-me-the-runtime-of-my-recursive-Python-solution
class Solution: def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ if not matrix or not matrix[0]: return False m = len(matrix) n = len(matrix[0]) return self.search(matrix,...
search-a-2d-matrix-ii
Can anyone tell me the runtime of my recursive Python solution?
amrmahmoud96
1
173
search a 2d matrix ii
240
0.507
Medium
4,559
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2847406/This-Solution-Can-be-used-for-both-Search-matrix-1-and-2
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: n,m = len(matrix[0]),len(matrix) a=False for i in range(m): if matrix[i][n-1]<target and matrix[i][0]<target: continue else: for j in range(n): ...
search-a-2d-matrix-ii
This Solution Can be used for both Search matrix 1 and 2
Navaneeth7
0
2
search a 2d matrix ii
240
0.507
Medium
4,560
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2824625/Python3-O(log(m)-%2B-log(n))
class Solution: def searchMatrix(self, matrix, target: int): m, n = len(matrix), len(matrix[0]) # start position cur_x, cur_y = 0, len(matrix[0])-1 while cur_x >=0 and cur_x < m and cur_y >=0 and cur_y < n: if matrix[cur_x][cur_y] == target: return True ...
search-a-2d-matrix-ii
Python3 O(log(m) + log(n)) ?
Alex2019
0
5
search a 2d matrix ii
240
0.507
Medium
4,561
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2800752/Python3-Simple-6-line-O(n-log-m)
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: ROWS, COLS = len(matrix), len(matrix[0]) for r in range(ROWS): c = min(bisect_left(matrix[r], target), COLS-1) if matrix[r][c] == target: return True return False
search-a-2d-matrix-ii
[Python3] Simple 6 line O(n log m)
jonathanbrophy47
0
2
search a 2d matrix ii
240
0.507
Medium
4,562
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2727475/Python-recursion.
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: def helper(i1, j1, i2, j2): if target < matrix[i1][j1] or target > matrix[i2][j2]: return False if i2 - i1 <= 1 and j2 - j1 <= 1: for i in range(i1, i2 + 1): ...
search-a-2d-matrix-ii
Python, recursion.
yiming999
0
4
search a 2d matrix ii
240
0.507
Medium
4,563
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2673972/Python-3-solution-explained.
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: cursorY = len(matrix) - 1 cursorX = 0 while (cursorY >= 0) and (cursorX < len(matrix[0])): if matrix[cursorY][cursorX] > target: cursorY -= 1 elif matrix[cursorY][c...
search-a-2d-matrix-ii
Python 3 solution, explained.
JustARendomGuy
0
5
search a 2d matrix ii
240
0.507
Medium
4,564
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2673512/Python3-Easy-Code
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: n=len(matrix) r=0 c=len(matrix[0])-1 while r<n and c>=0: ele=matrix[r][c] if ele==target: return True if ele>target: c-=1 ...
search-a-2d-matrix-ii
Python3 Easy Code
pranjalmishra334
0
31
search a 2d matrix ii
240
0.507
Medium
4,565
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2654432/faster-than-83-Python3
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: res = False x = 0 y = 0 while y < len(matrix) and matrix[y][0] <= target: x = 0 while x < len(matrix[0]) and matrix[y][x] <= target: if matrix[y][x] ...
search-a-2d-matrix-ii
faster than 83% Python3
egeergull
0
22
search a 2d matrix ii
240
0.507
Medium
4,566
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2509884/Python-and-Go
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for line in matrix: if target in line: return True return False
search-a-2d-matrix-ii
Python and Go答え
namashin
0
25
search a 2d matrix ii
240
0.507
Medium
4,567
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2496074/Efficient-Python3-Solution-using-BInary-Search
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: m, n = len(matrix), len(matrix[0]) col = n - 1 for row in range(m): l, r = 0, col while l <= r: mid = (l + r) >> 1 if matrix[row][mid] == target: retu...
search-a-2d-matrix-ii
Efficient Python3 Solution using BInary Search
priyanshbordia
0
76
search a 2d matrix ii
240
0.507
Medium
4,568
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2408352/Python3-oror-CPP-oror-Java-oror-O(log-mn)-oror
class Solution: def searchMatrix(self, nums: List[List[int]], target: int) -> bool: r, c = len(nums), len(nums[0]) r_idx, c_idx = 0, c-1 while(r_idx < r and c_idx >= 0): if(nums[r_idx][c_idx] == target): return True elif(nums[r_idx][c_idx] > target): c_i...
search-a-2d-matrix-ii
Python3 || CPP || Java || O(log mn) ||
devilmind116
0
42
search a 2d matrix ii
240
0.507
Medium
4,569
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2333321/Python3-Solution-or-Using-BinarySearch
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: def binarySearch(arr): l = 0 r = len(arr)-1 while l <= r: mid = (l+r)//2 if arr[mid] == target: return True ...
search-a-2d-matrix-ii
Python3 Solution | Using BinarySearch
arvindchoudhary33
0
14
search a 2d matrix ii
240
0.507
Medium
4,570
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2328276/2-line-solution-in-Python-or-Search-a-2D-Matrix-II
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: res = any(target in sub for sub in matrix) return res
search-a-2d-matrix-ii
2 line solution in Python | Search a 2D Matrix II
nishanrahman1994
0
16
search a 2d matrix ii
240
0.507
Medium
4,571
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2327708/Python3-oror-Short-code-completed-oror-7-lines
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for i in range(len(matrix)): for j in range(len(matrix[0])): if matrix[i][j] == target: return True break
search-a-2d-matrix-ii
Python3 || Short code completed || 7 lines
hustkrykx
0
3
search a 2d matrix ii
240
0.507
Medium
4,572
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2327533/Python3-148-ms-faster-than-100.00-of-Python3.-TC-O(m-log(n))
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for row in matrix: if target < row[0] or target > row[-1]: continue if row[bisect_left(row, target)] == target: return True return False
search-a-2d-matrix-ii
[Python3] 148 ms, faster than 100.00% of Python3. TC = O(m log(n))
geka32
0
11
search a 2d matrix ii
240
0.507
Medium
4,573
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2327428/Python-Awesome-Diagonal-Search-oror-O(m%2Bn)-oror-Documented
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: # we will start from top-right corner of the matrix row = 0; col = len(matrix[0])-1 # check and return True if matched, else move to bottom-left corner while row < len(matrix) and col >= 0: ...
search-a-2d-matrix-ii
[Python] Awesome Diagonal Search || O(m+n) || Documented
Buntynara
0
7
search a 2d matrix ii
240
0.507
Medium
4,574
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2327404/Easy-Double-Loop-Python3-Solution-183ms-(Top-88)-20.3MB-(Top-98)
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: match=False #Setting the default to False (1) for i in range(len(matrix)): #2 For loops to go through each element (2) for j in range(len(matrix[0])): if matrix[i][j]>target: ...
search-a-2d-matrix-ii
Easy Double Loop Python3 Solution 183ms (Top 88%); 20.3MB (Top 98%)
Char1ton
0
12
search a 2d matrix ii
240
0.507
Medium
4,575
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2326596/PY3-BINARY-SEARCH-EASY-APPROACH-(90-RUNTIME)
class Solution: def binarySearch(self,target,row,n): i=0 j=n-1 while i<=j: mid=(i+j)//2 if row[mid]==target: return True elif row[mid]<target: i=mid+1 else: j=mid-1 return False ...
search-a-2d-matrix-ii
PY3✔ BINARY SEARCH EASY APPROACH✔ (90% RUNTIME)💥
ChinnaTheProgrammer
0
15
search a 2d matrix ii
240
0.507
Medium
4,576
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2326275/GoPython-O(m%2Bn)-time-or-O(1)-space
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: m = len(matrix) n = len(matrix[0]) i = 0 j = n-1 while 0<=i<m and 0<=j<n: item = matrix[i][j] if item == target: return True elif...
search-a-2d-matrix-ii
Go/Python O(m+n) time | O(1) space
vtalantsev
0
10
search a 2d matrix ii
240
0.507
Medium
4,577
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2325643/Python-or-Two-solutions-using-Binary-Search-and-Brute-Force-approach
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: # //////// Brute Force approach TC: O(M*N) /////// for r in range(len(matrix)): for c in range(len(matrix[0])): if matrix[r][c] == target: return True return ...
search-a-2d-matrix-ii
Python | Two solutions using Binary Search and Brute Force approach
__Asrar
0
16
search a 2d matrix ii
240
0.507
Medium
4,578
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2325442/Using-set-in-Python-only-4-lines-code
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for mat in matrix: m = set(mat) if target in m: return True return False
search-a-2d-matrix-ii
Using set in Python only 4 lines code
ankurbhambri
0
10
search a 2d matrix ii
240
0.507
Medium
4,579
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2325081/Python3-or-Easy-to-Understand-or-Efficient
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for row in matrix: # apply binary search if self.binarySearch(row, target): return True return False def binarySearch(self, nums: List[int], t: int) -> bool: ...
search-a-2d-matrix-ii
✅Python3 | Easy to Understand | Efficient
thesauravs
0
10
search a 2d matrix ii
240
0.507
Medium
4,580
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2258993/Python3-or-Bidirectional-Binary-Search-or-Intuitive-and-Easy-to-Understand
class Solution: found = False def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: def recurse(lo, hi, arr): mid = (lo + hi) // 2 if arr[mid] == target: self.found = True return if lo >= hi: ...
search-a-2d-matrix-ii
Python3 | Bidirectional Binary Search | Intuitive and Easy to Understand
Ahbar
0
53
search a 2d matrix ii
240
0.507
Medium
4,581
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2194381/Python-Faster-than-99.86-oror-Easy-Solution
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: matrix=filter(lambda x: x[0]<=target and x[len(x)-1]>=target ,matrix) for i in matrix: if target in i: return True return False
search-a-2d-matrix-ii
Python Faster than 99.86% || Easy Solution
deshkarmm
0
54
search a 2d matrix ii
240
0.507
Medium
4,582
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2152357/Python-oneliner
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: return True if [x for x in matrix for x in x if x == target] else False
search-a-2d-matrix-ii
Python oneliner
StikS32
0
25
search a 2d matrix ii
240
0.507
Medium
4,583
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2110576/Python-or-Two-different-Solution-95-faster
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: # /////// Solution 1 ///////////// # //////// Brute Force approach TC: O(M*N) /////// for r in range(len(matrix)): for c in range(len(matrix[0])): if matrix[r][c] == target: ...
search-a-2d-matrix-ii
Python | Two different Solution 95% faster
__Asrar
0
69
search a 2d matrix ii
240
0.507
Medium
4,584
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/2057735/Python-or-Simple-solution
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: for row in matrix: if target in row: return True else: return False
search-a-2d-matrix-ii
Python | Simple solution
manikanthgoud123
0
43
search a 2d matrix ii
240
0.507
Medium
4,585
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/1961689/Easy-Python-Solution-with-Explanation
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: # determine the rows and columns in a matrix rows = len(matrix) cols = len(matrix[0]) def searchTarget(row,col): while row >=0 and col<cols: # print(row,cols, matrix[r...
search-a-2d-matrix-ii
Easy Python Solution - with Explanation
palakmehta
0
80
search a 2d matrix ii
240
0.507
Medium
4,586
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/1945174/Python-(Simple-Approach-and-Beginner-Friendly)
class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: whole = [] for i in matrix: for j in i: whole.append(j) if target in whole: return True else: return False
search-a-2d-matrix-ii
Python (Simple Approach and Beginner-Friendly)
vishvavariya
0
35
search a 2d matrix ii
240
0.507
Medium
4,587
https://leetcode.com/problems/search-a-2d-matrix-ii/discuss/1897616/Python-beginner-Friendly-(BS)
class Solution: def binarysrc(self,arr,k): l=0 r=len(arr)-1 while l<=r: m=(l+r)//2 if arr[m]==k: return m elif k>arr[m]: l=m+1 else: r=m-1 else: return -1 def searchMatri...
search-a-2d-matrix-ii
Python beginner Friendly (BS)
imamnayyar86
0
70
search a 2d matrix ii
240
0.507
Medium
4,588
https://leetcode.com/problems/different-ways-to-add-parentheses/discuss/2392799/Python3-Divide-and-Conquer%3A-Recursion-%2B-Memoization
class Solution(object): def diffWaysToCompute(self, s, memo=dict()): if s in memo: return memo[s] if s.isdigit(): # base case return [int(s)] calculate = {'*': lambda x, y: x * y, '+': lambda x, y: x + y, '-': lambda x, y: x -...
different-ways-to-add-parentheses
[Python3] Divide and Conquer: Recursion + Memoization
mhpd
4
156
different ways to add parentheses
241
0.633
Medium
4,589
https://leetcode.com/problems/different-ways-to-add-parentheses/discuss/1719419/Python-or-Recursive-or-Concise-Solution
class Solution: def diffWaysToCompute(self, expression: str) -> List[int]: ops = { "+": lambda x, y : x + y, "-": lambda x, y : x - y, "*": lambda x, y : x * y } res = [] for x, char in enumerate(expression): if char in ops: ...
different-ways-to-add-parentheses
Python | Recursive | Concise Solution
srihariv
4
233
different ways to add parentheses
241
0.633
Medium
4,590
https://leetcode.com/problems/different-ways-to-add-parentheses/discuss/1469756/Python-or-99.7-(20ms)-or-Recursion-%2B-Memo-or-Clean-and-Concise
class Solution: def diffWaysToCompute(self, expression: str) -> List[int]: nums = '0123456789' def op(a, b, c): if c == '+': return a + b elif c == '-': return a - b else: return a * b @lru_...
different-ways-to-add-parentheses
Python | 99.7% (20ms) | Recursion + Memo | Clean and Concise
detective_dp
3
819
different ways to add parentheses
241
0.633
Medium
4,591
https://leetcode.com/problems/different-ways-to-add-parentheses/discuss/605583/Simple-divide-and-conquer-solution-Python
class Solution: def diffWaysToCompute(self, input: str) -> List[int]: def helper(string): result=[] for i, c in enumerate(string): if c in "+-*": x=helper(string[:i]) ...
different-ways-to-add-parentheses
Simple divide and conquer solution Python
Ayu-99
3
286
different ways to add parentheses
241
0.633
Medium
4,592
https://leetcode.com/problems/different-ways-to-add-parentheses/discuss/2425384/Detailed-Python-solution-with-comments.
class Solution(object): def diffWaysToCompute(self, expression): res=[] def fn(s,memo={}): res=[] if s in memo: return memo[s] if s.isdigit(): return [int(s)] #if string containsonly numbers return that syms=['-'...
different-ways-to-add-parentheses
Detailed Python solution with comments.
babashankarsn
1
147
different ways to add parentheses
241
0.633
Medium
4,593
https://leetcode.com/problems/different-ways-to-add-parentheses/discuss/2205910/Python-Memoization-Solution
class Solution: def diffWaysToCompute(self, expression: str) -> List[int]: def clac(a, b, operator): if operator == "+": return a + b if operator == "-": return a - b if operator == "*": return a * b memo = {} def solve(expr):...
different-ways-to-add-parentheses
[Python] Memoization Solution
samirpaul1
1
95
different ways to add parentheses
241
0.633
Medium
4,594
https://leetcode.com/problems/different-ways-to-add-parentheses/discuss/1927153/Python-easy-to-read-and-understand-or-recursion-memoization
class Solution: def solve(self, s): if s.isdigit(): return [int(s)] ans = [] for i in range(len(s)): if s[i] in ["+", "-", "*"]: left = self.solve(s[:i]) right = self.solve(s[i + 1:]) for l in left: f...
different-ways-to-add-parentheses
Python easy to read and understand | recursion-memoization
sanial2001
1
307
different ways to add parentheses
241
0.633
Medium
4,595
https://leetcode.com/problems/different-ways-to-add-parentheses/discuss/1927153/Python-easy-to-read-and-understand-or-recursion-memoization
class Solution: def solve(self, s): if s.isdigit(): return [int(s)] if s in self.d: return self.d[s] ans = [] for i in range(len(s)): if s[i] in ["+", "-", "*"]: left = self.solve(s[:i]) right = self.solve(s[i+1:]) ...
different-ways-to-add-parentheses
Python easy to read and understand | recursion-memoization
sanial2001
1
307
different ways to add parentheses
241
0.633
Medium
4,596
https://leetcode.com/problems/different-ways-to-add-parentheses/discuss/764006/Python3-dp
class Solution: def diffWaysToCompute(self, input: str) -> List[int]: #pre-processing to tokenize input tokens = re.split(r'(\D)', input) mp = {"+": add, "-": sub, "*": mul} for i, token in enumerate(tokens): if token.isdigit(): tokens[i] = int(token) else: t...
different-ways-to-add-parentheses
[Python3] dp
ye15
1
410
different ways to add parentheses
241
0.633
Medium
4,597
https://leetcode.com/problems/different-ways-to-add-parentheses/discuss/2848618/Divide-and-Conquer
class Solution: def diffWaysToCompute(self, expression: str) -> List[int]: res=[] for i in range(len(expression)): if expression[i] in ["+","-","*"]: left=self.diffWaysToCompute(expression[:i]) right=self.diffWaysToCompute(expression[i+1:]) ...
different-ways-to-add-parentheses
Divide and Conquer
lillllllllly
0
1
different ways to add parentheses
241
0.633
Medium
4,598
https://leetcode.com/problems/different-ways-to-add-parentheses/discuss/2834445/Python-recursive-solution
class Solution: def __init__(self): self.result = [] def diffWaysToCompute(self, expression: str) -> List[int]: n = len(expression) signs = [] nums = [] num = 0 for i in range(n): if expression[i] in "+-*": signs.append(expression[i]) ...
different-ways-to-add-parentheses
Python recursive solution
SiciliaLeco
0
5
different ways to add parentheses
241
0.633
Medium
4,599