post_href
stringlengths
57
213
python_solutions
stringlengths
71
22.3k
slug
stringlengths
3
77
post_title
stringlengths
1
100
user
stringlengths
3
29
upvotes
int64
-20
1.2k
views
int64
0
60.9k
problem_title
stringlengths
3
77
number
int64
1
2.48k
acceptance
float64
0.14
0.91
difficulty
stringclasses
3 values
__index_level_0__
int64
0
34k
https://leetcode.com/problems/maximum-ice-cream-bars/discuss/1163987/Python-Solution
class Solution: def maxIceCream(self, costs: List[int], coins: int) -> int: costs.sort() res = 0 for cost in costs: if coins >= cost : coins -= cost res += 1 return res
maximum-ice-cream-bars
Python Solution
SaSha59
0
42
maximum ice cream bars
1,833
0.656
Medium
26,200
https://leetcode.com/problems/maximum-ice-cream-bars/discuss/1164123/Python3-Very-Simple-Solution
class Solution: def maxIceCream(self, costs: List[int], coins: int) -> int: if min(costs) > coins: return 0 if sum(costs) < coins: return len(costs) costs.sort() count = 0 for i in costs: if coins < i: break count += 1 coins -= i return count
maximum-ice-cream-bars
Python3 Very Simple Solution
VijayantShri
-1
46
maximum ice cream bars
1,833
0.656
Medium
26,201
https://leetcode.com/problems/single-threaded-cpu/discuss/2004757/Python-or-Priority-Queue
class Solution: def getOrder(self, tasks: List[List[int]]) -> List[int]: dic=defaultdict(list) for i in range(len(tasks)): dic[tasks[i][0]].append((tasks[i][1],i)) ans=[] keys=sorted(dic.keys()) while keys: k=keys.pop(0) pq=dic[k] heapq.heapify(pq) time=k while pq: p_time,ind=heapq.heappop(pq) ans.append(ind) time+=p_time while keys: if keys[0]>time: break for item in dic[keys.pop(0)]: heapq.heappush(pq,item) return ans
single-threaded-cpu
Python | Priority Queue
heckt27
1
83
single threaded cpu
1,834
0.42
Medium
26,202
https://leetcode.com/problems/single-threaded-cpu/discuss/1817100/Clean-Python-Two-Heaps
class Solution: def getOrder(self, tasks: List[List[int]]) -> List[int]: heap = [] available = [] for i, (e, p) in enumerate(tasks): heappush(heap, (e, p, i)) e, p, i = heappop(heap) last = e + p res = [i] while heap or available: while heap and heap[0][0] <= last: e, p, i = heappop(heap) heappush(available, (p, i, e)) if not available: e, p, i = heappop(heap) heappush(available, (p, i, e)) p, i, e = heappop(available) res.append(i) last = max(p + e, last + p) return res
single-threaded-cpu
Clean Python - Two Heaps
r_vaghefi
1
156
single threaded cpu
1,834
0.42
Medium
26,203
https://leetcode.com/problems/single-threaded-cpu/discuss/2806756/Python-min-heap
class Solution: def getOrder(self, tasks: List[List[int]]) -> List[int]: minHeap = [] res = [] for i in range(len(tasks)): tasks[i] += [i] tasks.sort(key=lambda x: x[0]) t = tasks[0][0] while minHeap or tasks: while tasks and t >= tasks[0][0]: e_time, p_time, idx = tasks.pop(0) heapq.heappush(minHeap, [p_time, idx]) if minHeap: p_time, idx = heapq.heappop(minHeap) t += p_time res.append(idx) continue t = tasks[0][0] return res
single-threaded-cpu
Python min heap
zananpech9
0
2
single threaded cpu
1,834
0.42
Medium
26,204
https://leetcode.com/problems/single-threaded-cpu/discuss/1815409/Solution-using-heapq-in-Python-3
class Solution: def getOrder(self, tasks: List[List[int]]) -> List[int]: def cpu_scheduler(): import heapq nonlocal tasks tasks = [(*task, i) for i, task in enumerate(tasks)] tasks.sort() pq = [] clk = tasks[0][0] i = 0 while i < len(tasks) or pq: while i < len(tasks) and tasks[i][0] <= clk: # as described, cpu would prefer tasks with shortest processing time. # if multiple tasks have the same shortest processing time, it will choose the task with the smallest index. # since heappush does not support "key" function, # we push task into our heap as a tuple that consists of these two attributes # in order to apply the sorting mechanism provided by python itself heapq.heappush(pq, (tasks[i][1], tasks[i][2])) i += 1 if pq: pt, pid = heapq.heappop(pq) yield pid clk += pt elif tasks[i][0] > clk: clk = tasks[i][0] return list(cpu_scheduler())
single-threaded-cpu
Solution using heapq in Python 3
mousun224
0
120
single threaded cpu
1,834
0.42
Medium
26,205
https://leetcode.com/problems/single-threaded-cpu/discuss/1166935/Python3-priority-queue
class Solution: def getOrder(self, tasks: List[List[int]]) -> List[int]: ans = [] pq = [] # min-heap t = 0 # end of prev task tasks.append([inf, inf]) for (enq, prc), i in sorted(zip(tasks, range(len(tasks)))): # adding a sentinel while pq and t < enq: tp, ii, te = heappop(pq) ans.append(ii) t = max(t, te) + tp # time finish processing this task heappush(pq, (prc, i, enq)) return ans
single-threaded-cpu
[Python3] priority queue
ye15
0
80
single threaded cpu
1,834
0.42
Medium
26,206
https://leetcode.com/problems/single-threaded-cpu/discuss/1166183/Python3-Heap-O(N-log-N)-With-comments
class Solution: class Task: def __init__(self,index,start,time): self.index = index self.start = start self.time = time def __lt__(self,other): #compare by duration and index if self.time != other.time: return self.time < other.time else: return self.index < other.index def getOrder(self, tasks: List[List[int]]) -> List[int]: #create task object tasks = [self.Task(i,t[0],t[1]) for i,t in enumerate(tasks)] #sort task object by their starting time and duration tasks = sorted(tasks, key = lambda x: (x.start,x.time)) #turn it into a queue so pop with constant time tasks = deque(tasks) pq = [] res = [] #make current time start of first task time = tasks[0].start while tasks or pq: if not pq: first = tasks[0] #push all tasks that start at the same time while tasks and first.start == tasks[0].start: heappush(pq,tasks.popleft()) else: #get top of heap current = heappop(pq) #update time time+=current.time #add popped index to result res.append(current.index) #add waiting tasks to the heap while tasks and tasks[0].start <= time: heappush(pq,tasks.popleft()) return res
single-threaded-cpu
Python3 Heap O(N log N) With comments
necilAlbayrak
0
129
single threaded cpu
1,834
0.42
Medium
26,207
https://leetcode.com/problems/single-threaded-cpu/discuss/1165019/Python-3-Minimum-Heap-(1872-ms)
class Solution: def getOrder(self, tasks: List[List[int]]) -> List[int]: # sort by start time, process time and index t = [(a, b, i) for i, (a, b) in enumerate(tasks)] t.sort() t = deque(t) # set start time as first task end time start = t[0][0] + t[0][1] ans = [t[0][2]] t.popleft() waited = [] while t or waited: # push into waited list if enqueueTIme earlier than start time while t and start >= t[0][0]: a, b, i = t.popleft() heappush(waited, (b, i)) # if idle then push into next task if not waited and t: a, b, i = t.popleft() heappush(waited, (b, i)) # pop out next task by process time and index if waited: work_b, work_i = heappop(waited) start += work_b ans.append(work_i) return ans
single-threaded-cpu
[Python 3] Minimum Heap (1872 ms)
chestnut890123
0
74
single threaded cpu
1,834
0.42
Medium
26,208
https://leetcode.com/problems/single-threaded-cpu/discuss/1164818/python3-Solution-using-sort-and-heapq-for-reference.
class Solution: def getOrder(self, tasks: List[List[int]]) -> List[int]: idxlist = list(zip(tasks, range(len(tasks)))) idxlist.sort(key=lambda x: (x[0][0], x[1])) idxlist = list(map(lambda x: (x[0][0], x[1], x[0][1]), idxlist)) heapq.heapify(idxlist) eqt, index, prt = heapq.heappop(idxlist) processQ = [(prt, index)] time = eqt while idxlist and idxlist[0][0] == eqt: eqt, index, prt = heapq.heappop(idxlist) heapq.heappush(processQ, (prt, index)) ans = deque([]) while processQ: prt, index = heapq.heappop(processQ) time += prt ans.append(index) while idxlist and idxlist[0][0] <= time: eqt, index, prt = heapq.heappop(idxlist) heapq.heappush(processQ, (prt, index)) if idxlist and not processQ: eqt, index, prt = heapq.heappop(idxlist) heapq.heappush(processQ, (prt, index)) time = eqt return ans
single-threaded-cpu
[python3] Solution using sort and heapq for reference.
vadhri_venkat
0
66
single threaded cpu
1,834
0.42
Medium
26,209
https://leetcode.com/problems/single-threaded-cpu/discuss/1164505/Python3-heapq
class Solution: def getOrder(self, tasks: List[List[int]]) -> List[int]: for i in range(len(tasks)): tasks[i] = [tasks[i][0],tasks[i][1],i] #add index into tasks array tasks.sort(key = lambda x: (x[0],x[1])) #sort task according to arrival time and burst time cpu_timestamp = tasks[0][0] + tasks[0][1] #save the current cpu time arrival + burst temp = [] #array for heapq operations res = [tasks[0][2]] #store the starting index result, this is final since we have sorted and got the optimal i = 1 while i<len(tasks): if cpu_timestamp < tasks[i][0]: #cant use a time which is less than current cpu time if temp: #process already stored items in heap burst,index,arri = heapq.heappop(temp) res.append(index) cpu_timestamp += burst else: #if heap is empty, we need to jump to the next value . This is for case like [100,100],[1000000000,100000000] heapq.heappush(temp, [tasks[i][1],tasks[i][2],tasks[i][0]]) i+=1 if i < len(tasks) and cpu_timestamp >=tasks[i][0]: #store all times that are greater than current cpu time heapq.heappush(temp, [tasks[i][1],tasks[i][2],tasks[i][0]]) i+=1 while temp: # if we reached the end of tasks array, but still have items in heap, pop one by one burst,index,arri = heapq.heappop(temp) res.append(index) return res
single-threaded-cpu
Python3 heapq
deleted_user
0
75
single threaded cpu
1,834
0.42
Medium
26,210
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/2724403/Simple-python-code-with-explanation
class Solution: #example 1 #result =[(1&amp;6)^(1&amp;5)^(2&amp;6)^(2&amp;5)^(3&amp;6)^(3&amp;5)] \ / \ / \ / # (1&amp;(6^5)) ^ (2&amp;(6^5)) ^ (3&amp;(6^5)) \ | / \ | / \ | / \ | / # ((1^2^3) &amp; (6^5)) def getXORSum(self, a, b): x = 0 for i in range(len(a)): x = x ^ a[i] y = 0 for j in range(len(b)): y = y ^ b[j] return x &amp; y
find-xor-sum-of-all-pairs-bitwise-and
Simple python code with explanation
thomanani
1
23
find xor sum of all pairs bitwise and
1,835
0.601
Hard
26,211
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/1164299/Python3-solution-with-comments-100-memory-efficient
class Solution: def getXORSum(self, arr1: List[int], arr2: List[int]) -> int: ''' 1. According to the hints, XORSum = (XORSum of arr1) bitwise AND (XORSum of arr2) 2. Calculate the XOR Sums of arr1 and arr2 separately and store them in separate variables 3. Perform bitwise AND on those XOR Sums ''' xor1 = arr1[0] #at first store the first element as the XOR sum of arr1 xor2 = arr2[0] #at first store the first element as the XOR sum of arr2 if len(arr1)>=2: for i in range(1,len(arr1)): xor1^=arr1[i] #iteratively XOR with the next element if len(arr2)>=2: for i in range(1,len(arr2)): xor2^=arr2[i] #iteratively XOR with the next element return xor1&amp;xor2 #bitwise AND of XORSum of arr1 and XORSum of arr2
find-xor-sum-of-all-pairs-bitwise-and
Python3 solution with comments, 100% memory efficient
bPapan
1
76
find xor sum of all pairs bitwise and
1,835
0.601
Hard
26,212
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/2413037/92-faster-95-memory-python-3-w-comments
class Solution: def getXORSum(self, arr1: List[int], arr2: List[int]) -> int: # GET MIN LOOP LENGTH length = min(len(arr1), len(arr2)) # Break into 2 sets a1 = 0 b1 = 0 # SHORT CUT IF THEY ARE THE SAME LENGTH if len(arr1) == len(arr2): while len(arr1): a1 ^= arr1.pop() b1 ^= arr2.pop() return (b1 &amp; a1) # IF YOU MADE IT THIS FAR THE ARRAYS ARE UNEVEN for idx in range(length): a1 ^= arr1.pop() b1 ^= arr2.pop() # IF ARRAY 1 IS LARGER RUN THROUGH THEM if len(arr1) > 0: while len(arr1): a1 ^= arr1.pop() if len(arr2) > 0: while len(arr2): b1 ^= arr2.pop() return (a1 &amp; b1)
find-xor-sum-of-all-pairs-bitwise-and
92% faster / 95% memory python 3 w/ comments
cengleby86
0
36
find xor sum of all pairs bitwise and
1,835
0.601
Hard
26,213
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/1565126/Python-Easy-Solution-or-Using-XOR
class Solution: def getXORSum(self, arr1: List[int], arr2: List[int]) -> int: ele1 = arr1[0] ele2 = arr2[0] for i in range(1, len(arr1)): ele1 = ele1 ^ arr1[i] for j in range(1, len(arr2)): ele2 = ele2 ^ arr2[j] return ele1 &amp; ele2
find-xor-sum-of-all-pairs-bitwise-and
Python Easy Solution | Using XOR
leet_satyam
0
85
find xor sum of all pairs bitwise and
1,835
0.601
Hard
26,214
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/1490551/Python3-or-O(32*(m%2Bn))
class Solution: def getXORSum(self, arr1: List[int], arr2: List[int]) -> int: ans = 0 for i in range(32): arr1_set_count = 0 arr2_set_count = 0 for x in arr1: if x&amp;(1<<i) != 0: arr1_set_count ^= 1 for x in arr2: if x&amp;(1<<i) != 0: arr2_set_count ^= 1 if (arr1_set_count &amp; arr2_set_count): ans |= (1<<i) return ans
find-xor-sum-of-all-pairs-bitwise-and
Python3 | O(32*(m+n))
Sanjaychandak95
0
34
find xor sum of all pairs bitwise and
1,835
0.601
Hard
26,215
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/1278432/Python3-Easiest
class Solution: def getXORSum(self, a1: List[int], a2: List[int]) -> int: ans1, ans2 = 0, 0 for i in range(len(a1)): ans1 ^= a1[i] for i in range(len(a2)): ans2 ^= a2[i] return ans1&amp;ans2
find-xor-sum-of-all-pairs-bitwise-and
Python3 Easiest
tglukhikh
0
77
find xor sum of all pairs bitwise and
1,835
0.601
Hard
26,216
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/1166939/Python3-1-line
class Solution: def getXORSum(self, arr1: List[int], arr2: List[int]) -> int: return reduce(xor, arr1) &amp; reduce(xor, arr2)
find-xor-sum-of-all-pairs-bitwise-and
[Python3] 1-line
ye15
0
42
find xor sum of all pairs bitwise and
1,835
0.601
Hard
26,217
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/1164173/Python-straight-forward-solution
class Solution: def getXORSum(self, arr1: List[int], arr2: List[int]) -> int: """ (a^b) &amp; (d^e^f) = a&amp;d ^ a&amp;e ^ a&amp;f ^ b&amp;d ^ b&amp;e ^ b&amp;f """ N1 = len(arr1) N2 = len(arr2) tmp1 = 0 tmp2 = 0 for i in range(max(N1, N2)): if i < N1: tmp1 = tmp1^arr1[i] if i < N2: tmp2 = tmp2^arr2[i] return tmp1 &amp; tmp2
find-xor-sum-of-all-pairs-bitwise-and
Python straight forward solution
AlbertWang
0
54
find xor sum of all pairs bitwise and
1,835
0.601
Hard
26,218
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1175067/Python3-self-explained
class Solution: def sumBase(self, n: int, k: int) -> int: ans = 0 while n: n, x = divmod(n, k) ans += x return ans
sum-of-digits-in-base-k
[Python3] self-explained
ye15
12
1,200
sum of digits in base k
1,837
0.768
Easy
26,219
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1175132/Python3-easy-solution
class Solution: def sumBase(self, n: int, k: int) -> int: output_sum = 0 while (n > 0) : rem = n % k output_sum = output_sum + rem n = int(n / k) return output_sum
sum-of-digits-in-base-k
{Python3} easy solution
AbhishekSingh212
5
427
sum of digits in base k
1,837
0.768
Easy
26,220
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1845509/3-Lines-Python-Solution-oror-95-Faster-oror-Memory-less-than-75
class Solution: def sumBase(self, n: int, k: int) -> int: ans=0 while n>0: ans+=n%k ; n//=k return ans
sum-of-digits-in-base-k
3-Lines Python Solution || 95% Faster || Memory less than 75%
Taha-C
3
118
sum of digits in base k
1,837
0.768
Easy
26,221
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1845509/3-Lines-Python-Solution-oror-95-Faster-oror-Memory-less-than-75
class Solution: def sumBase(self, n: int, k: int) -> int: return (x:=lambda y: 0 if not y else y%k + x(y//k))(n)
sum-of-digits-in-base-k
3-Lines Python Solution || 95% Faster || Memory less than 75%
Taha-C
3
118
sum of digits in base k
1,837
0.768
Easy
26,222
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1175073/PythonPython3-Solution
class Solution: def sumBase(self, n: int, k: int) -> int: cnt = 0 while n: cnt += (n % k) n //= k print(cnt) return cnt
sum-of-digits-in-base-k
Python/Python3 Solution
prasanthksp1009
3
212
sum of digits in base k
1,837
0.768
Easy
26,223
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2613822/Python-Divmod-Solution
class Solution: def sumBase(self, n: int, k: int) -> int: result = 0 # make repeated divmods to get the digits and # the leftover number while n: n, res = divmod(n, k) result += res return result
sum-of-digits-in-base-k
[Python] - Divmod Solution
Lucew
2
31
sum of digits in base k
1,837
0.768
Easy
26,224
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2287491/python-solution-fastest-and-efficient
class Solution: def sumBase(self, n: int, k: int) -> int: x=[] while n!=0: x.append(n%k) n=n//k return sum(x)
sum-of-digits-in-base-k
python solution fastest and efficient
yagnic40
2
42
sum of digits in base k
1,837
0.768
Easy
26,225
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2317805/Easy-solution-oror-PYTHON
```class Solution: def sumBase(self, n: int, k: int) -> int: stri = "" while True: if n < k: break div = int(n // k) stri += str(n % k) n = div stri += str(n) stri = stri[::-1] lst = [int(x) for x in stri] return (sum(lst))
sum-of-digits-in-base-k
Easy solution || PYTHON
Jonny69
1
54
sum of digits in base k
1,837
0.768
Easy
26,226
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2119575/5-lines-of-Python-Code-with-explanation
class Solution: def sumBase(self, n: int, k: int) -> int: sum1 = 0 while n: sum1 += n%k # It gives the remainder and also add in each step to the variable sum1 n //= k return sum1
sum-of-digits-in-base-k
5 lines of Python Code with explanation
prernaarora221
1
73
sum of digits in base k
1,837
0.768
Easy
26,227
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2786542/Python-solution
class Solution: def sumBase(self, n: int, k: int) -> int: remainders = [] if (n // k) < k: remainders.append(n % k) remainders.append(n//k) else: while (n // k) >= 1: remainders.append(n % k) if (n // k) < k: remainders.append(n // k) n = n // k return sum(remainders)
sum-of-digits-in-base-k
Python solution
samanehghafouri
0
1
sum of digits in base k
1,837
0.768
Easy
26,228
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2717358/Python-99.65faster-with-94less-memory
class Solution: def sumBase(self, n: int, k: int) -> int: ans = 0 while(1): s = n%k n = n//k ans+=s if(n<k): ans+=n break return ans
sum-of-digits-in-base-k
Python 99.65%faster with 94%less memory
morrismoppp
0
6
sum of digits in base k
1,837
0.768
Easy
26,229
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2598952/python3-recursion-45ms
class Solution: def sumBase(self, n: int, k: int) -> int: if n == 0: return 0 return (n % k) + self.sumBase(n // k, k)
sum-of-digits-in-base-k
python3 recursion 45ms
ranv1r
0
4
sum of digits in base k
1,837
0.768
Easy
26,230
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2442380/Python-for-beginners
class Solution: def sumBase(self, n: int, k: int) -> int: #Runtime: 34ms if(n<k): return n if(n==k): return 1 lis=[] while n>=k: remainder=n%k lis.append(remainder) n=n//k lis.append(n) #last value when n<k appending return sum(lis)
sum-of-digits-in-base-k
Python for beginners
mehtay037
0
23
sum of digits in base k
1,837
0.768
Easy
26,231
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1592353/Simple-Recursive-Solution
class Solution: def sumBase(self, n: int, k: int) -> int: if n <= 0: return 0 return n % k + self.sumBase(n // k, k)
sum-of-digits-in-base-k
Simple Recursive Solution
pmooreh
0
36
sum of digits in base k
1,837
0.768
Easy
26,232
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1220829/Python-Easy-solution
class Solution: def sumBase(self, n: int, k: int) -> int: ans = 0 while n>=k: ans = ans + n%k n = n//k ans = ans+n return ans
sum-of-digits-in-base-k
[Python] Easy solution
arkumari2000
0
182
sum of digits in base k
1,837
0.768
Easy
26,233
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1200393/Python3-simple-solution
class Solution: def sumBase(self, n: int, k: int) -> int: sum = 0 while n != 0: sum += n%k n = n//k return sum
sum-of-digits-in-base-k
Python3 simple solution
EklavyaJoshi
0
71
sum of digits in base k
1,837
0.768
Easy
26,234
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1180105/Python-3-A-Normal-Mathematical-Solution
class Solution: def sumBase(self, n: int, k: int) -> int: fin = 0 while n>=k: fin+=n%k n=n//k fin+=n return fin
sum-of-digits-in-base-k
[Python 3] A Normal Mathematical Solution
vamsi81523
0
88
sum of digits in base k
1,837
0.768
Easy
26,235
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1175602/python-sol-faster-than-100-less-memory-than-100
class Solution: def sumBase(self, num: int, base: int) -> int: tot = 0 base_num = "" while num>0: dig = int(num%base) if dig<10: base_num += str(dig) else: base_num += chr(ord('A')+dig-10) num //= base base_num = base_num[::-1] tot = 0 for i in base_num: tot += int(i) return tot
sum-of-digits-in-base-k
python sol faster than 100% , less memory than 100%
elayan
0
61
sum of digits in base k
1,837
0.768
Easy
26,236
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/1179374/Python-3-Sliding-Window-Explanation-with-Code
class Solution: def maxFrequency(self, nums: List[int], k: int) -> int: nums.sort() n = len(nums) sum_s_w = nums[0] fin = 1 i=0 for j in range(1,n): sum_s_w+=nums[j] mx = nums[j] while sum_s_w+k<mx*(j-i+1): sum_s_w -= nums[i] i += 1 fin = max(fin,j-i+1) return fin
frequency-of-the-most-frequent-element
[Python 3] Sliding Window Explanation with Code
vamsi81523
14
826
frequency of the most frequent element
1,838
0.386
Medium
26,237
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/1175037/Python3-binary-search
class Solution: def maxFrequency(self, nums: List[int], k: int) -> int: nums.sort() prefix = [0] for x in nums: prefix.append(prefix[-1] + x) ans = 0 for i in reversed(range(len(nums))): lo, hi = 0, i while lo < hi: mid = lo + hi >> 1 if nums[i] * (i - mid) + prefix[mid] - prefix[i] <= k: hi = mid else: lo = mid + 1 ans = max(ans, i - lo + 1) return ans
frequency-of-the-most-frequent-element
[Python3] binary search
ye15
6
543
frequency of the most frequent element
1,838
0.386
Medium
26,238
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/1175037/Python3-binary-search
class Solution: def maxFrequency(self, nums: List[int], k: int) -> int: nums.sort() ans = ii = sm = 0 for i in range(len(nums)): sm += nums[i] while k < nums[i]*(i-ii+1) - sm: sm -= nums[ii] ii += 1 ans = max(ans, i - ii + 1) return ans
frequency-of-the-most-frequent-element
[Python3] binary search
ye15
6
543
frequency of the most frequent element
1,838
0.386
Medium
26,239
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/1833162/Python-easy-to-read-and-understand-or-sliding-window
class Solution: def maxFrequency(self, nums: List[int], k: int) -> int: nums.sort() sums, i, ans = 0, 0, 0 for j in range(len(nums)): sums += nums[j] while nums[j]*(j-i+1) > sums+k: sums -= nums[i] i = i+1 ans = max(ans, j-i+1) return ans
frequency-of-the-most-frequent-element
Python easy to read and understand | sliding window
sanial2001
3
380
frequency of the most frequent element
1,838
0.386
Medium
26,240
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/1454840/Simple-Python-O(nlogn)-sort%2Bsliding-window-solution
class Solution: def maxFrequency(self, nums: List[int], k: int) -> int: nums.sort() left = right = ret = 0 window_sum = 0 while right < len(nums): # maintain the invariant that k is enough to # change all elements between left and right # inclusively to nums[right] window_sum += nums[right] while nums[right]*(right-left+1)-window_sum > k: window_sum -= nums[left] left += 1 ret = max(ret, right-left+1) right += 1 return ret
frequency-of-the-most-frequent-element
Simple Python O(nlogn) sort+sliding window solution
Charlesl0129
3
381
frequency of the most frequent element
1,838
0.386
Medium
26,241
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/2804621/Python-Prefix-Sum-with-2-Pointers%3A-O(nlogn)-time-O(n)-space
class Solution: def maxFrequency(self, nums: List[int], k: int) -> int: nums.sort() prefix = [] sum = 0 for i in range(len(nums)): sum += nums[i] prefix.append(sum) output = 1 l = 0 for r in range(1, len(nums)): while l < r: length = r - l + 1 expected = nums[r] * length actual = prefix[r] - (0 if l - 1 < 0 else prefix[l - 1]) diff = expected - actual if diff <= k: output = max(output, length) break else: l += 1 return output
frequency-of-the-most-frequent-element
Python Prefix Sum with 2 Pointers: O(nlogn) time, O(n) space
hqz3
0
8
frequency of the most frequent element
1,838
0.386
Medium
26,242
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/2699206/python-working-solution-or-90
class Solution: def maxFrequency(self, nums: List[int], k: int) -> int: nums.sort() l = 0 s = 0 ans = 1 for r in range(len(nums)): while (r-l)*nums[r] > (s+k) and l<r: s-=nums[l] l+=1 s+=nums[r] ans = max(ans,r-l+1) return ans
frequency-of-the-most-frequent-element
python working solution | 90%
Sayyad-Abdul-Latif
0
14
frequency of the most frequent element
1,838
0.386
Medium
26,243
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/2659795/python-beats-92-sort-and-sliding-window
class Solution: def maxFrequency(self, nums: List[int], k: int) -> int: nums.sort() l, r = 0, 0 res, total = 0, 0 for r in range(len(nums)): total += nums[r] while nums[r] * (r - l + 1) > total + k: total -= nums[l] l += 1 res = max(res, r - l + 1) return res
frequency-of-the-most-frequent-element
python beats 92% sort and sliding window
sahilkumar158
0
9
frequency of the most frequent element
1,838
0.386
Medium
26,244
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/2642657/Python-Easy-Solution
class Solution: def maxFrequency(self, nums: List[int], k: int) -> int: nums.sort() back = 0 res = 0 c = 0 for front in range(len(nums)): # acquire--------------------------------------- c += nums[front] # release--------------------------------------- # As the list is sorted for example [2,2,2,3,3,6,7], condition will check the validity, Let back be at 0 and front at 3 indexes respectively, its looks if all the elements from 0 to 3 are valid to change to value at 3 within given cost k...... while nums[front] * (front - back + 1) > c + k: c -= nums[back] back += 1 # collect--------------------------------------- res = max(res, front - back + 1) return res
frequency-of-the-most-frequent-element
Python Easy Solution
user6770yv
0
15
frequency of the most frequent element
1,838
0.386
Medium
26,245
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/2628885/Sliding-window-simple-and-easy-solution
class Solution: def maxFrequency(self, nums: List[int], k: int) -> int: nums.sort() l, r = 0, 0 total, res = 0, 0 while r < len(nums): total += nums[r] while (nums[r] * (r - l + 1 )) > total + k: total -= nums[l] l += 1 res = max(res, r - l + 1) r += 1 return res
frequency-of-the-most-frequent-element
Sliding window simple and easy solution
MaverickEyedea
0
19
frequency of the most frequent element
1,838
0.386
Medium
26,246
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/2283776/Python-Short-Prefix-Sum-and-Binary-Search-with-Detailed-Explanation
class Solution: def maxFrequency(self, nums: List[int], k: int) -> int: def freqExists(freq): for i in range(freq, n): if freq * nums[i-1] - (prefix_sum[i] - prefix_sum[i-freq]) <= k: return True return False nums.sort() prefix_sum = [0] + list(accumulate(nums)) n = len(prefix_sum) l, r = 1, len(nums)+1 ret = 1 while l < r: mid = (l+r)//2 if freqExists(mid): ret = mid l = mid + 1 else: r = mid return ret
frequency-of-the-most-frequent-element
[Python] Short Prefix Sum & Binary Search with Detailed Explanation
lukefall425
0
176
frequency of the most frequent element
1,838
0.386
Medium
26,247
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175044/Python3-greedy
class Solution: def longestBeautifulSubstring(self, word: str) -> int: vowels = "aeiou" ans = 0 cnt = prev = -1 for i, x in enumerate(word): curr = vowels.index(x) if cnt >= 0: # in the middle of counting if 0 <= curr - prev <= 1: cnt += 1 if x == "u": ans = max(ans, cnt) elif x == "a": cnt = 1 else: cnt = -1 elif x == "a": cnt = 1 prev = curr return ans
longest-substring-of-all-vowels-in-order
[Python3] greedy
ye15
9
703
longest substring of all vowels in order
1,839
0.486
Medium
26,248
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175044/Python3-greedy
class Solution: def longestBeautifulSubstring(self, word: str) -> int: ans = 0 cnt = unique = 1 for i in range(1, len(word)): if word[i-1] <= word[i]: cnt += 1 if word[i-1] < word[i]: unique += 1 else: cnt = unique = 1 if unique == 5: ans = max(ans, cnt) return ans
longest-substring-of-all-vowels-in-order
[Python3] greedy
ye15
9
703
longest substring of all vowels in order
1,839
0.486
Medium
26,249
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175044/Python3-greedy
class Solution: def longestBeautifulSubstring(self, word: str) -> int: ans = ii = 0 unique = 1 for i in range(1, len(word)): if word[i-1] > word[i]: ii = i unique = 1 elif word[i-1] < word[i]: unique += 1 if unique == 5: ans = max(ans, i-ii+1) return ans
longest-substring-of-all-vowels-in-order
[Python3] greedy
ye15
9
703
longest substring of all vowels in order
1,839
0.486
Medium
26,250
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175436/Straightforward-Python3-O(n)-stack-solution-with-explanations
class Solution: def longestBeautifulSubstring(self, word: str) -> int: d = {} d['a'] = {'a', 'e'} d['e'] = {'e', 'i'} d['i'] = {'i', 'o'} d['o'] = {'o', 'u'} d['u'] = {'u'} res, stack = 0, [] for c in word: # If stack is empty, the first char must be 'a' if len(stack) == 0: if c == 'a': stack.append(c) continue # If stack is NOT empty, # input char should be the same or subsequent to the last char in stack # e.g., last char in stack is 'a', next char should be 'a' or 'e' # e.g., last char in stack is 'e', next char should be 'e' or 'i' # ... # e.g., last char in stack is 'u', next char should be 'u' if c in d[stack[-1]]: stack.append(c) # If the last char in stack is eventually 'u', # then we have one beautiful substring as candidate, # where we record and update max length of beautiful substring (res) if c == 'u': res = max(res, len(stack)) else: stack = [] if c != 'a' else ['a'] return res
longest-substring-of-all-vowels-in-order
Straightforward Python3 O(n) stack solution with explanations
tkuo-tkuo
6
275
longest substring of all vowels in order
1,839
0.486
Medium
26,251
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1196458/Python3-simple-solution-using-two-pointer-approach
class Solution: def longestBeautifulSubstring(self, s: str) -> int: i,j,x = 0,0,0 while j < len(s): if s[j] in ['a', 'e', 'i', 'o', 'u'] and (s[j-1] <= s[j] or j == 0): j += 1 else: if len(set(s[i:j])) == 5: x = max(x,j-i) i = j j += 1 if len(set(s[i:j])) == 5: x = max(x,j-i) return x
longest-substring-of-all-vowels-in-order
Python3 simple solution using two pointer approach
EklavyaJoshi
3
125
longest substring of all vowels in order
1,839
0.486
Medium
26,252
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1221545/Python-fast-and-simple
class Solution: def longestBeautifulSubstring(self, word: str) -> int: begin = None best = 0 a_detected = False for index, value in enumerate(word): if not a_detected and value == 'a': begin = index a_detected = True elif a_detected and value < word[index-1]: if len(set(word[begin:index])) == 5: best = max(best, len(word[begin:index])) if value == 'a': begin = index else: begin = None a_detected = False best = max(best, len(word[begin:])) if a_detected and len(set(word[begin:])) == 5 else best return best
longest-substring-of-all-vowels-in-order
[Python] fast and simple
cruim
2
192
longest substring of all vowels in order
1,839
0.486
Medium
26,253
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175196/Python-Bruteforce-O(n)-Simple-easy-to-follow-solution
class Solution: def longestBeautifulSubstring(self, word: str) -> int: s=0 prev='a' l=[] for char in word: if char == 'a' and prev == 'a': prev=char s+=1 elif char == 'e' and (prev == 'a' or prev=='e'): prev=char s+=1 elif char == 'i' and (prev =='e' or prev=='i'): prev=char s+=1 elif char == 'o' and (prev == 'i' or prev=='o'): prev=char s+=1 elif char=='u' and (prev =='o' or prev=='u'): prev=char s+=1 if s>=5: l.append(s) else: if char !='a': s=0 prev='a' else: s=1 prev=char if not l: return 0 else: return max(l)
longest-substring-of-all-vowels-in-order
[Python] - Bruteforce O(n) - Simple easy to follow solution
ankitshah009
1
143
longest substring of all vowels in order
1,839
0.486
Medium
26,254
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/2769963/JAVAPYTHON-3-EASY-INTUITIVE-CODE-WITH-EXPLANATION
class Solution: def longestBeautifulSubstring(self, word: str) -> int: if len(word) < 5: return 0 ans = 0 a = e = i = o = u = False idx = 0 while idx < len(word): s = idx while idx < len(word) and word[idx] == 'a': idx += 1 a = True while idx < len(word) and a and word[idx] == 'e': idx += 1 e = True while idx < len(word) and e and word[idx] == 'i': idx += 1 i = True while idx < len(word) and i and word[idx] == 'o': idx += 1 o = True while idx < len(word) and o and word[idx] == 'u': idx += 1 u = True if u: ans = max(ans, idx-s) a = e = i = o = u = False if idx == s: idx += 1 return ans
longest-substring-of-all-vowels-in-order
[JAVA/PYTHON 3] EASY INTUITIVE CODE WITH EXPLANATION ✅
kamdartatv1
0
7
longest substring of all vowels in order
1,839
0.486
Medium
26,255
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/2686577/Python-Simple-Sliding-Window-with-HashSet-or-O(N)
class Solution: def longestBeautifulSubstring(self, word: str) -> int: if len(word) < 5: return 0 l = 0 r = 0 ans = 0 seen = set() while r < len(word): if word[r-1] > word[r]: l = r seen = set() seen.add(word[r]) if len(seen) > 4: ans = max(ans, r - l + 1) r += 1 return ans
longest-substring-of-all-vowels-in-order
Python Simple Sliding Window with HashSet | O(N)
amany5642
0
12
longest substring of all vowels in order
1,839
0.486
Medium
26,256
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/2318354/Python3-or-Sliding-Window-O(n)-with-Hashmap
class Solution: def longestBeautifulSubstring(self, word: str) -> int: L, R = 0, 0 output = 0 length = 0 #edge case: single letter strings1 if(len(word) < 5): return 0 if(word[0] != 'a'): while L < len(word) and word[L] != 'a': L += 1 R = L hashmap = {} while R < len(word)-1: #process right element! #extend length of sliding window! length += 1 char = word[R] hashmap[char] = R #good cases if(char == 'a' and (word[R+1] == 'a' or word[R+1] == 'e')): R += 1 continue if(char == 'e' and (word[R+1] == 'e' or word[R+1] == 'i')): R += 1 continue if(char == 'i' and (word[R+1] == 'i' or word[R+1] == 'o')): R+=1 continue if(char == 'o' and (word[R+1] == 'o' or word[R+1] == 'u')): R += 1 continue if(char == 'u' and (word[R+1] == 'u')): R += 1 continue else: if('a' in hashmap and 'e' in hashmap and 'i' in hashmap and 'o' in hashmap and 'u' in hashmap): output = max(output, length) #go to next valid sliding window! R += 1 L = R length = 0 hashmap = {} #edge case: longest beautiful substring is located at the end or is almost the entire string! R will always end #up at last index based on my while loop iteration up there! if(len(list(hashmap.keys()))) == 5 or (len(list(hashmap.keys())) == 4 and word[R] not in hashmap): output = max(output, length + 1) return output
longest-substring-of-all-vowels-in-order
Python3 | Sliding Window O(n) with Hashmap
JOON1234
0
40
longest substring of all vowels in order
1,839
0.486
Medium
26,257
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/2290518/Sliding-Window-or-Python3
class Solution: def longestBeautifulSubstring(self, word: str) -> int: window_size = 0 value = {'a':1,'e':2,'i':3,'o':4,'u':5} my_set = set() count = 1 last = word[0] my_set.add(last) for i in range(1,len(word)): if value[word[i]]>=value[last]: count += 1 last = word[i] my_set.add(last) elif value[word[i]]<value[last]: if len(my_set) == 5: window_size = max(window_size,count) my_set = {word[i]} count = 1 last = word[i] if count != 1 and len(my_set)==5: window_size = max(count,window_size) return window_size
longest-substring-of-all-vowels-in-order
Sliding Window | Python3
user7457RV
0
17
longest substring of all vowels in order
1,839
0.486
Medium
26,258
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/2102190/Python3-Solution-Noob
class Solution: def longestBeautifulSubstring(self, word: str) -> int: i = 0 j = 1 li = {'a':ord('a'),'e':ord('e'),'i':ord('i'),'o':ord('o'),'u':ord('u')} l = 0 h = {} h[word[0]] = 1 while j<len(word): if li[word[j]]>= li[word[j-1]]: if word[j] in h: h[word[j]]+=1 else: h[word[j]] = 1 j+=1 else: h = {} h[word[j]] = 1 i = j j+=1 if len(h)==5: l = max(l,j-i) return l
longest-substring-of-all-vowels-in-order
Python3 Solution Noob
Brillianttyagi
0
54
longest substring of all vowels in order
1,839
0.486
Medium
26,259
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1653693/Python-O(n)-time-O(1)-space-optimized-sliding-window-solution
class Solution: def longestBeautifulSubstring(self, word: str) -> int: vowels = {'a', 'e', 'i', 'o', 'u'} next_vowel = {'a':{'a', 'e'}, 'e':{'e', 'i'}, 'i':{'i', 'o'}, 'o':{'o', 'u'}, 'u':{'u'}} res = 0 n = len(word) if n < 5: return 0 left, right = 0, 1 while left < right and right < n: while left < n and word[left] != 'a': left += 1 right = left + 1 start = left # now word[left] == == word[start] == 'a' while right < n and word[right] in next_vowel[word[left]]: left += 1 right += 1 if left < n and word[left] == 'u': res = max(res, right-start) left = right right = left + 1 return res
longest-substring-of-all-vowels-in-order
Python O(n) time, O(1) space optimized sliding window solution
byuns9334
0
140
longest substring of all vowels in order
1,839
0.486
Medium
26,260
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1354998/One-pass-88-speed
class Solution: def longestBeautifulSubstring(self, word: str) -> int: vowels = "aeiou" idx_v = max_len = len_sub = 0 for c in word: if not len_sub: if c == "a": len_sub = 1 else: if c == vowels[idx_v]: len_sub += 1 elif idx_v < 4 and c == vowels[idx_v + 1]: len_sub += 1 idx_v += 1 else: if len_sub and idx_v == 4: max_len = max(max_len, len_sub) len_sub = 0 idx_v = 0 if c == "a": len_sub = 1 if len_sub and idx_v == 4: max_len = max(max_len, len_sub) return max_len
longest-substring-of-all-vowels-in-order
One pass, 88% speed
EvgenySH
0
79
longest substring of all vowels in order
1,839
0.486
Medium
26,261
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1216571/Python3-Concise-Two-Pointers
class Solution: def longestBeautifulSubstring(self, word: str) -> int: vowels = ['a', 'e', 'i', 'o', 'u'] l, r, longest = 0, 0, 0 while (l < len(word)): valid = True for vowel in vowels: valid &amp;= (r < len(word) and word[r] == vowel) while (r < len(word) and word[r] == vowel): r += 1 if (valid): longest = max(longest, r - l) l = r return longest
longest-substring-of-all-vowels-in-order
Python3 - Concise Two Pointers
Bruception
0
108
longest substring of all vowels in order
1,839
0.486
Medium
26,262
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1179961/Python-3-consider-consecutive-letters-as-one-and-find-'aeiou'
class Solution: def longestBeautifulSubstring(self, word: str) -> int: def convert(word): cnt = [] s = '' last = 'b' cur = 0 for c in word: if last == c: cur += 1 else: s += last cnt.append(cur) last = c cur = 1 s += last cnt.append(cur) return cnt[1:], s[1:] cnt, s = convert(word) res = 0 for i in range(len(s)-4): if s[i:i+5] == 'aeiou': res = max(res, sum(cnt[i:i+5])) return res
longest-substring-of-all-vowels-in-order
[Python 3] consider consecutive letters as one and find 'aeiou'
joysword
0
72
longest substring of all vowels in order
1,839
0.486
Medium
26,263
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1176459/python3-Easy-to-follow-Faster-than-100
class Solution: def longestBeautifulSubstring(self, word: str) -> int: # if the input string is empty if word == '': return 0 # we can always start from index 0 starts = [0] # if letters are not in ascending order, the corresponding index can be a starting point for i in range(1, len(word)): if ord(word[i]) < ord(word[i-1]): starts.append(i) result = 0 if len(starts) > 1: for i in range(1, len(starts)): if self.check(word[starts[i-1] : starts[i]]): if starts[i] - starts[i-1] > result: result = starts[i] - starts[i-1] if self.check(word[starts[-1] :]): if len(word) - starts[-1] > result: result = len(word) - starts[-1] return result # to check if all vowel letters exist inside the substring def check(self, input_str): if 'a' in input_str: if 'e' in input_str: if 'i' in input_str: if 'o' in input_str: if 'u' in input_str: return True return False
longest-substring-of-all-vowels-in-order
[python3] Easy to follow - Faster than 100%
zafarman
0
61
longest substring of all vowels in order
1,839
0.486
Medium
26,264
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175923/Simple-Python-Solution-or-Faster-than-100
class Solution: def longestBeautifulSubstring(self, word: str) -> int: next = {"": "", "a": "e", "e": "i", "i": "o", "o": "u", "u": "*"} ans, left, past = 0, None, "" for right in range(len(word)): current = word[right] if past != current: if current == "a": left = right elif current != next[past]: left = None if left != None and current == "u": ans = max(ans, right - left + 1) past = current return ans
longest-substring-of-all-vowels-in-order
Simple Python Solution | Faster than 100%
krip_vk
0
57
longest substring of all vowels in order
1,839
0.486
Medium
26,265
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175378/Python-O(n)
class Solution: def longestBeautifulSubstring(self, word: str) -> int: result = 0 preWord = word[0] counter = defaultdict(int) counter[preWord] += 1 totalVowels = 1 maxSoFar = 1 for w in word[1:]: if preWord == w or \ preWord == 'a' and w == 'e' or \ preWord == 'e' and w == 'i' or \ preWord == 'i' and w == 'o' or \ preWord == 'o' and w == 'u': pass else: maxSoFar = 0 counter = defaultdict(int) totalVowels = 0 preWord = w counter[w] += 1 if counter[w] == 1: totalVowels += 1 maxSoFar += 1 if totalVowels == 5: result = max(result, maxSoFar) return result
longest-substring-of-all-vowels-in-order
Python O(n)
pochy
0
43
longest substring of all vowels in order
1,839
0.486
Medium
26,266
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175166/Python-3%3A-O(n)-Simple-one-pass-solution-score-basis
class Solution: def longestBeautifulSubstring(self, word: str) -> int: score = { 'a': 1, 'e': 2, 'i': 3, 'o': 4, 'u': 5 } prevScore = 0 maxBeauty = beauty = 0 seen = set() for ch in word: currentScore = score[ch] if currentScore >= prevScore: seen.add(ch) beauty += 1 else: if len(seen) == 5 and beauty > maxBeauty: maxBeauty = beauty seen = {ch} beauty = 1 prevScore = currentScore return max(beauty, maxBeauty) if len(seen) == 5 else maxBeauty
longest-substring-of-all-vowels-in-order
[Python 3]: O(n) Simple one pass solution - score basis
AB07
0
63
longest substring of all vowels in order
1,839
0.486
Medium
26,267
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175161/Python3%3A-Linear-scan-and-update
class Solution: def longestBeautifulSubstring(self, word: str) -> int: d = {"a":0, "e":1, "i":2, "o":3 , "u":4} max_ = 0 curr_letter = 0 curr_sum = 0 sum_ = 0 for l in word: if d[l] == curr_letter: curr_sum += 1 elif curr_sum > 0 and d[l] == curr_letter + 1: sum_ += curr_sum curr_sum = 1 curr_letter += 1 else: if curr_sum > 0 and curr_letter == 4: max_ = max(max_, sum_+curr_sum) curr_sum = 1 if l == "a" else 0 curr_letter = 0 sum_ = 0 #print(l, curr_sum, curr_letter, max_) return max(max_, sum_+curr_sum) if (curr_sum > 0 and curr_letter == 4) else max_
longest-substring-of-all-vowels-in-order
Python3: Linear scan and update
zl3311
0
40
longest substring of all vowels in order
1,839
0.486
Medium
26,268
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175099/Python-simple-solution-O(n)
class Solution: def longestBeautifulSubstring(self, word: str) -> int: if word == 'aeiou': return 5 def check(s): res = '' ind = [] for i,c in enumerate(s): if res =='': res+=c ind.append(i) elif c == 'a' and res[-1] != c: res+=c ind.append(i) elif c == 'u' and res[-1] == c: ind[-1] = i elif res[-1] != c: res+=c ind.append(i) return ind ind = check(word) #print(ind) mx = 0 for i in range(len(ind)): for j in range(i,i+5): if j+4 <len(ind): if (word[ind[j]],word[ind[j+1]],word[ind[j+2]],word[ind[j+3]],word[ind[j+4]]) == ('a','e','i','o','u'): mx = max(mx, ind[j+4]-ind[j]+1) #print(ind[j+4], ind[j]) return mx
longest-substring-of-all-vowels-in-order
Python simple solution O(n)
deleted_user
0
61
longest substring of all vowels in order
1,839
0.486
Medium
26,269
https://leetcode.com/problems/maximum-building-height/discuss/1175057/Python3-greedy
class Solution: def maxBuilding(self, n: int, restrictions: List[List[int]]) -> int: restrictions.extend([[1, 0], [n, n-1]]) restrictions.sort() for i in reversed(range(len(restrictions)-1)): restrictions[i][1] = min(restrictions[i][1], restrictions[i+1][1] + restrictions[i+1][0] - restrictions[i][0]) ans = 0 for i in range(1, len(restrictions)): restrictions[i][1] = min(restrictions[i][1], restrictions[i-1][1] + restrictions[i][0] - restrictions[i-1][0]) ans = max(ans, (restrictions[i-1][1] + restrictions[i][0] - restrictions[i-1][0] + restrictions[i][1])//2) return ans
maximum-building-height
[Python3] greedy
ye15
2
257
maximum building height
1,840
0.353
Hard
26,270
https://leetcode.com/problems/maximum-building-height/discuss/1623986/Python-Beats-100-sort-%2B-one-pass-solution-O(nlog(n))
class Solution: def maxBuilding(self, n: int, restrictions: List[List[int]]) -> int: if not restrictions: return n - 1 restrictions.append([1, 0]) # Add the restriction for the initial position restrictions.sort(key=lambda x: x[1] + x[0]) # Sort by increasing i + h idx = 0 # The index in the restrictions array max_height = 0 while idx < len(restrictions): pos, h = restrictions[idx] idx += 1 while idx < len(restrictions) and restrictions[idx][1] - restrictions[idx][0] >= h - pos: # skip the next restriction if it is "above" the line starting from the current one idx += 1 if idx == len(restrictions): # Handles the last restriction: fill the line until the last position at n max_height = max(max_height, h + n - pos) break next_pos, next_h = restrictions[idx] # A bit of maths gives us the formula for the maximum height between two consecutive # restrictions max_height = max(max_height, (h + next_h + next_pos - pos) // 2) return max_height
maximum-building-height
[Python] Beats 100%, sort + one pass solution, O(nlog(n))
skasch
0
128
maximum building height
1,840
0.353
Hard
26,271
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1243646/Python3-simple-code-96-time-with-explanation
class Solution: def replaceDigits(self, s: str) -> str: ans = "" def shift(char, num): return chr(ord(char) + int(num)) for index in range(len(s)): ans += shift(s[index-1], s[index]) if index % 2 else s[index] return ans
replace-all-digits-with-characters
Python3 simple code 96% time, with explanation
albezx0
5
327
replace all digits with characters
1,844
0.798
Easy
26,272
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1186143/Python-1-Liner-%2B-Simple-Readable
class Solution: def replaceDigits(self, s: str) -> str: return ''.join(chr(ord(s[i-1]) + int(s[i])) if s[i].isdigit() else s[i] for i in range(len(s)))
replace-all-digits-with-characters
Python 1 Liner + Simple Readable
leeteatsleep
5
415
replace all digits with characters
1,844
0.798
Easy
26,273
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1186143/Python-1-Liner-%2B-Simple-Readable
class Solution: def replaceDigits(self, s: str) -> str: answer = [] for i, char in enumerate(s): if char.isdigit(): char = chr(ord(s[i-1]) + int(char)) answer.append(char) return ''.join(answer)
replace-all-digits-with-characters
Python 1 Liner + Simple Readable
leeteatsleep
5
415
replace all digits with characters
1,844
0.798
Easy
26,274
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1245765/Python3-Solution
class Solution: def replaceDigits(self, s: str) -> str: res = "" for index, char in enumerate(s): # for each odd number if index % 2 != 0 and index != 0: # get the previous character previous_ord = ord(s[index-1]) # get the current character # by summing ordinal of previous and current number this = previous_ord + int(char) # append the chr of the ordinal back to result res += chr(this) else: res += char return res
replace-all-digits-with-characters
Python3 Solution
rahulkp220
3
203
replace all digits with characters
1,844
0.798
Easy
26,275
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2807747/faster-than-100-python-solution
class Solution: def replaceDigits(self, s: str) -> str: a = list(s) for i in range(1, len(a), 2): a[i] = chr(ord(a[i - 1]) + int(a[i])) return ''.join(a)
replace-all-digits-with-characters
faster than 100% python solution
sushants007
2
57
replace all digits with characters
1,844
0.798
Easy
26,276
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1276488/Python-or-Two-solutions%3A-one-liner-and-readable
class Solution: def replaceDigits(self, s: str) -> str: ans = list() for i in range(len(s)): if s[i].isdigit(): ans.append(chr(ord(s[i-1]) + int(s[i]))) else: ans.append(s[i]) return ''.join(ans)
replace-all-digits-with-characters
Python | Two solutions: one-liner and readable
iaramer
2
134
replace all digits with characters
1,844
0.798
Easy
26,277
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1276488/Python-or-Two-solutions%3A-one-liner-and-readable
class Solution: def replaceDigits(self, s: str) -> str: return ''.join([chr(ord(s[i-1]) + int(s[i])) if s[i].isdigit() else s[i] for i in range(len(s))])
replace-all-digits-with-characters
Python | Two solutions: one-liner and readable
iaramer
2
134
replace all digits with characters
1,844
0.798
Easy
26,278
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2780630/Python-simple-solution-with-the-shift-function
class Solution: def replaceDigits(self, s: str) -> str: s = list(s) def shift(c, x): alphabet = 'abcdefghijklmnopqrstuvwxyz' index = alphabet.index(c) return alphabet[index + x] for i in range(1, len(s), 2): s[i] = shift(s[i - 1], int(s[i])) return ''.join(s)
replace-all-digits-with-characters
Python simple solution with the shift function
Mark_computer
1
6
replace all digits with characters
1,844
0.798
Easy
26,279
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1185902/PythonPython3-Solution
class Solution: def replaceDigits(self, s: str) -> str: alpha = 'abcdefghijklmnopqrstuvwxyz' s = list(s) for i in range(1,len(s),2): s[i] = alpha[(ord(s[i-1])-97)+int(s[i])] return ''.join(s)
replace-all-digits-with-characters
Python/Python3 Solution
prasanthksp1009
1
181
replace all digits with characters
1,844
0.798
Easy
26,280
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2810115/Python3-Solution-shift-with-chr-and-ord
class Solution: def replaceDigits(self, s: str) -> str: i = 0 ans = "" while i < len(s): ans = ans + s[i] if i + 1 < len(s): ans = ans + chr(ord(s[i]) + int(s[i+1])) i += 2 return ans
replace-all-digits-with-characters
Python3 Solution - shift with chr and ord
sipi09
0
2
replace all digits with characters
1,844
0.798
Easy
26,281
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2774280/Replace-All-Digits-with-Characters-or-PYTHON
class Solution: def replaceDigits(self, s: str) -> str: result="" a="abcdefghijklmnopqrstuvwxyz" for i in s: if i.isdigit(): result+=a[a.index(result[-1]) + int(i)] else: result+=i return result
replace-all-digits-with-characters
Replace All Digits with Characters | PYTHON
saptarishimondal
0
1
replace all digits with characters
1,844
0.798
Easy
26,282
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2764977/python-solution-90-faster
class Solution: def replaceDigits(self, s: str) -> str: alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] new_string = [] for i in range(0, len(s) - 1, 2): new_string.append(s[i]) index_alphabet_to_replace = alphabets.index(s[i]) + int(s[i+1]) new_string.append(alphabets[index_alphabet_to_replace]) if len(s) % 2 == 0: return "".join(new_string) elif len(s) % 2 != 0 and s[-1].isdigit() is False: return "".join(new_string) + s[-1]
replace-all-digits-with-characters
python solution 90% faster
samanehghafouri
0
2
replace all digits with characters
1,844
0.798
Easy
26,283
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2743144/Python3-Utterly-unreadable-One-Liner
class Solution: def replaceDigits(self, s: str) -> str: return "".join([f"{char}{chr(ord(char)+int(s[idx*2+1])) if s[idx*2+1:] else ''}" for idx, char in enumerate(s[::2])])
replace-all-digits-with-characters
[Python3] - Utterly unreadable One-Liner
Lucew
0
3
replace all digits with characters
1,844
0.798
Easy
26,284
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2709912/Runtime-20-ms-faster-than-91-or-Easy-to-Understand-or-Python
class Solution(object): def replaceDigits(self, s): alpha = {'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} num = {1: 'a',2: 'b',3: 'c', 4: 'd', 5: 'e', 6:'f', 7:'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k',12: 'l',13: 'm', 14: 'n', 15: 'o', 16:'p', 17:'q', 18: 'r', 19: 's', 20: 't', 21: 'u',22: 'v',23: 'w', 24: 'x', 25: 'y', 26:'z'} ans = '' for i in range(0, len(s)): if i % 2 == 0: ans += s[i] else: ans += num[alpha[s[i-1]] + int(s[i])] return ans
replace-all-digits-with-characters
Runtime 20 ms, faster than 91% | Easy to Understand | Python
its_krish_here
0
5
replace all digits with characters
1,844
0.798
Easy
26,285
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2659942/Runtime%3A-30-ms-or-Memory-Usage%3A-13.8-MB
class Solution: def replaceDigits(self, s: str) -> str: if len(s) == 1: return s result = [] * len(s) for i, j in zip(range(0, len(s), 2), range(1, len(s), 2)): result.append(s[i]) result.append(chr(ord(s[i]) + int(s[j]))) if len(s) % 2: result.append(s[-1]) return "".join(result)
replace-all-digits-with-characters
Runtime: 30 ms | Memory Usage: 13.8 MB
kcstar
0
4
replace all digits with characters
1,844
0.798
Easy
26,286
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2603903/Python-Solution
class Solution: def replaceDigits(self, s: str) -> str: r='' n=len(s) for i in range(n): if i%2!=0: # print(ord(s[i-1])+int(s[i])) r+=chr(ord(s[i-1])+int(s[i])) else: r+=s[i] return r
replace-all-digits-with-characters
Python Solution
Siddharth_singh
0
24
replace all digits with characters
1,844
0.798
Easy
26,287
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2574359/Python-easy-to-understand-solution
class Solution: def replaceDigits(self, s: str) -> str: r= "" n= len(s) for i in range(n): if s[i].isdigit(): t= s[i-1] k= ord(t) m= int(s[i]) r+= chr(k+m) else: r+=s[i] return r
replace-all-digits-with-characters
Python easy to understand solution
trickycat10
0
8
replace all digits with characters
1,844
0.798
Easy
26,288
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2535827/Replace-All-Digits-with-Characters
class Solution: def replaceDigits(self, s: str) -> str: out = [] for i in range(len(s)): if s[i].isdigit(): out.append(chr(ord(s[i-1])+int(s[i]))) else: out.append(s[i]) return ("".join(out))
replace-all-digits-with-characters
Replace All Digits with Characters
dhananjayaduttmishra
0
8
replace all digits with characters
1,844
0.798
Easy
26,289
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2324259/Short-python-solution-beats-97-(chr-and-ord)
class Solution: def replaceDigits(self, s: str) -> str: for i in range(len(s)): if i%2: s = (s[:i] + (chr(ord(s[i-1]) + int(s[i]))) + s[i+1:]) return s
replace-all-digits-with-characters
Short python solution - beats 97% (chr and ord)
SabeerN
0
45
replace all digits with characters
1,844
0.798
Easy
26,290
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2215657/Python-simple-and-understanbale
class Solution: def replaceDigits(self, s: str) -> str: result = "" for index in range(0, len(s)): if s[index].isdigit(): result += self.shift(s[index-1], int(s[index])) else: result += s[index] return result def shift(self, c, number) -> str: next_c = ord(c) + number return chr(next_c)
replace-all-digits-with-characters
Python simple and understanbale
CleverUzbek
0
35
replace all digits with characters
1,844
0.798
Easy
26,291
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2038694/Faster-than-28
class Solution: def replaceDigits(self, s: str) -> str: a="abcdefghijklmnopqrstuvwxyz" b="" for i in range(len(s)): if(s[i].isdigit()): b+=a[a.index(b[-1])+int(s[i])] else: b+=s[i] return (b)
replace-all-digits-with-characters
Faster than 28%
Durgavamsi
0
21
replace all digits with characters
1,844
0.798
Easy
26,292
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2002360/Python-O(N)-Time-Complexity
class Solution: def replaceDigits(self, s: str) -> str: res = [] #s = [i for i in s] for i in range(len(s)): if i%2 != 0: index = ord(s[i-1]) - ord('a') shift = chr((index + int(s[i])) % 26 + ord('a')) res.append(shift) else: res.append(s[i]) return ''.join(res)
replace-all-digits-with-characters
Python - O(N) Time Complexity
dayaniravi123
0
43
replace all digits with characters
1,844
0.798
Easy
26,293
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1976505/Python-Easy-Solution-or-38ms
class Solution: def replaceDigits(self, s: str) -> str: final_str = "" for i in range(len(s)): if not s[i].isdecimal(): final_str += s[i] else: final_str += chr(ord(s[i-1])+int(s[i])) return final_str
replace-all-digits-with-characters
Python Easy Solution | 38ms
pranavv14
0
76
replace all digits with characters
1,844
0.798
Easy
26,294
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1927415/Python-Clean-and-Simple!-%2B-One-Liner
class Solution: def replaceDigits(self, s): def shift(c,x): return chr(ord(c)+int(x)) return "".join(shift(s[i-1],s[i]) if s[i].isdigit() else s[i] for i in range(len(s)))
replace-all-digits-with-characters
Python - Clean and Simple! + One-Liner
domthedeveloper
0
51
replace all digits with characters
1,844
0.798
Easy
26,295
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1927415/Python-Clean-and-Simple!-%2B-One-Liner
class Solution: def replaceDigits(self, s): return "".join(chr(ord(s[i-1])+int(s[i])) if s[i].isdigit() else s[i] for i in range(len(s)))
replace-all-digits-with-characters
Python - Clean and Simple! + One-Liner
domthedeveloper
0
51
replace all digits with characters
1,844
0.798
Easy
26,296
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1867277/Python-solution-with-memory-less-than-98
class Solution: def replaceDigits(self, s: str) -> str: res = "" for i in range(len(s)): if s[i].isalpha(): res += s[i] elif s[i].isnumeric(): res += chr(ord(s[i-1]) + int(s[i])) return res
replace-all-digits-with-characters
Python solution with memory less than 98%
alishak1999
0
79
replace all digits with characters
1,844
0.798
Easy
26,297
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1859466/Python-dollarolution
class Solution: def replaceDigits(self, s: str) -> str: word = '' for i in range(len(s)): if s[i].isalpha(): word += s[i] continue word += chr(ord(s[i-1])+int(s[i])) return word
replace-all-digits-with-characters
Python $olution
AakRay
0
35
replace all digits with characters
1,844
0.798
Easy
26,298
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1764886/Replace-All-Digits-with-Characters-solution-easy
class Solution: def replaceDigits(self, s: str) -> str: news = "" for i in range(1,len(s),2): new = ord(s[i-1]) + int(s[i]) news += s[i-1] + chr(new) if ord(s[-1]) not in range(48,58): news += s[-1] return news
replace-all-digits-with-characters
Replace All Digits with Characters solution easy
seabreeze
0
38
replace all digits with characters
1,844
0.798
Easy
26,299