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/maximum-length-of-repeated-subarray/discuss/1324567/well-explained-oror-With-and-Without-DP-oror-99.66-faster-oror-Easily-Understandable | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
m=len(nums1)
n=len(nums2)
dp = [[0 for _ in range(n+1)] for _ in range(m+1)]
res=0
for i in range(m):
for j in range(n):
if nums1[i]==nums2[j]:
dp[i+1][j+1]=dp[i][j]+1
... | maximum-length-of-repeated-subarray | π well-explained || [ With and Without DP ] || 99.66 % faster || Easily-Understandable π | abhi9Rai | 10 | 994 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,800 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/1557033/Very-simple-and-fast-Python-solution | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
res = 0
# left to right sliding
for k in range(len(nums1)):
s = 0
for (x1,x2) in zip(nums1[k:],nums2):
if x1==x2:
s += 1
else:
... | maximum-length-of-repeated-subarray | Very simple and fast Python solution | cyrille-k | 9 | 520 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,801 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/958194/Python3-%22Longest-Common-Substring%22-concept-!! | class Solution:
def findLength(self, A: List[int], B: List[int]) -> int:
if not A or not B:
return 0
return self.lcs(A,B)
def lcs(self,a,b):
n = len(a)
m = len(b)
dp = [[0]*(n+1) for i in range(m+1)]
res = 0
... | maximum-length-of-repeated-subarray | [Python3] "Longest Common Substring" concept !! | tilak_ | 4 | 565 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,802 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2600837/Python-Simple-Python-Solution-Using-Dynamic-Programming | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
result = 0
dp = [[0] * (len(nums2)+1) for _ in range(len(nums1)+1)]
for i in range(len(nums1)):
for j in range(len(nums2)):
if nums1[i] == nums2[j]:
new_value = dp[i][j] + 1
dp[i+1][j+1] = new_value
r... | maximum-length-of-repeated-subarray | [ Python ] β
β
Simple Python Solution Using Dynamic Programming π₯³βπ | ASHOK_KUMAR_MEGHVANSHI | 3 | 417 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,803 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2599274/python3-oror-7-lines-memo-wexplanation-oror-TM%3A-8179 | class Solution: # 1) We build memo, a 2Darray, 2) iterate thru nums1 & nums2
# in reverse to populate memo, and then 3) find the max element
# in memo; its row and col in memo shows the starting indices
#... | maximum-length-of-repeated-subarray | python3 || 7 lines, memo, w/explanation || T/M: 81%/79% | warrenruud | 3 | 271 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,804 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/915316/Python3-DP-and-binary-search | class Solution:
def findLength(self, A: List[int], B: List[int]) -> int:
m, n = len(A), len(B)
dp = [[0]*(n+1) for _ in range(m+1)] # (m+1) x (n+1)
ans = 0
for i in reversed(range(m)):
for j in reversed(range(n)):
if A[i] == B[j]: dp[i][j] = 1 + dp[i+1][j+... | maximum-length-of-repeated-subarray | [Python3] DP & binary search | ye15 | 3 | 222 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,805 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/915316/Python3-DP-and-binary-search | class Solution:
def findLength(self, A: List[int], B: List[int]) -> int:
def fn(k):
"""Return True if a subarray of length k can be found in A and B."""
seen = {}
rh = 0 # rolling hash
for i in range(len(A)):
rh = (100*rh + A[i] - (i... | maximum-length-of-repeated-subarray | [Python3] DP & binary search | ye15 | 3 | 222 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,806 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2599312/Python-Elegant-and-Short-or-Bottom-Up-DP | class Solution:
"""
Time: O(n*m)
Memory: O(n*m)
"""
def findLength(self, a: List[int], b: List[int]) -> int:
n, m = len(a), len(b)
dp = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(n):
for j in range(m):
if a[i] == b[j]:
dp[i + 1][j + 1] = dp[i][j] + 1
return max(map(max, dp)) | maximum-length-of-repeated-subarray | Python Elegant & Short | Bottom-Up DP | Kyrylo-Ktl | 2 | 260 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,807 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/1449088/Simple-Python-O(mn)-dynamic-programming-solution | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
# let dp[i][j] be the maximum length of repeated subarray
# ending with the ith element in nums1 and jth element in nums2
# state transition:
# dp[i][j] = dp[i-1][j-1]+1 if nums[i-1] == nums[j-1]
... | maximum-length-of-repeated-subarray | Simple Python O(mn) dynamic programming solution | Charlesl0129 | 2 | 228 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,808 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/772768/Python-1-liner | class Solution:
def findLength(self, A: List[int], B: List[int]) -> int:
return max(map(max,reduce(lambda y,a:y+[[a==b and(1+(i and y[-1][i-1]))for i,b in enumerate(B)]],A,[[0]*len(B)]))) | maximum-length-of-repeated-subarray | Python 1-liner | ekovalyov | 2 | 318 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,809 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2637344/Python3-Solution-using-2D-DP-oror-Tabulation-Solution | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
m = len(nums1)
n = len(nums2)
dp = [[0]*n for _ in range(m)]
maxval = 0
for i in range(m):
for j in range(n):
if nums1[i] == nums2[j]:
if i > 0 and... | maximum-length-of-repeated-subarray | Python3 Solution using 2D DP || Tabulation Solution | hoo__mann | 1 | 38 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,810 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2599421/SIMPLE-PYTHON3-SOLUTION-98-fassster-than-others | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
strnum2 = ''.join([chr(x) for x in nums2])
strmax = ''
ans = 0
for num in nums1:
strmax += chr(num)
if strmax in strnum2:
ans = max(ans,len(strmax))
el... | maximum-length-of-repeated-subarray | β
β SIMPLE PYTHON3 SOLUTION β
β98% fassster than others | rajukommula | 1 | 132 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,811 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2543259/Longest-common-substring-or-Python-DP | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
m=len(nums1)
n=len(nums2)
dp=[[0 for i in range(m+1)]for j in range(n+1)]
ans=0
for i in range(1,n+1):
for j in range(1,m+1):
if nums2[i-1]==nums1[j-1]:
... | maximum-length-of-repeated-subarray | Longest common substring | Python DP | Prithiviraj1927 | 1 | 108 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,812 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/1777997/Java-Python3-Simple-DP-Solution-(Top-Down-and-Bottom-Up) | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
m, n = len(nums1), len(nums2)
maxlen = 0
@lru_cache(None)
def dp(i: int, j: int) -> int:
nonlocal maxlen
if i == m:
return 0
if j == n:
... | maximum-length-of-repeated-subarray | β
[Java / Python3] Simple DP Solution (Top-Down & Bottom-Up) | JawadNoor | 1 | 153 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,813 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/1777997/Java-Python3-Simple-DP-Solution-(Top-Down-and-Bottom-Up) | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
m, n = len(nums1), len(nums2)
dp = [[0]*(n+1) for _ in range(m+1)]
maxlen = 0
for i in range(m):
for j in range(n):
if nums1[i] == nums2[j]:
dp[i+1][j+1] =... | maximum-length-of-repeated-subarray | β
[Java / Python3] Simple DP Solution (Top-Down & Bottom-Up) | JawadNoor | 1 | 153 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,814 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/1325736/Python3Without-DP-or-faster-than-99.21-or-chr-or-Explained | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
# Fetch the Ascii value for each elements and store them as string
nums1 = ''.join([chr(x) for x in nums1])
nums2 = ''.join([chr(x) for x in nums2])
bi, sm = "", ""
if len(nums1) < len(... | maximum-length-of-repeated-subarray | [Python3]Without DP | faster than 99.21% | chr | Explained | SushilG96 | 1 | 138 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,815 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/1090222/Easy-Python-or-99-Speed-or-Binary-Search-%2B-HashSet-Database | class Solution:
def findLength(self, A, B):
if len(A)>len(B):
A,B = B,A
A = tuple(A)
B = tuple(B)
La, Lb = len(A), len(B)
def isvalid(k):
D = set( A[i:i+k] for i in range(La-k+1) )
for j in range(Lb-k+1):
... | maximum-length-of-repeated-subarray | Easy Python | 99% Speed | Binary Search + HashSet Database | Aragorn_ | 1 | 195 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,816 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2818125/Python-Dynamic-Programming-Solution | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
n, m = len(nums1), len(nums2)
dp = [[0] * (m+1) for _ in range(n+1)]
for i in range(n - 1, -1, -1):
for j in range(m - 1, -1, -1):
if nums1[i] == nums2[j]:
dp[i][j... | maximum-length-of-repeated-subarray | Python Dynamic Programming Solution | Zhouyao_Xie | 0 | 4 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,817 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2818105/Python-Sliding-Window-Solution | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
max_len = 0
n, m = len(nums1), len(nums2)
for i in range(-n+1, n+m):
l = 0
for s2 in range(len(nums2)):
s1 = s2 + i
if s1 < 0: continue
if... | maximum-length-of-repeated-subarray | Python Sliding Window Solution | Zhouyao_Xie | 0 | 2 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,818 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2607979/Python-3-Rolling-Hash-%2B-Binary-Search-or-Clean-Code-or-O(log(min(m-n))-*-max(m-n)) | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
l, r = 0, min(len(nums1), len(nums2)) + 1
# for i in range(r + 1):
# print(i, self.exist_length_k(nums1, nums2, i))
while l < r: # O(log(min(m, n)) * O(exist_length_k) = O(log(m... | maximum-length-of-repeated-subarray | [Python 3] Rolling Hash + Binary Search | Clean Code | O(log(min(m, n)) * max(m, n)) | ge-li | 0 | 17 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,819 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2603100/Easy-explanation-Bottom-up-DP-but-not-from-n-1-to-0! | class Solution(object):
def findLength(self, A, B):
dp = [[0] * (len(B)) for _ in range(len(A))]
ans = 0
for i in range(len(A)):
for j in range(len(B)):
if A[i] == B[j]:
dp[i][j] = 1
if i > 0 and j > 0 and dp[i-1][j-1]:
... | maximum-length-of-repeated-subarray | Easy explanation, Bottom up DP but not from n-1 to 0! | imanhn | 0 | 12 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,820 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2603028/Faster-than-99.69-FAST-and-SiMPLE-python-solution-Runtime%3A-155-ms-faster-than-99.69-of-Python3 | class Solution:
def findLength(self, nums1, nums2):
string_num2 = "".join([chr(ch) for ch in nums2])
curr = ""
ans = 0
for num in nums1:
curr += chr(num)
# if curr in num2 then check the length of curr
if curr in string_num2:
ans =... | maximum-length-of-repeated-subarray | Faster than 99.69% FAST and SiMPLE python solution [Runtime: 155 ms, faster than 99.69% of Python3] | 01_Toyota_Land_Cruiser | 0 | 13 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,821 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2600479/GolangPython-O(M*N)-time-or-O(min(MN))-space.-Dynamic-programming-solution | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
if len(nums2) > len(nums1):
nums1,nums2 = nums2,nums1
prev = [0 for _ in range(len(nums2)+1)]
curr = [0 for _ in range(len(nums2)+1)]
answer = 0
for i in range(1,len(nums... | maximum-length-of-repeated-subarray | Golang/Python O(M*N) time | O(min(M,N)) space. Dynamic programming solution | vtalantsev | 0 | 17 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,822 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2600464/Simple-%22python%22-Solution | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
nums2_str = "".join([chr(i) for i in nums2])
max_str = ""
res = 0
for i in nums1:
max_str += chr(i)
if max_str in nums2_str:
res = max(res,len(max_str))
... | maximum-length-of-repeated-subarray | Simple "python" Solution | anandchauhan8791 | 0 | 45 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,823 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2599631/Python-Solution-or-LCS-or-DP | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
n=len(nums1)
m=len(nums2)
dp=[[0]*(m+1) for i in range(n+1)]
ans=0
for i in range(1, n+1):
for j in range(1, m+1):
if nums1[i-1]==nums2[j-1]:
dp[i]... | maximum-length-of-repeated-subarray | Python Solution | LCS | DP | Siddharth_singh | 0 | 50 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,824 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2457543/Easy-DP | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
n = len(nums1)
m = len(nums2)
dp = [[0 for i in range(n+1)] for j in range(m+1)]
ans = 0
for i in range(1,m+1):
for j in range(1,n+1):
if nums1[j-1] == nums2[... | maximum-length-of-repeated-subarray | Easy DP | jayeshmaheshwari555 | 0 | 34 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,825 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2450234/Maximum-Length-of-Repeated-Subarray-oror-Python3-oror-DP | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
dp = [[0] * (len(nums1)+1) for i in range(len(nums2)+1)]
ans = 0
for i in range(1, len(dp)): # for nums2
for j in range(1, len(dp[0])): # for nums1
if(nums2[i-1] == nums1[j-1... | maximum-length-of-repeated-subarray | Maximum Length of Repeated Subarray || Python3 || DP | vanshika_2507 | 0 | 45 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,826 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2393218/Python3-oror-Tabulation-method-oror-Dynamic-Programming-oror-Elegant-and-clean | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
dp = [[0]*(len(nums1)+ 1) for _ in range(len(nums2) + 1)]
max_len = 0
for row in range(len(nums2)):
for col in range(len(nums1)):
if nums2[row] == nums1[col]:
dp[r... | maximum-length-of-repeated-subarray | Python3 || Tabulation method || Dynamic Programming || Elegant and clean | Sefinehtesfa34 | 0 | 32 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,827 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2334271/Very-Simple-Bottom-Up-or-Python | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
dp = [[0 for _ in range(len(nums2)+1)] for _ in range(len(nums1)+1)]
ans = 0
for i in range(1,len(dp)):
for j in range(1,len(dp[0])):
if nums1[i-1] == nums2[j-1]:
... | maximum-length-of-repeated-subarray | Very Simple Bottom-Up | Python | bliqlegend | 0 | 61 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,828 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/2140474/Python3-or-DP-or-T(n)-O(m*n)-S(n)-O(n) | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
m, n = len(nums1), len(nums2)
mx = -float('inf')
prev = [0 for _ in range(n + 1)]
cur = [0 for _ in range(n + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
... | maximum-length-of-repeated-subarray | Python3 | DP | T(n) = O(m*n), S(n) = O(n) | Ploypaphat | 0 | 39 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,829 |
https://leetcode.com/problems/maximum-length-of-repeated-subarray/discuss/1324844/Python-3-Binary-Search-%2B-Rolling-Hash-(164ms)-O(log(min(m-n))*(m%2Bn)) | class Solution:
def findLength(self, nums1: List[int], nums2: List[int]) -> int:
def helper(l):
base = 1 << 7
M = 10 **9 + 7
a = pow(base, l, M)
t1, t2 = 0, 0
hashes = set()
for i in range(len(nums1)):
t1 = (base * t1 + ... | maximum-length-of-repeated-subarray | [Python 3] Binary Search + Rolling Hash (164ms) O(log(min(m, n))*(m+n)) | chestnut890123 | 0 | 100 | maximum length of repeated subarray | 718 | 0.515 | Medium | 11,830 |
https://leetcode.com/problems/find-k-th-smallest-pair-distance/discuss/2581420/Simple-python-binary-search | class Solution:
def smallestDistancePair(self, nums: List[int], k: int) -> int:
def getPairs(diff):
l = 0
count = 0
for r in range(len(nums)):
while nums[r] - nums[l] > diff:
l += 1
count += r - l
return count
nums.sort()
l, r = 0, nums[-1] - nums[0]
while l < r:
mid = (l + r)... | find-k-th-smallest-pair-distance | Simple python binary search | shubhamnishad25 | 1 | 123 | find k th smallest pair distance | 719 | 0.364 | Hard | 11,831 |
https://leetcode.com/problems/find-k-th-smallest-pair-distance/discuss/1495743/Python3-binary-search | class Solution:
def smallestDistancePair(self, nums: List[int], k: int) -> int:
nums.sort()
def fn(val):
"""Return count of pairs whose diff <= val."""
ans = ii = 0
for i, x in enumerate(nums):
while ii < i and x - nums[ii] > val: ii += 1... | find-k-th-smallest-pair-distance | [Python3] binary search | ye15 | 1 | 112 | find k th smallest pair distance | 719 | 0.364 | Hard | 11,832 |
https://leetcode.com/problems/find-k-th-smallest-pair-distance/discuss/2732256/Sort-%2B-Binary-Search-Distance-Value-%2B-Sliding-Window-(Beats-94.72) | class Solution:
def smallestDistancePair(self, nums: List[int], k: int) -> int:
nums.sort()
N = len(nums)
def less(v):
"""number of distances < v"""
cnt, left = 0, 0
for right in range(1, N):
while left < right and nums[right] - nums[left]... | find-k-th-smallest-pair-distance | Sort + Binary Search Distance Value + Sliding Window (Beats 94.72%) | GregHuang | 0 | 11 | find k th smallest pair distance | 719 | 0.364 | Hard | 11,833 |
https://leetcode.com/problems/longest-word-in-dictionary/discuss/2075147/Python-O(n-log(n))-Time-O(n)-Space-Faster-Than-95 | class Solution:
def longestWord(self, words: List[str]) -> str:
words.sort() # for smallest lexicographical order
visited = {""} # hashset to keep a track of visited words
res = ''
for word in words:
if word[:-1] in visited: # ... | longest-word-in-dictionary | [Python] O(n log(n)) Time, O(n) Space Faster Than 95% | samirpaul1 | 4 | 217 | longest word in dictionary | 720 | 0.518 | Medium | 11,834 |
https://leetcode.com/problems/longest-word-in-dictionary/discuss/2795751/Binary-search-O(nlogn)-time-without-allocated-memory | class Solution:
def longestWord(self, words: List[str]) -> str:
words.sort()
max_len = 1
symbol = ']'
ans = None
for i in range(len(words)):
if len(words[i]) == 1:
words[i] = words[i] + symbol
if max_len < len(words[i]):
... | longest-word-in-dictionary | Binary search O(nlogn) time, without allocated memory | zlmrfi | 0 | 7 | longest word in dictionary | 720 | 0.518 | Medium | 11,835 |
https://leetcode.com/problems/longest-word-in-dictionary/discuss/2648390/Solution-using-dictionary-Python3 | class Solution:
def longestWord(self, words: List[str]) -> str:
word_map = {}
max_len = 0
for word in sorted(words):
if len(word) == 1:
word_map[word] = True
max_len = max(max_len, len(word))
elif word_map.get(word[:-1]):
... | longest-word-in-dictionary | Solution using dictionary Python3 | ogbird | 0 | 7 | longest word in dictionary | 720 | 0.518 | Medium | 11,836 |
https://leetcode.com/problems/longest-word-in-dictionary/discuss/2415280/Python-beats-95-simple-solution-without-Trie-DFS-%2B-memoization | class Solution:
def longestWord(self, words: List[str]) -> str:
# dfs + memo
# Complexity Analysis:
# Time: O(NK)
# Space: O(N)
max_word = ''
def dfs(word):
nonlocal max_word
if word in memo: return memo[wo... | longest-word-in-dictionary | Python - beats 95% - simple solution without Trie - DFS + memoization | ChenAmos | 0 | 48 | longest word in dictionary | 720 | 0.518 | Medium | 11,837 |
https://leetcode.com/problems/longest-word-in-dictionary/discuss/1895623/Python-easy-to-read-and-understand | class Solution:
def longestWord(self, words: List[str]) -> str:
words.sort()
ans = ''
contains = set([''])
for word in words:
if word[:-1] in contains:
contains.add(word)
if len(word) > len(ans):
ans = word
retur... | longest-word-in-dictionary | Python easy to read and understand | sanial2001 | 0 | 85 | longest word in dictionary | 720 | 0.518 | Medium | 11,838 |
https://leetcode.com/problems/longest-word-in-dictionary/discuss/1827757/Python-or-Sorting | class Solution:
def longestWord(self, words: List[str]) -> str:
words.sort(key=lambda item:(-len(item),item),reverse=True)
#print(words)
for word in words[::-1]:
tmp=''
for ch in word:
tmp+=ch
if tmp not in words:
br... | longest-word-in-dictionary | Python | Sorting | heckt27 | 0 | 32 | longest word in dictionary | 720 | 0.518 | Medium | 11,839 |
https://leetcode.com/problems/longest-word-in-dictionary/discuss/1552208/python3-simple-hash-and-sort-or-64ms-beats-98-or-easy-to-understand | class Solution:
def longestWord(self, words: List[str]) -> str:
words = set(words)
lens = sorted([(w, len(w)) for w in words], key = lambda x: (-x[1], x[0]))
for w, w_len in lens:
nxt = False
for i in range(w_len-1, 0, -1):
if w[:i] not in words:
... | longest-word-in-dictionary | [python3] simple hash and sort | 64ms beats 98% | easy to understand | kevintancs | 0 | 47 | longest word in dictionary | 720 | 0.518 | Medium | 11,840 |
https://leetcode.com/problems/longest-word-in-dictionary/discuss/1201780/Easy-Python-Solution-Beats-99-runtime | class Solution:
def longestWord(self, words: List[str]) -> str:
# Function which checks whether the given word is present in dictionary or not
def isContain(word, dict1):
temp_str = ''
for i in range(len(word)):
temp_str += word[i]
if temp_str in dic... | longest-word-in-dictionary | Easy Python Solution, Beats 99% runtime | dakshal33 | 0 | 189 | longest word in dictionary | 720 | 0.518 | Medium | 11,841 |
https://leetcode.com/problems/longest-word-in-dictionary/discuss/694152/Python3-chill-with-sort-beats-98.7 | class Solution:
def longestWord(self, words: List[str]) -> str:
if not words:
return None
seen = {}
w = ""
for i in sorted(words):
if len(i) > 1 and i[:-1] not in seen:
continue
seen[i] = {}
if len(i) > len(w):
w = i
return w | longest-word-in-dictionary | Python3 - chill with sort π beats 98.7% | shankha117 | 0 | 85 | longest word in dictionary | 720 | 0.518 | Medium | 11,842 |
https://leetcode.com/problems/longest-word-in-dictionary/discuss/1220854/Python3-simple-solution-using-sorting | class Solution:
def longestWord(self, words: List[str]) -> str:
words.sort(reverse=True)
words.sort(key=lambda x : len(x))
i = len(words)-1
while i > -1:
flag = True
for j in range(1,len(words[i])):
if words[i][:j] not in words:
... | longest-word-in-dictionary | Python3 simple solution using sorting | EklavyaJoshi | -1 | 83 | longest word in dictionary | 720 | 0.518 | Medium | 11,843 |
https://leetcode.com/problems/accounts-merge/discuss/2014051/Python-easy-to-read-and-understand-or-DFS | class Solution:
def dfs(self, graph, node, visit):
visit.add(node)
for nei in graph[node]:
if nei not in visit:
self.dfs(graph, nei, visit)
self.res.append(node)
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
graph = collec... | accounts-merge | Python easy to read and understand | DFS | sanial2001 | 7 | 507 | accounts merge | 721 | 0.564 | Medium | 11,844 |
https://leetcode.com/problems/accounts-merge/discuss/286039/My-easy-to-understand-Python3-Union-Find-solution | class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]
def union(x, y):
rootx = find(x)
rooty = find(y)
if rootx ==... | accounts-merge | My easy to understand Python3 Union Find solution | maysonma98 | 5 | 696 | accounts merge | 721 | 0.564 | Medium | 11,845 |
https://leetcode.com/problems/accounts-merge/discuss/1602208/Python3-Union-Find-Explained | class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
self.p = {i:i for i in range(len(accounts))} # parents
eta = dict() # maps email to account
for i, acc in enumerate(accounts):
for email in acc[1:]:
if email in ... | accounts-merge | [Python3] Union-Find, Explained | artod | 4 | 313 | accounts merge | 721 | 0.564 | Medium | 11,846 |
https://leetcode.com/problems/accounts-merge/discuss/2543526/Python-DFS-Solution-Intuitive | class Solution:
def __init__(self):
self.accountConnections = defaultdict(set)
self.emailToName = dict()
self.seen = set()
def buildConnections(self, accounts):
for account in accounts:
name = account[0]
key = account[1]
for i in range(1, ... | accounts-merge | Python DFS Solution - Intuitive | EdwinJagger | 1 | 110 | accounts merge | 721 | 0.564 | Medium | 11,847 |
https://leetcode.com/problems/accounts-merge/discuss/1372984/Python-Simple-15-Lines-of-code-using-List-property.-O(M2) | class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
tagged = {}
for cur_group in accounts:
for mail in cur_group[1:]:
if mail not in tagged:
tagged[mail] = cur_group
orig_group = tagged[mail]
... | accounts-merge | [Python] Simple 15 Lines of code using List property. O(M^2) | annieFromTaiwan | 1 | 262 | accounts merge | 721 | 0.564 | Medium | 11,848 |
https://leetcode.com/problems/accounts-merge/discuss/1070360/Python-or-Set-%2B-Dict-or-Easy-to-Understand-or-Iterative-Solution | class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
users,seen,curr,merged=set(),dict(),0,[]
for a in accounts:
if a[0] not in users:
for email in a[1:]:
seen[email]=curr
curr+=1
merged... | accounts-merge | Python | Set + Dict | Easy to Understand | Iterative Solution | rajatrai1206 | 1 | 311 | accounts merge | 721 | 0.564 | Medium | 11,849 |
https://leetcode.com/problems/accounts-merge/discuss/2814677/Python3-Union-Find-Solution | class Solution:
def find(self, email: str, parent) -> str:
if email != parent[email]:
parent[email] = self.find(parent[email], parent)
return parent[email]
def union(self, email1: str, email2: str, parent) -> None:
if email1 not in parent:... | accounts-merge | Python3 Union Find Solution | destifo | 0 | 5 | accounts merge | 721 | 0.564 | Medium | 11,850 |
https://leetcode.com/problems/accounts-merge/discuss/2786504/python-dfs-approach | class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
# T : N = number of accounts
# K = max number of emails per user
# O(N*K) * O(N * log(k)) => sorting purpose
# therefore final time complexity is O(N * K * (N * log(K)))
... | accounts-merge | python dfs approach | sahilkumar158 | 0 | 7 | accounts merge | 721 | 0.564 | Medium | 11,851 |
https://leetcode.com/problems/accounts-merge/discuss/2750190/Intuitive-construction-of-adjacent-graph-and-basic-dfs | class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
#create a adjacent lists that have connections
dic = collections.defaultdict(set)
email_to_name={}
visited = set()
ans = []
for account in accounts:
name = account[0]
... | accounts-merge | Intuitive construction of adjacent graph and basic dfs | fellowshiptech | 0 | 3 | accounts merge | 721 | 0.564 | Medium | 11,852 |
https://leetcode.com/problems/accounts-merge/discuss/2158901/Connected-Components-oror-DFS-oror-Fastest-Optimal-Solution-oror-Easy-to-Understand | class Solution:
# This dfs function will return the components for each node.
def dfs(self, comp, idx, visited, graph, mailsList):
visited[idx] = True
comp.append(mailsList[idx])
for neighbour in graph[mailsList[idx]]:
neighbourIdx = mailsList.index(neighbour)
if ... | accounts-merge | Connected Components || DFS || Fastest Optimal Solution || Easy to Understand | Vaibhav7860 | 0 | 86 | accounts merge | 721 | 0.564 | Medium | 11,853 |
https://leetcode.com/problems/accounts-merge/discuss/1817838/A-DFS-approach-not-the-best-but-want-to-share-just-step-by-step | class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
hashmap={}
for i in range(len(accounts)):
for j in range(1,len(accounts[i])):
if accounts[i][j] not in hashmap:
hashmap[accounts[i][j]] = accounts[i]
... | accounts-merge | A DFS approach, not the best but want to share, just step by step | Sunshinelalala | 0 | 93 | accounts merge | 721 | 0.564 | Medium | 11,854 |
https://leetcode.com/problems/accounts-merge/discuss/1602882/Union-find-solution-faster-than-94.83 | class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
rank = list(range(len(accounts)))
def find(x):
if rank[x] == x:
return x
rank[x] = find(rank[x])
return rank[x]
mails = {} # mail : ac... | accounts-merge | Union find solution, faster than 94.83% | timetoai | 0 | 95 | accounts merge | 721 | 0.564 | Medium | 11,855 |
https://leetcode.com/problems/accounts-merge/discuss/979002/Python-Iterative-Solution | class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
email_to_ids = collections.defaultdict(list)
visited_accounts = [False]*len(accounts)
for i, acct in enumerate(accounts):
for email in acct[1:]:
email_to_ids[email].... | accounts-merge | Python Iterative Solution | cj1989 | 0 | 212 | accounts merge | 721 | 0.564 | Medium | 11,856 |
https://leetcode.com/problems/remove-comments/discuss/2446606/Easy-to-understand-using-Python | class Solution:
def removeComments(self, source: List[str]) -> List[str]:
ans, inComment = [], False
new_str = ""
for c in source:
if not inComment: new_str = ""
i, n = 0, len(c)
# inComment, we find */
while i < n:
if inComment... | remove-comments | Easy to understand using Python | fguo10 | 3 | 567 | remove comments | 722 | 0.38 | Medium | 11,857 |
https://leetcode.com/problems/remove-comments/discuss/1457370/Python3-1-pass | class Solution:
def removeComments(self, source: List[str]) -> List[str]:
ans = []
comment = False # True for block comment
for line in source:
if not comment: ans.append([]) # if not started as comment
i = 0
while i < len(line):
... | remove-comments | [Python3] 1-pass | ye15 | 1 | 241 | remove comments | 722 | 0.38 | Medium | 11,858 |
https://leetcode.com/problems/remove-comments/discuss/1395358/99.77-faster-Python-solution-easy-understand | class Solution:
def removeComments(self, source):
#decide the order for processing, return True if need to first process '//'
def who_first(line, commenting):
atype, btype, ctype = line.find('//'), line.find('/*'), line.find('*/')
if atype != -1 and ctype != -1 and commenting:
... | remove-comments | 99.77% faster Python solution, easy understand | bob23 | 1 | 464 | remove comments | 722 | 0.38 | Medium | 11,859 |
https://leetcode.com/problems/remove-comments/discuss/981251/Python-one-pass-with-explanation | class Solution:
def removeComments(self, source: List[str]) -> List[str]:
'''
is_block shows if we're in a block comment or not
Iterate over current line and detect line comments or start/end of block comments.
End line early for a line comment, update is_block for block sta... | remove-comments | Python one-pass with explanation | gins1 | 1 | 395 | remove comments | 722 | 0.38 | Medium | 11,860 |
https://leetcode.com/problems/remove-comments/discuss/2351084/Python3-One-pass-with-guard-clauses-Explained-no-regex-just-bools | class Solution(object):
def removeComments(self, source):
"""
:type source: List[str]
:rtype: List[str]
"""
# initialize some variables to save lines and valid characters
result = []
current_line = ""
# initialize some state variables... | remove-comments | Python3 - One pass with guard clauses - Explained - no regex just bools | Lucew | 0 | 93 | remove comments | 722 | 0.38 | Medium | 11,861 |
https://leetcode.com/problems/remove-comments/discuss/2351084/Python3-One-pass-with-guard-clauses-Explained-no-regex-just-bools | class Solution(object):
def removeComments(self, source):
result = []
current_line = ""
in_block = False
in_line = False
skip_next = False
for line in source:
skip_next = False
in_line = False
for ... | remove-comments | Python3 - One pass with guard clauses - Explained - no regex just bools | Lucew | 0 | 93 | remove comments | 722 | 0.38 | Medium | 11,862 |
https://leetcode.com/problems/remove-comments/discuss/1451235/Python-one-pass-readable-code-with-comments | class Solution:
def removeComments(self, source: List[str]) -> List[str]:
result = []
SINGLE_LINE_COMMENT = '//'
MULTI_LINE_COMMENT_START = '/*'
MULTI_LINE_COMMENT_END = '*/'
is_multi_line_comment = False # this is to check if the current symbol withing multiline comment
... | remove-comments | Python, one pass, readable code with comments | arsamigullin | 0 | 279 | remove comments | 722 | 0.38 | Medium | 11,863 |
https://leetcode.com/problems/remove-comments/discuss/1421637/Regex-solution | class Solution:
pat1 = re.compile(r"//[^~]*~")
pat2 = re.compile(r"/\*.*?\*/")
def removeComments(self, source: List[str]) -> List[str]:
one_line = "~".join(source) + "~"
idx_pat1 = one_line.find("//")
idx_pat2 = one_line.find("/*")
while -1 < idx_pat1 or -1 < idx_pat2:
... | remove-comments | Regex solution | EvgenySH | 0 | 173 | remove comments | 722 | 0.38 | Medium | 11,864 |
https://leetcode.com/problems/find-pivot-index/discuss/2321669/Python-99.85-faster-or-Simplest-solution-with-explanation-or-Beg-to-Adv-or-Prefix-Sum | class Solution:
def findMiddleIndex(self, nums: List[int]) -> int:
left = 0 # nums[0] + nums[1] + ... + nums[middleIndex-1]
right = sum(nums) # nums[middleIndex+1] + nums[middleIndex+2] + ... + nums[nums.length-1]
for i, num in enumerate(nums): # we can use normal for loop as well.
... | find-pivot-index | Python 99.85% faster | Simplest solution with explanation | Beg to Adv | Prefix Sum | rlakshay14 | 9 | 725 | find pivot index | 724 | 0.535 | Easy | 11,865 |
https://leetcode.com/problems/find-pivot-index/discuss/1905287/My-easiest-to-understand-solution-that-beats-92-python-solutions. | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
s1,s2=0,sum(nums)
for i in range(len(nums)):
s2-=nums[i]
if s1==s2:
return i
s1+=nums[i]
return -1 | find-pivot-index | My easiest to understand solution that beats 92% python solutions. | tkdhimanshusingh | 8 | 393 | find pivot index | 724 | 0.535 | Easy | 11,866 |
https://leetcode.com/problems/find-pivot-index/discuss/2233492/Python3-Solution-with-using-prefix-sum | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
right_sum = sum(nums)
left_sum = 0
for i in range(len(nums)):
right_sum -= nums[i]
if left_sum == right_sum:
return i
left_sum += nums[i]
... | find-pivot-index | [Python3] Solution with using prefix sum | maosipov11 | 5 | 220 | find pivot index | 724 | 0.535 | Easy | 11,867 |
https://leetcode.com/problems/find-pivot-index/discuss/2303414/Python-solution-with-explanation | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
# We'll assume the right sum as sum of all the elements in the list.
right_sum = sum(nums)
# We'll assume the left sum as 0.
left_sum = 0
# Now we'll iterate in the whole list
for... | find-pivot-index | Python solution with explanation | yashkumarjha | 2 | 227 | find pivot index | 724 | 0.535 | Easy | 11,868 |
https://leetcode.com/problems/find-pivot-index/discuss/2165091/Python-or-Two-easy-solution-space-optimized | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
# ///// Solution 2 space optimized TC: O(N) and SC: O(1) ///////
leftSum = 0
rightSum = sum(nums)
for i in range(len(nums)):
leftSum += nums[i]
if leftSum == rightSum:
return i
... | find-pivot-index | Python | Two easy solution space optimized | __Asrar | 2 | 185 | find pivot index | 724 | 0.535 | Easy | 11,869 |
https://leetcode.com/problems/find-pivot-index/discuss/1508815/Simple-or-Python-3-or-140-ms-faster-than-96.15 | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
l_sum = 0
r_sum = sum(nums)
for index, num in enumerate(nums):
l_sum += num
r_sum -= num
if l_sum - num == r_sum:
return index
return -1 | find-pivot-index | Simple | Python 3 | 140 ms, faster than 96.15% | deep765 | 2 | 264 | find pivot index | 724 | 0.535 | Easy | 11,870 |
https://leetcode.com/problems/find-pivot-index/discuss/785434/Three-Liner-Python-Solution | class Solution(object):
def pivotIndex(self, nums):
for i in range(len(nums)):
if sum(nums[:i:])==sum(nums[i+1::]):return i
return -1 | find-pivot-index | Three Liner Python Solution | rachitsxn292 | 2 | 371 | find pivot index | 724 | 0.535 | Easy | 11,871 |
https://leetcode.com/problems/find-pivot-index/discuss/2277128/Python3-O(N)-time-and-O(1)-space | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
length = len(nums)
for i in range(1,length):
nums[i] += nums[i-1]
def isPivot(index):
if index == 0:
return nums[-1] - nums[index] == 0
if index == length - 1:
... | find-pivot-index | π Python3 O(N) time and O(1) space | Dark_wolf_jss | 1 | 93 | find pivot index | 724 | 0.535 | Easy | 11,872 |
https://leetcode.com/problems/find-pivot-index/discuss/2170806/Python-oror-O(1)-Space | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
n=len(nums)
if n==1:
return nums[0]
left=0
array_sum=sum(nums)
#If 0th index is Pivot
if array_sum-nums[0]==0:
return 0
... | find-pivot-index | Python || O(1) Space | aksgupta98 | 1 | 84 | find pivot index | 724 | 0.535 | Easy | 11,873 |
https://leetcode.com/problems/find-pivot-index/discuss/2094476/Python3-O(n)ororO(1)-Runtime%3A-153ms-90.08 | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
return self.findPivotIndexOptimal(nums)
# O(n) || O(1)
# runtime: 153ms 90.08%
def findPivotIndexOptimal(self, nums):
if not nums:
return nums
leftSum = 0
totalSum = sum(nums)
... | find-pivot-index | Python3 O(n)||O(1) Runtime: 153ms 90.08% | arshergon | 1 | 90 | find pivot index | 724 | 0.535 | Easy | 11,874 |
https://leetcode.com/problems/find-pivot-index/discuss/1508276/short-python-O(n)-time-Clean-Code | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
s=sum(nums)
l=len(nums)
res=[]
st=0
for i in range(0,l-1):
st=st+nums[i]
if st==(s-nums[i+1])/2:
res.append(i+1)
if sum(nums[1:])==0: res.append(0)
if sum(num... | find-pivot-index | short python O(n) time Clean Code | srikarakkina | 1 | 131 | find pivot index | 724 | 0.535 | Easy | 11,875 |
https://leetcode.com/problems/find-pivot-index/discuss/1502069/Python-solution-with-one-hash-map | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
right_sums = {}
current_sum = 0
for i in range(len(nums) - 1, -1, -1):
right_sums[i] = current_sum
current_sum += nums[i]
current_sum = 0
for i in range(len(nums)):
... | find-pivot-index | Python solution with one hash map | prnvshrn | 1 | 78 | find pivot index | 724 | 0.535 | Easy | 11,876 |
https://leetcode.com/problems/find-pivot-index/discuss/1355141/Python-Solution | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
nums_sum = sum(nums)
n = len(nums)
left_sum, right_sum = 0, nums_sum
for i in range(n):
right_sum -= nums[i]
if left_sum == right_sum:
return i
left_sum += nums[i]
... | find-pivot-index | Python Solution | mariandanaila01 | 1 | 185 | find pivot index | 724 | 0.535 | Easy | 11,877 |
https://leetcode.com/problems/find-pivot-index/discuss/1173032/simple-to-understand-python | class Solution(object):
def pivotIndex(self, nums):
sumL = 0
sumR = sum(nums)
for i in range(len(nums)):
sumR -= nums[i]
if sumL == sumR:
return i
sumL += nums[i]
return -1 | find-pivot-index | simple to understand python | pheobhe | 1 | 89 | find pivot index | 724 | 0.535 | Easy | 11,878 |
https://leetcode.com/problems/find-pivot-index/discuss/452281/efficient-as-well-as-easy-to-understand-beats-94-in-run-time-and-100-in-memory. | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
"""Easy to understand """
piv = -1
sum_after_i= sum(nums)
sum_before_i = 0
if len(nums) not in [0,1]:
for i in range(len(nums)):
sum_after_i -=nums[i] #sum of numbers after i'th... | find-pivot-index | efficient as well as easy to understand, beats 94% in run time and 100% in memory. | sudhirkumarshahu80 | 1 | 341 | find pivot index | 724 | 0.535 | Easy | 11,879 |
https://leetcode.com/problems/find-pivot-index/discuss/2844117/Python-using-while-Loop | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
n = len(nums)
nums.append(0)
pivot, left, right = 0, 0, sum(nums[1:])
while(pivot < n):
if(left == right):
return pivot
else:
left += nums[pivot]
righ... | find-pivot-index | Python using while Loop | esdark | 0 | 1 | find pivot index | 724 | 0.535 | Easy | 11,880 |
https://leetcode.com/problems/find-pivot-index/discuss/2844109/Find-pivot-index | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
left_sum=0
total=sum(nums)
for index in range(len(nums)):
right_sum= total-nums[index]-left_sum
if left_sum==right_sum:
return index
left_sum+... | find-pivot-index | Find pivot index | sid_1603_ | 0 | 1 | find pivot index | 724 | 0.535 | Easy | 11,881 |
https://leetcode.com/problems/find-pivot-index/discuss/2841759/My-first-solutions-no-hate-please | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
length = len(nums)
for i in range(length):
s = 0
s = sum(nums[0:i])
if s == sum(nums[i+1:length]):
return i
return -1 | find-pivot-index | My first solutions, no hate please | Slangeredet | 0 | 3 | find pivot index | 724 | 0.535 | Easy | 11,882 |
https://leetcode.com/problems/find-pivot-index/discuss/2835013/pivot-index | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
for i in range(len(nums)):
if sum(nums[:i]) == sum(nums[i + 1:]):
return i
return -1 | find-pivot-index | pivot index | arthur54342 | 0 | 3 | find pivot index | 724 | 0.535 | Easy | 11,883 |
https://leetcode.com/problems/find-pivot-index/discuss/2835013/pivot-index | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
right_sum = sum(nums)
left_sum = 0
prev_item = 0
for i, item in enumerate(nums):
if i > 0:
left_sum += prev_item
right_sum -= item
if left_sum == right_sum:
... | find-pivot-index | pivot index | arthur54342 | 0 | 3 | find pivot index | 724 | 0.535 | Easy | 11,884 |
https://leetcode.com/problems/find-pivot-index/discuss/2828632/Python-with-explanation | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
## below for auto-compute
total = sum(nums) #O(n)
## below for 1st var for auto-compute & criteria
leftSum = 0 #to compute the left sum
## loop through whole array
for i in range(len(nums)):
rightSum =... | find-pivot-index | Python with explanation | neon_pegasus | 0 | 10 | find pivot index | 724 | 0.535 | Easy | 11,885 |
https://leetcode.com/problems/find-pivot-index/discuss/2828023/Python-Keep-sliding-pivot-point-adding-to-left-taking-from-right | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
pivot, left, right = 0, 0, sum(nums) - nums[0]
while pivot < len(nums):
# base case - if left and right of current index is equivalent
if left == right:
return pivot
# otherwise... | find-pivot-index | [Python] Keep sliding pivot point, adding to left, taking from right | graceiscoding | 0 | 5 | find pivot index | 724 | 0.535 | Easy | 11,886 |
https://leetcode.com/problems/find-pivot-index/discuss/2819287/Python-code | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
i=0
j=len(nums)
firstpart=[]
secondpart=[]
firstpart = nums[:j-1]
secondpart = nums[1:]
if(sum(secondpart)==0 ):
return 0
while(i<len(nums)-1):
if(sum(num... | find-pivot-index | Python code | saisupriyavaru | 0 | 5 | find pivot index | 724 | 0.535 | Easy | 11,887 |
https://leetcode.com/problems/find-pivot-index/discuss/2819026/Python-My-O(n)-Solution | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
# build right sum list - O(n)
right_sum_list_inverse = [0]
for i in range(len(nums)-1, 0, -1):
right_sum_list_inverse.append(right_sum_list_inverse[-1]+nums[i])
# compare each left sum with ... | find-pivot-index | [Python] My O(n) Solution | manytenks | 0 | 7 | find pivot index | 724 | 0.535 | Easy | 11,888 |
https://leetcode.com/problems/find-pivot-index/discuss/2816359/Simplest-solution | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
sr=0
for i in range(0,len(nums)):
sr=sr+nums[i]
sl=0
for i in range (0,len(nums)):
sr=sr-nums[i]
if (sl==sr):
return i
sl=sl+nums[i]
return -1 | find-pivot-index | Simplest solution | pratyushjain99 | 0 | 5 | find pivot index | 724 | 0.535 | Easy | 11,889 |
https://leetcode.com/problems/find-pivot-index/discuss/2814386/Python-easy-solution-Sliding-window-approach | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
left = 0
right = sum(nums[1:])
for i in range(len(nums)):
if left==right:
return i
left=left+nums[i]
if i+1 == len(nums):
right=0
else:
... | find-pivot-index | Python easy solution Sliding window approach | ankansharma1998 | 0 | 8 | find pivot index | 724 | 0.535 | Easy | 11,890 |
https://leetcode.com/problems/find-pivot-index/discuss/2814027/My-Solution | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
rightSum = sum(nums) - nums[0]
leftSum = 0
if leftSum == rightSum: return 0
for i in range(1, len(nums)):
leftSum += nums[i-1]
rightSum -= nums[i]
if leftSum == rightSum: return i
... | find-pivot-index | My Solution | phuocnguyenquang34 | 0 | 4 | find pivot index | 724 | 0.535 | Easy | 11,891 |
https://leetcode.com/problems/find-pivot-index/discuss/2811631/recursive-sol | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
left = 0
right = sum(nums)
for i in range(0,len(nums)):
right -= nums[i]
if left == right:
return i
left += nums[i]
return -1 | find-pivot-index | recursive sol | roger880327 | 0 | 4 | find pivot index | 724 | 0.535 | Easy | 11,892 |
https://leetcode.com/problems/find-pivot-index/discuss/2810362/Python3-368-ms-28 | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
left_sum = 0
right_sum = sum(nums)
for i, val in enumerate(nums):
if left_sum == right_sum - val:
return i
left_sum += val
right_sum -= val
return -1 | find-pivot-index | Python3 368 ms 28% | ajrs | 0 | 2 | find pivot index | 724 | 0.535 | Easy | 11,893 |
https://leetcode.com/problems/find-pivot-index/discuss/2798102/Pivot-entry-in-an-Array-My-Solution | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
initial_right = sum(nums[1:])
left, right = 0, initial_right
if len(nums) == 1 or initial_right == 0:
return 0
for i in range(1,len(nums)):
left += nums[i-1]
right -= nums[i]
... | find-pivot-index | Pivot entry in an Array - My Solution | marked01one | 0 | 3 | find pivot index | 724 | 0.535 | Easy | 11,894 |
https://leetcode.com/problems/find-pivot-index/discuss/2795632/PythonGo-O(n)-balance-scale-solution | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
left_sum = sum(nums)
right_sum = 0
for idx, num in enumerate(nums):
left_sum -= num
if left_sum == right_sum:
return idx
right_sum += num
return -1 | find-pivot-index | Python/Go O(n) balance scale solution | rocketb | 0 | 9 | find pivot index | 724 | 0.535 | Easy | 11,895 |
https://leetcode.com/problems/find-pivot-index/discuss/2793485/SUM-code | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
suml = sum(nums)
left_sum = 0
for i in range(len(nums)):
if left_sum == (suml - (nums[i] + left_sum)):
return i
left_sum += nums[i]
return -1 | find-pivot-index | SUM code | Julierv | 0 | 1 | find pivot index | 724 | 0.535 | Easy | 11,896 |
https://leetcode.com/problems/find-pivot-index/discuss/2793483/SUM-code | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
suml = sum(nums)
left_sum = 0
for i in range(len(nums)):
if left_sum == (suml - (nums[i] + left_sum)):
return i
left_sum += nums[i]
return -1 | find-pivot-index | SUM code | Julierv | 0 | 1 | find pivot index | 724 | 0.535 | Easy | 11,897 |
https://leetcode.com/problems/find-pivot-index/discuss/2792312/Python3-%3A-Using-Two-Pass-HashTable | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
# using Two Pass Hashtable
# Time Complexity : O(n~2n)
# Space Complexity : O(n)
if len(nums) < 2:
return 0 if nums[0] == 0 else -1
postfix_sum = nums[:]
for i in range(2, len(nums) + ... | find-pivot-index | Python3 : Using Two Pass HashTable | boming05292 | 0 | 2 | find pivot index | 724 | 0.535 | Easy | 11,898 |
https://leetcode.com/problems/find-pivot-index/discuss/2791396/Basic-Approach | class Solution:
def pivotIndex(self, nums: List[int]) -> int:
tot_sum = sum(nums)
lsum = 0
for i in range(len(nums)):
if lsum == (tot_sum - nums[i]- lsum):
return i
else:
lsum = lsum + nums[i]
return -1 | find-pivot-index | Basic Approach | siddwho819 | 0 | 2 | find pivot index | 724 | 0.535 | Easy | 11,899 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.