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/subarray-sum-equals-k/discuss/1759711/Python-Simple-Python-Solution-Using-PrefixSum-and-Dictionary | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
ans=0
prefsum=0
d={0:1}
for num in nums:
prefsum = prefsum + num
if prefsum-k in d:
ans = ans + d[prefsum-k]
if prefsum not in d:
d[prefsum] = 1
else:
d[prefsum] = d[prefsum]+1
return ans | subarray-sum-equals-k | [ Python ] ✅✅ Simple Python Solution Using PrefixSum and Dictionary 🔥✌ | ASHOK_KUMAR_MEGHVANSHI | 192 | 26,200 | subarray sum equals k | 560 | 0.44 | Medium | 9,800 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2301103/Python-HashMap-O(n)-(Explanation) | class Solution(object):
def subarraySum(self, nums, k):
currSum = 0
ansCount = 0
prevSums = {0:1}
for num in nums:
currSum += num
if currSum - k in prevSums:
ansCount += prevSums[currSum - k]
... | subarray-sum-equals-k | [Python] HashMap O(n) (Explanation) | Derek_Shimoda | 5 | 288 | subarray sum equals k | 560 | 0.44 | Medium | 9,801 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/701139/Very-short-python-solution | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
count = 0
seen_sums = defaultdict(int)
for acc in accumulate(nums, initial=0):
count += seen_sums[acc - k]
seen_sums[acc] += 1
return count | subarray-sum-equals-k | Very short python solution | auwdish | 4 | 1,000 | subarray sum equals k | 560 | 0.44 | Medium | 9,802 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1546271/Python-3-Clean-8-Line-Solution-Time%3A-99.86 | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
sums = defaultdict(int)
curr = res = 0
sums[0] = 1
for num in nums:
curr += num
res += sums[curr - k]
sums[curr] += 1
return res | subarray-sum-equals-k | Python 3 Clean 8 Line Solution, Time: 99.86 % | user7776g | 3 | 219 | subarray sum equals k | 560 | 0.44 | Medium | 9,803 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1760170/Python3-Solution-using-a-hashmap-with-intuitive-explanation. | class Solution(object):
def subarraySum(self, nums: List[int], k: int) -> int:
count = 0
hashmap = {0: 1}
curr_cumulative_sum = 0
for num in nums:
curr_cumulative_sum += num
if curr_cumulative_sum - k in hashmap:
... | subarray-sum-equals-k | [Python3] Solution using a hashmap with intuitive explanation. | seankala | 2 | 213 | subarray sum equals k | 560 | 0.44 | Medium | 9,804 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1719417/Python-Easy-Solutionor-PrefixSum-or-HashMap | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
freq_dict={0:1}
cnt=sum_till_now=0
for num in nums:
sum_till_now+=num
cnt+=freq_dict.get((sum_till_now-k),0)
freq_dict[sum_till_now]= freq_dict.get(sum_till_now,0)+1
... | subarray-sum-equals-k | Python Easy Solution| PrefixSum | HashMap | shandilayasujay | 2 | 418 | subarray sum equals k | 560 | 0.44 | Medium | 9,805 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2802562/Python-oror-Easy-oror-97.05-Faster-oror-Using-prefix-sum | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
prefixsum=c=0
d=dict()
d[0]=1 #For the condition when prefixsum = k
for i in nums:
prefixsum+=i
if prefixsum-k in d:
c+=d[prefixsum-k]
if prefixsum in d:
... | subarray-sum-equals-k | Python || Easy || 97.05% Faster || Using prefix sum | DareDevil_007 | 1 | 127 | subarray sum equals k | 560 | 0.44 | Medium | 9,806 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2602833/Concise-python-solution | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
res = 0
currSum = 0
prefixSum = {0 : 1}
for num in nums:
currSum += num
diff = currSum - k
res += prefixSum.get(diff, 0)
prefixSum[currSum] = 1 + prefixSum.... | subarray-sum-equals-k | Concise python solution | MaverickEyedea | 1 | 50 | subarray sum equals k | 560 | 0.44 | Medium | 9,807 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2188200/HashMap-Approach-oror-Sliding-Window-Approach-by-Aditya-Verma | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
i = 0
j = 0
n = len(nums)
s = 0
subarrayCount = 0
while j < n:
s += nums[j]
if s < k:
j += 1
elif s == k:
subarrayCount += 1
... | subarray-sum-equals-k | HashMap Approach || Sliding Window Approach by Aditya Verma | Vaibhav7860 | 1 | 538 | subarray sum equals k | 560 | 0.44 | Medium | 9,808 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2188200/HashMap-Approach-oror-Sliding-Window-Approach-by-Aditya-Verma | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
hashMap = {0 : 1}
n = len(nums)
s = 0
subarrayCount = 0
for i in range(n):
s += nums[i]
subarrayCount += hashMap.get(s-k, 0)
hashMap[s] = hashMap.get(s, 0) + 1
r... | subarray-sum-equals-k | HashMap Approach || Sliding Window Approach by Aditya Verma | Vaibhav7860 | 1 | 538 | subarray sum equals k | 560 | 0.44 | Medium | 9,809 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1759703/Python-solution-85-faster | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
d = {}
d[0] = 1
s = 0
ans = 0
for i in nums:
s+=i
if d.get(s-k):
ans+=d[s-k]
if d.get(s):
d[s]+=1
else:
d[s] ... | subarray-sum-equals-k | Python solution 85% faster | khanter | 1 | 156 | subarray sum equals k | 560 | 0.44 | Medium | 9,810 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1392671/Python-solution-using-hashmap | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
hashmap = {0:1}
count = 0
sums = 0
for i in nums:
sums += i
if sums-k in hashmap:
count += hashmap[sums-k] # important step
if sums in ... | subarray-sum-equals-k | Python solution using hashmap | Saura_v | 1 | 303 | subarray sum equals k | 560 | 0.44 | Medium | 9,811 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1053844/Python-O(n) | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
sum_freq = {0:1}
t = 0
for s in accumulate(nums):
t += sum_freq.get(s-k, 0)
sum_freq[s] = sum_freq.get(s, 0) + 1
return t | subarray-sum-equals-k | Python O(n) | milton0825 | 1 | 432 | subarray sum equals k | 560 | 0.44 | Medium | 9,812 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/472456/Python3-two-solutions | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
if len(nums) == 1:
if nums[0] == k: return 1
else: return 0
ht = {}
ht[0] = 1
count,sums=0,0
for num in nums:
sums += num
count += ht.get(sums-k,0)
ht[sums] = ht.get(sums,0) + 1
return count
def subarraySum1(self, num... | subarray-sum-equals-k | Python3 two solutions | jb07 | 1 | 457 | subarray sum equals k | 560 | 0.44 | Medium | 9,813 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/429474/Python3-6-line-O(N)-(95.06) | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
ans = prefix = 0
freq = Counter({0:1})
for x in nums:
prefix += x
ans += freq[prefix - k]
freq[prefix] += 1
return ans | subarray-sum-equals-k | [Python3] 6-line O(N) (95.06%) | ye15 | 1 | 408 | subarray sum equals k | 560 | 0.44 | Medium | 9,814 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2834246/Python-or-HashMapDict-or-With-Pictures-explanations-and-diagrams | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
prefix = 0
seen = {0:1}
res = 0
for num in nums:
prefix += num
if prefix - k in seen:
res += seen[prefix-k]
seen[prefix] = 1 + seen.get(prefix, 0) # this does no... | subarray-sum-equals-k | 🎉Python | HashMap/Dict | With Pictures, explanations and diagrams | Arellano-Jann | 0 | 6 | subarray sum equals k | 560 | 0.44 | Medium | 9,815 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2827391/Python3-very-short-solution-with-dictionary | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
s = result = 0
d = {}
for n in nums:
s += n
result += d.get(s - k, 0) + (s == k)
d[s] = d.get(s, 0) + 1
return result | subarray-sum-equals-k | Python3 very short solution with dictionary | user3238Gj | 0 | 1 | subarray sum equals k | 560 | 0.44 | Medium | 9,816 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2811348/Python-oror-Easy-To-Understand-oror-Dictionary | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
currentSum=0
count=0
myDict={}
for i in range(len(nums)):
currentSum+=nums[i]
diff=currentSum-k
# if the sum is equal to k
if diff==0:
count+=1
... | subarray-sum-equals-k | Python || Easy To Understand || Dictionary | vishal_niet | 0 | 4 | subarray sum equals k | 560 | 0.44 | Medium | 9,817 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2782303/Python-O(n)-Solution | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
result = 0
curSum = 0
prefixSums = {0:1}
for n in nums:
curSum += n
diff = curSum - k
result += prefixSums.get(diff, 0)
prefixSums[curSum] = 1 + prefixSu... | subarray-sum-equals-k | Python O(n) Solution | agnishwar29 | 0 | 7 | subarray sum equals k | 560 | 0.44 | Medium | 9,818 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2747456/Prefix-Sum-Fast-and-Easy-Solution | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
d = {0 : 1}
psum = 0
res = 0
for i in range(len(nums)):
psum += nums[i]
if psum - k in d:
res += d[psum - k]
d[psum] = d.get(psum, 0) + 1
return (res) | subarray-sum-equals-k | Prefix Sum - Fast and Easy Solution | user6770yv | 0 | 4 | subarray sum equals k | 560 | 0.44 | Medium | 9,819 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2740306/Python-Solution | class Solution(object):
def subarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
table = {0: 1}
sum = 0
ans = 0
for i in range(len(nums)):
sum += nums[i]
if sum - k in table:
... | subarray-sum-equals-k | Python Solution | DietCoke777 | 0 | 4 | subarray sum equals k | 560 | 0.44 | Medium | 9,820 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2685758/Hashmap-solution-or-O(N)-TC-O(N)-SC | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
res=0
hashmap=dict()
temp=0
hashmap[0]=1
for i in nums:
temp+=i
if temp-k in hashmap:
res+=hashmap[temp-k]
if temp not in hashmap:
hashma... | subarray-sum-equals-k | Hashmap solution | O(N) TC O(N) SC | sundram_somnath | 0 | 21 | subarray sum equals k | 560 | 0.44 | Medium | 9,821 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2682391/Python-Simple-Sol-using-map | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
ans,n=0,len(nums)-1
pre=0
d={}
d[0]=1
for i in nums:
pre+=i
if pre-k in d:
ans+=d[pre-k]
d[pre]=d.get(pre,0) + 1
return ans | subarray-sum-equals-k | Python Simple Sol using map | pranjalmishra334 | 0 | 19 | subarray sum equals k | 560 | 0.44 | Medium | 9,822 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2663479/Python3-Solution-oror-O(N)-Time-and-Space-Complexity | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
prefixSum=0
dic={}
count=0
for i in range(len(nums)):
prefixSum+=nums[i]
if prefixSum not in dic:
dic[prefixSum]=0
if prefixSum==k:
coun... | subarray-sum-equals-k | Python3 Solution || O(N) Time & Space Complexity | akshatkhanna37 | 0 | 6 | subarray sum equals k | 560 | 0.44 | Medium | 9,823 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2547370/EASY-PYTHON3-SOLUTION | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
ans=0
prefsum=0
d={0:1}
for num in nums:
prefsum = prefsum + num
if prefsum-k in d:
ans = ans + d[prefsum-k]
if prefsum not in d:
d[prefsum] = 1
else:
d[prefsum] = d[prefsum]+1
return ans | subarray-sum-equals-k | ✅✔🔥 EASY PYTHON3 SOLUTION 🔥✔✅ | rajukommula | 0 | 27 | subarray sum equals k | 560 | 0.44 | Medium | 9,824 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2516296/Python-runtime-29.27-memory-74.63 | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
ans = 0
cur_sum = 0
d = {cur_sum:1}
for e in nums:
cur_sum += e
diff = cur_sum - k
if diff in d:
ans += d[diff]
if cur_sum not in d:
... | subarray-sum-equals-k | Python, runtime 29.27%, memory 74.63% | tsai00150 | 0 | 66 | subarray sum equals k | 560 | 0.44 | Medium | 9,825 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/2300204/Python3-oror-Prefix-sum-and-Hash-Map | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
d = {0:1}
n = len(nums)
ans, s = 0, 0
for r in range(n):
s += nums[r]
if s-k in d:
ans += d[s-k]
if s in d:
d[s] += 1
else:
d[s] = 1
return ans | subarray-sum-equals-k | Python3 || Prefix sum and Hash Map | sagarhasan273 | 0 | 47 | subarray sum equals k | 560 | 0.44 | Medium | 9,826 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1999575/one-pass-beat-all | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
# dict store presum value and freq
d, cur_sum, ret = defaultdict(int), 0, 0
for n in nums:
cur_sum += n
ret += d[cur_sum - k]
d[cur_sum] += 1
if cur_sum == k: ret += 1
ret... | subarray-sum-equals-k | one pass beat all | BichengWang | 0 | 113 | subarray sum equals k | 560 | 0.44 | Medium | 9,827 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1986136/store-all-pre-sums-check-if-sum-k-belongs-to-the-hashmap | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
s,count=0,0
track={0:1}
for i in nums:
s+=i
diff =s-k
if diff in track:
count+= track[diff]
track[s] = track.get(s,0)+1
return count | subarray-sum-equals-k | store all pre sums check if sum-k belongs to the hashmap | Varunneo | 0 | 76 | subarray sum equals k | 560 | 0.44 | Medium | 9,828 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1955995/Python-(Simple-Approach-and-Beginner-Friendly) | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
count = 0
dict = {0:1}
prefix = 0
for i in range (0, len(nums)):
prefix+=nums[i]
if prefix-k in dict:
count+=dict[prefix-k]
if prefix i... | subarray-sum-equals-k | Python (Simple Approach and Beginner-Friendly) | vishvavariya | 0 | 167 | subarray sum equals k | 560 | 0.44 | Medium | 9,829 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1892746/3-Lines-Python-Solution-oror-75-Faster-oror-Memory-less-than-80 | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
s=0 ; ans=0 ; C=Counter()
for num in nums: C[s]+=1 ; s+=num ; ans+=C[s-k]
return ans | subarray-sum-equals-k | 3-Lines Python Solution || 75% Faster || Memory less than 80% | Taha-C | 0 | 138 | subarray sum equals k | 560 | 0.44 | Medium | 9,830 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1889039/My-easy-to-understand-compact-code-in-Linear-time-complexity. | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
ans=0
d={0:1}
s=0
for i in nums:
s+=i
if s-k in d:
ans+=d[s-k]
if s in d:
d[s]+=1
else:
d[s]=1
return ans | subarray-sum-equals-k | My easy to understand compact code in Linear time complexity. | tkdhimanshusingh | 0 | 160 | subarray sum equals k | 560 | 0.44 | Medium | 9,831 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1841255/Python3-Clean-solution-with-prefix-sum-map | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
sum_i = 0
# Map: prefix sum -> number of times prefix sum appears
prevSumMap = {}
prevSumMap[0] = 1
res = 0
for i in range(0, len(nums)):
sum_i += nums[i]
sum_j = s... | subarray-sum-equals-k | [Python3] Clean solution with prefix sum map | leqinancy | 0 | 36 | subarray sum equals k | 560 | 0.44 | Medium | 9,832 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1761129/560.-Subarray-Sum-Equals-K | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
Nsum = 0
hashMap = { 0 : 1}
result = 0
for i in nums:
Nsum += i
diff = Nsum - k
result += hashMap.get(diff, 0)
hashMap[Nsum] = 1 + hashMap.get(Nsum, 0)
... | subarray-sum-equals-k | 560. Subarray Sum Equals K | prasunbhunia | 0 | 108 | subarray sum equals k | 560 | 0.44 | Medium | 9,833 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1760167/Python3-Dictionary-Hashmap-kills-everything | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
temp,presum,count = 0, defaultdict(int), 0
presum[0] = 1
for num in nums:
temp += num
if temp - k in presum:
count += presum[temp - k]
presum[temp] += 1
return c... | subarray-sum-equals-k | [Python3] Dictionary - Hashmap kills everything | Rainyforest | 0 | 39 | subarray sum equals k | 560 | 0.44 | Medium | 9,834 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1754224/Python-Prefix-sum-solution-explained | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
# will store the count of th prefix sum
# we have seen till now, we're also going
# to add the fact that we've seen a psum
# of 0 already to account for when the whole
# subarray is of sum == k, more on th... | subarray-sum-equals-k | [Python] Prefix sum solution explained | buccatini | 0 | 177 | subarray sum equals k | 560 | 0.44 | Medium | 9,835 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1669408/Subarray-Sum2k-via-Prefix | class Solution:
def subarraySum(self, nums, k):
hmap = defaultdict(int, {0: 1})
preFix = 0; count = 0
for ele in nums:
preFix += ele
preFix_k = preFix - k
if preFix_k in hmap:
count += hmap[preFix_k] ### count how many previous cumulative sum = preFix - k
hmap[preFix] += 1
return... | subarray-sum-equals-k | Subarray Sum2k via Prefix | zwang198 | 0 | 66 | subarray sum equals k | 560 | 0.44 | Medium | 9,836 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1621201/Running-sum-and-frequency-dict-solution | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
count=0
sum_count={}
sum_count[0]=1
curr_sum=0
for i in range(len(nums)):
curr_sum+=nums[i]
if curr_sum-k in sum_count:
... | subarray-sum-equals-k | Running sum and frequency dict solution | namantejaswi | 0 | 155 | subarray sum equals k | 560 | 0.44 | Medium | 9,837 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1581856/Python-Easy-Solution-or-Brute-Force-and-Optimal-Approach | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
# Brute Force - Time: O(n) - (TLE)
count = 0
for i in range(len(nums)):
sum = 0
for j in range(i, len(nums)):
sum += nums[j]
if sum == k:
count += 1
return count
# Using HashTable - Time and Space: O(n) and O(n)
res ... | subarray-sum-equals-k | Python Easy Solution | Brute Force and Optimal Approach | leet_satyam | 0 | 536 | subarray sum equals k | 560 | 0.44 | Medium | 9,838 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1483967/Python-Clean-Hashmap | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
N, cumv, count = len(nums), 0, 0
hashmap = {0: 1}
for i in range(N):
cumv += nums[i]
if cumv-k in hashmap:
count += hashmap[cumv-k]
if cumv not in hashmap:
... | subarray-sum-equals-k | [Python] Clean Hashmap | soma28 | 0 | 528 | subarray sum equals k | 560 | 0.44 | Medium | 9,839 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1288054/Python | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
padding = 10
result = set()
for window_size in range(len(nums)+padding):
for i in range(len(nums)+padding):
if not nums[i:i+window_size]: continue
... | subarray-sum-equals-k | [Python] | dev-josh | 0 | 288 | subarray sum equals k | 560 | 0.44 | Medium | 9,840 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1288054/Python | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
result, prefix = 0, list(itertools.accumulate([0] + nums))
for window_size in range(1, len(prefix)):
for pos in range(0, len(prefix)-window_size):
... | subarray-sum-equals-k | [Python] | dev-josh | 0 | 288 | subarray sum equals k | 560 | 0.44 | Medium | 9,841 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/578409/Python3-Solution-for-560-and-974 | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
result,total,hmap = 0,0,{}
for num in nums:
hmap[total] = hmap.get(total,0) + 1
total += num
if hmap.get(total-k):
result += hmap[total-k]
return result | subarray-sum-equals-k | [Python3] Solution for #560 and #974 | pratushah | 0 | 371 | subarray sum equals k | 560 | 0.44 | Medium | 9,842 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/578409/Python3-Solution-for-560-and-974 | class Solution:
def subarraysDivByK(self, A: List[int], K: int) -> int:
hmap,total,result = {},0,0
for num in A:
hmap[total] = hmap.get(total,0) + 1
total += num
total %= K
if hmap.get(total):
result += hmap[total]
return result | subarray-sum-equals-k | [Python3] Solution for #560 and #974 | pratushah | 0 | 371 | subarray sum equals k | 560 | 0.44 | Medium | 9,843 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/389119/Solution-in-Python-3-(five-lines) | class Solution:
def subarraySum(self, n: List[int], k: int) -> int:
L, C, D, t = len(n), [0]+list(itertools.accumulate(n)), collections.defaultdict(int), 0
for i in range(L):
D[C[i]] += 1
t += D[C[i+1]-k]
return t
- Junaid Mansuri
(LeetCode ID)@hotmail.com | subarray-sum-equals-k | Solution in Python 3 (five lines) | junaidmansuri | 0 | 1,200 | subarray sum equals k | 560 | 0.44 | Medium | 9,844 |
https://leetcode.com/problems/subarray-sum-equals-k/discuss/1210911/Simple-Python3-Solution-using-Dictionary | class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
if len(nums)==0:
return 0
rSum=0
count=0
res_map={}
res_map[0]=1
for i in range(len(nums)):
rSum+=nums[i]
compliment=rSum-k
if ... | subarray-sum-equals-k | Simple Python3 Solution using Dictionary | rajpatel9498 | -2 | 157 | subarray sum equals k | 560 | 0.44 | Medium | 9,845 |
https://leetcode.com/problems/array-partition/discuss/390198/Algorithm-and-solution-in-python3 | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
sum_ = 0
for i in range(0,len(nums),2):
sum_ += nums[i]
return sum_
# Time : 356 ms
# Memory : 16.7 M | array-partition | Algorithm and solution in python3 | ramanaditya | 47 | 3,200 | array partition | 561 | 0.767 | Easy | 9,846 |
https://leetcode.com/problems/array-partition/discuss/390198/Algorithm-and-solution-in-python3 | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
return sum(sorted(nums)[::2])
# Time : 332 ms
# Memory : 16.5 M | array-partition | Algorithm and solution in python3 | ramanaditya | 47 | 3,200 | array partition | 561 | 0.767 | Easy | 9,847 |
https://leetcode.com/problems/array-partition/discuss/1432853/Python-1-line | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
return sum(sorted(nums)[::2]) | array-partition | Python 1 line | phuoctfx11137 | 2 | 300 | array partition | 561 | 0.767 | Easy | 9,848 |
https://leetcode.com/problems/array-partition/discuss/1967927/Python-oror-5-line-sort-and-linear-add | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
summ = 0
for i in range(0, len(nums), 2):
summ += nums[i]
return summ | array-partition | Python || 5-line sort and linear add | gulugulugulugulu | 1 | 69 | array partition | 561 | 0.767 | Easy | 9,849 |
https://leetcode.com/problems/array-partition/discuss/1775242/Two-Line-Solution-or-Python-3-or-List-Comprehension-or-Step-by-Step-explanation | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums = sorted(nums)
return sum([nums[i] for i in range(0, len(nums), 2)]) | array-partition | ✔Two Line Solution | Python 3 | List Comprehension | Step by Step explanation | Coding_Tan3 | 1 | 66 | array partition | 561 | 0.767 | Easy | 9,850 |
https://leetcode.com/problems/array-partition/discuss/1299900/Python3-dollarolution | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums, s = sorted(nums), 0
for i in range(0,len(nums),2):
s += nums[i]
return s | array-partition | Python3 $olution | AakRay | 1 | 218 | array partition | 561 | 0.767 | Easy | 9,851 |
https://leetcode.com/problems/array-partition/discuss/1097450/Python-simple-and-easy-to-understand-small-solution | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
return sum(nums[0::2]) | array-partition | Python simple and easy to understand small solution | coderash1998 | 1 | 128 | array partition | 561 | 0.767 | Easy | 9,852 |
https://leetcode.com/problems/array-partition/discuss/1017890/Simple-solution-faster-than-98.96 | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
l=sorted(nums,reverse=True) #We can note than the maximum sum will be when we consider minimum element among each largest pairs,
#so we sort the list to arrange the pairs in order
return sum([l[i] for ... | array-partition | Simple solution- faster than 98.96% | thisisakshat | 1 | 215 | array partition | 561 | 0.767 | Easy | 9,853 |
https://leetcode.com/problems/array-partition/discuss/682988/Unbelievable-Simple-and-Super-Fast | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums=sorted(nums)
a=0
for i in range(0,len(nums),2):
a+=nums[i]
return a | array-partition | Unbelievable Simple and Super Fast | excalibur12 | 1 | 198 | array partition | 561 | 0.767 | Easy | 9,854 |
https://leetcode.com/problems/array-partition/discuss/2772781/One-Liner-Python-Solution | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
return sum(sorted(nums)[::2]) | array-partition | One Liner Python Solution | dnvavinash | 0 | 3 | array partition | 561 | 0.767 | Easy | 9,855 |
https://leetcode.com/problems/array-partition/discuss/2716067/easyy-Python-%3A) | class Solution:
def arrayPairSum(self, nums):
return sum(sorted(nums)[::2]) | array-partition | easyy Python :) | MaryLuz | 0 | 4 | array partition | 561 | 0.767 | Easy | 9,856 |
https://leetcode.com/problems/array-partition/discuss/2687947/for-beginners-python-solution-with-explanation | class Solution:
def arrayPairSum(self, s: List[int]) -> int:
s.sort()
sum=0
for i in range(len(s)):
if i%2==0:
sum+=s[i]
return(sum) | array-partition | for beginners python solution with explanation | AMBER_FATIMA | 0 | 2 | array partition | 561 | 0.767 | Easy | 9,857 |
https://leetcode.com/problems/array-partition/discuss/2633799/Python3-oror-TC-O(NlogN)-SC-O(1)-Optimized-Approach | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
total = 0
for i in range(0,len(nums)-1,2):
total+=nums[i]
return total | array-partition | Python3 || TC = O(NlogN) , SC = O(1), Optimized Approach | shacid | 0 | 5 | array partition | 561 | 0.767 | Easy | 9,858 |
https://leetcode.com/problems/array-partition/discuss/2569034/SIMPLE-PYTHON3-SOLUTION-95faster | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
ans = 0
for i in range(1,len(nums),2):
ans += min(nums[i-1],nums[i])
return ans | array-partition | ✅✔ SIMPLE PYTHON3 SOLUTION ✅✔95%faster | rajukommula | 0 | 38 | array partition | 561 | 0.767 | Easy | 9,859 |
https://leetcode.com/problems/array-partition/discuss/2555653/using-sort | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
# nums is an array that has an even length
# order of elements don't matter for pairings
# sort the array in ascending order
# sum of every other element from the end down (choosing min)
# Time O(N^2) Space O(1)
... | array-partition | using sort | andrewnerdimo | 0 | 22 | array partition | 561 | 0.767 | Easy | 9,860 |
https://leetcode.com/problems/array-partition/discuss/2432042/Python-solution-sorting | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
sorted_nums = sorted(nums)
maxim_sum = 0
for i in range(len(nums) // 2):
maxim_sum += sorted_nums[0]
sorted_nums.pop(0)
sorted_nums.pop(0)
return maxim_sum | array-partition | Python solution sorting | samanehghafouri | 0 | 17 | array partition | 561 | 0.767 | Easy | 9,861 |
https://leetcode.com/problems/array-partition/discuss/2416880/Python-One-Liner | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
return sum(sorted(nums)[::2]) | array-partition | Python One Liner | Abhi_-_- | 0 | 22 | array partition | 561 | 0.767 | Easy | 9,862 |
https://leetcode.com/problems/array-partition/discuss/2347882/Python-Simple-with-Explanation | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
return sum(nums[::2]) | array-partition | Python Simple with Explanation | drblessing | 0 | 33 | array partition | 561 | 0.767 | Easy | 9,863 |
https://leetcode.com/problems/array-partition/discuss/2174491/Python-one-line-solution | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
return sum(sorted(nums)[::2]) | array-partition | Python one line solution | writemeom | 0 | 36 | array partition | 561 | 0.767 | Easy | 9,864 |
https://leetcode.com/problems/array-partition/discuss/1988778/Python-Easiest-Solution-With-Explanation-or-Sorting-or-Beg-to-adv | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort() # Firstly we are sorting the array to have pairs & as array is already sorted, implies the first elem of the pair is the smaller one.
maximized_sum = 0 # taking a empty variable to for saving the resul... | array-partition | Python Easiest Solution With Explanation | Sorting | Beg to adv | rlakshay14 | 0 | 47 | array partition | 561 | 0.767 | Easy | 9,865 |
https://leetcode.com/problems/array-partition/discuss/1981785/Python-3-Solution-O(n)-Time-complexity-Counting-Sort | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
# Counting Sort Algorithm
max_elm = max(nums)
min_elm = min(nums)
count_nums = [0 for x in range(min_elm, max_elm + 1)]
for i in range(len(nums)):
count_nums[nums[i] - min_elm] += 1
for i in range(1,... | array-partition | Python 3 Solution O(n) Time complexity, Counting Sort | AprDev2011 | 0 | 57 | array partition | 561 | 0.767 | Easy | 9,866 |
https://leetcode.com/problems/array-partition/discuss/1928213/easy-python-code | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
sum = 0
for i in range(0,len(nums),2):
sum += nums[i]
return sum | array-partition | easy python code | dakash682 | 0 | 39 | array partition | 561 | 0.767 | Easy | 9,867 |
https://leetcode.com/problems/array-partition/discuss/1793709/2-Lines-Python-Solution-oror-80-Faster-oror-Memory-less-than-90 | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
return sum(nums[::2]) | array-partition | 2-Lines Python Solution || 80% Faster || Memory less than 90% | Taha-C | 0 | 112 | array partition | 561 | 0.767 | Easy | 9,868 |
https://leetcode.com/problems/array-partition/discuss/1681014/Python3-Solution-or-1-line-answer | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
return sum(sorted(nums)[::2]) | array-partition | Python3 Solution | 1 line answer | satyam2001 | 0 | 88 | array partition | 561 | 0.767 | Easy | 9,869 |
https://leetcode.com/problems/array-partition/discuss/1370300/Runtime%3A-244-ms-faster-than-99.27-of-Python3 | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
total = 0
nums = sorted(nums)
for i in range(0, len(nums),2):
total += nums[i]
return total | array-partition | Runtime: 244 ms, faster than 99.27% of Python3 | estetika | 0 | 66 | array partition | 561 | 0.767 | Easy | 9,870 |
https://leetcode.com/problems/array-partition/discuss/1294150/Easy-Python-Solution(96.92) | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
c=0
for i in range(0,len(nums),2):
c+=nums[i]
return c | array-partition | Easy Python Solution(96.92%) | Sneh17029 | 0 | 300 | array partition | 561 | 0.767 | Easy | 9,871 |
https://leetcode.com/problems/array-partition/discuss/1241391/Simple-fast-Python3 | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
ans=0
for x in range(0, len(nums),2):
ans+=nums[x]
return ans | array-partition | Simple, fast Python3 | sk37 | 0 | 92 | array partition | 561 | 0.767 | Easy | 9,872 |
https://leetcode.com/problems/array-partition/discuss/1087880/Slow-python-(easy-to-understand) | class Solution:
def arrayPairSum(self, nums: list[int]):
nums = sorted(nums) # sorts the numbers in lowest to largest
final_answer = 0
skip_num = False # used to skip loops go i = 1 then next loop skips then i = 3
for i in range(len(nums) - 1):
if skip_num: # skips number because we don't want the i + ... | array-partition | Slow python (easy to understand) | Jamie_2345 | 0 | 50 | array partition | 561 | 0.767 | Easy | 9,873 |
https://leetcode.com/problems/array-partition/discuss/1035605/Python3-simple-and-easy-to-understand-solution | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
sum = 0
for i in range(0,len(nums),2):
sum += nums[i]
return sum | array-partition | Python3 simple and easy to understand solution | EklavyaJoshi | 0 | 74 | array partition | 561 | 0.767 | Easy | 9,874 |
https://leetcode.com/problems/array-partition/discuss/336498/Solution-in-Python-3-(-one-line-) | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
return sum(j for i,j in enumerate(sorted(nums)) if i%2==0)
- Python 3
- Junaid Mansuri | array-partition | Solution in Python 3 ( one line ) | junaidmansuri | 0 | 553 | array partition | 561 | 0.767 | Easy | 9,875 |
https://leetcode.com/problems/array-partition/discuss/258978/Python3-two-liner-99.89-faster | class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
return sum(nums[::2]) | array-partition | Python3 two liner 99.89% faster | tico82003 | 0 | 392 | array partition | 561 | 0.767 | Easy | 9,876 |
https://leetcode.com/problems/binary-tree-tilt/discuss/1617385/Recursive-solution-Python | class Solution:
def findTilt(self, root: Optional[TreeNode]) -> int:
def rec(node):
nonlocal res
if not node:
return 0
left_sum = rec(node.left)
right_sum = rec(node.right)
res += abs(left_sum - right_sum)
r... | binary-tree-tilt | Recursive solution Python | kryuki | 3 | 192 | binary tree tilt | 563 | 0.595 | Easy | 9,877 |
https://leetcode.com/problems/binary-tree-tilt/discuss/1209453/Python-3-Pure-Recursion. | class Solution:
def findTilt(self, root: TreeNode) -> int:
return self.traverse(root)[1]
def traverse(self,root):
if not root:
return 0,0
'''
lvsum & rvsum --> Sum of **values** all left and right children nodes respectively
ltsum & rtsum --> Sum of **tilt** of all left and right children nodes ... | binary-tree-tilt | Python 3 Pure Recursion. | Ajinkya-Sonawane | 2 | 184 | binary tree tilt | 563 | 0.595 | Easy | 9,878 |
https://leetcode.com/problems/binary-tree-tilt/discuss/1617729/Python3-Postorder-DFS-or-Time-O(n)-or-Space-O(h)-or-Optimal-Solution | class Solution:
def __init__(self):
self.ans = 0
def _dfs(self, root):
if not root:
return 0
left = self._dfs(root.left)
right = self._dfs(root.right)
self.ans += abs(left - right)
return left + right + root.val
def findTilt(... | binary-tree-tilt | [Python3] Postorder DFS | Time O(n) | Space O(h) | Optimal Solution | PatrickOweijane | 1 | 110 | binary tree tilt | 563 | 0.595 | Easy | 9,879 |
https://leetcode.com/problems/binary-tree-tilt/discuss/1617371/Python3-post-order-dfs | class Solution:
def findTilt(self, root: Optional[TreeNode]) -> int:
mp = {None: 0}
ans = 0
prev = None
node = root
stack = []
while node or stack:
if node:
stack.append(node)
node = node.left
else:
... | binary-tree-tilt | [Python3] post-order dfs | ye15 | 1 | 29 | binary tree tilt | 563 | 0.595 | Easy | 9,880 |
https://leetcode.com/problems/binary-tree-tilt/discuss/1596277/Python-3-recursive-solution | class Solution:
def findTilt(self, root: Optional[TreeNode]) -> int:
self.res = 0
def helper(root):
if root is None:
return 0
left, right = helper(root.left), helper(root.right)
self.res += abs(left - right)
return root.val... | binary-tree-tilt | Python 3 recursive solution | dereky4 | 1 | 96 | binary tree tilt | 563 | 0.595 | Easy | 9,881 |
https://leetcode.com/problems/binary-tree-tilt/discuss/1975694/Python-DFS-Solution-Faster-Than-92.90-No-Memory-Used-Explained-Via-Comments | class Solution:
def findTilt(self, root: Optional[TreeNode]) -> int:
ans = [0]
def dfs(tree):
# this is the base case
if not tree:
return 0
# go far to the left
left = dfs(tree.left)
# go gar to the right
... | binary-tree-tilt | Python, DFS Solution, Faster Than 92.90%, No Memory Used, Explained Via Comments | Hejita | 0 | 35 | binary tree tilt | 563 | 0.595 | Easy | 9,882 |
https://leetcode.com/problems/binary-tree-tilt/discuss/1618031/Python-simple-DFS-with-explanation | class Solution:
def findTilt(self, root: Optional[TreeNode]) -> int:
self.res = 0 #global tilt sum
def dfs(node):
if not node: return 0 # if node is null return 0
# traverse depth wise till we reach the leaf nodes
leftSum = dfs(node.left)
... | binary-tree-tilt | Python simple DFS with explanation | abkc1221 | 0 | 31 | binary tree tilt | 563 | 0.595 | Easy | 9,883 |
https://leetcode.com/problems/binary-tree-tilt/discuss/1617939/Easy-python-DFS-O(N)-solution-with-comments | class Solution:
def findTilt(self, root: Optional[TreeNode]) -> int:
ans=0
def DFS(node):
nonlocal ans
if not node:
return 0
# left is the accumulated sum of the underlying left nodes
# right is the accumu... | binary-tree-tilt | Easy python DFS O(N) solution with comments | Sadia11 | 0 | 18 | binary tree tilt | 563 | 0.595 | Easy | 9,884 |
https://leetcode.com/problems/binary-tree-tilt/discuss/1617712/Python3-Recursion-dfs-solution | class Solution:
def __init__(self):
self.res = 0
def traversal(self, node):
if not node:
return 0
l_v = self.traversal(node.left)
r_v = self.traversal(node.right)
self.res += abs(l_v - r_v)
return node.val + l_v + r_v
de... | binary-tree-tilt | [Python3] Recursion dfs solution | maosipov11 | 0 | 10 | binary tree tilt | 563 | 0.595 | Easy | 9,885 |
https://leetcode.com/problems/binary-tree-tilt/discuss/1613074/Recursion-solution-92-speed | class Solution:
def findTilt(self, root: Optional[TreeNode]) -> int:
ans = 0
def traverse(node: Optional[TreeNode]) -> int:
nonlocal ans
if not node:
return 0
left_sum = traverse(node.left)
right_sum = traverse(node.right)
... | binary-tree-tilt | Recursion solution, 92% speed | EvgenySH | 0 | 98 | binary tree tilt | 563 | 0.595 | Easy | 9,886 |
https://leetcode.com/problems/binary-tree-tilt/discuss/1516216/Python3-solution | class Solution:
def __init__(self):
self.s = 0
def findTilt(self, root: Optional[TreeNode]) -> int:
if not root or (not root.left and not root.right):
return 0
elif root.left and not root.right:
self.findTilt(root.left)
self.s += abs(root.left.val)
... | binary-tree-tilt | Python3 solution | EklavyaJoshi | 0 | 36 | binary tree tilt | 563 | 0.595 | Easy | 9,887 |
https://leetcode.com/problems/binary-tree-tilt/discuss/1397940/Python3-greater90-Using-Preorder. | class Solution:
def findTilt(self, root: Optional[TreeNode]) -> int:
def preorder(node):
if node is None:
return 0
leftSum = preorder(node.left)
rightSum = preorder(node.right)
preorder.tilt += abs(leftSum - rightSum)
... | binary-tree-tilt | [Python3] >90% Using Preorder. | whitehatbuds | 0 | 101 | binary tree tilt | 563 | 0.595 | Easy | 9,888 |
https://leetcode.com/problems/binary-tree-tilt/discuss/929150/Python3-Simple-Intuitive | class Solution:
def findTilt(self, root: TreeNode) -> int:
cur_sum = [0]
def helper(node: TreeNode):
if not node: return 0
lt = helper(node.left)
rt = helper(node.right)
cur_sum[0] += abs(lt-rt)
return node.val + lt + rt
helper(root... | binary-tree-tilt | [Python3] Simple Intuitive | nachiketsd | 0 | 37 | binary tree tilt | 563 | 0.595 | Easy | 9,889 |
https://leetcode.com/problems/find-the-closest-palindrome/discuss/2581120/Only-5-Cases-to-Consider%3A-Concise-implementation-in-Python-with-Full-Explanation | class Solution:
def find_next_palindrome(self, n, additive):
l = len(n)
if l == 0:
return 0
first_half = str(int(n[:l // 2 + l % 2]) + additive)
return int(first_half + first_half[(-1 - l%2)::-1])
def nearestPalindromic(self, n: str) -> str:
m = i... | find-the-closest-palindrome | Only 5 Cases to Consider: Concise implementation in Python with Full Explanation | metaphysicalist | 3 | 269 | find the closest palindrome | 564 | 0.22 | Hard | 9,890 |
https://leetcode.com/problems/find-the-closest-palindrome/discuss/851681/Python-solution-with-detail-expalanation | class Solution:
def nearestPalindromic(self, n: str) -> str:
"""
1. The nearest palindrome falls into the boundary of
[10 ** (maxLen - 1) - 1, 10 ** maxLen + 1].
2. We should only consider the change on the first half part of n and
reverse it to the second half of n a... | find-the-closest-palindrome | Python solution with detail expalanation | eroneko | 2 | 624 | find the closest palindrome | 564 | 0.22 | Hard | 9,891 |
https://leetcode.com/problems/find-the-closest-palindrome/discuss/2814760/Python3-Solution | class Solution:
def nearestPalindromic(self, S):
N = len(S)
Val = lambda x: abs(int(S) - int(x))
P = int(S[:(N + 1) >> 1])
Dp = [i + (i[:len(i)-(N&1)])[::-1] for i in map(str, range(P - 1, P + 2))] + ['1' + '0'*(N-1) + '1']
ans = '9' * (N - 1) or '0'
for i in Dp:... | find-the-closest-palindrome | ✔ Python3 Solution | satyam2001 | 0 | 6 | find the closest palindrome | 564 | 0.22 | Hard | 9,892 |
https://leetcode.com/problems/find-the-closest-palindrome/discuss/2536145/Python3-not-difficult-but-extremely-tedious | class Solution:
def nearestPalindromic(self, n: str) -> str:
sz = len(n)
cand = n[:sz//2] + (n[sz//2] if sz&1 else "") + n[0:sz//2][::-1]
def fn(carry):
digits = list(map(int, n))[:(sz+1)//2]
i = (sz-1)//2
while i >= 0 and carry:
... | find-the-closest-palindrome | [Python3] not difficult but extremely tedious | ye15 | 0 | 111 | find the closest palindrome | 564 | 0.22 | Hard | 9,893 |
https://leetcode.com/problems/array-nesting/discuss/1438213/CLEAN-and-SHORT-PYTHON-O(N)-TIME-O(N)-SPACE | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
res, l = 0, len(nums)
globalSet = set()
for k in range(l):
if k not in globalSet:
currLength, currSet, val = 0, set(), k
while True:
if nums[val] in currSet: break
... | array-nesting | CLEAN & SHORT PYTHON, O(N) TIME, O(N) SPACE | kushagrabainsla | 6 | 442 | array nesting | 565 | 0.565 | Medium | 9,894 |
https://leetcode.com/problems/array-nesting/discuss/1424880/Python-3-or-Array-Clean-O(N)-or-Explanation | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
ans = cnt = 0
for i, idx in enumerate(nums):
if idx < 0: continue # avoid revisit
while nums[idx] >= 0:
cnt, nums[idx], idx = cnt+1, -1, nums[idx] # increment length; mar... | array-nesting | Python 3 | Array, Clean O(N) | Explanation | idontknoooo | 2 | 106 | array nesting | 565 | 0.565 | Medium | 9,895 |
https://leetcode.com/problems/array-nesting/discuss/364272/Solution-in-Python-3-(beats-100)-(only-seven-lines) | class Solution:
def arrayNesting(self, n: List[int]) -> int:
S = {}
for i in range(len(n)):
if n[i] == -1: continue
m, a = 0, i
while n[a] != -1: n[a], a, b, m = -1, n[a], a, m + 1
S[i] = m + S.pop(b, 0)
return max(S.values())
- Python 3
(LeetCode ID)@hotmail.com | array-nesting | Solution in Python 3 (beats 100%) (only seven lines) | junaidmansuri | 2 | 447 | array nesting | 565 | 0.565 | Medium | 9,896 |
https://leetcode.com/problems/array-nesting/discuss/1951057/Python3-solution | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
def dfs(node):
visited.add(node)
this_group.add(node)
neigh = nums[node]
if neigh not in visited:
dfs(neigh)
ans = 0
visited = set()
for node in ra... | array-nesting | Python3 solution | dalechoi | 1 | 58 | array nesting | 565 | 0.565 | Medium | 9,897 |
https://leetcode.com/problems/array-nesting/discuss/1438675/Python3-solution-or-Explained-with-comments | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
# you can use set to mark the element where you've been before but instead of extra memory you can just give the value of -1 to every element you've visited
# then you just check if the current element is different from -1, if so you ha... | array-nesting | Python3 solution | Explained with comments | FlorinnC1 | 1 | 46 | array nesting | 565 | 0.565 | Medium | 9,898 |
https://leetcode.com/problems/array-nesting/discuss/1535428/Java-Python-oror-Easy-Approach-with-Explanation-oror-DFS-oror-Cycle | class Solution(object):
def arrayNesting(self, nums):
self.maxClength= 0;
self.visited= [False]*len(nums);
for i in range( 0, len(nums)):
self.dfs( nums, self.visited, i, 0);
return self.maxClength;
def dfs ( self, nums, visited, i, count):
... | array-nesting | Java, Python || Easy Approach with Explanation || DFS || Cycle | swapnilGhosh | 0 | 151 | array nesting | 565 | 0.565 | Medium | 9,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.