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/count-days-spent-together/discuss/2588222/Python-simplest-approach
class Solution: def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int: m = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] a, b = int(arriveAlice[:2]), int(arriveAlice[3:]) c, d = int(leaveAlice[:2]), int(leaveAlice[3:]) e, f = int(arriveBob[:2]), int(arriveBob[3:]) g, h = int(leaveBob[:2]), int(leaveBob[3:]) i = sum(m[:a])+b j = sum(m[:c])+d k = sum(m[:e])+f l = sum(m[:g])+h # print(i, j, k, l) return max(0, min(j, l)-max(i, k)+1)
count-days-spent-together
Python simplest approach
mrprashantkumar
0
14
count days spent together
2,409
0.428
Easy
32,900
https://leetcode.com/problems/count-days-spent-together/discuss/2588162/PYTHON-3-Record-all-days-for-Alice-and-Bob-and-count-common-days
class Solution: def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int: dm=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] aam=int(arriveAlice[:2]) aad=int(arriveAlice[3:]) lam=int(leaveAlice[:2]) lad=int(leaveAlice[3:]) abm=int(arriveBob[:2]) abd=int(arriveBob[3:]) lbm=int(leaveBob[:2]) lbd=int(leaveBob[3:]) alice=[] bob=[] while aam!=lam or aad!=lad: if aam<lam: for i in range(aad,dm[aam-1]+1): alice.append((i,aam)) aam+=1 aad=1 else: for i in range(aad,lad+1): alice.append((i,aam)) aad=lad alice.append((lad,lam)) while abm!=lbm or abd!=lbd: if abm<lbm: for i in range(abd,dm[abm-1]+1): bob.append((i,abm)) abm+=1 abd=1 else: for i in range(abd,lbd+1): bob.append((i,abm)) abd=lbd bob.append((lbd,lbm)) return len(set(alice).intersection(set(bob)))
count-days-spent-together
[PYTHON 3] Record all days for Alice and Bob and count common days
shreyasjain0912
0
11
count days spent together
2,409
0.428
Easy
32,901
https://leetcode.com/problems/count-days-spent-together/discuss/2588041/Strange-yet-effective-solution
class Solution: def countDaysTogether(self, arriveAlice: str, leaveAlice: str, arriveBob: str, leaveBob: str) -> int: year=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] aa=arriveAlice.split('-') aa[0]=int(aa[0]) aa[1]=int(aa[1]) ab=arriveBob.split('-') ab[0]=int(ab[0]) ab[1]=int(ab[1]) start=max(aa,ab) la=leaveAlice.split('-') la[0]=int(la[0]) la[1]=int(la[1]) lb=leaveBob.split('-') lb[0]=int(lb[0]) lb[1]=int(lb[1]) end=min(la,lb) #print(start,end) if ab>la or start>end: return 0 ans=0 while start[0]!=end[0]: ans+=(year[start[0]-1]-start[1]+1) start[0]+=1 start[1]=1 ans+=(end[1]-start[1]+1) return ans
count-days-spent-together
Strange yet effective solution
RickSanchez101
0
16
count days spent together
2,409
0.428
Easy
32,902
https://leetcode.com/problems/count-days-spent-together/discuss/2587998/Secret-Python-Answer-Using-Date-Object
class Solution: def countDaysTogether(self, aa: str, la: str, ab: str, lb: str) -> int: aa = aa.split('-') la = la.split('-') ab = ab.split('-') lb = lb.split('-') start1 = date(2013,int(aa[0]),int(aa[1])) end1 = date(2013,int(la[0]),int(la[1])) start2 = date(2013,int(ab[0]),int(ab[1])) end2 = date(2013,int(lb[0]),int(lb[1])) overlaps = start1 <= end2 and end1 >= start2 if not overlaps: return 0 return abs(max(start1, start2)-min(end1, end2)).days +1
count-days-spent-together
[Secret Python Answer🤫🐍👌😍] Using Date Object
xmky
0
28
count days spent together
2,409
0.428
Easy
32,903
https://leetcode.com/problems/maximum-matching-of-players-with-trainers/discuss/2588450/Python-code-using-recursion
class Solution: def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int: n = len(players) m = len(trainers) players.sort() trainers.sort() dp = {} def helper(i,j): if i==n or j==m: return 0 if (i,j) in dp: return dp[(i,j)] if players[i]<=trainers[j]: dp[(i,j)] = 1+helper(i+1,j+1) else: dp[(i,j)] = helper(i,j+1) return dp[(i,j)] return helper(0,0)
maximum-matching-of-players-with-trainers
Python code using recursion
kamal0308
2
53
maximum matching of players with trainers
2,410
0.598
Medium
32,904
https://leetcode.com/problems/maximum-matching-of-players-with-trainers/discuss/2591966/Python3-Greedy-and-Two-pointers-Solution-Beats-100
class Solution: def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int: players.sort() trainers.sort() res = 0 i, j = 0, 0 while i < len(players) and j < len(trainers): if players[i] <= trainers[j]: res += 1 i += 1 j += 1 return res
maximum-matching-of-players-with-trainers
Python3 Greedy & Two pointers Solution - Beats 100%
siyu_
1
34
maximum matching of players with trainers
2,410
0.598
Medium
32,905
https://leetcode.com/problems/maximum-matching-of-players-with-trainers/discuss/2591290/Extremely-Simple-Implementation-python3.-Faster-than-100-solutions
class Solution: def matchPlayersAndTrainers(self, players: List[int], t: List[int]) -> int: c = j = i = 0 players.sort() t.sort() while i <= len(players) - 1 and j <= len(t) - 1: if players[i] <= t[j]: c += 1 i += 1 j += 1 return c
maximum-matching-of-players-with-trainers
Extremely Simple Implementation python3. Faster than 100% solutions
varunshrivastava2706
1
16
maximum matching of players with trainers
2,410
0.598
Medium
32,906
https://leetcode.com/problems/maximum-matching-of-players-with-trainers/discuss/2587879/Python3-Sort-%2B-Greedy
class Solution: def matchPlayersAndTrainers(self, P: List[int], T: List[int]) -> int: #Sort P, T = sorted(P, reverse=True), sorted(T, reverse=True) #Original amount of players n = len(P) #Greedy while T and P: if P[-1] <= T[-1]: P.pop() T.pop() #Amount of players popped return n - len(P)
maximum-matching-of-players-with-trainers
[Python3] Sort + Greedy
0xRoxas
1
26
maximum matching of players with trainers
2,410
0.598
Medium
32,907
https://leetcode.com/problems/maximum-matching-of-players-with-trainers/discuss/2840085/Python-oror-simple-solution-oror-beats-98
class Solution: def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int: players.sort() trainers.sort() i,j,ans=0,0,0 while i<len(players) and j<len(trainers): if players[i]<=trainers[j]: ans+=1 i+=1 j+=1 else: j+=1 return ans
maximum-matching-of-players-with-trainers
Python || simple solution || beats 98%
sud18
0
1
maximum matching of players with trainers
2,410
0.598
Medium
32,908
https://leetcode.com/problems/maximum-matching-of-players-with-trainers/discuss/2599353/Python-or-2Two-approach-or-Easy-or
class Solution: def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int: #two pointer players.sort() trainers.sort() ans = 0 i,j = len(players)-1,len(trainers)-1 while i>=0 and j>=0: if players[i]<=trainers[j]: ans+=1 i-=1 j-=1 else: i-=1 return ans from heapq import heapify,heappop class Solution: def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int: #heap solution ans = 0 trainers.sort() heapify(players) for t in trainers: if len(players)==0: break if players[0] <= t: heappop(players) ans+= 1 return ans
maximum-matching-of-players-with-trainers
🐍Python | 2️⃣Two approach | 💁Easy |
Brillianttyagi
0
15
maximum matching of players with trainers
2,410
0.598
Medium
32,909
https://leetcode.com/problems/maximum-matching-of-players-with-trainers/discuss/2594547/Python3-or-Sort-and-pop-or-O(n*log(n))-time-O(n)-space-or-Explained
class Solution: def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int: players = sorted(players) trainers = sorted(trainers) matchings = 0 while players and trainers: if players[-1] <= trainers[-1]: matchings += 1 trainers.pop() players.pop() return matchings
maximum-matching-of-players-with-trainers
Python3 | Sort and pop | O(n*log(n)) time, O(n) space | Explained
NotTheSwimmer
0
10
maximum matching of players with trainers
2,410
0.598
Medium
32,910
https://leetcode.com/problems/maximum-matching-of-players-with-trainers/discuss/2588606/Python3-two-pointers
class Solution: def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int: trainers.sort() ans = j = 0 for i, p in enumerate(sorted(players)): while j < len(trainers) and p > trainers[j]: j += 1 if j < len(trainers): ans += 1 j += 1 return ans
maximum-matching-of-players-with-trainers
[Python3] two pointers
ye15
0
7
maximum matching of players with trainers
2,410
0.598
Medium
32,911
https://leetcode.com/problems/maximum-matching-of-players-with-trainers/discuss/2588063/easy-sort-python-Greedy
class Solution: def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int: trainers.sort(reverse=True) players.sort(reverse=True) sum=0 i=0 j=0 count=0 for i in range(len(players)): if trainers[j]>=players[i]: j+=1 count+=1 if (j==len(trainers)): break return count
maximum-matching-of-players-with-trainers
easy sort python Greedy
a-ma-n
0
6
maximum matching of players with trainers
2,410
0.598
Medium
32,912
https://leetcode.com/problems/maximum-matching-of-players-with-trainers/discuss/2588039/Secret-Python-Answer-Double-Sort-Greedy
class Solution: def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int: players.sort() trainers.sort() i = j = 0 mat = 0 while i < len(players) and j < len(trainers): if players[i] <= trainers[j]: trainers[j]-= players[i] mat +=1 i+=1 j+=1 else: j+=1 return mat
maximum-matching-of-players-with-trainers
[Secret Python Answer🤫🐍👌😍] Double Sort Greedy
xmky
0
6
maximum matching of players with trainers
2,410
0.598
Medium
32,913
https://leetcode.com/problems/maximum-matching-of-players-with-trainers/discuss/2587964/Python-or-SUPER-Simple-or-No-Pointers-or-Match-Weakest-Trainer-to-Weakest-Player-(9-lines-of-code)
class Solution: def matchPlayersAndTrainers(self, players: List[int], trainers: List[int]) -> int: players.sort(reverse=True) trainers.sort(reverse=True) tot = 0 while trainers and players: if trainers[-1] >= players[-1]: tot += 1 players.pop() trainers.pop() return tot
maximum-matching-of-players-with-trainers
Python | SUPER Simple | No Pointers | Match Weakest Trainer to Weakest Player (9 lines of code)
dginovker
0
9
maximum matching of players with trainers
2,410
0.598
Medium
32,914
https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/discuss/2588110/Secret-Python-Answer-Sliding-Window-with-Double-Dictionary-of-Binary-Count
class Solution: def smallestSubarrays(self, nums: List[int]) -> List[int]: def create(m): t = 0 for n in m: if m[n] > 0: t = t | (1 << n) return t def add(a,m): ans = bin( a ) s = str(ans)[2:] for i, b in enumerate( s[::-1]): if b == '1': m[i] += 1 def remove(a,m): ans = bin( a ) s = str(ans)[2:] for i, b in enumerate( s[::-1]): if b == '1': m[i] -= 1 res = [] n = defaultdict(int) for i in nums: add(i,n) m = defaultdict(int) r = 0 c = 0 for i,v in enumerate(nums): # The last check is for if nums[i] == 0, in that case we still want to add to the map while r < len(nums) and (create(m) != create(n) or (c==0 and nums[i] ==0)): add(nums[r],m) r+=1 c+=1 res.append(c) remove(nums[i],m) remove(nums[i],n) c-=1 return res
smallest-subarrays-with-maximum-bitwise-or
[Secret Python Answer🤫🐍👌😍] Sliding Window with Double Dictionary of Binary Count
xmky
1
113
smallest subarrays with maximum bitwise or
2,411
0.403
Medium
32,915
https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/discuss/2602963/100-Understand-or-Python3
class Solution: def smallestSubarrays(self, nums: List[int]) -> List[int]: ans = defaultdict(deque) for i in range(len(nums)): for bit in range(31): if nums[i] &amp; (1 << bit): ans[bit].append(i) res = [] for i in range(len(nums)): end = i for _, arr in ans.items(): if arr and i > arr[0]: arr.popleft() if arr: end = max(end, arr[0]) res.append(end-i+1) return res
smallest-subarrays-with-maximum-bitwise-or
100% Understand | Python3
leet_satyam
0
65
smallest subarrays with maximum bitwise or
2,411
0.403
Medium
32,916
https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/discuss/2589636/Python3-or-2-Pointers-or-Sliding-Window
class Solution: def smallestSubarrays(self, nums: List[int]) -> List[int]: n = len(nums) ret = [0] * n i, j = n - 1, n - 1 bitCount = [0] * 32 # add bits of k to bitCount def addCount(k): cnt = 0 while k > 0: bitCount[cnt] += k &amp; 1 k >>= 1 cnt += 1 # check if removing bits of k from bitCount decreases the current bitwise OR of numbers between i and j def checkCount(k): cnt = 0 while k > 0: if k &amp; 1 == 1 and bitCount[cnt] == 1: return False k >>= 1 cnt += 1 return True # remove bits of k from bitCount def removeCount(k): cnt = 0 while k > 0: bitCount[cnt] -= (k &amp; 1) k >>= 1 cnt += 1 while i > -1: addCount(nums[i]) while checkCount(nums[j]) and i < j: removeCount(nums[j]) j -= 1 i -= 1 ret[i+1] = j - i return ret
smallest-subarrays-with-maximum-bitwise-or
Python3 | 2 Pointers | Sliding Window
DheerajGadwala
0
53
smallest subarrays with maximum bitwise or
2,411
0.403
Medium
32,917
https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/discuss/2588925/Python-3Sliding-window%2BHash-map
class Solution: def smallestSubarrays(self, nums: List[int]) -> List[int]: # store digits with 1 bit digits = defaultdict(lambda: defaultdict(int)) for num in set(nums): if not num: continue d = bin(num)[2:] for i, x in enumerate(d[::-1]): if x == '1': digits[num][i] += 1 n = len(nums) r = n - 1 ans = [0] * n tmp = defaultdict(int) for l in reversed(range(n)): for x in digits[nums[l]]: tmp[x] += 1 # to be removed number will not cause current digits to have zero count after removal while r > l and all(tmp[x] > 1 for x in digits[nums[r]]): for x in digits[nums[r]]: tmp[x] -= 1 r -= 1 ans[l] = r - l + 1 return ans
smallest-subarrays-with-maximum-bitwise-or
[Python 3]Sliding window+Hash map
chestnut890123
0
54
smallest subarrays with maximum bitwise or
2,411
0.403
Medium
32,918
https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/discuss/2588614/O(n)-Heuristic-with-maximum-position-of-bit-1-on-every-bit-position-(Example-Solution)
class Solution: def smallestSubarrays(self, nums: List[int]) -> List[int]: def bins(k): ret = [] while k>0: ret.append(k%2) k = k // 2 return ret n = len(nums) h = [[] for _ in range(32)] for i in range(n): bi = bins(nums[i]) for j in range(len(bi)): if bi[j]==1: h[j].append(i) print(nums) print("h:", {i:h[i] for i in range(len(h)) if len(h[i])>0}) vt = [0 for _ in range(32)] lh = [len(h[i]) for i in range(32)] ans = [] for i in range(n): r = i for k in range(32): if vt[k]<lh[k] and h[k][vt[k]]<i: vt[k] += 1 if vt[k]<lh[k]: r = max(r, h[k][vt[k]]) print("+ ", i, r, "size:", r - i + 1, "h:", [h[i][vt[i]:] for i in range(len(h)) if len(h[i])>0]) ans.append(r - i + 1) print("ans:", ans) print("="*20, "\n") return ans print = lambda *a, **aa: ()
smallest-subarrays-with-maximum-bitwise-or
O(n) Heuristic with maximum position of bit 1 on every bit position (Example Solution)
dntai
0
13
smallest subarrays with maximum bitwise or
2,411
0.403
Medium
32,919
https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/discuss/2588614/O(n)-Heuristic-with-maximum-position-of-bit-1-on-every-bit-position-(Example-Solution)
class Solution: def smallestSubarrays(self, nums: List[int]) -> List[int]: """ TLE with BinSearch """ def bins(k): ret = [] while k>0: ret.append(k%2) k = k // 2 return ret n = len(nums) h = [{}] for i in range(n): bi = bins(nums[i]) hi = {i:1 for i in range(len(bi)) if bi[i]==1} # print(nums[i], hi) ti = {k:v for k, v in h[i].items()} # print("-->", ti) for k in hi: ti[k] = ti.get(k, 0) + 1 h.append(ti) # print("-->", ti, h) # print(h) ans = [] for i in range(n): x = 0 for k in h[n]: if h[n].get(k, 0) - h[i].get(k, 0)>0: x += 1 l, r = i, n-1 while l<r: mid = (l + r) // 2 xmid = 0 for k in h[mid+1]: if h[mid+1].get(k, 0) - h[i].get(k, 0)>0: xmid += 1 if xmid!=x: l = mid + 1 else: r = mid # print("-->", r, r - i + 1) size = r - i + 1 ans.append(size) # print("="*20) return ans print = lambda *a, **aa: ()
smallest-subarrays-with-maximum-bitwise-or
O(n) Heuristic with maximum position of bit 1 on every bit position (Example Solution)
dntai
0
13
smallest subarrays with maximum bitwise or
2,411
0.403
Medium
32,920
https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/discuss/2588613/Python3-All-about-bits
class Solution: def smallestSubarrays(self, nums: List[int]) -> List[int]: ans = [0]*len(nums) seen = [0]*30 for i in range(len(nums)-1, -1, -1): for j in range(30): if nums[i] &amp; 1<<j: seen[j] = i ans[i] = max(1, max(seen)-i+1) return ans
smallest-subarrays-with-maximum-bitwise-or
[Python3] All about bits
ye15
0
28
smallest subarrays with maximum bitwise or
2,411
0.403
Medium
32,921
https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/discuss/2588276/Python3-Reverse-Sliding-Window-or-O(n-log(30))
class Solution: def smallestSubarrays(self, A): n, l = len(nums), len(bin(max(nums))) - 2 bins = [format(num, '0' + str(l) + 'b') for num in nums] def can_remove(i): if all(bins[i][j] != '1' or set_bits[j] != 1 for j in range(l)): update_bits(i, -1) return True return False def update_bits(i, op): for j in range(l): if bins[i][j] == '1': set_bits[j] += op res, set_bits = [0] * (n), [0] * l update_bits(n - 1, 1) st = en = n - 1 while st >= 0: res[st] = en - st + 1 if st != en and can_remove(en): res[st] -= 1 en -= 1 else: st -= 1 if st >= 0: update_bits(st, 1) return res
smallest-subarrays-with-maximum-bitwise-or
Python3 Reverse Sliding Window | O(n log(30))
ryangrayson
0
18
smallest subarrays with maximum bitwise or
2,411
0.403
Medium
32,922
https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/discuss/2588048/Python3-or-Binary-Search-oror-PrefixSum
class Solution: def smallestSubarrays(self, nums: List[int]) -> List[int]: def findClosest(ind,arr): if ind>arr[-1]: return 0 idx=bisect.bisect_left(arr,ind) return arr[idx]-ind binary=[] n=len(nums) for i in range(n): temp=[0 for i in range(32)] for bit in range(32): if nums[i]&amp;(1<<bit): temp[31-bit]=1 binary.append(temp) hmap=defaultdict(list) for i in range(32): for j in range(n): if binary[j][i]==1: hmap[i].append(j) ans=[0 for i in range(n)] for i in range(n): maxVal=0 for j in range(32): if j in hmap: val=findClosest(i,hmap[j]) maxVal=max(maxVal,val) else: continue ans[i]=maxVal+1 return ans
smallest-subarrays-with-maximum-bitwise-or
[Python3] | Binary Search || PrefixSum
swapnilsingh421
0
44
smallest subarrays with maximum bitwise or
2,411
0.403
Medium
32,923
https://leetcode.com/problems/smallest-subarrays-with-maximum-bitwise-or/discuss/2588048/Python3-or-Binary-Search-oror-PrefixSum
class Solution: def smallestSubarrays(self, nums: List[int]) -> List[int]: n=len(nums) lastVal=[0 for i in range(32)] ans=[0 for i in range(n)] for i in range(n-1,-1,-1): maxVal=0 for j in range(32): if nums[i]&amp;(1<<j): lastVal[31-j]=0 else: lastVal[31-j]=lastVal[31-j]+1 if i<n-1 else -float('inf') maxVal=max(maxVal,lastVal[31-j]) ans[i]=max(ans[i],maxVal)+1 return ans
smallest-subarrays-with-maximum-bitwise-or
[Python3] | Binary Search || PrefixSum
swapnilsingh421
0
44
smallest subarrays with maximum bitwise or
2,411
0.403
Medium
32,924
https://leetcode.com/problems/minimum-money-required-before-transactions/discuss/2588620/Python3-Greedy
class Solution: def minimumMoney(self, transactions: List[List[int]]) -> int: ans = val = 0 for cost, cashback in transactions: ans += max(0, cost - cashback) val = max(val, min(cost, cashback)) return ans + val
minimum-money-required-before-transactions
[Python3] Greedy
ye15
0
19
minimum money required before transactions
2,412
0.391
Hard
32,925
https://leetcode.com/problems/smallest-even-multiple/discuss/2601867/LeetCode-The-Hard-Way-Explained-Line-By-Line
class Solution: def smallestEvenMultiple(self, n: int) -> int: # it's just asking for LCM of 2 and n return lcm(2, n)
smallest-even-multiple
🔥 [LeetCode The Hard Way] 🔥 Explained Line By Line
wingkwong
5
258
smallest even multiple
2,413
0.879
Easy
32,926
https://leetcode.com/problems/smallest-even-multiple/discuss/2601867/LeetCode-The-Hard-Way-Explained-Line-By-Line
class Solution: def smallestEvenMultiple(self, n: int) -> int: # alternatively, we can use GCD to calculate LCM return (2 * n) // gcd(2, n)
smallest-even-multiple
🔥 [LeetCode The Hard Way] 🔥 Explained Line By Line
wingkwong
5
258
smallest even multiple
2,413
0.879
Easy
32,927
https://leetcode.com/problems/smallest-even-multiple/discuss/2601867/LeetCode-The-Hard-Way-Explained-Line-By-Line
class Solution: def smallestEvenMultiple(self, n: int) -> int: # or simply check if it is divisable by 2, if so return n # else return its double return 2 * n if n &amp; 1 else n
smallest-even-multiple
🔥 [LeetCode The Hard Way] 🔥 Explained Line By Line
wingkwong
5
258
smallest even multiple
2,413
0.879
Easy
32,928
https://leetcode.com/problems/smallest-even-multiple/discuss/2803420/Python-or-TC-O(1)-or-Simple-Solution
class Solution: def smallestEvenMultiple(self, n: int) -> int: if n % 2 == 0: # check if n is even return as it is because it will be the smallest even no return n else: return n*2 # else multipy it with 2 to convert it into smallest event multiple
smallest-even-multiple
Python | TC O(1) | Simple Solution
sahil193101
2
68
smallest even multiple
2,413
0.879
Easy
32,929
https://leetcode.com/problems/smallest-even-multiple/discuss/2590431/Short-Python-Answer-One-Line-and-15-Characters
class Solution: def smallestEvenMultiple(self, n: int) -> int: return lcm(n,2)
smallest-even-multiple
[Short Python Answer🤫🐍👌😍] One Line and 15 Characters
xmky
2
75
smallest even multiple
2,413
0.879
Easy
32,930
https://leetcode.com/problems/smallest-even-multiple/discuss/2590272/Python3-parity
class Solution: def smallestEvenMultiple(self, n: int) -> int: return ((n&amp;1)+1)*n
smallest-even-multiple
[Python3] parity
ye15
2
25
smallest even multiple
2,413
0.879
Easy
32,931
https://leetcode.com/problems/smallest-even-multiple/discuss/2676098/Simple-solution-with-Python-or-95-faster-and-less-memory-used
class Solution: def smallestEvenMultiple(self, n: int) -> int: if n % 2 == 0: return n return n * 2
smallest-even-multiple
Simple solution with Python | 95 % faster and less memory used
fazliddindehkanoff
1
4
smallest even multiple
2,413
0.879
Easy
32,932
https://leetcode.com/problems/smallest-even-multiple/discuss/2845719/Python-one-liner-solution
class Solution: def smallestEvenMultiple(self, n: int) -> int: return n if n%2 == 0 else n*2
smallest-even-multiple
Python one liner solution
pratiklilhare
0
1
smallest even multiple
2,413
0.879
Easy
32,933
https://leetcode.com/problems/smallest-even-multiple/discuss/2833230/Python3-Simple-easy-and-understandable
class Solution: def smallestEvenMultiple(self, n: int) -> int: mult = 1 while mult > 0: if (n*mult)%2 == 0: return n*mult mult += 1
smallest-even-multiple
[Python3] Simple, easy and understandable
Silvanus20
0
2
smallest even multiple
2,413
0.879
Easy
32,934
https://leetcode.com/problems/smallest-even-multiple/discuss/2828785/Python-O(1)
class Solution: def smallestEvenMultiple(self, n: int) -> int: return n << 1 if n % 2 == 1 else n
smallest-even-multiple
Python O(1)
chingisoinar
0
1
smallest even multiple
2,413
0.879
Easy
32,935
https://leetcode.com/problems/smallest-even-multiple/discuss/2825051/Python-Oneliner
class Solution: def smallestEvenMultiple(self, n: int) -> int: return [n, n*2][n%2]
smallest-even-multiple
Python Oneliner
emrecoltu
0
1
smallest even multiple
2,413
0.879
Easy
32,936
https://leetcode.com/problems/smallest-even-multiple/discuss/2821716/Python3-Solution-with-using-bit-manipulation
class Solution: def smallestEvenMultiple(self, n: int) -> int: if n &amp; 1 == 0: return n return n * 2
smallest-even-multiple
[Python3] Solution with using bit manipulation
maosipov11
0
2
smallest even multiple
2,413
0.879
Easy
32,937
https://leetcode.com/problems/smallest-even-multiple/discuss/2810422/Python-Solution-or-One-Liner-or-100-Faster
class Solution: def smallestEvenMultiple(self, n: int) -> int: return n * 2 if n &amp; 1 else n
smallest-even-multiple
Python Solution | One Liner | 100% Faster
Gautam_ProMax
0
4
smallest even multiple
2,413
0.879
Easy
32,938
https://leetcode.com/problems/smallest-even-multiple/discuss/2770912/Easy-Python-Solution
class Solution: def smallestEvenMultiple(self, n: int) -> int: if n%2==0: return n else: return n*2
smallest-even-multiple
Easy Python Solution
SheetalMehta
0
1
smallest even multiple
2,413
0.879
Easy
32,939
https://leetcode.com/problems/smallest-even-multiple/discuss/2763121/python-easy-solution
class Solution: def smallestEvenMultiple(self, n: int) -> int: if n%2 ==0: return n else: return n*2
smallest-even-multiple
python easy solution
user7798V
0
1
smallest even multiple
2,413
0.879
Easy
32,940
https://leetcode.com/problems/smallest-even-multiple/discuss/2760680/Hard-Solution-or-if-condition-or-Python3
class Solution: def smallestEvenMultiple(self, n: int) -> int: if n % 2: return 2 * n return n
smallest-even-multiple
Hard Solution | if condition | Python3
joshua_mur
0
3
smallest even multiple
2,413
0.879
Easy
32,941
https://leetcode.com/problems/smallest-even-multiple/discuss/2747843/Simple-python-code-with-explanation
class Solution: def smallestEvenMultiple(self, n): #if n is odd if n&amp;1 == 1: #smallest positive multiple of 2 and n #is multiplication of both (2 and n ) return (n*2) #if n is even else: #smallest positive multiple of 2 and n #is n itself return (n)
smallest-even-multiple
Simple python code with explanation
thomanani
0
4
smallest even multiple
2,413
0.879
Easy
32,942
https://leetcode.com/problems/smallest-even-multiple/discuss/2721203/I-don't-know-my-code-is-not-working
class Solution: def smallestEvenMultiple(self, n: int) -> int: def mult(n): if(n%2==0): print(n) return n else: mult(n*2) mult(n) why my code is not working please help me!
smallest-even-multiple
I don't know my code is not working
user3076TM
0
3
smallest even multiple
2,413
0.879
Easy
32,943
https://leetcode.com/problems/smallest-even-multiple/discuss/2721177/Python-simple-solution
class Solution: def smallestEvenMultiple(self, n: int) -> int: while(True): if(n%2==0): return n else: n=n*2
smallest-even-multiple
Python simple solution
user3076TM
0
2
smallest even multiple
2,413
0.879
Easy
32,944
https://leetcode.com/problems/smallest-even-multiple/discuss/2695811/Python3-Solution-beats-95-in-memory
class Solution: def smallestEvenMultiple(self, n: int) -> int: return n if n%2==0 else n*2
smallest-even-multiple
Python3 Solution, beats 95% in memory
sipi09
0
3
smallest even multiple
2,413
0.879
Easy
32,945
https://leetcode.com/problems/smallest-even-multiple/discuss/2656225/Easy-Python-solution
class Solution: def smallestEvenMultiple(self, n: int) -> int: if n%2==0: return n else: return n*2
smallest-even-multiple
Easy Python solution
abhint1
0
4
smallest even multiple
2,413
0.879
Easy
32,946
https://leetcode.com/problems/smallest-even-multiple/discuss/2645875/Python-One-Liner-No-Branching-97
class Solution: def smallestEvenMultiple(self, n: int) -> int: return n if (n &amp; 1 == 0) else n << 1
smallest-even-multiple
Python One Liner No Branching 97%
Brent_Pappas
0
15
smallest even multiple
2,413
0.879
Easy
32,947
https://leetcode.com/problems/smallest-even-multiple/discuss/2645875/Python-One-Liner-No-Branching-97
class Solution: def smallestEvenMultiple(self, n: int) -> int: return n << (n &amp; 1)
smallest-even-multiple
Python One Liner No Branching 97%
Brent_Pappas
0
15
smallest even multiple
2,413
0.879
Easy
32,948
https://leetcode.com/problems/smallest-even-multiple/discuss/2640759/100-EASY-TO-UNDERSTANDSUPER-SHORTSIMPLECLEAN
class Solution: def smallestEvenMultiple(self, n: int) -> int: if n % 2 == 0: return n else: return 2*n
smallest-even-multiple
🔥100% EASY TO UNDERSTAND/SUPER SHORT/SIMPLE/CLEAN🔥
YuviGill
0
18
smallest even multiple
2,413
0.879
Easy
32,949
https://leetcode.com/problems/smallest-even-multiple/discuss/2626788/Python
class Solution: def smallestEvenMultiple(self, n: int) -> int: return n * 2 if n % 2 == 1 else n
smallest-even-multiple
Python
blue_sky5
0
17
smallest even multiple
2,413
0.879
Easy
32,950
https://leetcode.com/problems/smallest-even-multiple/discuss/2625221/EASY-TO-UNDERSTAND-oror-PYTHON3-oror-BRUTE-FORCE
class Solution: def smallestEvenMultiple(self, n: int) -> int: a=2 return n*2//gcd(n,a) def gcd(a,b): if a>b: temp=b else: temp=a for i in range(1,temp+1): if ((a%i==0) and (b%i==0)): gcd=i return gcd
smallest-even-multiple
EASY TO UNDERSTAND || PYTHON3 || BRUTE FORCE
pratiyushray2152
0
10
smallest even multiple
2,413
0.879
Easy
32,951
https://leetcode.com/problems/smallest-even-multiple/discuss/2625049/python-solution-96.73-faster
class Solution: def smallestEvenMultiple(self, n: int) -> int: if n % 2 == 0 and n < n * 2: return n return n * 2
smallest-even-multiple
python solution 96.73 % faster
samanehghafouri
0
8
smallest even multiple
2,413
0.879
Easy
32,952
https://leetcode.com/problems/smallest-even-multiple/discuss/2614450/Python-solution
class Solution: def smallestEvenMultiple(self, n: int) -> int: if n % 2 == 0: return int((n / 2)*2) elif n % 2 != 0: return (n*2) ```
smallest-even-multiple
Python solution
Dronix
0
11
smallest even multiple
2,413
0.879
Easy
32,953
https://leetcode.com/problems/smallest-even-multiple/discuss/2600549/Easy-and-Understand-python-solution
class Solution: def smallestEvenMultiple(self, n: int) -> int: for i in range(n,2*n+1): if i%n==0 and i%2==0: break return i
smallest-even-multiple
Easy and Understand python solution
jalajk
0
8
smallest even multiple
2,413
0.879
Easy
32,954
https://leetcode.com/problems/smallest-even-multiple/discuss/2600368/Python-oneliner
class Solution: def smallestEvenMultiple(self, n: int) -> int: return n if n%2 == 0 else n*2
smallest-even-multiple
Python oneliner
StikS32
0
8
smallest even multiple
2,413
0.879
Easy
32,955
https://leetcode.com/problems/smallest-even-multiple/discuss/2595962/Python3-Easy-One-Liner
class Solution: def smallestEvenMultiple(self, n: int) -> int: return n if n % 2 == 0 else 2 * n
smallest-even-multiple
[Python3] Easy One-Liner
ivnvalex
0
8
smallest even multiple
2,413
0.879
Easy
32,956
https://leetcode.com/problems/smallest-even-multiple/discuss/2595336/Pythonoror-one-liner
class Solution: def smallestEvenMultiple(self, n: int) -> int: return n if n%2==0 else n*2
smallest-even-multiple
Python|| one-liner
shersam999
0
4
smallest even multiple
2,413
0.879
Easy
32,957
https://leetcode.com/problems/smallest-even-multiple/discuss/2591821/Python3-oror-Simple-Solution
class Solution: def smallestEvenMultiple(self, n: int) -> int: i = n while True: if i % 2 == 0 and i % n == 0: return i i += 1
smallest-even-multiple
Python3 || Simple Solution
abhijeetmallick29
0
4
smallest even multiple
2,413
0.879
Easy
32,958
https://leetcode.com/problems/smallest-even-multiple/discuss/2590544/Python-Solution-oror-O(1)-Time-Complexity
class Solution: def smallestEvenMultiple(self, n: int) -> int: if n%2 == 0: return n return n*2
smallest-even-multiple
Python Solution || O(1) Time Complexity
a_dityamishra
0
7
smallest even multiple
2,413
0.879
Easy
32,959
https://leetcode.com/problems/smallest-even-multiple/discuss/2590424/Python-Solution-easiest
class Solution: def smallestEvenMultiple(self, n: int) -> int: if n % 2 == 0: return n else: return n * 2
smallest-even-multiple
Python Solution easiest
113377code
0
10
smallest even multiple
2,413
0.879
Easy
32,960
https://leetcode.com/problems/smallest-even-multiple/discuss/2590300/Python-Simple-Python-Solution-or-100-Faster
class Solution: def smallestEvenMultiple(self, n: int) -> int: result = 0 for num in range(1, 301): if num % 2 == 0 and num % n == 0: result = num break return result
smallest-even-multiple
[ Python ] ✅✅ Simple Python Solution | 100 % Faster 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
38
smallest even multiple
2,413
0.879
Easy
32,961
https://leetcode.com/problems/smallest-even-multiple/discuss/2590233/Python-or-Math-One-liner
class Solution: def smallestEvenMultiple(self, n: int) -> int: return n if n % 2 == 0 else 2 * n
smallest-even-multiple
Python | Math One-liner
sr_vrd
0
20
smallest even multiple
2,413
0.879
Easy
32,962
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2594370/Python3-oror-Str-join-and-split-2-lines-oror-TM-375ms15.7-MB
class Solution: def longestContinuousSubstring(self, s: str) -> int: arr = ''.join(['1' if ord(s[i])-ord(s[i-1]) == 1 else ' ' for i in range(1,len(s))]).split() return max((len(ones)+1 for ones in arr), default = 1)
length-of-the-longest-alphabetical-continuous-substring
Python3 || Str join & split, 2 lines || T/M 375ms/15.7 MB
warrenruud
3
38
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,963
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2592937/Python-Solution-or-Easy-to-Understand
class Solution: def longestContinuousSubstring(self, s: str) -> int: alphabet = 'abcdefghijklmnopqrstuvwxyz' start, max_len = 0, 0 for i in range(1, len(s) + 1): # from length 1 to len(s) if s[start:i] in alphabet: # check whether slice is consecutive max_len = max(max_len, i - start) else: start = i - 1 return max_len
length-of-the-longest-alphabetical-continuous-substring
Python Solution | Easy to Understand
cyber_kazakh
1
30
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,964
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2590521/Python-Solution-With-Stack
class Solution: def longestContinuousSubstring(self, s: str) -> int: st = [] c = 0 for i in s: if not st: st.append(i) else: if i>st[-1] and ord(i) == ord(st[-1]) + 1: st.append(i) else: c = max(c,len(st)) st = [i] return max(c,len(st))
length-of-the-longest-alphabetical-continuous-substring
Python Solution With Stack
a_dityamishra
1
17
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,965
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2590284/Self-Understandable-solution%3A
class Solution: def longestContinuousSubstring(self, s: str) -> int: x=set() p=[s[0]] for i in s[1:]: if ord(p[-1])+1==ord(i): p.append(i) else: x.add("".join(p)) p=[i] x.add("".join(p)) ans=sorted(x,key=len)[-1] return len(ans)
length-of-the-longest-alphabetical-continuous-substring
Self Understandable solution:
goxy_coder
1
35
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,966
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2848977/python3
class Solution: def longestContinuousSubstring(self, s: str) -> int: alphabet = "abcdefghijklmnopqrstuvwxyz" counter = 1 count_index = 0 for i in range(len(s) - 1): if alphabet.index(s[i]) + 1 == alphabet.index(s[i + 1]): counter += 1 else: if counter >=count_index: count_index = counter counter = 1 else: counter = 1 return counter if counter > count_index else count_index
length-of-the-longest-alphabetical-continuous-substring
python3
Geniuss87
0
1
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,967
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2761483/easy-python-solution
class Solution: def longestContinuousSubstring(self, s: str) -> int: i = 0 maxCount = 0 while i < len(s) : counter = 1 for j in range(i, min(i+25, len(s)-1)) : if ord(s[j+1]) - ord(s[j]) == 1 : counter += 1 else : break i += counter if counter > maxCount : maxCount = counter return maxCount
length-of-the-longest-alphabetical-continuous-substring
easy python solution
sghorai
0
4
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,968
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2722738/Python-O(N)-Easy-to-understand
class Solution: def longestContinuousSubstring(self, s: str) -> int: c=high=1 for i in range(0,len(s)-1): if ord(s[i]) + 1==ord(s[i+1]): c=c+1 if c>high: high=c else: c=1 return(high)
length-of-the-longest-alphabetical-continuous-substring
Python O(N)- Easy to understand
envyTheClouds
0
3
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,969
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2687279/92-Accepted-or-Simple-and-Easy-to-Understand-or-Python
class Solution(object): def longestContinuousSubstring(self, s): dic = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6,'g':7, 'h':8, 'i':9, 'j':10, 'k':11, 'l':12, 'm':13, 'n':14, 'o':15, 'p':16, 'q':17, 'r':18, 's':19, 't':20, 'u':21, 'v':22, 'w':23, 'x':24, 'y':25, 'z':26} ans = 1 currCount = 1 for i in range(1, len(s)): if dic[s[i]] == (dic[s[i - 1]] + 1): currCount += 1 else: if currCount > ans: ans = currCount currCount = 1 return currCount if currCount > ans else ans
length-of-the-longest-alphabetical-continuous-substring
92% Accepted | Simple and Easy to Understand | Python
its_krish_here
0
4
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,970
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2660416/Python3-O(n)-Time-or-O(1)-Space-or-Intuitive-Solution.
class Solution: def longestContinuousSubstring(self, s: str) -> int: res = curr = 1 for i in range(1, len(s)): if ord(s[i]) == ord(s[i - 1]) + 1: curr += 1 else: res, curr = max(curr, res), 1 return max(res, curr)
length-of-the-longest-alphabetical-continuous-substring
Python3 - O(n) Time | O(1) Space | Intuitive Solution.
yukkk
0
11
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,971
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2597980/Python3-or-Easy-sliding-window-solution-or-One-pass
class Solution: def longestContinuousSubstring(self, s: str) -> int: left = 0 N = len(s) ans = 1 for right in range(1, N): if ord(s[right - 1]) + 1 == ord(s[right]): ans = max(ans, right - left + 1) else: left = right return ans
length-of-the-longest-alphabetical-continuous-substring
Python3 | Easy sliding window solution | One pass
sidheshwar_s
0
19
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,972
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2591818/Python3-oror-Simple-Solution-oror-Linear-Time-oror-O(1)-space
class Solution: def longestContinuousSubstring(self, s: str) -> int: res = 1 count = 1 for i in range(1,len(s)): if ord(s[i]) == ord(s[i-1])+1: count += 1 else: res = max(res,count) count = 1 return max(res,count)
length-of-the-longest-alphabetical-continuous-substring
Python3 || Simple Solution || Linear Time || O(1) space
abhijeetmallick29
0
4
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,973
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2590893/Single-Pass-Python-%2B-TypeScript-..-Short-simple-with-explanations
class Solution: def longestContinuousSubstring(self, s: str) -> int: # List of char codes; prepend the first character as a code: codes = list() codes.append(ord(s[0])) # Longest sequence ever observed longest_ever = 1 # Longest subsequence observed this time longest_this = 1 # For each character in s except for the first character, we'll: # 1. Convert it to a char code using ord() # 2. Append it to our list of char codes, which is a char code # representation of the string s # 3. Is the char code we just observed one more than the one # we observed before this? # A. If yes, then we'll capture that length using longest_this # B. If no, then we noticed a break in sequence. We'll reset longest_this to 1 # and we'll capture the overall results (whichever is greater: what we have # in longest_ever, or what we observed this time in longest_this). We'll # 4. There's a possibility that the longest one ends on the final char (no break # observed.. so for that case, we want to perform another comparison .. if the longest # was at the very end, then it'll be captured with another max(longest_ever, longest_this)) for c in s[1:]: codes.append(ord(c)) # Is this observed char one more than the prior observed char? if codes[-1] == codes[-2] + 1: longest_this += 1 # This observed char isn't sequential from the last observed char. Reset longest_this # and reflect overall longest observed length in longest_ever. else: longest_ever = max(longest_ever, longest_this) longest_this = 1 # The longest may have been the final subsequence, which wouldn't have ended with a break return max(longest_ever, longest_this)
length-of-the-longest-alphabetical-continuous-substring
Single Pass, Python + TypeScript .. Short, simple, with explanations
n1ghtc0re_neko
0
9
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,974
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2590676/python-interesting-one-pass-solution
class Solution: def longestContinuousSubstring(self, s: str) -> int: alphas = "abcdefghijklmnopqrstuvwxyz" myalpha = {alphas[i]:i for i in range(len(alphas))} ans = 0 count = 0 prev = myalpha[s[0]] - 1 for i in s: if myalpha[i] == prev + 1: count += 1 else: ans = max(ans, count) count = 1 prev = myalpha[i] ans = max(ans, count) return ans
length-of-the-longest-alphabetical-continuous-substring
[python] interesting one pass solution
tolimitiku
0
9
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,975
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2590555/Python-3-intuitive-solution-O(n)
class Solution: def longestContinuousSubstring(self, s: str) -> int: prev = 0 cnt = 1 res = 1 for c in s: if ord(c) - prev == 1: cnt += 1 res = max(res, cnt) else: cnt = 1 prev = ord(c) return res
length-of-the-longest-alphabetical-continuous-substring
Python 3 intuitive solution O(n)
hahashabi
0
3
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,976
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2590489/Python-O(n)-1-pass-easiest-solution
class Solution: def longestContinuousSubstring(self, s: str) -> int: if len(s) == 1: return 1 maxRes = 1 res = 1 for i in range(1, len(s)): if ord(s[i - 1]) + 1 == ord(s[i]): res += 1 maxRes = max(res, maxRes) else: res = 1 return maxRes
length-of-the-longest-alphabetical-continuous-substring
Python O(n) 1 pass easiest solution
113377code
0
6
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,977
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2590443/Python-Simple-Python-Solution-Using-Sliding-Window
class Solution: def longestContinuousSubstring(self, s: str) -> int: t = "abcdefghijklmnopqrstuvwxyz" result = 1 index = 0 while index < len(s): current_max = 1 current_element = s[index] next_index = index + 1 while next_index < len(s): if ord(current_element) + 1 == ord(s[next_index]): current_element = s[next_index] current_max = current_max + 1 next_index = next_index + 1 else: index = next_index - 1 break result = max(result, current_max) if next_index == len(s): index = next_index - 1 index = index + 1 return result
length-of-the-longest-alphabetical-continuous-substring
[ Python ] ✅✅ Simple Python Solution Using Sliding Window 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
18
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,978
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2590395/Python-Answer-Double-loop-Easy-O(n)
class Solution: def longestContinuousSubstring(self, s: str) -> int: t = 0 al = "abcdefghijklmnopqrstuvwxyz" for i,c in enumerate(s): c = ord(c) - ord('a') count = 1 ii = 1 for k in range(c+1, 26): if i + ii < len(s) and al[k] == s[i+ii] : count+=1 ii+=1 else: break t = max(t, count) return t
length-of-the-longest-alphabetical-continuous-substring
[Python Answer🤫🐍👌😍] Double loop Easy O(n)
xmky
0
4
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,979
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2590328/Python3-counter
class Solution: def longestContinuousSubstring(self, s: str) -> int: ans = cnt = 0 for i, ch in enumerate(s): if i == 0 or ord(s[i-1])+1 != ord(ch): cnt = 1 else: cnt += 1 ans = max(ans, cnt) return ans
length-of-the-longest-alphabetical-continuous-substring
[Python3] counter
ye15
0
6
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,980
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2590205/Python-or-Sliding-Window
class Solution: def longestContinuousSubstring(self, s: str) -> int: n = len(s) left = 0; right = 1 maxLength = 0 while right <= n: if right < n and ord(s[right]) == ord(s[right - 1]) + 1 and ord(s[right]) <= ord('z'): right += 1 else: maxLength = max(maxLength, right - left) left = right; right += 1 return maxLength
length-of-the-longest-alphabetical-continuous-substring
Python | Sliding Window
sr_vrd
0
11
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,981
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2590192/O(n)-using-pattern-condition
class Solution: def longestContinuousSubstring(self, s: str) -> int: ans = 0 cnt = 0 pre = -1 for i in range(len(s)): ni = ord(s[i]) - ord('a') if pre==-1 or pre+1!=ni: cnt = 1 else: cnt = cnt + 1 pre = ni ans = max(cnt, ans) return ans
length-of-the-longest-alphabetical-continuous-substring
O(n) using pattern condition
dntai
0
5
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,982
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2590138/Python3-One-Pass
class Solution: def longestContinuousSubstring(self, s: str) -> int: cnt, res = 1, 1 for idx in range(1, len(s)): if ord(s[idx]) - ord(s[idx - 1]) == 1: cnt += 1 else: cnt = 1 res = max(res, cnt) return res
length-of-the-longest-alphabetical-continuous-substring
[Python3] One Pass
0xRoxas
0
49
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,983
https://leetcode.com/problems/length-of-the-longest-alphabetical-continuous-substring/discuss/2590097/Python-Easy-O(n)-solution
class Solution: def longestContinuousSubstring(self, s: str) -> int: cnt = 1 res = 0 for i in range(1, len(s)): if ord(s[i]) - ord(s[i-1]) == 1: cnt += 1 res = max(res, cnt) else: cnt = 1 return max(res, cnt)
length-of-the-longest-alphabetical-continuous-substring
Python Easy O(n) solution
MiKueen
0
9
length of the longest alphabetical continuous substring
2,414
0.558
Medium
32,984
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2825833/Python-DFS
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: def dfs(n1, n2, level): if not n1: return if level % 2: n1.val, n2.val = n2.val, n1.val dfs(n1.left, n2.right, level + 1) dfs(n1.right, n2.left, level + 1) dfs(root.left, root.right, 1) return root
reverse-odd-levels-of-binary-tree
Python, DFS
blue_sky5
0
4
reverse odd levels of binary tree
2,415
0.757
Medium
32,985
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2817727/Simple-Python3-or-Faster-Than-97
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if not root.left: return root def dfs(node1, node2, rev): if rev: node1.val, node2.val = node2.val, node1.val if node1.left: dfs(node1.left, node2.right, not rev) dfs(node1.right, node2.left, not rev) dfs(root.left, root.right, True) return root
reverse-odd-levels-of-binary-tree
Simple Python3 | Faster Than 97%
ryangrayson
0
4
reverse odd levels of binary tree
2,415
0.757
Medium
32,986
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2804540/2-appraoches-bfsdfs
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: q,c = [root],0 while q: c+=1 n = len(q) if c%2==0: s,e = 0,n-1 while s<e: q[s].val,q[e].val = q[e].val,q[s].val s+=1 e-=1 for i in range(n): x = q.pop(0) if x.left: q.append(x.left) if x.right: q.append(x.right) return root
reverse-odd-levels-of-binary-tree
2 appraoches bfs,dfs
Rtriders
0
3
reverse odd levels of binary tree
2,415
0.757
Medium
32,987
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2769777/Go-through-rows-reassign-values-in-place-99-speed
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: row = [root] odd = False while row: if odd: values = [node.val for node in row] values.reverse() for node, value in zip(row, values): node.val = value odd = not odd new_row = [] for node in row: if node.left: new_row.append(node.left) if node.right: new_row.append(node.right) row = new_row return root
reverse-odd-levels-of-binary-tree
Go through rows, reassign values in place, 99% speed
EvgenySH
0
3
reverse odd levels of binary tree
2,415
0.757
Medium
32,988
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2751367/Easy-to-Understand-or-Deque-Solution-or-Python
class Solution(object): def reverseOddLevels(self, root): flag = False q = deque() q.append(root) while q: temp = [] if flag: for i in range(len(q)): temp.append(q[i].val) temp = temp[::-1] for i in range(len(q)): p = q.popleft() if flag: p.val = temp[i] if p.left: q.append(p.left) if p.right: q.append(p.right) flag = not(flag) return root
reverse-odd-levels-of-binary-tree
Easy to Understand | Deque Solution | Python
its_krish_here
0
2
reverse odd levels of binary tree
2,415
0.757
Medium
32,989
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2655349/PythonJSGoC%2B%2B-O(n)-by-DFS-w-Comment
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: # worker function help us to swap node value on odd level def worker(_left, _right, odd_level): # check current level nodes are non-leaf if not (_left and _right): return if odd_level: # swap node value on odd level _left.val, _right.val = _right.val, _left.val # DFS to next level, and toggle boolean flag worker(_left.left, _right.right, not odd_level) worker(_left.right, _right.left, not odd_level) return # -------------------------------------- # We start our task on level 1 worker(root.left, root.right, True) return root
reverse-odd-levels-of-binary-tree
Python/JS/Go/C++ O(n) by DFS [w/ Comment]
brianchiang_tw
0
27
reverse odd levels of binary tree
2,415
0.757
Medium
32,990
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2592213/Python-or-Easy-orNoob
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: def rev(node1,node2,level): if node1 is None and node2 is None: return else: if (level)%2!=0: node1.val,node2.val = node2.val , node1.val rev(node1.left,node2.right,level+1) rev(node1.right,node2.left,level+1) rev(root.left,root.right,1) return root
reverse-odd-levels-of-binary-tree
Python | Easy |Noob
Brillianttyagi
0
38
reverse odd levels of binary tree
2,415
0.757
Medium
32,991
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2591383/Python3-or-DFS-or-Single-Traversal
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: def dfs(root_1,root_2,lvl): if not root_1 or not root_2: return False lr=dfs(root_1.left,root_2.right,lvl+1) rr=dfs(root_1.right,root_2.left,lvl+1) if lvl%2==0 and lr and rr: root_1.left.val,root_2.right.val=root_2.right.val,root_1.left.val root_1.right.val,root_2.left.val=root_2.left.val,root_1.right.val return True lr=dfs(root.left,root.right,1) if lr: root.left.val,root.right.val=root.right.val,root.left.val return root
reverse-odd-levels-of-binary-tree
[Python3] | DFS | Single Traversal
swapnilsingh421
0
6
reverse odd levels of binary tree
2,415
0.757
Medium
32,992
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2590939/python3-Bfs-sol-for-reference
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: levels = 0 st = deque([root]) stidx = 0 while stidx < len(st): start = stidx end = len(st)-1 midway = (start+end)//2 for i in range(start, end+1): node = st[i] stidx += 1 if levels %2 == 1 and i <= midway: st[i].val, st[end-(i-start)].val = st[end-(i-start)].val, st[i].val if node.left: st.append(node.left) if node.right: st.append(node.right) levels += 1 return root
reverse-odd-levels-of-binary-tree
[python3] Bfs sol for reference
vadhri_venkat
0
2
reverse odd levels of binary tree
2,415
0.757
Medium
32,993
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2590747/python-Interesting-bfs-solution
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: queue = deque([root]) ans = [] isodd = True while queue: temp = queue.copy() temp2 = [] for i in range(len(queue)): curr = queue.popleft() if curr.left: queue.append(curr.left) queue.append(curr.right) temp2.append(curr.left.val) temp2.append(curr.right.val) if isodd: temp2.reverse() count = 0 while temp: var = temp.popleft() if var.left: var.left.val = temp2[count] var.right.val = temp2[count+1] count += 2 isodd = not isodd return root
reverse-odd-levels-of-binary-tree
[python] Interesting bfs solution
tolimitiku
0
17
reverse odd levels of binary tree
2,415
0.757
Medium
32,994
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2590602/Python3-BFS-and-invert-simple-solution
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: hashmap = {} res = [] def bfs(node, i): if not node: return if i == len(res): res.append([]) if i % 2 != 0: res[i].append(node) bfs(node.left, i + 1) bfs(node.right, i + 1) bfs(root, 0) for row in res: for i in range(len(row) // 2): a, b = row[i], row[len(row) - i - 1] a.val, b.val = b.val, a.val return root
reverse-odd-levels-of-binary-tree
Python3 BFS and invert simple solution
hahashabi
0
9
reverse odd levels of binary tree
2,415
0.757
Medium
32,995
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2590600/Python-Solution-using-BFS-and-Reverse
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: if not root.left and not root.right: return root #Reverses the values of odd level nodes def reverse(root,depth,levels): if not root.left and not root.right: return if depth % 2 == 0: root.left.val = levels[depth+1].pop() root.right.val = levels[depth+1].pop() reverse(root.left,depth+1,levels) reverse(root.right,depth+1,levels) return root # Collect the odd level nodes and store it in HashMap ex: { 1: [3,5] } q, levels = collections.deque(), collections.defaultdict(list) q.append((root,0)) while q: n = len(q) for _ in range(n): node,depth = q.popleft() if depth % 2 != 0: levels[depth].append(node.val) if node.left: q.append((node.left,depth+1)) if node.right: q.append((node.right,depth+1)) return reverse(root,0,levels)
reverse-odd-levels-of-binary-tree
Python Solution using BFS and Reverse
parthberk
0
19
reverse odd levels of binary tree
2,415
0.757
Medium
32,996
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2590522/Python-Simple-Python-Solution-Using-Level-Order-Traversal
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: def height(node): if node == None: return 0 return max(height(node.left), height(node.right)) + 1 def LevelOrderPrint(node, level,mid_result): if node == None: return None else: if level == 1: mid_result.append(node.val) else: LevelOrderPrint(node.left, level-1,mid_result) LevelOrderPrint(node.right, level-1,mid_result) return mid_result Height = height(root) array = [] for h in range(1,Height+1): if h % 2 != 0: array = array + LevelOrderPrint(root, h,[]) else: array = array + LevelOrderPrint(root, h,[])[::-1] def insertLevelOrder(array, index, array_length): root = None if index < array_length: root = TreeNode(array[index]) root.left = insertLevelOrder(array, 2 * index + 1, array_length) root.right = insertLevelOrder(array, 2 * index + 2, array_length) return root return insertLevelOrder(array, 0, len(array))
reverse-odd-levels-of-binary-tree
[ Python ] ✅✅ Simple Python Solution Using Level Order Traversal 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
41
reverse odd levels of binary tree
2,415
0.757
Medium
32,997
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2590497/Python-Solution-With-Level-Order-Traversal
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: ans = [] q = [root] k = 0 while q: l = [] for i in range(len(q)): d = q.pop(0) if d.left: q.append(d.left) if d.right: q.append(d.right) l.append(d.val) if k%2 == 0: ans.extend(l) else: ans.extend(l[::-1]) k+=1 def insertLevelOrder(arr, i, n): root = None if i < n: root = TreeNode(arr[i]) root.left = insertLevelOrder(arr, 2 * i + 1, n) root.right = insertLevelOrder(arr, 2 * i + 2, n) return root root = None n = len(ans) root = insertLevelOrder(ans,0,n) return root
reverse-odd-levels-of-binary-tree
Python Solution With Level Order Traversal
a_dityamishra
0
24
reverse odd levels of binary tree
2,415
0.757
Medium
32,998
https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/2590365/Python3-bfs
class Solution: def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]: level = 0 queue = deque([root]) while queue: if level&amp;1: for i in range(len(queue)//2): queue[i].val, queue[~i].val = queue[~i].val, queue[i].val level ^= 1 for _ in range(len(queue)): node = queue.popleft() if node.left: queue.append(node.left) if node.right: queue.append(node.right) return root
reverse-odd-levels-of-binary-tree
[Python3] bfs
ye15
0
14
reverse odd levels of binary tree
2,415
0.757
Medium
32,999