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(a... | 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:]... | 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... | 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]))
star... | 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 ... | 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
... | 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
... | 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()... | 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
... | 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... | 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
... | 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 += ... | 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]:
... | 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]:
... | 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.p... | 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:]
... | 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] & (1 << bit):
ans[bit].append(i)
res = []
for i in range(len(nums)):
... | 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[c... | 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]):
... | 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):
... | 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)
... | 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] & 1<<j: seen[j] = i
ans[i] = max(1, max(seen)-i+1)
return a... | 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)
... | 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):
t... | 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]&(1<<j):
l... | 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 & 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&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 & 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 & 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&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
... | 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 & 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 & 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
r... | 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 = ... | 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:
... | 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=... | 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:
... | 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 :... | 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 = ... | 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
... | 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 ti... | 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:
... | 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... | 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)
... | 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]):
... | 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+... | 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:... | 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 = n... | 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)
... | 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
... | 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 ... | 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:
... | 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
... | 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):
... | 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 ... | 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)... | 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 = no... | 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 l... | 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
... | 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()
... | 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)
... | 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 ... | 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:... | 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)
... | 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&1:
for i in range(len(queue)//2):
queue[i].val, queue[~i].val = queue[~i].val, queue[i].va... | reverse-odd-levels-of-binary-tree | [Python3] bfs | ye15 | 0 | 14 | reverse odd levels of binary tree | 2,415 | 0.757 | Medium | 32,999 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.