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/shortest-unsorted-continuous-subarray/discuss/2055149/3-Python-solution-(Sorting-DP-and-most-optimal-one) | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
if len(nums)<2: return 0
smallest, biggest = [float('inf')]*len(nums), [float('-inf')]*len(nums)
#Biggest Element Array
for i in range(len(nums)):
biggest[i] = max(biggest[i-1], nums[i])
#Smal... | shortest-unsorted-continuous-subarray | 3 Python solution (Sorting, DP and most optimal one) | abrarjahin | 1 | 81 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,100 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2055149/3-Python-solution-(Sorting-DP-and-most-optimal-one) | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
#Find Max Index
right, maxVal = -1, float("-inf")
for i in range(len(nums)):
if nums[i] >= maxVal: maxVal = nums[i]
else: right = i
left, minVal = len(nums), float("inf")
#Find Min... | shortest-unsorted-continuous-subarray | 3 Python solution (Sorting, DP and most optimal one) | abrarjahin | 1 | 81 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,101 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2004599/Python3-Solution-with-using-two-pointers | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
_max = nums[0]
end = - 1
for i in range(1, len(nums)):
if _max > nums[i]:
end = i
else:
_max = nums[i]
_min = nums[-1]
begin =... | shortest-unsorted-continuous-subarray | [Python3] Solution with using two-pointers | maosipov11 | 1 | 40 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,102 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2003285/PYTHON-Simple-solution-easy-to-understand | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
temp = nums.copy()
temp.sort()
if temp==nums:
return 0
# print(temp)
# print(nums)
for i in range(0,len(temp)):
if temp[i]==nums[i]:
pass
else:
... | shortest-unsorted-continuous-subarray | PYTHON Simple solution easy to understand | Airodragon | 1 | 131 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,103 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2003032/Python-or-Two-Pointers-or-Easy-to-Understand-or-Beat-90 | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
sorted_nums = nums[:]
sorted_nums.sort()
start = 0
end = len(nums) -1
while start <= end and (nums[start] == sorted_nums[start] or nums[end] == sorted_nums[end]):
if nums[start] == so... | shortest-unsorted-continuous-subarray | Python | Two Pointers | Easy to Understand | Beat 90% | Mikey98 | 1 | 48 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,104 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/1091038/Python-or-Two-pointers-or-O(N)-Time-or-O(1)-Space | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
left, right = 0, len(nums)-1
while left < len(nums)-1 and nums[left] <= nums[left+1]:
left += 1
if left == len(nums)-1:
return 0
while right > 0 and nums[right] >= nums[right-1]:
... | shortest-unsorted-continuous-subarray | Python | Two-pointers | O(N) Time | O(1) Space | Rakesh301 | 1 | 209 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,105 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2837158/Python3-or-Two-Pass-Solution-or-Explanation | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
n = len(nums)
startPoint,endPoint = n,-1
currMin = nums[-1]
for i in range(n-2,-1,-1):
if nums[i] > currMin:
startPoint = i
else:
currMin = nums[i]
... | shortest-unsorted-continuous-subarray | [Python3] | Two Pass Solution | Explanation | swapnilsingh421 | 0 | 2 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,106 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2735584/Binary-Search-python3-Solution-or-O(n)-time-or-O(1)-space | class Solution:
def findLowPosition(self, low: int, hi: int, nums: List[int], target: int) -> int:
ans = low
while low <= hi:
mid = (low+hi)//2
if target >= nums[mid]:
low = mid+1
else:
ans = mid
... | shortest-unsorted-continuous-subarray | Binary Search python3 Solution | O(n) time | O(1) space | destifo | 0 | 3 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,107 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2711595/Python3-solution | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
if nums == sorted(nums):
return 0
check = sorted(nums)
res = 0
l = 0
for r in range(len(nums)):
if nums[r] != check[r]:
l = r
break
for i in... | shortest-unsorted-continuous-subarray | Python3 solution | user9458RI | 0 | 4 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,108 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2676995/Python-solution | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
nums2 = sorted(nums)
n = len(nums)
start , end = n ,0
for i in range(len(nums)):
if nums[i] != nums2[i]:
start = min(start, i)
end = max(end,i)
if start == n:
... | shortest-unsorted-continuous-subarray | Python solution | Sheeza | 0 | 6 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,109 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2672542/Python-Easy-Solution | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
S_nums = sorted(nums)
if S_nums == nums:
return 0
back = len(nums) - 1
front = 0
ff, bf = False, False #front and back flags
while front < back:
if S_nums[front] != nums... | shortest-unsorted-continuous-subarray | Python Easy Solution | user6770yv | 0 | 5 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,110 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2666779/Python-3-(nlogn) | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
unsorted=nums
sorted_=sorted(nums)
left=float('inf')
right=float('-inf')
for i in range(len(nums)):
if unsorted[i]!=sorted_[i]:
left=min(left,i)
right=max(right... | shortest-unsorted-continuous-subarray | Python 3 (nlogn) | Haseeb_io | 0 | 1 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,111 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2656035/Python-solution-using-two-pointer | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
i , j = 0 , len(nums) - 1
while( i < len(nums) - 1 and nums[i] <= nums[i+1]):
i += 1
# if array is sorted
if(i == len(nums)-1):
return 0
while( j > 0 and nums[j] >= nums[j-1]):... | shortest-unsorted-continuous-subarray | Python solution using two pointer | rajitkumarchauhan99 | 0 | 3 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,112 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2621289/Python3-Solution-or-O(nlogn) | class Solution:
def findUnsortedSubarray(self, A):
ans, count = 0, 0
for a, b in zip(A, sorted(A)):
if a != b or count:
count += 1
if a != b: ans = count
return ans | shortest-unsorted-continuous-subarray | ✔ Python3 Solution | O(nlogn) | satyam2001 | 0 | 9 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,113 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2562276/Python-Easiest-Solution | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
check = sorted(nums)
i = 0
j = len(nums)-1
while i<len(nums) and nums[i] == check[i]:
i+=1
while j>=0 and nums[j] == check[j]:
j-=1
if j<i:
return 0
els... | shortest-unsorted-continuous-subarray | Python Easiest Solution | Abhi_009 | 0 | 45 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,114 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2240418/Simple-Approach-oror-Clean-Code | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
n = len(nums)
if n == 1:
return 0
minimum, maximum = float('inf'), float('-inf')
for i in range(n):
if i == 0:
if (nums[i] > nums[i + 1]):
... | shortest-unsorted-continuous-subarray | Simple Approach || Clean Code | Vaibhav7860 | 0 | 48 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,115 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2005459/Python3-or-Simple-approach-or-With-explanation | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
temp = sorted(nums)
left,right = 0,0
flag = 0
for ind, (i1, s1) in enumerate(zip(temp, nums)):
if i1 == s1:
continue
else:
flag += 1
... | shortest-unsorted-continuous-subarray | Python3 | Simple approach | With explanation | volts440 | 0 | 5 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,116 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2005288/Python-O(n)-time-and-O(1)-space-Solution | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
n = len(nums) - 1
maxi, end, mini, start = nums[0], -1, nums[-1], 0
for i in range(n+1):
if nums[i] >= maxi: maxi = nums[i]
else: end = i
if nums[n - i] <= mini: mini = nums[n - i]
... | shortest-unsorted-continuous-subarray | ✅ Python O(n) time and O(1) space Solution | dhananjay79 | 0 | 26 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,117 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2005034/Beginner-Python-Solution-Using-sorted()-oror-Explained | class Solution(object):
def findUnsortedSubarray(self, nums):
minIndex, maxIndex = 1000, 0
sortedArr = sorted(nums)
for i in range(len(nums)):
if nums[i]!=sortedArr[i]:
minIndex = min(minIndex,i)
maxIndex = max(maxIndex,i)
... | shortest-unsorted-continuous-subarray | Beginner Python Solution Using sorted() || Explained | NathanPaceydev | 0 | 28 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,118 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2005014/Python-TC%3AO(n)-and-SC%3AO(1)-solution | class Solution(object):
def findUnsortedSubarray(self, li):
n = len(li)
if n==1: return 0
if li == sorted(li): return 0
start,end = 0,n-1
while start < n - 1 and li[start] <= li[start+1]:
start += 1
while end > 0 and li[end] >= li[end-1]... | shortest-unsorted-continuous-subarray | Python, TC:O(n) and SC:O(1) solution | Namangarg98 | 0 | 14 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,119 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2004865/Python-or-TC-O(N)-SC-O(N)-or-Easy-explanation | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
nums2 = sorted(nums)
start = float("inf")
end = float("-inf")
for i,n in enumerate(nums):
if nums[i] != nums2[i]:
start = min(start, i)
end = max(end,... | shortest-unsorted-continuous-subarray | Python | TC-O(N) - SC-O(N) | Easy explanation | Patil_Pratik | 0 | 5 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,120 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2004644/Python-or-O(n)-Time-or-O(1)-Space-or-Easy | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
# find the lowest index where adjacent are not in sorted order
l = 0
while l < len(nums)-1:
if nums[l] > nums[l+1]:
break
l+=1
# if nothing found return 0 ... | shortest-unsorted-continuous-subarray | Python | O(n) Time | O(1) Space | Easy | kewinMalone | 0 | 6 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,121 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2004314/Python-Solution-or-MultiPass-and-Two-Pointer-Based | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
if len(nums) == 1:
return 0
start = 0
prevPtr = nums[0]
for i in range(0,len(nums)):
if prevPtr > nums[i]:
start = i
else:
prevPtr =... | shortest-unsorted-continuous-subarray | Python Solution | MultiPass and Two Pointer Based | Gautam_ProMax | 0 | 14 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,122 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2004299/Python3-Using-Sorting | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
og_nums = nums.copy()
nums.sort()
index1 = -inf
index2 = inf
i = 0
j = len(nums) - 1
# Compare sorted list against original list
# i indexes from left to right
# j... | shortest-unsorted-continuous-subarray | Python3 Using Sorting | user5622HA | 0 | 16 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,123 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2004076/Daily-May-Leetcode-Challenge-day-3 | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
temp=nums.copy()
temp.sort()
lower=0
for i in range(len(nums)):
if(temp[i]!=nums[i]):
lower=i
break
temp.reverse()
nums.reverse()
higher=0
... | shortest-unsorted-continuous-subarray | Daily May Leetcode Challenge day 3 | shivangtomar | 0 | 13 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,124 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2004076/Daily-May-Leetcode-Challenge-day-3 | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
temp=nums.copy()
temp.sort()
c=[]
for i in range(len(nums)):
if(nums[i]!=temp[i]):
c.append(i)
if(len(nums)<=1):
return 0
elif(temp==nums):
retu... | shortest-unsorted-continuous-subarray | Daily May Leetcode Challenge day 3 | shivangtomar | 0 | 13 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,125 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2003722/Python-SOlution-using-Sorting-or-T%3AO(nlogn)orS%3AO(n) | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
temp = sorted(nums)
left_bound, right_bound = 0, 0
unsorted_subarry_exists = False
# Identify the leftmost index where the sort poperty breaks
for i in range(len(nums)):
if nums[i] != temp[i]... | shortest-unsorted-continuous-subarray | Python SOlution, using Sorting | T:O(nlogn)|S:O(n) | pradeep288 | 0 | 4 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,126 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2003667/Python-O(n)-Super-Easy-to-understand-O(n) | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
# Determine the 'correct' position for every number
sortedNums = sorted(nums)
first = None # first(leftmost) index we encounter a number 'out of place'
last = None # last(rightmost) index we encounte... | shortest-unsorted-continuous-subarray | Python O(n) Super Easy to understand - O(n) | rahul_3141 | 0 | 13 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,127 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2003618/python-java-sorting-and-two-pointers-(Time-Onlogn-space-On) | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
table = []
for i in range(len(nums)) :
table.append([nums[i], i])
table.sort()
l = -1
for i in range(len(nums)) :
if table[i][1] != i : break
l = i
r = len(nums)
while r > l :
r... | shortest-unsorted-continuous-subarray | python, java - sorting & two pointers (Time Onlogn, space On) | ZX007java | 0 | 8 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,128 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2003612/Python-O(N)-solution-with-Intuition-explained-with-Example | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
start = - 1
end = 0
end_val = nums[0]
l = len(nums)
start_val = nums[l - 1]
if l < 2 :
return 0
for i in range(len(nums )) :
#end position is the index at which e... | shortest-unsorted-continuous-subarray | [Python] O(N) solution with Intuition explained with Example | Ojasvi-Verma | 0 | 3 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,129 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2003503/Python-3-solution-fast | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
n = len(nums)
start = -1
end = -2
for i, num in enumerate(sorted(nums)):
if num != nums[i]:
if start == -1:
start = i
else:
end ... | shortest-unsorted-continuous-subarray | Python 3 solution fast | bad_karma25 | 0 | 14 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,130 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2003161/Python-Easy-Solution-or-One-loop | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
res = 0
tmp = 0
for n1, n2 in zip(nums, sorted(nums)) :
if n1 != n2 :
if not tmp : res += 1
else :
res += 1+tmp
tmp = 0
eli... | shortest-unsorted-continuous-subarray | [ Python ] Easy Solution | One loop | crazypuppy | 0 | 12 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,131 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2003090/easy-python-code | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
a = 0
b = len(nums)
for i in range(len(nums)):
if nums[a] == min(nums[a:]):
a+=1
else:
break
for i in range(len(nums)):
if nums[b-1] == max(nums... | shortest-unsorted-continuous-subarray | easy python code | dakash682 | 0 | 15 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,132 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/1870523/Python-easy-to-read-and-understand-or-two-pointers | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
n = len(nums)
start, end = n-1, 0
prev = nums[0]
for i in range(1, n):
if nums[i] < prev:
end = i
else:
prev = nums[i]
nxt = nums[... | shortest-unsorted-continuous-subarray | Python easy to read and understand | two-pointers | sanial2001 | 0 | 71 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,133 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/1775476/Python-Easy-and-clean-sorting-solution | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
n = len(nums)
start = -1
end = -2
for i, num in enumerate(sorted(nums)):
if num != nums[i]:
if start == -1:
start = i
else:
... | shortest-unsorted-continuous-subarray | [Python] Easy and clean sorting solution | nomofika | 0 | 111 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,134 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/1586025/WEEB-DOES-PYTHON | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
arr = sorted(nums)
low, high = 0, 0
foundStart = False
for i in range(len(nums)):
if nums[i] != arr[i] and not foundStart:
foundStart = True
low = i
elif nums[i] != arr[i] and foundStart:
high = i
return high - low +... | shortest-unsorted-continuous-subarray | WEEB DOES PYTHON | Skywalker5423 | 0 | 101 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,135 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/1083225/PythonPython3-Shortest-Unsorted-Continuous-Subarray | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
# first we sort the array to know the correct sorted order
b = sorted(nums)
# initialize the start and end index to None to get the start and end index of the array
# that needs to be sorted in order to get the array complet... | shortest-unsorted-continuous-subarray | [Python/Python3] Shortest Unsorted Continuous Subarray | newborncoder | 0 | 124 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,136 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/1082125/python3-solution-O(n)-time-and-O(1)-space | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
i=0
n=len(nums)
res=-1
mn=sys.maxsize
while(i<n):
if i<n-1 and nums[i]>nums[i+1]:
x=nums[i]
i+=1
mn=min(nums[i],mn)
while(i<n an... | shortest-unsorted-continuous-subarray | python3 solution O(n) time and O(1) space | _Rehan12 | 0 | 26 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,137 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/355660/Python-O(n)-in-time-O(1)-in-space-(no-sort-required) | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
lo = hi = 0
max_ = float("-inf")
for i in range(len(nums)):
max_ = max(max_, nums[i])
if nums[i] < max_: hi = i+1
min_ = float("inf")
for i in reversed(range(hi)): ... | shortest-unsorted-continuous-subarray | Python O(n) in time, O(1) in space (no sort required) | ye15 | 0 | 95 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,138 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/355660/Python-O(n)-in-time-O(1)-in-space-(no-sort-required) | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
mn, mx = inf, -inf
lo = hi = None
for i in range(len(nums)):
if nums[ i] < (mx := max(mx, nums[ i])): hi = i
if nums[~i] > (mn := min(mn, nums[~i])): lo = len(nums) + ~i
return hi - lo + ... | shortest-unsorted-continuous-subarray | Python O(n) in time, O(1) in space (no sort required) | ye15 | 0 | 95 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,139 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/317984/Simon's-Note-Python3-easy-to-understand | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
sort_nums=sorted(nums)
temp=['0' if i==j else '1' for i,j in zip(sort_nums,nums)]
temp1=''.join(temp)
return len(temp1.strip('0')) | shortest-unsorted-continuous-subarray | [🎈Simon's Note🎈] Python3 easy to understand | SunTX | 0 | 100 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,140 |
https://leetcode.com/problems/shortest-unsorted-continuous-subarray/discuss/2007844/Python-O(n)-Time-with-Sorting | class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
sortednums = sorted(nums)
if sortednums == nums:
return 0
start = 0
end = len(nums)-1
for i in range(len(nums)):
if nums[i]!=sortednums[i]:
start = i
... | shortest-unsorted-continuous-subarray | Python O(n) Time with Sorting | Vamsidhar01 | -1 | 19 | shortest unsorted continuous subarray | 581 | 0.363 | Medium | 10,141 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2148966/Python-DP-2-approaches-using-LCS | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m,n=len(word1),len(word2)
@cache
def lcs(i, j): # find longest common subsequence
if i==m or j==n:
return 0
return 1 + lcs(i+1, j+1) if word1[i]==word2[j] else max(lcs(i... | delete-operation-for-two-strings | Python DP 2 approaches using LCS ✅ | constantine786 | 49 | 3,000 | delete operation for two strings | 583 | 0.594 | Medium | 10,142 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2148966/Python-DP-2-approaches-using-LCS | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
if len(word1)>len(word2):
word2,word1=word1,word2
m,n=len(word1),len(word2)
prev=[0] * (m+1)
for i in range(n-1, -1, -1):
curr=[0] * (m+1)
for j ... | delete-operation-for-two-strings | Python DP 2 approaches using LCS ✅ | constantine786 | 49 | 3,000 | delete operation for two strings | 583 | 0.594 | Medium | 10,143 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2149499/PYTHON-oror-EXPLAINED-oror | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m = len(word1)
n = len(word2)
a = []
for i in range(m+1):
a.append([])
for j in range(n+1):
a[-1].append(0)
for i in range(m):
for j in range(n):... | delete-operation-for-two-strings | ✔️ PYTHON || EXPLAINED || ;] | karan_8082 | 22 | 1,100 | delete operation for two strings | 583 | 0.594 | Medium | 10,144 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2158892/Extension-of-LCS | class Solution:
def minDistance(self, text1: str, text2: str) -> int:
dp = {}
n = len(text1)
m = len(text2)
def LCS(i,j):
if i< 0 or i>=n or j<0 or j>=m:
return 0
if (i,j) in dp:
return dp[(i,j)]
re... | delete-operation-for-two-strings | 📌 Extension of LCS | Dark_wolf_jss | 4 | 24 | delete operation for two strings | 583 | 0.594 | Medium | 10,145 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/1277585/two-dp-solution-or-recursive-%2B-memo-or-iterative-or-easy-to-understand | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
memo = {}
def abhi(a,b):
if a==len(word1):
return len(word2)-b
elif b==len(word2):
return len(word1)-a
elif (a,b) in memo:... | delete-operation-for-two-strings | two dp solution | recursive + memo | iterative | easy to understand | chikushen99 | 3 | 210 | delete operation for two strings | 583 | 0.594 | Medium | 10,146 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/1277585/two-dp-solution-or-recursive-%2B-memo-or-iterative-or-easy-to-understand | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
n=len(word1)
m=len(word2)
dp=[ [0]*(m+1) for j in range(n+1) ]
for i in range(1,m+1):
dp[0][i] = i
for j in range(1,n+1):
dp[j][0] = j
... | delete-operation-for-two-strings | two dp solution | recursive + memo | iterative | easy to understand | chikushen99 | 3 | 210 | delete operation for two strings | 583 | 0.594 | Medium | 10,147 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2764194/Python-oror-easy-soluoror-using-DP-bottomup | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m=len(word1)
n=len(word2)
dp=[]
for i in range (m+1):
dp.append([0]*(n+1))
for i in range (m+1):
dp[i][0]=i
for i in range (n+1):
dp[0][i]=i
for i in rang... | delete-operation-for-two-strings | Python || easy solu|| using DP bottomup | tush18 | 1 | 159 | delete operation for two strings | 583 | 0.594 | Medium | 10,148 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2151465/Python-Simple-Solution-or-Dynamic-Programming-(longest-common-subsequence) | class Solution:
# this can we solved using longest common subsequence
def minDistance(self, w1: str, w2: str) -> int:
n , m = len(w1), len(w2)
@cache
def lcs(n, m):
# base case if either of the string is empty then return 0
if n < 0 or m < 0: return 0
... | delete-operation-for-two-strings | ✅ Python Simple Solution | Dynamic Programming (longest common subsequence) | Nk0311 | 1 | 62 | delete operation for two strings | 583 | 0.594 | Medium | 10,149 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2149863/java-python-modification-of-Levenstein-algo | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
n1, n2 = len(word1) + 1, len(word2) + 1
t = []
for i in range(n1): t.append([0]*n2)
for x in range(n2): t[0][x] = x
for y in range(n1): t[y][0] = y
y, py = 1, 0
while y != n1 :
x, px = 1, 0
while x ... | delete-operation-for-two-strings | java, python - modification of Levenstein algo | ZX007java | 1 | 32 | delete operation for two strings | 583 | 0.594 | Medium | 10,150 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2149234/Python3-or-DP-or-LCS | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
if word1 in word2:
return len(word2)-len(word1)
if word2 in word1:
return len(word1)-len(word2)
m, n = len(word1), len(word2)
ans = self.lcs(word1, word2, n, m)
return n+m-(2*ans)
... | delete-operation-for-two-strings | Python3 | DP | LCS | H-R-S | 1 | 62 | delete operation for two strings | 583 | 0.594 | Medium | 10,151 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/1508089/Python-3Space-complexity-O(n)Straightforward-DP-wo-LCS-Faster-than-97-solution | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
# Make sure that word1 is always shorter
# to reduce memory usage
if len(word1) > len(word2):
word1, word2 = word2, word1
n1, n2 = len(word1), len(word2)
# Use only O(n) space ... | delete-operation-for-two-strings | [Python 3][Space complexity O(n)][Straightforward DP w/o LCS] Faster than 97% solution | ynnekuw | 1 | 44 | delete operation for two strings | 583 | 0.594 | Medium | 10,152 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/1472660/Python-Top-down-Solution | class Solution:
def minDistance(self, X: str, Y: str) -> int:
m, n = len(X), len(Y)
li = [[0 for j in range(n+1)] for i in range(m+1)]
for i in range(1, m+1):
for j in range(1, n+1):
if X[i-1] == Y[j-1]:
li[i][j] = 1 + li[i-1][j-1]
... | delete-operation-for-two-strings | Python Top down Solution | Namangarg98 | 1 | 94 | delete operation for two strings | 583 | 0.594 | Medium | 10,153 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/1196531/Python-Simple-preprocessing-that-beats-99.58 | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
# ---------- Preprocessing ----------------
l12 = len(word1)+len(word2)
s1 = set(word1)
s2 = set(word2)
word2 = [w for w in word2 if w in s1]
word1 = [w for w in word1 if w in s2]
# -------... | delete-operation-for-two-strings | [Python] Simple preprocessing that beats 99.58% | Cvaniak | 1 | 109 | delete operation for two strings | 583 | 0.594 | Medium | 10,154 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/937653/Python3-DP-using-LCS-concept.-beats-92 | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
if not word1 and not word2:
return 0
if not word1 or not word2:
return 1
return self.lcs(word1,word2)
def lcs(self,s1,s2):
... | delete-operation-for-two-strings | [Python3] DP using LCS concept. beats 92% | tilak_ | 1 | 134 | delete operation for two strings | 583 | 0.594 | Medium | 10,155 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2781816/simple-python-DP-solution-using-LCS | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
l1 = len(word1)
l2 = len(word2)
t = [[-1 for i in range(l2 + 1)] for j in range(l1 + 1)]
for i in range(l1 + 1):
for j in range(l2 + 1):
if i == 0 or j == 0:
t[i][j] = 0
for i in range(1, l1 + 1):
for j in range(1, l2 + 1)... | delete-operation-for-two-strings | simple python DP solution using LCS | nikhitamore | 0 | 2 | delete operation for two strings | 583 | 0.594 | Medium | 10,156 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2778114/Python-DP-solution | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m, n = len(word1), len(word2)
dp1 = [0] * (n+1)
dp0 = dp1[:]
for i in range(m):
for j in range(n):
if word1[i] == word2[j]:
dp1[j+1] = dp0[j] + 1
els... | delete-operation-for-two-strings | Python DP solution | Kylin2015 | 0 | 1 | delete operation for two strings | 583 | 0.594 | Medium | 10,157 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2776065/LCS-approach-faster-than-98.09 | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
if not word1 and not word2:
return 0
if not word1 or not word2:
return 1
return self.lcs(word1,word2)
def lcs(self,s1,s2):
... | delete-operation-for-two-strings | LCS approach faster than 98.09% | SId609 | 0 | 2 | delete operation for two strings | 583 | 0.594 | Medium | 10,158 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2764297/Python-Solution%3A-DP-%2B-Memoization-oror-Recurrsive-approach | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m=len(word1)
n=len(word2)
dp=[[-1 for i in range(n)] for j in range(m)]
def lcs(m,n):
if m<0 or n<0:
return 0
if dp[m][n]!=-1:
return dp[m][n]
... | delete-operation-for-two-strings | Python Solution: DP + Memoization || Recurrsive approach | utsa_gupta | 0 | 5 | delete operation for two strings | 583 | 0.594 | Medium | 10,159 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2755762/Python-Solution-with-Easy-understandable-Approach. | class Solution:
def minDistance(self, A: str, B: str) -> int:
dp = [[0 for _ in range((len(B)+1))] for _ in range(len(A)+1)]
for i in range(1,len(A)+1):
for j in range(1,len(B)+1):
if A[i-1] == B[j-1]:
dp[i][j] = 1 + dp[i-1][j-1]
else:
... | delete-operation-for-two-strings | Python Solution with Easy understandable Approach. | aryan_codes_here | 0 | 3 | delete operation for two strings | 583 | 0.594 | Medium | 10,160 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2585018/Top-Down-Approach-oror-DP-oror-Python3-Solutionoror-LCS | class Solution:
def minDistance(self, x: str, y: str) -> int:
n = len(x)
m = len(y)
dp = [[-1 for _ in range(m+1)] for _ in range(n+1)]
for i in range(n+1):
for j in range(m+1):
if i==0 or j==0:
dp[i][j] = 0
... | delete-operation-for-two-strings | Top-Down Approach || DP || Python3 Solution|| LCS | ajinkyabhalerao11 | 0 | 34 | delete operation for two strings | 583 | 0.594 | Medium | 10,161 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2507604/Python3-or-Tabular-DP | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
n,m=len(word1),len(word2)
dp=[[0 for i in range(n+1)] for j in range(m+1)]
dp[0][0]=0
for i in range(1,n+1):
dp[0][i]=dp[0][i-1]+1
for j in range(1,m+1):
dp[j][0]=dp[j-1][0]+1
... | delete-operation-for-two-strings | [Python3] | Tabular DP | swapnilsingh421 | 0 | 24 | delete operation for two strings | 583 | 0.594 | Medium | 10,162 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2317480/Python-3-oror-Dynamic-programming-pretty-efficient | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
rows, cols = len(word1)+1, len(word2)+1
dp = [[0 for _ in range(cols)] for _ in range(rows)]
for r in range(rows):
dp[r][0] = 1 + dp[r-1][0] if r else 0
for c in range(cols):
dp[0][c] = 1 + dp[0][c-1] if c else 0
for row in range... | delete-operation-for-two-strings | Python 3 || Dynamic programming pretty efficient | sagarhasan273 | 0 | 15 | delete operation for two strings | 583 | 0.594 | Medium | 10,163 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2249454/Python3-DP-solution-O(N2)-space-and-time-or-LCS-Approach | class Solution:
def minDistance(self, x: str, y: str) -> int:
#DP question because it asks 'optimal' = minimum
#category of DP? LCS. why? because 2 strings are given and integer output is expected
#this is exactly what the i/p and o/p was in case of LCS
#matrix takes O(N^2) SPACE
#time: O(N^2)
... | delete-operation-for-two-strings | Python3 DP solution O(N^2) space and time | LCS Approach | aiyer0987 | 0 | 37 | delete operation for two strings | 583 | 0.594 | Medium | 10,164 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2197273/Python-solution-using-Dynamic-programming-Longest-Common-Subsequence | class Solution:
def lcs(self, text1,text2) -> int:
dp = [[0 for j in range(len(text2) + 1)] for i in range(len(text1) + 1)]
for i in range(len(text1) - 1,-1,-1):
for j in range(len(text2) - 1, -1, -1):
if text1[i] == text2[j]:
dp[i][j] = 1 + dp[i+1][j+... | delete-operation-for-two-strings | Python solution using Dynamic programming Longest Common Subsequence | nishanrahman1994 | 0 | 30 | delete operation for two strings | 583 | 0.594 | Medium | 10,165 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2160241/Python-simple-DP-using-only-two-rows | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
curr = list(range(len(word2) + 1))
for i1 in range(len(word1)):
curr, prev = [i1 + 1 for _ in range(len(word2) + 1)], curr
for i2 in range(len(word2)):
if word1[i1] == word2[i2]:
... | delete-operation-for-two-strings | Python, simple DP using only two rows | blue_sky5 | 0 | 22 | delete operation for two strings | 583 | 0.594 | Medium | 10,166 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2151788/Python-DP-(LCS) | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
s1 = word1 ; s2 = word2
dp = [ [0]*(len(s2)+1) for _ in range(len(s1)+1) ]
for i in range(1, len(s1)+1):
for j in range(1, len(s2)+1):
if s1[i-1] == s2[j-1]:
... | delete-operation-for-two-strings | Python DP (LCS) | lokeshsenthilkumar | 0 | 23 | delete operation for two strings | 583 | 0.594 | Medium | 10,167 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2151657/Python3-Solution-with-using-dynamic-programming | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
dp = [[0 for _ in range(len(word2) + 1)] for _ in range(len(word1) + 1)]
for i in range(len(word1) + 1):
for j in range(len(word2) + 1):
if i == 0 or j == 0:
dp[i][j] = i + ... | delete-operation-for-two-strings | [Python3] Solution with using dynamic programming | maosipov11 | 0 | 16 | delete operation for two strings | 583 | 0.594 | Medium | 10,168 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2150989/WEEB-DOES-PYTHONC%2B%2B-DP | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
dp = [[0 for i in range(len(word1)+1)] for j in range(len(word2)+1)]
for i in range(1, len(word2)+1):
for j in range(1, len(word1)+1):
if word1[j-1] == word2[i-1]:
dp[i][j] = 1 + dp[i-1][j-1]
else:
dp[i][j] = max(dp[i-1][j... | delete-operation-for-two-strings | WEEB DOES PYTHON/C++ DP | Skywalker5423 | 0 | 14 | delete operation for two strings | 583 | 0.594 | Medium | 10,169 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2149923/Easy-Python-Solution-or-Fast | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m = len(word1)
n = len(word2)
dp = [[10000]*(n+1) for _ in range(m+1)]
for i in range(m+1):
dp[i][0] = i
for i in range(n+1):
dp[0][i] = i
... | delete-operation-for-two-strings | Easy Python Solution | Fast | Into_You | 0 | 28 | delete operation for two strings | 583 | 0.594 | Medium | 10,170 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/2149622/Python3-or-LCS-or-Top-Down | class Solution:
def minDistance(self, s1: str, s2: str) -> int:
@lru_cache(None)
def lcs(i, j):
if i >= len(s1) or j >= len(s2):
return 0
if s1[i] == s2[j]:
return 1 + lcs(i+1, j+1)
else:
... | delete-operation-for-two-strings | Python3 | LCS | Top-Down | suhrid | 0 | 11 | delete operation for two strings | 583 | 0.594 | Medium | 10,171 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/1762275/Python-super-simple-solution-using-%22longest-common-subsequence%22-problem | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
# longest common subsequence
m, n = len(word1), len(word2)
h = {}
def lcs(i, j):
if i < 0 or j < 0:
return 0
elif (i, j) in h:
return h[(i, j)]
... | delete-operation-for-two-strings | Python super simple solution using "longest common subsequence" problem | byuns9334 | 0 | 41 | delete operation for two strings | 583 | 0.594 | Medium | 10,172 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/1439200/583.-Delete-Operation-for-Two-Strings-Python-DP-LCS-Solution | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
lcsLength = self.getLCSLength(word1, word2)
return len(word1) + len(word2) - (2 * lcsLength)
def getLCSLength(self, word1: str, word2: str):
storage = [[0 for _ in range(len(word2) + 1)] for _ in ra... | delete-operation-for-two-strings | 583. Delete Operation for Two Strings - Python DP LCS Solution | rohanpednekar_ | 0 | 49 | delete operation for two strings | 583 | 0.594 | Medium | 10,173 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/1196751/Python3-DFS-%2B-Memoization | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
@cache
def dfs(i, j):
if (i == len(word1)):
return len(word2) - j
if (j == len(word2)):
return len(word1) - i
if (word1[i] == word2[j]):
return df... | delete-operation-for-two-strings | Python3 - DFS + Memoization | Bruception | 0 | 161 | delete operation for two strings | 583 | 0.594 | Medium | 10,174 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/1195982/You-will-not-find-easier-simpler-python-code-than-this-100-pure | class Solution:
def minDistance(self, w1: str, w2: str) -> int:
b , s = (w1, w2) if len(w1) >= len(w2) else (w2, w1)
p = defaultdict(list)
for i, c in enumerate(s):
p[c].append(i)
l = [0] * (len(s) + 1)
for c in b:
for i in reversed... | delete-operation-for-two-strings | You will not find easier simpler python code than this 100% pure😯 | Khacker | 0 | 106 | delete operation for two strings | 583 | 0.594 | Medium | 10,175 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/1117731/Python-~7-lines-clean-code | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
@functools.lru_cache(None)
def recurse(i, j):
x, y = word1[i:i+1], word2[j:j+1]
if x == "" or y == "": return max(len(word1[i:]), len(word2[j:]))
if x == y: return recurse(i+1, j+1)
... | delete-operation-for-two-strings | Python ~7 lines - clean code | dev-josh | 0 | 131 | delete operation for two strings | 583 | 0.594 | Medium | 10,176 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/884355/Python3-top-down-dp | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
@lru_cache(None)
def fn(i1, i2):
"""Return minimum steps to make word1 and word2 the same."""
if i1 == len(word1): return len(word2) - i2
if i2 == len(word2): return len(word1) - i1
... | delete-operation-for-two-strings | [Python3] top-down dp | ye15 | 0 | 59 | delete operation for two strings | 583 | 0.594 | Medium | 10,177 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/884355/Python3-top-down-dp | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m, n = len(word1), len(word2)
dp = [[0]*(n+1) for _ in range(m+1)]
for i in range(m): dp[i][n] = m-i
for j in range(n): dp[m][j] = n-j
for i in range(m-1, -1, -1):
for j in range(n-1, -1, -1): ... | delete-operation-for-two-strings | [Python3] top-down dp | ye15 | 0 | 59 | delete operation for two strings | 583 | 0.594 | Medium | 10,178 |
https://leetcode.com/problems/delete-operation-for-two-strings/discuss/389808/Simon's-Note-Python3-DP | class Solution:
def minDistance(self, word1: str, word2: str) -> int:
dp=[[0 for _ in range(len(word2)+1)]for _ in range(len(word1)+1)]
for i in range(1,len(dp)):
for j in range(1,len(dp[0])):
if word1[i-1]==word2[j-1]:
dp[i][j]=dp[i-1][j-1]+1
... | delete-operation-for-two-strings | [🎈Simon's Note🎈] Python3 DP | SunTX | 0 | 62 | delete operation for two strings | 583 | 0.594 | Medium | 10,179 |
https://leetcode.com/problems/erect-the-fence/discuss/864619/Python-Solution-with-Monotone-Chain-Algorithm | class Solution:
def outerTrees(self, points: List[List[int]]) -> List[List[int]]:
"""
Use Monotone Chain algorithm.
"""
def is_clockwise(
p0: List[int], p1: List[int], p2: List[int]) -> bool:
"""
Determine the orientation the slope p0p2 is on t... | erect-the-fence | Python Solution with Monotone Chain Algorithm | eroneko | 4 | 542 | erect the fence | 587 | 0.523 | Hard | 10,180 |
https://leetcode.com/problems/erect-the-fence/discuss/2828937/python3-graham's-scan-method | class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
def check_clockwise(p1,p2,p3):
x1,y1 = p1
x2,y2 = p2
x3,y3 = p3
"""
slope of p1 and p2 will be y2-y1/x2-x1
slope of p2 ... | erect-the-fence | python3 graham's scan method | shashank732001 | 2 | 206 | erect the fence | 587 | 0.523 | Hard | 10,181 |
https://leetcode.com/problems/erect-the-fence/discuss/1442799/Python3-convex-hull-via-Graham-scan | class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
# convex hull via Graham scan
xx, yy = min(trees, key=lambda x: (x[1], x[0])) # reference point
mp = {}
for x, y in trees: mp.setdefault(atan2(y-yy, x-xx), []).append([x, y])
t... | erect-the-fence | [Python3] convex hull via Graham scan | ye15 | 2 | 213 | erect the fence | 587 | 0.523 | Hard | 10,182 |
https://leetcode.com/problems/erect-the-fence/discuss/2831393/Python-full-explanation-! | class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
def compare_slopes(point1, point2, point3):
x_point1, y_point1 = point1
x_point2, y_point2 = point2
x_point3, y_point3 = point3
return ((y_point3 - y_point2)*(x_point2 - x_point1)... | erect-the-fence | Python = full explanation ! | fatalbanana | 1 | 17 | erect the fence | 587 | 0.523 | Hard | 10,183 |
https://leetcode.com/problems/erect-the-fence/discuss/2830891/easy-python3-Erect-the-Fence | class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
def clockwise(p1,p2,p3):
x1,y1=p1
x2,y2=p2
x3,y3=p3
return ((y3-y2)*(x2-x1)-(y2-y1)*(x3-x2))
trees.sort()
upper=[]
lower=[]
for t in t... | erect-the-fence | #easy-python3 #Erect the Fence🔥🔥🔥🔥🔥🔥🔥🔥🔥 | abhishekkhajuria | 1 | 36 | erect the fence | 587 | 0.523 | Hard | 10,184 |
https://leetcode.com/problems/erect-the-fence/discuss/2848672/Python3-Pyhton-Graham's-scan | class Solution(object):
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
def cal_direct(a, b, c):
return (c[1] - b[1]) * (b[0] - a[0]) - (b[1] - a[1]) * (c[0] - b[0])
def updateEdges(tree):
while len(upper_edges) >= 2 and cal_direct(upper_edges[-2], u... | erect-the-fence | [Python3 / Pyhton] Graham's scan | happycoaster | 0 | 1 | erect the fence | 587 | 0.523 | Hard | 10,185 |
https://leetcode.com/problems/erect-the-fence/discuss/2831382/Faster-than-89 | class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
l=len(trees)
if l<=3:
return trees
minx=min(t[0] for t in trees)
miny=None
for i in range(l):
if trees[i][0]==minx and (miny==None or trees[i][1]<miny):
... | erect-the-fence | Faster than 89% | mbeceanu | 0 | 9 | erect the fence | 587 | 0.523 | Hard | 10,186 |
https://leetcode.com/problems/erect-the-fence/discuss/2831345/Monotone-chain-convex-hull-beats-96 | class Solution:
def outerTrees(self, points: List[List[int]]) -> List[List[int]]:
"""Computes the convex hull of a set of 2D points.
Input: an iterable sequence of (x, y) pairs representing the points.
Output: a list of vertices of the convex hull in counter-clockwise order,
sta... | erect-the-fence | Monotone chain convex hull beats 96% | Mencibi | 0 | 9 | erect the fence | 587 | 0.523 | Hard | 10,187 |
https://leetcode.com/problems/erect-the-fence/discuss/2831278/python-the-most-efficient-solution-with-97-time-and-90-space-beats | class Solution:
def fslope(self,one,two):
if one[0] == two[0]:
return float("inf")
return abs((two[1]-one[1])/(two[0]-one[0]))
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
trees.sort()
# print(trees)
n = len(trees)
vis = {0}
... | erect-the-fence | python the most efficient solution with 97% time and 90% space beats | benon | 0 | 19 | erect the fence | 587 | 0.523 | Hard | 10,188 |
https://leetcode.com/problems/erect-the-fence/discuss/2831055/587.-Erect-the-Fence-or-Python3-or-Monotone-Chain-Algorithm | class Solution:
def outerTrees(self, points: List[List[int]]) -> List[List[int]]:
"""
Use Monotone Chain algorithm.
"""
def is_clockwise(
p0: List[int], p1: List[int], p2: List[int]) -> bool:
return (p1[1] - p0[1]) * (p2[0] - p0[0]) > (p2[1] - p0[1]) * (p... | erect-the-fence | 587. Erect the Fence | Python3 | Monotone Chain Algorithm | AndrewMitchell25 | 0 | 13 | erect the fence | 587 | 0.523 | Hard | 10,189 |
https://leetcode.com/problems/erect-the-fence/discuss/2830952/Andrew's-Algorithm-with-Convex-Hull-Points-in-Clockwise-Order-The-Best | class Solution:
def outerTrees(self, trees: list[list[int]]) -> list[list[int]]:
if len(trees) < 3:
return trees
upper_hull, lower_trees = self.convex_hull_half(trees, True)
if len(lower_trees) == 2:
return upper_hull
lower_hull, _ = self.convex_hull_half(lo... | erect-the-fence | Andrew's Algorithm with Convex Hull Points in Clockwise Order, The Best | Triquetra | 0 | 12 | erect the fence | 587 | 0.523 | Hard | 10,190 |
https://leetcode.com/problems/erect-the-fence/discuss/2830812/Upper-Hull-and-Lower-Hull-using-monotonic-stack-PYTHONPYTHON3 | class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
trees.sort()
stack = []
def angle(p, q, r):
return ((q[0] - p[0]) * (r[1] - q[1])) - ((q[1] - p[1]) * (r[0] - q[0]))
for x, y in trees:
while len(stack) >= 2 and angle(stack[-2], ... | erect-the-fence | Upper Hull and Lower Hull using monotonic stack PYTHON/PYTHON3 | shiv-codes | 0 | 16 | erect the fence | 587 | 0.523 | Hard | 10,191 |
https://leetcode.com/problems/erect-the-fence/discuss/2830330/Python-3-implementation-for-Graham-Scan | class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
if len(trees) < 4:
return trees
stack = []
pivot = self.getSeed(trees)
def comparator(p: List[int], q: List[int]) -> int:
diff = self.orientation(pivot, p, q) - self.ori... | erect-the-fence | Python 3 implementation for Graham Scan | varun_date | 0 | 39 | erect the fence | 587 | 0.523 | Hard | 10,192 |
https://leetcode.com/problems/erect-the-fence/discuss/2830165/Python-(Simple-Maths) | class Solution:
def outerTrees(self, trees):
def cross(p1,p2,p3):
return (p2[0]-p1[0])*(p3[1]-p1[1]) - (p2[1]-p1[1])*(p3[0]-p1[0])
def construct(points):
stack = []
for i in points:
while len(stack) >= 2 and cross(stack[-2],stack[-1],i) > 0:
... | erect-the-fence | Python (Simple Maths) | rnotappl | 0 | 70 | erect the fence | 587 | 0.523 | Hard | 10,193 |
https://leetcode.com/problems/erect-the-fence/discuss/2830015/Python-or-Andrew's-Method-for-Convex-Hull | class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
def left_turn(p, r, s):
return (p[0]-s[0])*(r[1]-s[1]) - (p[1]-s[1])*(r[0]-s[0])
points = sorted(set(tuple(t) for t in trees))
if len(points) <= 2:
return points
upp... | erect-the-fence | Python | Andrew's Method for Convex Hull | on_danse_encore_on_rit_encore | 0 | 27 | erect the fence | 587 | 0.523 | Hard | 10,194 |
https://leetcode.com/problems/erect-the-fence/discuss/2829737/Python-soln-(slope-intuition) | class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
def slope(x1,x2,y1,y2):
return (y2-y1)/(x2-x1)
trees.sort()
X={}
for x,y in trees:
if x in X:
X[x].append(y)
else:
X[x]=[y]
q=d... | erect-the-fence | Python soln (slope intuition) | DhruvBagrecha | 0 | 28 | erect the fence | 587 | 0.523 | Hard | 10,195 |
https://leetcode.com/problems/erect-the-fence/discuss/2829638/Python-or-Andrew's-Monotone-Chain-convex-hull-algorithm | class Solution:
"""Compute the convex hull of a set of points.
Use Andrew's Monotone Chain algorithm, which has order O(N log(N)),
where N is the number of input points.
"""
def cross(self, p, a, b):
"""Return the cross product of the vectors p -> a and p -> b."""
return (... | erect-the-fence | Python | Andrew's Monotone Chain convex hull algorithm | LordVader1 | 0 | 35 | erect the fence | 587 | 0.523 | Hard | 10,196 |
https://leetcode.com/problems/erect-the-fence/discuss/2829448/Erect-The-Fence-or-Optimal-Solution-in-Python | class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
trees = sorted(trees)
upper, lower = [], []
def interaction(X, Y, Z):
B1, B2, A1, A2, T1, T2 = chain(X, Y, Z) ... | erect-the-fence | Erect The Fence | Optimal Solution in Python | jashii96 | 0 | 26 | erect the fence | 587 | 0.523 | Hard | 10,197 |
https://leetcode.com/problems/erect-the-fence/discuss/2829415/Python-using-slope-formula | class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
points = trees
points.sort(key=lambda x: x[0])
import math
def slope(p1, p2):
num = (p2[1]-p1[1])
denom = (p2[0]-p1[0])
if denom==0: return num*1000
ret... | erect-the-fence | Python using slope formula | mgaber6 | 0 | 36 | erect the fence | 587 | 0.523 | Hard | 10,198 |
https://leetcode.com/problems/erect-the-fence/discuss/2829288/best-python-solution | class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
def clockwise(p1,p2,p3):
x1,y1=p1
x2,y2=p2
x3,y3=p3
return ((y3-y2)*(x2-x1)-(y2-y1)*(x3-x2)) # if it return 0 that mean it clockwise
trees.sort()
upper=[]
... | erect-the-fence | best python solution | ashishneo | 0 | 40 | erect the fence | 587 | 0.523 | Hard | 10,199 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.