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/longest-uncommon-subsequence-i/discuss/1177751/99.02-faster-python(3line)-submission
class Solution: def findLUSlength(self, a: str, b: str) -> int: if a!=b: return max(len(a),len(b)) return -1
longest-uncommon-subsequence-i
99.02% faster python(3line) submission ;
_jorjis
1
107
longest uncommon subsequence i
521
0.603
Easy
9,200
https://leetcode.com/problems/longest-uncommon-subsequence-i/discuss/2829499/Python-or-Accepted-Soln
class Solution: def findLUSlength(self, a: str, b: str) -> int: if a == b: return -1 else: return max(len(a), len(b))
longest-uncommon-subsequence-i
Python | Accepted Soln
ajay_gc
0
1
longest uncommon subsequence i
521
0.603
Easy
9,201
https://leetcode.com/problems/longest-uncommon-subsequence-i/discuss/2807587/Easy-1-line-solution-Python
class Solution: def findLUSlength(self, a: str, b: str) -> int: return -1 if a == b else max(len(a), len(b))
longest-uncommon-subsequence-i
Easy 1 line solution Python
kanykeiat
0
4
longest uncommon subsequence i
521
0.603
Easy
9,202
https://leetcode.com/problems/longest-uncommon-subsequence-i/discuss/2645477/Easy-and-optimal-Solution
class Solution: def findLUSlength(self, a: str, b: str) -> int: return max(len(a),len(b)) if a!=b else -1
longest-uncommon-subsequence-i
Easy and optimal Solution
Raghunath_Reddy
0
6
longest uncommon subsequence i
521
0.603
Easy
9,203
https://leetcode.com/problems/longest-uncommon-subsequence-i/discuss/2495758/Python-simple-solution
class Solution: def findLUSlength(self, a: str, b: str) -> int: return -1 if a == b else max(len(a), len(b))
longest-uncommon-subsequence-i
Python simple solution
StikS32
0
23
longest uncommon subsequence i
521
0.603
Easy
9,204
https://leetcode.com/problems/longest-uncommon-subsequence-i/discuss/1617446/python3-1-line
class Solution: def findLUSlength(self, a: str, b: str) -> int: return -1 if a==b else max(len(a),len(b))
longest-uncommon-subsequence-i
python3 1 line
mikekaufman4
0
150
longest uncommon subsequence i
521
0.603
Easy
9,205
https://leetcode.com/problems/longest-uncommon-subsequence-i/discuss/1298880/Python3-dollarolution
class Solution: def findLUSlength(self, a: str, b: str) -> int: if a == b: return -1 else: if len(a) > len(b): return len(a) return len(b)
longest-uncommon-subsequence-i
Python3 $olution
AakRay
0
119
longest uncommon subsequence i
521
0.603
Easy
9,206
https://leetcode.com/problems/longest-uncommon-subsequence-i/discuss/870787/Python3-1-line
class Solution: def findLUSlength(self, a: str, b: str) -> int: return max(len(a), len(b)) if a != b else -1
longest-uncommon-subsequence-i
[Python3] 1-line
ye15
0
54
longest uncommon subsequence i
521
0.603
Easy
9,207
https://leetcode.com/problems/longest-uncommon-subsequence-i/discuss/481980/Python3%3A-simple-and-fastest
class Solution: def findLUSlength(self, a: str, b: str) -> int: if len(a) != len(b): return max(len(a), len(b)) return -1 if a == b else len(a)
longest-uncommon-subsequence-i
Python3: simple and fastest
andnik
0
208
longest uncommon subsequence i
521
0.603
Easy
9,208
https://leetcode.com/problems/longest-uncommon-subsequence-ii/discuss/380412/Solution-in-Python-3-(beats-~100)
class Solution: def findLUSlength(self, S: List[str]) -> int: C = collections.Counter(S) S = sorted(C.keys(), key = len, reverse = True) for i,s in enumerate(S): if C[s] != 1: continue b = True for j in range(i): I, c = -1, True for i in s: I = S[j].find(i,I+1)...
longest-uncommon-subsequence-ii
Solution in Python 3 (beats ~100%)
junaidmansuri
2
648
longest uncommon subsequence ii
522
0.404
Medium
9,209
https://leetcode.com/problems/longest-uncommon-subsequence-ii/discuss/831309/Python3-find-the-longest-unique-str-and-not-a-subseq-of-any-other-longer-one-LUS-II
class Solution: def findLUSlength(self, strs: List[str]) -> int: def isSubseq(a, b): j = 0 for i in range(len(b)): if a[j] == b[i]: j += 1 if j == len(a): return True return False c = ...
longest-uncommon-subsequence-ii
Python3 find the longest unique str and not a subseq of any other longer one - LUS II
r0bertz
1
439
longest uncommon subsequence ii
522
0.404
Medium
9,210
https://leetcode.com/problems/longest-uncommon-subsequence-ii/discuss/2768354/Python-straightforward-solution
class Solution: def findLUSlength(self, strs: List[str]) -> int: mem = dict() for s in strs: if len(s) not in mem: mem[len(s)] = list() mem[len(s)].append(s) mem = sorted(list(mem.items()), key=lambda x: x[0], reverse=True) def check(s1, s2):...
longest-uncommon-subsequence-ii
Python, straightforward solution
yiming999
0
7
longest uncommon subsequence ii
522
0.404
Medium
9,211
https://leetcode.com/problems/longest-uncommon-subsequence-ii/discuss/871246/Python3-check-for-subsequence
class Solution: def findLUSlength(self, strs: List[str]) -> int: def fn(p, s): """Return True if p is a subsequence of s.""" ss = iter(s) return all(ch in ss for ch in p) ans = -1 for i, s in enumerate(strs): for ii in range...
longest-uncommon-subsequence-ii
[Python3] check for subsequence
ye15
0
312
longest uncommon subsequence ii
522
0.404
Medium
9,212
https://leetcode.com/problems/continuous-subarray-sum/discuss/1582670/Python-Easy-Solution-or-Brute-Force-and-Optimal-Approach
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: # Brute Force: O(𝑛^2) - TLE count = 0 for i in range(len(nums)): sum = 0 for j in range(i, len(nums)): sum += nums[j] if sum % k == 0: return True return False class Solution: def checkSubarraySum(self, nums: L...
continuous-subarray-sum
Python Easy Solution | Brute Force and Optimal Approach
leet_satyam
5
603
continuous subarray sum
523
0.285
Medium
9,213
https://leetcode.com/problems/continuous-subarray-sum/discuss/1568794/Python-Simple-Solution-with-Detailed-Comments-(PrefixSum-Hashmap)
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: prefix_sums = defaultdict(lambda:float(inf)) #Key is prefix_sum%k, value is earliest occurence of the same prefix_sum running_sum = 0 for i, n in enumerate(nums): running_sum ...
continuous-subarray-sum
[Python] Simple Solution with Detailed Comments (PrefixSum Hashmap)
neurologicalstyle
5
861
continuous subarray sum
523
0.285
Medium
9,214
https://leetcode.com/problems/continuous-subarray-sum/discuss/2744854/Python-Simple-and-Easy-Way-to-Solve-or-99-Faster
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: lookup = {0:-1} curr_sum = 0 for i, n in enumerate(nums): if k != 0: curr_sum = (curr_sum + n) % k else: curr_sum += n if curr_sum not in ...
continuous-subarray-sum
✔️ Python Simple and Easy Way to Solve | 99% Faster 🔥
pniraj657
4
526
continuous subarray sum
523
0.285
Medium
9,215
https://leetcode.com/problems/continuous-subarray-sum/discuss/1562568/Simple-Python-solution-accumulative-mod
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: myDict={0:-1} #For edge cases where the first index is included in the solution ex: [2,4] k=3 total=0 for idx,n in enumerate(nums): total+=n if total%k not in m...
continuous-subarray-sum
Simple Python 🐍 solution accumulative mod
InjySarhan
3
428
continuous subarray sum
523
0.285
Medium
9,216
https://leetcode.com/problems/continuous-subarray-sum/discuss/2073306/Python-or-O(N)-solution-using-hashmap
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: # ////// TC O(N) ////// remainder = { 0 : -1} # mapping remainder with last index total = 0 for i,n in enumerate(nums): total += n rem = total % k if rem not in ...
continuous-subarray-sum
Python | O(N) solution using hashmap
__Asrar
2
603
continuous subarray sum
523
0.285
Medium
9,217
https://leetcode.com/problems/continuous-subarray-sum/discuss/1604929/Python-VERY-simple-o(n)-compute-o(min(k-n))-space
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: sumsVisitedSoFar = set([0]) curSum, prevSum = 0, 0 for i,n in enumerate(nums): curSum += n curSum = curSum % k if i!=0 and curSum in sumsVisitedSoFar: return True ...
continuous-subarray-sum
Python, VERY simple, o(n) compute, o(min(k, n)) space
ko082528
1
403
continuous subarray sum
523
0.285
Medium
9,218
https://leetcode.com/problems/continuous-subarray-sum/discuss/1233340/Timeout-Brute-Force-Recursive-Approach
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: n = len(nums) if n <= 1: return False s = nums[0] for i in range(1,n): s += nums[i] if s % k == 0: return True return self.checkSubarraySum(nums[1:], k)
continuous-subarray-sum
[Timeout] Brute Force | Recursive Approach
user6148Qk
1
180
continuous subarray sum
523
0.285
Medium
9,219
https://leetcode.com/problems/continuous-subarray-sum/discuss/870762/Python3-prefix-modulo
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: prefix = 0 # prefix modulo seen = {0: -1} for i, x in enumerate(nums): prefix += x if k: prefix %= k if prefix in seen and i - seen[prefix] >= 2: return True seen.s...
continuous-subarray-sum
[Python3] prefix modulo
ye15
1
304
continuous subarray sum
523
0.285
Medium
9,220
https://leetcode.com/problems/continuous-subarray-sum/discuss/2839794/A-follow-up-question-Find-the-maximal-subarray-sum-which-is-a-multiple-of-k
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: presum = 0 presum_list = [] d = dict() d[0] = -1 ans = -1 # default answer, return it if there is no subarray sum that is a multiple of k for i, n_i in enumerate(nums): presum +=...
continuous-subarray-sum
A follow up question -- Find the maximal subarray sum which is a multiple of k
MACHMichael
0
2
continuous subarray sum
523
0.285
Medium
9,221
https://leetcode.com/problems/continuous-subarray-sum/discuss/2808104/O(n)-solution-using-Python
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: d = {0:-1} _sum = 0 for i in range(len(nums)): _sum += nums[i] temp = _sum%k if temp in d: if i-d[temp] >= 2: return True else: ...
continuous-subarray-sum
O(n) solution using Python
dushyantRathore
0
6
continuous subarray sum
523
0.285
Medium
9,222
https://leetcode.com/problems/continuous-subarray-sum/discuss/2807117/python-hashtable-prefix-sum
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: # Time complexity: O(nums.length). # We perform O(nums.length) operations with a hash map, each taking O(1) time on average. # Space complexity: O(min⁡{nums.length,k}). # The size of a hash map does not exc...
continuous-subarray-sum
python hashtable, prefix sum
sahilkumar158
0
4
continuous subarray sum
523
0.285
Medium
9,223
https://leetcode.com/problems/continuous-subarray-sum/discuss/2800630/Python-official-solution
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: # initialize the hash map with index 0 for sum 0 hash_map = {0: 0} s = 0 for i in range(len(nums)): s += nums[i] # if the remainder s % k occurs for the first time if s % ...
continuous-subarray-sum
Python official solution
subidit
0
4
continuous subarray sum
523
0.285
Medium
9,224
https://leetcode.com/problems/continuous-subarray-sum/discuss/2786672/Sub-array-sum-easy-understanding
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: #take a hash Map which stores the prefix sum of the remainder with their index #suppose if we get the same same remainder, then we got a sub array in the middele which is divisible by k we can return true #one more ...
continuous-subarray-sum
Sub array sum-easy understanding
RaviShanker__Thadishetti
0
6
continuous subarray sum
523
0.285
Medium
9,225
https://leetcode.com/problems/continuous-subarray-sum/discuss/2752674/Python-Optimal-Solution-w-ONE-BRANCH-only
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: s, sum_loc_dict = 0, {0: -1} for i, n in enumerate(nums): s = (s + n) % k if (pre_i := sum_loc_dict.get(s, i)) < i - 1: return True sum_loc_dict[s] = sum_loc_dict.get(s, p...
continuous-subarray-sum
Python Optimal Solution w/ ONE BRANCH only
bylin
0
4
continuous subarray sum
523
0.285
Medium
9,226
https://leetcode.com/problems/continuous-subarray-sum/discuss/2752203/Python-or-Optimized-Solution-or
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: n = len(nums) # initialize hash map with remainder 0 and 0 elements.. hash_map = {0:0} s = 0 for i in range(n): s += nums[i] # check if remainder not exist in hash map.. ...
continuous-subarray-sum
Python | Optimized Solution |
quarnstric_
0
2
continuous subarray sum
523
0.285
Medium
9,227
https://leetcode.com/problems/continuous-subarray-sum/discuss/2747076/Modular-Arithmetic-Dictionary-or-Python
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: if len(nums)<2: return False dic = {0:-1} total = 0 for ind, val in enumerate(nums): total = (total + val)%k if total in dic: if ind - dic[t...
continuous-subarray-sum
Modular Arithmetic, Dictionary | Python
Abhi_-_-
0
2
continuous subarray sum
523
0.285
Medium
9,228
https://leetcode.com/problems/continuous-subarray-sum/discuss/2746862/Prefix-Sum-Approach-Fast-and-Easy-Solution
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: hashmap = {0 : -1} prefix_sum = 0 for ptr, val in enumerate(nums): prefix_sum += val remainder = prefix_sum % k if remainder not in hashmap: hashmap[remainder] = p...
continuous-subarray-sum
Prefix Sum Approach - Fast and Easy Solution
user6770yv
0
9
continuous subarray sum
523
0.285
Medium
9,229
https://leetcode.com/problems/continuous-subarray-sum/discuss/2746382/Python-Solution-orT%3A-O(n)-orS%3AO(n)
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: remainder = {0:-1} total = 0 for i, n in enumerate(nums): total+=n rem= total%k if rem not in remainder: remainder[rem] = i elif ...
continuous-subarray-sum
Python Solution |T: O(n) |S:O(n)
pradeep288
0
8
continuous subarray sum
523
0.285
Medium
9,230
https://leetcode.com/problems/continuous-subarray-sum/discuss/2746370/Python!-8-Line-solution.-O(N)
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: presum = defaultdict(int) for i, a in enumerate(nums): presum[i] = (presum[i-1] + a) % k presum[-1] = 0 indices = defaultdict(list) for i, a in presum.items(): indi...
continuous-subarray-sum
😎Python! 8 Line solution. O(N)
aminjun
0
20
continuous subarray sum
523
0.285
Medium
9,231
https://leetcode.com/problems/continuous-subarray-sum/discuss/2746263/Modulus-beating-96-in-time-and-98-in-space
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: st = set() n = len(nums) cumsum = 0 # create a prev cumsum to enhance a lag prev_cumsum = 0 for i in range(n): if i == 0: cumsum += nums[i] cumsum %= k ...
continuous-subarray-sum
Modulus - beating 96% in time and 98% in space
leonine9
0
6
continuous subarray sum
523
0.285
Medium
9,232
https://leetcode.com/problems/continuous-subarray-sum/discuss/2746137/python-easy-solution-with-good-runtime
class Solution(): def checkSubarraySum(self, nums, k): dic = {0:-1} summ = 0 for i, n in enumerate(nums): if k != 0: summ = (summ + n) % k else: summ += n if summ not in dic: dic[summ] = i ...
continuous-subarray-sum
python easy solution with good runtime
V_Bhavani_Prasad
0
8
continuous subarray sum
523
0.285
Medium
9,233
https://leetcode.com/problems/continuous-subarray-sum/discuss/2745998/Python-or-HashMap-(storing-remainder-and-the-index)
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: remainderDict = {0:-1} total = 0 for i, n in enumerate(nums): total += n remainder = total % k if remainder not in remainderDict: remainderDict[remainder] = i ...
continuous-subarray-sum
Python | HashMap (storing remainder and the index)
KevinJM17
0
9
continuous subarray sum
523
0.285
Medium
9,234
https://leetcode.com/problems/continuous-subarray-sum/discuss/2745951/PYTHON3-Well-explained-Dict-based-solution
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: # here, we store first indices for sequences that fulfill condition; # remainder 0 (when prefix sum equals k) is the only # one that is allowed to occur just once, thus, we add # it to the map w...
continuous-subarray-sum
🧙🏻‍♂️ PYTHON3 Well explained Dict based solution
karinadelcheva
0
6
continuous subarray sum
523
0.285
Medium
9,235
https://leetcode.com/problems/continuous-subarray-sum/discuss/2745890/Fast-Python-Solution-(Hashmap)-Time%3A-O(N)-Space%3A-O(K)
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: # sum_nums = sum(nums) # if nums == [23,2,6,4,7] and k ==13: # return False # else: # if sum_nums %k ==0: # for i in nums: # for j in n...
continuous-subarray-sum
Fast Python Solution (Hashmap) ; Time: O(N), Space: O(K)
avs-abhishek123
0
9
continuous subarray sum
523
0.285
Medium
9,236
https://leetcode.com/problems/continuous-subarray-sum/discuss/2745887/Python-Solution-or-95-Faster-or-Prefix-Running-Sum-DIVMOD-Based
class Solution: # @lru_cache(None) def checkSubarraySum(self, nums: List[int], k: int) -> bool: runningSum = 0 store = { runningSum: -1 } for idx, num in enumerate(nums): runningSum += num runningSum %= k ...
continuous-subarray-sum
Python Solution | 95% Faster | Prefix Running Sum DIVMOD Based
Gautam_ProMax
0
8
continuous subarray sum
523
0.285
Medium
9,237
https://leetcode.com/problems/continuous-subarray-sum/discuss/2745884/Python-solution
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: remainder = {} for idx, num in enumerate(nums): if idx > 0: nums[idx] += nums[idx-1] num = nums[idx] if num%k == 0 and idx >= 1: return True if...
continuous-subarray-sum
Python solution
tesfish
0
5
continuous subarray sum
523
0.285
Medium
9,238
https://leetcode.com/problems/continuous-subarray-sum/discuss/2745773/Python.-Simple-solution-using-dictionary.-O(n)
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: s, mem = 0, {0: -1} #key - residue, value - index of the first instance for i, n in enumerate(nums): m = (n + s) % k #if you find current residue in the dictionary #and the distance betw...
continuous-subarray-sum
Python. Simple solution using dictionary. O(n)
nonchalant-enthusiast
0
3
continuous subarray sum
523
0.285
Medium
9,239
https://leetcode.com/problems/continuous-subarray-sum/discuss/2745476/Python-or-Simple-prefix-sum-solution
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: div_rem = defaultdict(list) carry = 0 prefix = [0]*len(nums) for i in range(len(nums)): carry += nums[i] prefix[i] = carry div_rem[carry % k].append(i) # Simple ca...
continuous-subarray-sum
Python | Simple prefix sum solution
LordVader1
0
29
continuous subarray sum
523
0.285
Medium
9,240
https://leetcode.com/problems/continuous-subarray-sum/discuss/2745072/Python-or-O(k)-space-or-Pigeon-Hole-Principle-or-Hashmap-or-Prefix-Sum
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: n = len(nums) # making prefix of the array % k nums[0] %= k for i in range(1, n): nums[i] = (nums[i - 1] + nums[i]) % k # hashmap to store all the indices of a particular number (0 to k -...
continuous-subarray-sum
Python | O(k) space | Pigeon Hole Principle | Hashmap | Prefix Sum
deepaklaksman
0
28
continuous subarray sum
523
0.285
Medium
9,241
https://leetcode.com/problems/continuous-subarray-sum/discuss/2744617/Help-me-with-the-time-Python3-solution.
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: d , s = {0:-1} , 0 for i, n in enumerate(nums): if k != 0: s = (s + n) % k else: s += n if s not in d: d[s] = i else: ...
continuous-subarray-sum
Help me with the time Python3 solution.
rupamkarmakarcr7
0
7
continuous subarray sum
523
0.285
Medium
9,242
https://leetcode.com/problems/continuous-subarray-sum/discuss/2744489/Solution-Using-Python3
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: dic = {0:-1} n = len(nums) prefix = 0 for i in range(n): prefix = (prefix + nums[i])%k if prefix not in dic: dic[prefix] = i else: if (i - ...
continuous-subarray-sum
Solution Using Python3
dnvavinash
0
15
continuous subarray sum
523
0.285
Medium
9,243
https://leetcode.com/problems/continuous-subarray-sum/discuss/2744479/Python-O(n)-solution-faster
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: rem={0:-1} total=0 for i,j in enumerate(nums): total+=j r=total%k if( r not in rem): rem[r]=i elif(i-rem[r]>1): return True re...
continuous-subarray-sum
Python O(n) solution faster
Raghunath_Reddy
0
21
continuous subarray sum
523
0.285
Medium
9,244
https://leetcode.com/problems/continuous-subarray-sum/discuss/2744408/Python3-solution-O(n)-space-and-time
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: modArray = {0:-1} s = 0 for i in range(len(nums)): s += nums[i] if s%k in modArray: if i - modArray[s%k] >= 2: return True else: ...
continuous-subarray-sum
Python3 solution O(n) space and time
slavaheroes
0
13
continuous subarray sum
523
0.285
Medium
9,245
https://leetcode.com/problems/continuous-subarray-sum/discuss/2744264/Super-Simple-Python-Solution!-O(n)-Space-and-Time!
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: seen, prev, curr = set(), 0, 0 for n in nums: prev, curr = curr, (curr + n) % k if curr in seen: return True seen.add(prev) return False
continuous-subarray-sum
🔥 🐍 ✅ Super Simple Python Solution! O(n) Space and Time!
Cavalier_Poet
0
14
continuous subarray sum
523
0.285
Medium
9,246
https://leetcode.com/problems/continuous-subarray-sum/discuss/2665749/Solution-in-python-using-dictionary
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: #prefix sum for i in range(1,len(nums)): nums[i]=nums[i]+nums[i-1] #finding remainder remainder=[i%k for i in nums] #value with sum zero has index -1 d={0:-1} ...
continuous-subarray-sum
Solution in python using dictionary
harshmishra0014
0
6
continuous subarray sum
523
0.285
Medium
9,247
https://leetcode.com/problems/continuous-subarray-sum/discuss/2664312/Python3-Solution-oror-O(N)-Time-and-Space-Complexity
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: prefixSum=0 dic={} count=0 for i in range(len(nums)): prefixSum+=nums[i] rem=prefixSum%k if rem not in dic: dic[rem]=0 if rem==0: ...
continuous-subarray-sum
Python3 Solution || O(N) Time & Space Complexity
akshatkhanna37
0
15
continuous subarray sum
523
0.285
Medium
9,248
https://leetcode.com/problems/continuous-subarray-sum/discuss/2576940/Python-runtime-O(n)-memory-O(min(nk))
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: d = {0:-1} s = 0 for i in range(len(nums)): s += nums[i] if s%k not in d: d[s%k] = i elif s%k in d and d[s%k] < i-1: return True return Fal...
continuous-subarray-sum
Python, runtime O(n), memory O(min(n,k))
tsai00150
0
91
continuous subarray sum
523
0.285
Medium
9,249
https://leetcode.com/problems/continuous-subarray-sum/discuss/1743652/WEEB-DOES-PYTHONC%2B%2B-PREFIX-SUM
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: dp = defaultdict(int) dp[0] = -1 # key : remainder, value : leftmost idx curSum = 0 for i in range(len(nums)): curSum += nums[i] if curSum % k in dp and i - dp[curSum % k] >= 2: return True if curSum % k not in dp: ...
continuous-subarray-sum
WEEB DOES PYTHON/C++ PREFIX SUM
Skywalker5423
0
222
continuous subarray sum
523
0.285
Medium
9,250
https://leetcode.com/problems/continuous-subarray-sum/discuss/765441/Python3%3A-Brute-solution-quadratic-time-complexity
class Solution: def checkSubarraySum(self, nums: List[int], k: int) -> bool: k = abs(k) required_sum = 0 for outer_idx, i in enumerate(nums[0:-1]): local_sum = i for inner_idx, j in enumerate(nums[outer_idx+1:]): local_sum+=j if k>0 and...
continuous-subarray-sum
Python3: Brute solution, quadratic time complexity
bhattacharyya_d
0
149
continuous subarray sum
523
0.285
Medium
9,251
https://leetcode.com/problems/continuous-subarray-sum/discuss/511295/Python3-hash-table-O(n)
class Solution: def checkSubarraySum(self, nums, k): cum_sum_mod = [] cur_sum = 0 for i in range(len(nums)): cur_sum = cur_sum + nums[i] cum_sum_mod.append(cur_sum % k if k != 0 else cur_sum) hash_table = {0:-1} for i in range(len(cum_sum_mod)): ...
continuous-subarray-sum
Python3, hash table, O(n)
complexfilter
0
341
continuous subarray sum
523
0.285
Medium
9,252
https://leetcode.com/problems/continuous-subarray-sum/discuss/389183/Solution-in-Python-3-(beats-~99)-(Using-Set)-(No-Dictionary)
class Solution: def checkSubarraySum(self, N: List[int], K: int) -> bool: L, S, s = len(N), set([0]), 0 for i in range(L-1): if (N[i]+N[i+1]) == 0 or (K and not (N[i]+N[i+1]) % K): return True if K == 0: return False for n in N: a, s = s, (s + n) % K if s in S and a != s: retur...
continuous-subarray-sum
Solution in Python 3 (beats ~99%) (Using Set) (No Dictionary)
junaidmansuri
-7
949
continuous subarray sum
523
0.285
Medium
9,253
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/1077760/Python.-very-clear-and-simplistic-solution.
class Solution: def findLongestWord(self, s: str, d: List[str]) -> str: def is_subseq(main: str, sub: str) -> bool: i, j, m, n = 0, 0, len(main), len(sub) while i < m and j < n and n - j >= m - i: if main[i] == sub[j]: i += 1 j += 1...
longest-word-in-dictionary-through-deleting
Python. very clear and simplistic solution.
m-d-f
6
879
longest word in dictionary through deleting
524
0.512
Medium
9,254
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/871111/Python3-scan
class Solution: def findLongestWord(self, s: str, d: List[str]) -> str: def fn(word): """Return True if word matches a subsequence of s.""" ss = iter(s) return all(c in ss for c in word) return next((w for w in sorted(d, key=lambda x: (-len(x), ...
longest-word-in-dictionary-through-deleting
[Python3] scan
ye15
4
199
longest word in dictionary through deleting
524
0.512
Medium
9,255
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/871111/Python3-scan
class Solution: def findLongestWord(self, s: str, d: List[str]) -> str: def fn(word): """Return True if word matches a subsequence of s.""" ss = iter(s) return all(c in ss for c in word) ans = "" for w in d: if fn(w) and (le...
longest-word-in-dictionary-through-deleting
[Python3] scan
ye15
4
199
longest word in dictionary through deleting
524
0.512
Medium
9,256
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/871111/Python3-scan
class Solution: def findLongestWord(self, s: str, d: List[str]) -> str: def fn(word): """Return True if word is a subsequence of s.""" i = 0 for c in word: while i < len(s) and s[i] != c: i += 1 if i == len(s): return False ...
longest-word-in-dictionary-through-deleting
[Python3] scan
ye15
4
199
longest word in dictionary through deleting
524
0.512
Medium
9,257
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/871111/Python3-scan
class Solution: def findLongestWord(self, s: str, d: List[str]) -> str: def fn(word): """Return True if word is a subsequence of s.""" i = 0 for c in s: if i < len(word) and word[i] == c: i += 1 return i == len(word) ...
longest-word-in-dictionary-through-deleting
[Python3] scan
ye15
4
199
longest word in dictionary through deleting
524
0.512
Medium
9,258
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/871111/Python3-scan
class Solution: def findLongestWord(self, s: str, d: List[str]) -> str: ans = "" for word in d: ss = iter(s) if all(c in ss for c in word) and (len(word) > len(ans) or len(word) == len(ans) and word < ans): ans = word return ans
longest-word-in-dictionary-through-deleting
[Python3] scan
ye15
4
199
longest word in dictionary through deleting
524
0.512
Medium
9,259
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/2607247/Python-simple-straight-forward-solution
class Solution: def findLongestWord(self, s: str, dictionary: list[str]) -> str: def is_valid(string): # can we get the string by deleting some letters or not i = 0 for letter in s: if letter == string[i]: i += 1 if i == len(string...
longest-word-in-dictionary-through-deleting
Python simple straight-forward solution
Mark_computer
2
118
longest word in dictionary through deleting
524
0.512
Medium
9,260
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/2087464/Clean-commented-Python-Faster-than-99.5-hashmap-one-pass
class Solution: def findLongestWord(self, s: str, dictionary: List[str]) -> str: ans = "" # store for each starting letter the potential word. We don't store the word directly, but its index in the dictionary. # In addition we store '0' as pointer to the starting character. We will...
longest-word-in-dictionary-through-deleting
Clean, commented Python, Faster than 99.5%, hashmap, one-pass
boris17
1
44
longest word in dictionary through deleting
524
0.512
Medium
9,261
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/1705859/Python-simple-solution-using-hashmap-and-sorting-and-two-pointers
class Solution: def findLongestWord(self, s: str, dictionary: List[str]) -> str: def compare(str1, str2): m, n = len(str1), len(str2) i, j = 0, 0 while i < m and j < n: if j == n-1 and str1[i] == str2[j]: return True ...
longest-word-in-dictionary-through-deleting
Python simple solution using hashmap and sorting and two-pointers
byuns9334
1
153
longest word in dictionary through deleting
524
0.512
Medium
9,262
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/1078966/Very-simple-Python-solution-O(mn)
class Solution: def findLongestWord(self, s: str, d: List[str]) -> str: res="" maxLen=0 for word in d: pW=0 pS=0 while pW<len(word) and pS<len(s): if word[pW]==s[pS]: pW+=1 pS+=1 ...
longest-word-in-dictionary-through-deleting
Very simple Python 🐍 solution O(mn)
InjySarhan
1
162
longest word in dictionary through deleting
524
0.512
Medium
9,263
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/639917/Python-easy-to-understand-solution-with-isSubsequence()
class Solution: def isSubsequence(self, s: str, t: str) -> bool: index = 0 for i in range(0, len(t)): if index == len(s): return True if s[index] == t[i]: index += 1 return index == len(s) def findLongestWord(self, s: str, d: L...
longest-word-in-dictionary-through-deleting
Python easy to understand solution with isSubsequence()
Ningmin
1
251
longest word in dictionary through deleting
524
0.512
Medium
9,264
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/639917/Python-easy-to-understand-solution-with-isSubsequence()
class Solution: def isSubsequence(self, s: str, t: str) -> bool: index = 0 for i in range(0, len(t)): if index == len(s): return True if s[index] == t[i]: index += 1 return index == len(s) def findLongestWord(self, s: str, d: L...
longest-word-in-dictionary-through-deleting
Python easy to understand solution with isSubsequence()
Ningmin
1
251
longest word in dictionary through deleting
524
0.512
Medium
9,265
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/2779884/easy-python-solution
class Solution: def checkPresence(self, word, target) : index = 0 for i in range(len(target)) : if target[i] == word[index] : index += 1 if index == len(word) : return True return len(word) == index ...
longest-word-in-dictionary-through-deleting
easy python solution
sghorai
0
4
longest word in dictionary through deleting
524
0.512
Medium
9,266
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/2715642/Python-O(NM)-O(M)
class Solution: def findLongestWord(self, s: str, dictionary: List[str]) -> str: pointers = [0] * len(dictionary) res = "" for char in s: for i in range(len(pointers)): idx, word = pointers[i], dictionary[i] if idx == len(word): ...
longest-word-in-dictionary-through-deleting
Python - O(NM), O(M)
Teecha13
0
5
longest word in dictionary through deleting
524
0.512
Medium
9,267
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/2484070/Python3-or-Sorting
class Solution: def isSubsequence(self,w,s): p1,p2=0,0 while p1<len(w) and p2<len(s): if w[p1]==s[p2]: p1+=1 p2+=1 return p1==len(w) def findLongestWord(self, s: str, dictionary: List[str]) -> str: ans='' dictionary.sort() f...
longest-word-in-dictionary-through-deleting
[Python3] | Sorting
swapnilsingh421
0
19
longest word in dictionary through deleting
524
0.512
Medium
9,268
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/2091793/Python-or-Sorting-and-Checking-isSubsequence-approach
class Solution: def isSubsequence(self,s,word): if len(s) < len(word): return False i,j = 0,0 while i < len(s) and j < len(word): if s[i] == word[j]: i += 1 j += 1 else: i += 1 return len(word) == j ...
longest-word-in-dictionary-through-deleting
Python | Sorting and Checking isSubsequence approach
__Asrar
0
47
longest word in dictionary through deleting
524
0.512
Medium
9,269
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/1078290/python-ez-understanding-solution
class Solution: def check_sub(self,target,s): ''' helper function that determine whether the target word is a candidate or not ''' if len(target) > len(s): return False i,j = 0,0 while i < len(target) and j < len(s): if target[i] == s[j]: ...
longest-word-in-dictionary-through-deleting
python ez understanding solution
yingziqing123
0
72
longest word in dictionary through deleting
524
0.512
Medium
9,270
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/1077970/Brute-force-easy-to-understand-Python-solution
class Solution: def findLongestWord(self, s: str, d: List[str]) -> str: le=len(s) res="" for word in d: n=len(word) i,j=0,0 count=0 while(i<n and j<le): if s[j]==word[i]: count+=1 i+=1 ...
longest-word-in-dictionary-through-deleting
Brute force easy to understand Python solution
_Rehan12
0
44
longest word in dictionary through deleting
524
0.512
Medium
9,271
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/1077633/Python-or-Explained-and-Easy-and-Fast-or-Beats-90
class Solution: def findLongestWord(self, s: str, d: List[str]) -> str: def check(s: str, d: str) -> bool: it = iter(s) return all(l in it for l in d) m = "" for i in d: if check(s, i) and (len(m) < len(i) or (len(m) == len(i) and m > i)): ...
longest-word-in-dictionary-through-deleting
Python | Explained & Easy & Fast | Beats 90%
SlavaHerasymov
0
119
longest word in dictionary through deleting
524
0.512
Medium
9,272
https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/discuss/715631/Python3-check-isSubsequence-using-str.find-Longest-Word-in-Dictionary-through-Deleting
class Solution: def findLongestWord(self, s: str, d: List[str]) -> str: def isSubsequence(a: str, b: str) -> bool: i = 0 for c in a: if (i := b.find(c, i)) == -1: return False i += 1 return True heap = [...
longest-word-in-dictionary-through-deleting
Python3 check isSubsequence using str.find - Longest Word in Dictionary through Deleting
r0bertz
0
244
longest word in dictionary through deleting
524
0.512
Medium
9,273
https://leetcode.com/problems/contiguous-array/discuss/577489/Python-O(n)-by-partial-sum-and-dictionary.-90%2B-w-Visualization
class Solution: def findMaxLength(self, nums: List[int]) -> int: partial_sum = 0 # table is a dictionary # key : partial sum value # value : the left-most index who has the partial sum value table = { 0: -1} max_length = 0 for idx, number in enume...
contiguous-array
Python O(n) by partial sum and dictionary. 90%+ [w/ Visualization]
brianchiang_tw
12
1,700
contiguous array
525
0.468
Medium
9,274
https://leetcode.com/problems/contiguous-array/discuss/1743700/Python-3-(700ms)-or-O(n)-Dictionary-HashMap-Solution-or-One-Pass-Easy-to-Understand
class Solution: def findMaxLength(self, nums: List[int]) -> int: m,c=0,0 d={0:-1} for i in range(len(nums)): if nums[i]==0: c-=1 else: c+=1 if c in d: m=max(m,i-d[c]) else: d[c]=i ...
contiguous-array
Python 3 (700ms) | O(n) Dictionary HashMap Solution | One-Pass Easy to Understand
MrShobhit
4
375
contiguous array
525
0.468
Medium
9,275
https://leetcode.com/problems/contiguous-array/discuss/1951726/Python-solution-with-comment
class Solution: def findMaxLength(self, nums: List[int]) -> int: dic = {0:-1} # dic record: count(the times we met 1 - the time we met 0) : idx count = 0 # lets say count == 3, it means until this idx, if 0 appeared x time, 1 appeared ...
contiguous-array
Python solution with comment
byroncharly3
2
136
contiguous array
525
0.468
Medium
9,276
https://leetcode.com/problems/contiguous-array/discuss/1760153/WEEB-DOES-PYTHONC%2B%2B-PREFIX-SUM
class Solution: def findMaxLength(self, nums: List[int]) -> int: dp = defaultdict(int) dp[0] = -1 curSum = 0 result = 0 for i in range(len(nums)): if nums[i] == 1: curSum += 1 else: curSum -= 1 if curSum not in dp: # store leftmost idx for longest subarray dp[curSum] = i else: ...
contiguous-array
WEEB DOES PYTHON/C++ PREFIX SUM
Skywalker5423
2
93
contiguous array
525
0.468
Medium
9,277
https://leetcode.com/problems/contiguous-array/discuss/577773/Python3-using-prefix-sum
class Solution: def findMaxLength(self, nums: List[int]) -> int: seen = {0: -1} ans = prefix = 0 for i, x in enumerate(nums): prefix += 2*x-1 ans = max(ans, i - seen.setdefault(prefix, i)) return ans
contiguous-array
[Python3] using prefix sum
ye15
2
254
contiguous array
525
0.468
Medium
9,278
https://leetcode.com/problems/contiguous-array/discuss/1634324/Python-oror-Easy-Solution-oror-beat~99.6
class Solution: def findMaxLength(self, nums: List[int]) -> int: count, maxi = 0, 0 d = {0: -1} for i, j in enumerate(nums): if j == 0: count += -1 else: count += 1 try: temp = i - d[count] if maxi < temp: maxi = temp except: d[count] = i return maxi
contiguous-array
Python || Easy Solution || beat~99.6%
naveenrathore
1
185
contiguous array
525
0.468
Medium
9,279
https://leetcode.com/problems/contiguous-array/discuss/1450366/Python-3-Solution-Beats-100-Of-Python-Submissions
class Solution: def findMaxLength(self, nums: List[int]) -> int: n=len(nums) d={} best=0 summ=0 for i in range(n): summ+=1 if nums[i]==1 else -1 if summ==0: best=i+1 continue if summ in d: if ...
contiguous-array
Python 3 Solution Beats 100% Of Python Submissions
reaper_27
1
133
contiguous array
525
0.468
Medium
9,280
https://leetcode.com/problems/contiguous-array/discuss/2686576/Python3-O(n)-time
class Solution: def findMaxLength(self, nums: List[int]) -> int: count = [[0, 0]] c0, c1 = 0, 0 for i in nums: if i == 0: c0 += 1 else: c1 += 1 count.append([c0, c1]) d = {} res = 0 ...
contiguous-array
Python3 O(n) time
ddawngbof
0
2
contiguous array
525
0.468
Medium
9,281
https://leetcode.com/problems/contiguous-array/discuss/2408804/Fully-Detailed-Python-Explanation-for-Beginners-or-faster-89.16-or-Easy-to-understand
class Solution: def findMaxLength(self, nums: List[int]) -> int: """ Main ideas: 1. Let "count" be the sum (of list elements) with inital value of 0 2. Add 1 to "count" when we encounter 1 in list; subtract 1 from "count" when we encounter 0 in list 3. If we get the ...
contiguous-array
Fully Detailed Python Explanation for Beginners | faster 89.16% | Easy to understand
harishmanjunatheswaran
0
40
contiguous array
525
0.468
Medium
9,282
https://leetcode.com/problems/contiguous-array/discuss/2206787/Simple-and-Easy-Approach-oror-Hashmap
class Solution: def findMaxLength(self, nums: List[int]) -> int: count = 0 n = len(nums) maxLen = 0 hashMap = {} for i in range(n): if nums[i] == 0: count -= 1 else: count += 1 if count == 0: ...
contiguous-array
Simple and Easy Approach || Hashmap
Vaibhav7860
0
74
contiguous array
525
0.468
Medium
9,283
https://leetcode.com/problems/contiguous-array/discuss/1818386/Python-easy-to-read-and-understand
class Solution: def findMaxLength(self, nums: List[int]) -> int: sums = 0 ans = 0 d = {0: -1} for i in range(len(nums)): sums += -1 if nums[i] == 0 else 1 if sums in d: ans = max(ans, i-d[sums]) else: d[sums] = i ...
contiguous-array
Python easy to read and understand
sanial2001
0
163
contiguous array
525
0.468
Medium
9,284
https://leetcode.com/problems/contiguous-array/discuss/1776518/Python3-linear-time-solution-prefixsum
class Solution: def findMaxLength(self, nums: List[int]) -> int: #replace the 0s with -1s, so that sum of equal no. of 0s and 1s becomes zero for i in range(len(nums)): if nums[i] == 0: nums[i] = -1 prefixSumNums = [0]*len(nums) my_dict =...
contiguous-array
Python3 - linear time solution - prefixsum
sourav-saha
0
58
contiguous array
525
0.468
Medium
9,285
https://leetcode.com/problems/contiguous-array/discuss/1755027/Python-Easy-and-clean-solution-O(n)-(beats-93.61)
class Solution: def findMaxLength(self, nums: List[int]) -> int: n = len(nums) d = dict() d[0] = -1 pre_sum, ans = 0, 0 for i in range(n): pre_sum += 1 if nums[i] else -1 if pre_sum in d: ans = max(ans, i - d[pre_sum]) ...
contiguous-array
[Python] Easy and clean solution O(n) (beats 93.61%)
nomofika
0
154
contiguous array
525
0.468
Medium
9,286
https://leetcode.com/problems/contiguous-array/discuss/1743983/Easiest-Python-Solution-Using-Dictionary
class Solution: def findMaxLength(self, nums: List[int]) -> int: d={} maxlen=0 prefix_sum=0 n=len(nums) for i in range(n): prefix_sum = prefix_sum-1 if nums[i]==0 else prefix_sum+1 if prefix_sum==0: maxlen=max(maxlen,i+1) el...
contiguous-array
📍Easiest Python Solution Using Dictionary
AdityaTrivedi88
0
16
contiguous array
525
0.468
Medium
9,287
https://leetcode.com/problems/contiguous-array/discuss/1743669/Python3-Hashmap
class Solution: def findMaxLength(self, nums: List[int]) -> int: dic = {0 : -1} accumulate, res = 0, 0 for i,num in enumerate(nums): accumulate += 1 if num == 1 else -1 if accumulate in dic: res = max(res, i - dic[accumulate]) else: ...
contiguous-array
[Python3] Hashmap
Rainyforest
0
12
contiguous array
525
0.468
Medium
9,288
https://leetcode.com/problems/contiguous-array/discuss/1743521/Python-Solutionor-T%3AO(n)orS%3AO(n)orHashmap
class Solution: def findMaxLength(self, nums: List[int]) -> int: max_len, sum = 0, 0 hash_map = {} for i in range(len(nums)): if nums[i] == 0: sum -= 1 else: sum += 1 if sum == 0: max_len = i + 1 ...
contiguous-array
Python Solution| T:O(n)|S:O(n)|Hashmap
pradeep288
0
40
contiguous array
525
0.468
Medium
9,289
https://leetcode.com/problems/contiguous-array/discuss/1743453/Python-Simple-and-Clean-Python-Solution-Using-Dictionary
class Solution: def findMaxLength(self, nums: List[int]) -> int: count=0 max_len=0 d={} d[0]=-1 for i in range(len(nums)): if nums[i]==1: count = count + 1 else: count = count - 1 if count not in d: d[count]=i else: max_len=max(max_len,i-d[count]) return ma...
contiguous-array
[ Python ]✔✔ Simple and Clean Python Solution Using Dictionary 🔥✌🔥
ASHOK_KUMAR_MEGHVANSHI
0
85
contiguous array
525
0.468
Medium
9,290
https://leetcode.com/problems/contiguous-array/discuss/1743269/525.-Contiguous-Array
class Solution: def findMaxLength(self, nums: List[int]) -> int: lookup = {0: -1} length = 0 val = 0 for i, num in enumerate(nums): if num == 1: val += 1 else: val -= 1 if val in lookup: length = ma...
contiguous-array
525. Contiguous Array
prasunbhunia
0
59
contiguous array
525
0.468
Medium
9,291
https://leetcode.com/problems/contiguous-array/discuss/1622406/Build-Logic-step-by-step-greater-Brute-Force-to-Efficient-Solution
class Solution: def findMaxLength(self, nums: List[int]) -> int: #Brute Force Solution --- Throws Time Limit Exceeded #What the code is doing : #First of all, we will fix ith element and then traverse from ith element till the last element #Using two separate variables, o...
contiguous-array
Build Logic step by step --> Brute Force to Efficient Solution
aarushsharmaa
0
72
contiguous array
525
0.468
Medium
9,292
https://leetcode.com/problems/contiguous-array/discuss/1582745/Python-Easy-Solution-or-O(n)-Approach
class Solution: def findMaxLength(self, nums: List[int]) -> int: hashMap = {0: -1} add = 0 maxLen = 0 for i in range(len(nums)): if nums[i] == 0: add += -1 else: add += 1 if add in hashMap: maxLen = max(maxLen, i-hashMap[add]) else: hashMap[add] = i return maxLen
contiguous-array
Python Easy Solution | O(n) Approach
leet_satyam
0
119
contiguous array
525
0.468
Medium
9,293
https://leetcode.com/problems/contiguous-array/discuss/1070206/Easy-to-understand-Python3-solution.
class Solution: def findMaxLength(self, nums: List[int]) -> int: counts = {0: [-1]} count = 0 length = 0 for i in range(len(nums)): if nums[i] == 0: count -= 1 else: ...
contiguous-array
Easy to understand Python3 solution.
RuslanKalashnikov
0
178
contiguous array
525
0.468
Medium
9,294
https://leetcode.com/problems/contiguous-array/discuss/653476/Python-O(n)-7-lines
class Solution: def findMaxLength(self, nums: List[int]) -> int: level, max_value, d = 0, 0, {0: -1} for i, n in enumerate(nums): level += (-1, 1)[n] max_value = max(max_value, i - d.setdefault(level, i)) return max_value
contiguous-array
Python O(n), 7 lines
dliamuziki2017
0
126
contiguous array
525
0.468
Medium
9,295
https://leetcode.com/problems/contiguous-array/discuss/577533/Python3-very-detailed-explanation-easy-to-understand-contrary-to-all-other-posts-here
class Solution: def findMaxLength(self, nums: List[int]) -> int: acc = 0 # Stores the value for the current position in the nums # Why we have diff_dict? # we need to know how many elements before we had the difference value matching the current value at question # Example: ...
contiguous-array
Python3 very detailed explanation, easy to understand, contrary to all other posts here
ozyinc
0
134
contiguous array
525
0.468
Medium
9,296
https://leetcode.com/problems/beautiful-arrangement/discuss/1094146/Python-Backtracking-the-more-intuitive-way
class Solution: def countArrangement(self, n: int) -> int: self.count = 0 self.backtrack(n, 1, []) return self.count def backtrack(self, N, idx, temp): if len(temp) == N: self.count += 1 return for i in range(1, N+1): ...
beautiful-arrangement
Python Backtracking, the more intuitive way
IamCookie
8
872
beautiful arrangement
526
0.646
Medium
9,297
https://leetcode.com/problems/beautiful-arrangement/discuss/1908252/python-permutation-solution
class Solution: def countArrangement(self, n: int) -> int: ans=[] def permute(l,p,index): if l==[]: ans.append(p) for i,v in enumerate(l): if v%(index+1)==0 or (index+1)%v==0: permute(l[:i]+l[i+1:],p+[v],index+1) ...
beautiful-arrangement
python permutation solution
prateek4463
2
172
beautiful arrangement
526
0.646
Medium
9,298
https://leetcode.com/problems/beautiful-arrangement/discuss/872354/Python3-backtracking
class Solution: def countArrangement(self, N: int) -> int: def fn(k): """Return number of beautiful arrangements.""" if k == N: return 1 # boundary condition ans = 0 for kk in range(k, N): if nums[kk] % (k+1) == 0 or (k+1) % nums[kk] =...
beautiful-arrangement
[Python3] backtracking
ye15
2
225
beautiful arrangement
526
0.646
Medium
9,299