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/min-cost-climbing-stairs/discuss/2737248/O(n)-time-and-O(1)-space-in-python3-explained
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: twoback = cost[0] oneback = cost[1] curr = 0 for i in range(2, len(cost)): curr = cost[i] + min(twoback, oneback) twoback = oneback oneback = curr return min(twoback,...
min-cost-climbing-stairs
O(n) time and O(1) space in python3 explained
ThatTallProgrammer
0
3
min cost climbing stairs
746
0.625
Easy
12,300
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2736065/Minimum-cost-to-reach-Stair
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: stepcount = len(cost) stepcost = [0]*(stepcount+1) if stepcount<3: return min(cost) stepcost[1] = cost[0] stepcost[2] = cost[1] for i in range(2,stepcount): ste...
min-cost-climbing-stairs
Minimum cost to reach Stair
tkrishnakumar30
0
2
min cost climbing stairs
746
0.625
Easy
12,301
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2728934/Python-Solution-or-1D-Bottom-Up-Dynamic-Programming-Based
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: # len(cost) + 1 length auxillary array dp = [0] * (len(cost) + 1) # cost of dp[n] = 0 : top # cost of dp[n-1], dp[n-2] are costs[n-1], costs[n-2] respectively dp[-2] = cost[-1] dp[-3] = cost[-2] # bu...
min-cost-climbing-stairs
Python Solution | 1D - Bottom Up Dynamic Programming Based
Gautam_ProMax
0
7
min cost climbing stairs
746
0.625
Easy
12,302
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2715120/Min-Cost-Climbing-Stairs-in-python-very-easy-solution
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: cost.append(0) for i in range(len(cost)-3,-1,-1) : cost[i] += min(cost[i+1],cost[i+2]) return min(cost[0],cost[1])
min-cost-climbing-stairs
Min Cost Climbing Stairs in python very easy solution
jashii96
0
2
min cost climbing stairs
746
0.625
Easy
12,303
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2663502/Python-intuitive-decision-in-3-lines
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: for i in range(2, len(cost)): cost[i] += min(cost[i-1], cost[i-2]) return min(cost[-1], cost[-2])
min-cost-climbing-stairs
Python, intuitive decision in 3 lines
drixe
0
4
min cost climbing stairs
746
0.625
Easy
12,304
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2659214/here-is-my-solution-O(n)-greatergreater%3A)
class Solution: def minCostClimbingStairs(self, c: List[int]) -> int: c.append(0) #cost for top floor if(len(c)==2): return min(c[0],c[1]) else: prev2=c[0] prev1=c[1] for i in range(2,len(c)): crnt1=prev1+c[i]...
min-cost-climbing-stairs
here is my solution O(n)->>:)
re__fresh
0
3
min cost climbing stairs
746
0.625
Easy
12,305
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2650530/(beats-90)
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: table=list(map(lambda x:0,range(len(cost)+1))) table[-1]=1 i=len(cost) while i>=0: if table!=0: for t in (1,2): f=i-t if f>=0 : ...
min-cost-climbing-stairs
(beats 90%)
chitranshagrawal1804
0
1
min cost climbing stairs
746
0.625
Easy
12,306
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2645914/Python-recursion.
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: def climb(idx: int) -> int: if len(cost) - idx < 3: # reach top of stairs return cost[idx] elif idx in memo: # no need to calculate again return memo[idx] else: ...
min-cost-climbing-stairs
Python, recursion.
woora3
0
12
min cost climbing stairs
746
0.625
Easy
12,307
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2645698/Python-Solution
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: cost.append(0) for i in range(len(cost) - 3, -1, -1): cost[i] = min((cost[i] + cost[i+1]), cost[i] + cost[i + 2]) return min(cost[0], cost[1])
min-cost-climbing-stairs
Python Solution
mueezsheersho95
0
4
min cost climbing stairs
746
0.625
Easy
12,308
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2644086/Python-easy-solution
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: totalCost = [0]*len(cost) totalCost[-1], totalCost[-2] = cost[-1], cost[-2] for i in range(len(cost)-3, -1, -1): totalCost[i] = cost[i] + min(totalCost[i+1], totalCost[i+2]) return min(totalCost[0], ...
min-cost-climbing-stairs
Python easy solution
Jack_Chang
0
48
min cost climbing stairs
746
0.625
Easy
12,309
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2595738/Simple-python-code-with-explanation
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: cost.append(0) for i in range(len(cost)-3,-1,-1): cost[i] = min((cost[i] + cost[i+1]) , (cost[i] + cost[i+2])) return min(cost[0],cost[1])
min-cost-climbing-stairs
Simple python code with explanation
thomanani
0
40
min cost climbing stairs
746
0.625
Easy
12,310
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2586032/Self-Understandable-Python%3A
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: slow=cost[0] fast=cost[1] for i in range(2,len(cost)): slow,fast=fast,cost[i]+min(slow,fast) return min(slow,fast)
min-cost-climbing-stairs
Self Understandable Python:
goxy_coder
0
16
min cost climbing stairs
746
0.625
Easy
12,311
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2547215/EASY-PYTHON3-SOLUTION-O(N)
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: dp = [0]*len(cost) dp [-1], dp [-2] = cost[-1], cost[-2] for i in range(len(cost)-3, -1, -1): dp[i] = min(dp[i+1], dp[i+2]) + cost[i] return min(dp[0],dp[1])
min-cost-climbing-stairs
βœ…βœ”πŸ”₯ EASY PYTHON3 SOLUTION πŸ”₯O(N)βœ…βœ”
rajukommula
0
65
min cost climbing stairs
746
0.625
Easy
12,312
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2541187/python-dp-solution-using-recursion
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: dp={} def check(idx): if idx>len(cost)-1: return 0 if idx in dp: return dp[idx] dp[idx]=cost[idx]+min(check(idx+1),check(idx+2)) return dp[idx] ...
min-cost-climbing-stairs
python dp solution using recursion
benon
0
47
min cost climbing stairs
746
0.625
Easy
12,313
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2442330/PYTHON-3-oror-Clean-and-Short-Solution-oror-DP
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: cost.append(0) for i in range(len(cost)-3, -1, -1): cost[i] += min(cost[i+1], cost[i+2]) return min(cost[0], cost[1])
min-cost-climbing-stairs
[PYTHON 3] || Clean and Short Solution || DP
WhiteBeardPirate
0
37
min cost climbing stairs
746
0.625
Easy
12,314
https://leetcode.com/problems/min-cost-climbing-stairs/discuss/2415637/Python3-Simple-Min-Cost-w-Explanation
class Solution: def minCostClimbingStairs(self, cost: List[int]) -> int: # Handle empty cost case if not cost: return 0 # min_cost is the minimum cost to get to some step i min_cost = [0] * len(cost) min_cost[0] = cost[0] # Loop through. At each i, ...
min-cost-climbing-stairs
[Python3] Simple Min Cost w Explanation
connorthecrowe
0
85
min cost climbing stairs
746
0.625
Easy
12,315
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/2069691/no-loops
class Solution: def dominantIndex(self, nums: List[int]) -> int: if len(nums) is 1: return 0 dom = max(nums) i = nums.index(dom) nums.remove(dom) if max(nums) * 2 <= dom: return i return -1
largest-number-at-least-twice-of-others
no loops
andrewnerdimo
3
90
largest number at least twice of others
747
0.465
Easy
12,316
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/2645620/Python-Simple-or-Clean-or-One-Pass-or-beat-98-O(N)-and-O(1)
class Solution: def dominantIndex(self, nums: List[int]) -> int: largest = max(nums) for idx, val in enumerate(nums): if val==largest: new_idx = idx continue if val*2>largest: return -1 return new_idx
largest-number-at-least-twice-of-others
βœ… [Python] Simple | Clean | One Pass | beat 98% O(N) & O(1)
girraj_14581
1
28
largest number at least twice of others
747
0.465
Easy
12,317
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/2403749/No-Loops-Solution
class Solution: def dominantIndex(self, nums: List[int]) -> int: ind = nums.index(max(nums)) nums.sort() if 2*nums[-2] <= nums[-1]: return ind return -1
largest-number-at-least-twice-of-others
No Loops Solution
Abhi_-_-
1
53
largest number at least twice of others
747
0.465
Easy
12,318
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1944193/Python-Fast-Solution-or-Sorting-Solution
class Solution: def dominantIndex(self, nums): if len(nums) == 1: return 0 s = sorted(nums, reverse=True) largest, secondLargest = s[0], s[1] return nums.index(largest) if largest >= 2*secondLargest else -1
largest-number-at-least-twice-of-others
Python - Fast Solution | Sorting Solution
domthedeveloper
1
85
largest number at least twice of others
747
0.465
Easy
12,319
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1944193/Python-Fast-Solution-or-Sorting-Solution
class Solution: def dominantIndex(self, nums): max1_val, max1_idx, max2_val = -1, -1, -1 for i,n in enumerate(nums): if n > max1_val: max2_val = max1_val max1_val, max1_idx = n, i elif n > max2_val: max2_val = n ...
largest-number-at-least-twice-of-others
Python - Fast Solution | Sorting Solution
domthedeveloper
1
85
largest number at least twice of others
747
0.465
Easy
12,320
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1317645/Python3-dollarolution(Faster-than-99)
class Solution: def dominantIndex(self, nums: List[int]) -> int: if len(nums) == 1: return 0 n = nums[0:] n.sort() if n[-1] >= 2 * n[-2]: return nums.index(n[-1]) return -1
largest-number-at-least-twice-of-others
Python3 $olution(Faster than 99%)
AakRay
1
176
largest number at least twice of others
747
0.465
Easy
12,321
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/401820/Python-Simple-and-Concise-solution
class Solution: def dominantIndex(self, nums: List[int]) -> int: if len(nums)==1: return 0 n = sorted(nums) if n[-1]>=n[-2]*2: return nums.index(n[-1]) else: return -1
largest-number-at-least-twice-of-others
Python Simple and Concise solution
saffi
1
155
largest number at least twice of others
747
0.465
Easy
12,322
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/2816996/Easy-Python-Solution-and-two-linear-solution
class Solution: def dominantIndex(self, nums: List[int]) -> int: largest_ele = float("-inf") second_largest_ele = float("-inf") for num in nums: if num > largest_ele: second_largest_ele = largest_ele largest_ele = num if num < ...
largest-number-at-least-twice-of-others
Easy Python Solution and two linear solution
danishs
0
1
largest number at least twice of others
747
0.465
Easy
12,323
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/2770835/Logical-and-easy-solution.-Python.-Time%3A0(N)-Space-0(1)
class Solution: def dominantIndex(self, nums: List[int]) -> int: #Find the original index largest = nums[0] currind = 0 for ind, val in enumerate (nums): if val > largest: largest = val currind = ind nums.remove(largest) ...
largest-number-at-least-twice-of-others
Logical and easy solution. Python. Time:0(N), Space 0(1)
dapilk101
0
3
largest number at least twice of others
747
0.465
Easy
12,324
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/2733378/Python-Solution-Simple-Sorting-algorithm
class Solution: def dominantIndex(self, nums: List[int]) -> int: arr = sorted(nums) #.sort() highest = arr[-1] second = arr[-2] if second == 0: return nums.index(highest) if highest // second >= 2: return nums.index(highest) return -1
largest-number-at-least-twice-of-others
Python Solution - Simple Sorting algorithm
dayaniravi123
0
1
largest number at least twice of others
747
0.465
Easy
12,325
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/2728742/One-pass-constant-space-python-solution
class Solution: def dominantIndex(self, nums: List[int]) -> int: max1, max2 = -1, -1 m1id = -1 for i in range(len(nums)): n = nums[i] if n > max1: max2 = max1 m1id = i max1 = n elif n > max2: ...
largest-number-at-least-twice-of-others
One pass constant space python solution
lmnvs
0
1
largest number at least twice of others
747
0.465
Easy
12,326
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/2726772/Python-Solution-Using-Max-Heap
class Solution: def dominantIndex(self, nums: List[int]) -> int: h = [] for index in range(len(nums)): heappush(h, (-1*nums[index], index)) #Reverse min heap to max heap by reversing sign. largest_num_pair = heappop(h) #Get largest pair. largest_num = -1*largest_num_pair...
largest-number-at-least-twice-of-others
Python Solution Using Max Heap
mephiticfire
0
3
largest number at least twice of others
747
0.465
Easy
12,327
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/2642341/Python-Simple-and-Easy-Solution
class Solution(object): def dominantIndex(self, n): l = n l = sorted(l) r = l.pop() if r >= (l[-1])*2: return n.index(r) return -1
largest-number-at-least-twice-of-others
Python Simple and Easy Solution
SouravSingh49
0
13
largest number at least twice of others
747
0.465
Easy
12,328
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/2305581/Python-5-liner-oror-Beats-88-in-runtime
class Solution: def dominantIndex(self, nums: List[int]) -> int: index=nums.index(max(nums)) nums.sort(reverse=True) if nums[0]>=nums[1]*2: return index else: return -1
largest-number-at-least-twice-of-others
Python 5 liner || Beats 88% in runtime
keertika27
0
41
largest number at least twice of others
747
0.465
Easy
12,329
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/2263479/Python-2-Liner
class Solution: def dominantIndex(self, nums: List[int]) -> int: s = sorted([(nums[i], i) for i in range(len(nums))], reverse=True) return s[0][1] if s[0][0] / 2 >= s[1][0] else -1
largest-number-at-least-twice-of-others
Python 2-Liner
amaargiru
0
22
largest number at least twice of others
747
0.465
Easy
12,330
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/2130704/Python-simple-solution
class Solution: def dominantIndex(self, nums: List[int]) -> int: if len(nums) == 1: return 0 return nums.index(max(nums)) if sorted(nums)[-1] >= sorted(nums)[-2]*2 else -1
largest-number-at-least-twice-of-others
Python simple solution
StikS32
0
46
largest number at least twice of others
747
0.465
Easy
12,331
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/2015323/Python3-using-for-loop
class Solution: def dominantIndex(self, nums: List[int]) -> int: maxN = max(nums) indexMaxN = nums.index(maxN) isTwice = None nums.sort() if len(nums) == 1: return 0 for n in range(len(nums)): if nums[n] != maxN: ...
largest-number-at-least-twice-of-others
[Python3] using for loop
Shiyinq
0
26
largest number at least twice of others
747
0.465
Easy
12,332
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1926619/largest-number-at-least-twice-of-others
class Solution: def dominantIndex(self, nums: List[int]) -> int: m=max(nums) a=nums.index(m) arr=[] g=0 for i in nums: if i!=m: arr.append(i) if len(arr)!=0: g=max(arr) if m>=2*g: return a else: ...
largest-number-at-least-twice-of-others
largest number at least twice of others
anil5829354
0
27
largest number at least twice of others
747
0.465
Easy
12,333
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1877010/Python-l-Simple-O(2n)
class Solution: def dominantIndex(self, nums: List[int]) -> int: n = len(nums) max_1 = -inf max_2 = -inf for i in range(n): if max_1 < nums[i]: max_1 = nums[i] idx = i for num in nums: if num != max_1: max_2 = max(num,max_2) if max_1 >= 2*max_2 : return idx return -1
largest-number-at-least-twice-of-others
Python l Simple O(2n)
morpheusdurden
0
43
largest number at least twice of others
747
0.465
Easy
12,334
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1857544/Simple-and-Easy-to-Understand-python-solution
class Solution: def dominantIndex(self, nums: List[int]) -> int: largest_number = max(nums) for number in nums: if number != largest_number and (number + number) > largest_number: return -1 return nums.index(largest_number)
largest-number-at-least-twice-of-others
Simple and Easy to Understand python solution
hardik097
0
31
largest number at least twice of others
747
0.465
Easy
12,335
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1853076/Python-(Simple-approach-and-Beginner-friendly)
class Solution: def dominantIndex(self, nums: List[int]) -> int: if len(nums) <= 1: return 0 else: nums1 = nums[:] nums1.sort() if (nums1[-2]*2) <= nums1[-1]: return nums.index(nums1[-1]) return -1
largest-number-at-least-twice-of-others
Python (Simple approach and Beginner-friendly)
vishvavariya
0
47
largest number at least twice of others
747
0.465
Easy
12,336
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1834341/3-Lines-Python-Solution-oror-50-Faster-oror-Memory-less-than-75
class Solution: def dominantIndex(self, nums: List[int]) -> int: if len(nums)==1: return 0 S=sorted(nums) return [nums.index(S[-1]) if S[-1]>=S[-2]*2 else -1][0]
largest-number-at-least-twice-of-others
3-Lines Python Solution || 50% Faster || Memory less than 75%
Taha-C
0
34
largest number at least twice of others
747
0.465
Easy
12,337
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1834341/3-Lines-Python-Solution-oror-50-Faster-oror-Memory-less-than-75
class Solution: def dominantIndex(self, nums: List[int]) -> int: return nums.index(max(nums)) if all(i*2<=max(nums) for i in nums if i!=max(nums)) else -1
largest-number-at-least-twice-of-others
3-Lines Python Solution || 50% Faster || Memory less than 75%
Taha-C
0
34
largest number at least twice of others
747
0.465
Easy
12,338
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1815071/My-solution-in-Python-and-Kotlin
class Solution: def dominantIndex(self, nums: List[int]) -> int: """ determine if BIGGEST value is at least twice larger than any other integer """ biggest_val = max(nums) final_index = None for i in range(len(nums)): num = nums[i] ...
largest-number-at-least-twice-of-others
My solution in Python and Kotlin
SleeplessChallenger
0
39
largest number at least twice of others
747
0.465
Easy
12,339
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1809298/Python-O(nlogn)-easy-to-understand
class Solution: def dominantIndex(self, nums: List[int]) -> int: n = sorted(nums) if len(nums) == 1: return 0 else: if (2*n[-2]) <= n[-1]: return nums.index(n[-1]) else: return -1
largest-number-at-least-twice-of-others
Python O(nlogn) easy to understand
zamya7
0
48
largest number at least twice of others
747
0.465
Easy
12,340
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1627527/Python-one-pass-solution-with-O(1)-space
class Solution: def dominantIndex(self, nums: List[int]) -> int: n = len(nums) # base case: if n == 1: return 0 # init a min heap min_heap = [] # Time analysis for the itertion: n * O(1) = O(n) for i in range(n): # takes O(log2) = O(1) ...
largest-number-at-least-twice-of-others
Python one-pass solution with O(1) space
aldy97
0
102
largest number at least twice of others
747
0.465
Easy
12,341
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1370113/Python-3-O(n)-1-pass
class Solution: def dominantIndex(self, nums: List[int]) -> int: idx, m1, m2 = -1, -1, -1 for i, n in enumerate(nums): if n > m1: idx, m1, m2 = i, n, m1 elif n > m2: m2 = n return idx if (m1 >= 2*m2) else -1
largest-number-at-least-twice-of-others
Python 3, O(n) 1 pass
MihailP
0
168
largest number at least twice of others
747
0.465
Easy
12,342
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1355169/Python-Solution
class Solution: def dominantIndex(self, nums: List[int]) -> int: n = len(nums) if n < 2: return 0 # idx1 is the index of the largest element in array and idx2 is the index of the second-largest element in array if nums[0] < nums[1]: idx1, idx2 = 1, 0 else: ...
largest-number-at-least-twice-of-others
Python Solution
mariandanaila01
0
141
largest number at least twice of others
747
0.465
Easy
12,343
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1246134/Python3-simple-solution-by-two-approaches
class Solution: def dominantIndex(self, nums: List[int]) -> int: if len(nums) == 1: return 0 temp = nums.copy() nums.sort() return temp.index(nums[-1]) if nums[-2]*2 <= nums[-1] else -1
largest-number-at-least-twice-of-others
Python3 simple solution by two approaches
EklavyaJoshi
0
68
largest number at least twice of others
747
0.465
Easy
12,344
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1246134/Python3-simple-solution-by-two-approaches
class Solution: def dominantIndex(self, nums: List[int]) -> int: if len(nums) == 1: return 0 n1 = max(nums) i = nums.index(n1) n2 = max(nums[:i]+nums[i+1:]) return i if n2*2 <= n1 else -1
largest-number-at-least-twice-of-others
Python3 simple solution by two approaches
EklavyaJoshi
0
68
largest number at least twice of others
747
0.465
Easy
12,345
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1155913/single-scan-python3-linear-time-constant-sapce.
class Solution: def dominantIndex(self, nums: List[int]) -> int: # single scan? set the first and second largest as you go through # and check that the second * 2 is not > first first, second = 0, 0 max_index = 0 for i, x in enumerate(nums): if x > first:...
largest-number-at-least-twice-of-others
single scan python3 linear time, constant sapce.
normalpersontryingtopayrent
0
53
largest number at least twice of others
747
0.465
Easy
12,346
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1036570/PythonPython3-or-One-liner-and-Detailed-or-Largest-Number-At-Least-Twice-of-Others
class Solution: def dominantIndex(self, nums) -> int: return nums.index(max(nums)) if all(2 * x <= max(nums) if x != max(nums) else True for x in nums) else -1
largest-number-at-least-twice-of-others
[Python/Python3] | One-liner & Detailed | Largest Number At Least Twice of Others
newborncoder
0
161
largest number at least twice of others
747
0.465
Easy
12,347
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/1036570/PythonPython3-or-One-liner-and-Detailed-or-Largest-Number-At-Least-Twice-of-Others
class Solution: def dominantIndex(self, nums) -> int: max_nums = max(nums) if all(2*x <= max_nums if x != max_nums else True for x in nums): return nums.index(max_nums) else: return -1
largest-number-at-least-twice-of-others
[Python/Python3] | One-liner & Detailed | Largest Number At Least Twice of Others
newborncoder
0
161
largest number at least twice of others
747
0.465
Easy
12,348
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/538816/Python-Two-Pass
class Solution: def dominantIndex(self, nums: List[int]) -> int: max_val = nums[0] index = 0 for i in range(1,len(nums)): if nums[i] > max_val: max_val = nums[i] index = i for i,num in enumerate(nums): if num * 2 <= max_val: ...
largest-number-at-least-twice-of-others
Python Two Pass
pratushah
0
114
largest number at least twice of others
747
0.465
Easy
12,349
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/343071/Python3-self-explanatory-and-short-solution
class Solution: def dominantIndex(self, nums: List[int]) -> int: if len(nums)==1:return 0 n=sorted(nums) if n[-1]>=2*n[-2]: return nums.index(n[-1]) return -1
largest-number-at-least-twice-of-others
Python3 self explanatory and short solution
ketan35
0
92
largest number at least twice of others
747
0.465
Easy
12,350
https://leetcode.com/problems/largest-number-at-least-twice-of-others/discuss/336481/Solution-in-Python-3
class Solution: def dominantIndex(self, nums: List[int]) -> int: M = max(nums) return nums.index(M) if len([0 for i in nums if 2*i <= M]) == len(nums)-1 else -1 - Python 3 - Junaid Mansuri
largest-number-at-least-twice-of-others
Solution in Python 3
junaidmansuri
0
316
largest number at least twice of others
747
0.465
Easy
12,351
https://leetcode.com/problems/shortest-completing-word/discuss/1277850/Python3-Simple-solution-without-Dictionary
class Solution: def shortestCompletingWord(self, P: str, words: List[str]) -> str: alphs="" res="" for p in P: if p.isalpha(): alphs+=p.lower() for word in words: if all(alphs.count(alphs[i]) <= word.coun...
shortest-completing-word
Python3 Simple solution without Dictionary
Rei4126
4
335
shortest completing word
748
0.592
Easy
12,352
https://leetcode.com/problems/shortest-completing-word/discuss/2419448/Python-magic%3A-3-steps
class Solution: def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str: req = Counter(filter(str.isalpha, licensePlate.lower())) words.sort(key=len) for count, word in zip(map(Counter, words), words): if count >= req: return word
shortest-completing-word
βœ…Python magic: 3 stepsπŸ”₯
blest
0
28
shortest completing word
748
0.592
Easy
12,353
https://leetcode.com/problems/shortest-completing-word/discuss/1791199/5-Lines-Python-Solution-oror-75-Faster-oror-Memory-Less-than-85
class Solution: def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str: l, mx, ans = ''.join([x.lower() for x in licensePlate if x.isalpha()]), 0, '' for word in words: sumChars = sum([min(word.count(char),l.count(char)) for char in set(l)]) if sumChars ...
shortest-completing-word
5-Lines Python Solution || 75% Faster || Memory Less than 85%
Taha-C
0
155
shortest completing word
748
0.592
Easy
12,354
https://leetcode.com/problems/shortest-completing-word/discuss/1690838/Python-simple-solution
class Solution: def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str: license = defaultdict(int) for s in licensePlate: if s.isalpha(): license[s.lower()] += 1 cand = "" candlen = float('inf') def c...
shortest-completing-word
Python simple solution
byuns9334
0
143
shortest completing word
748
0.592
Easy
12,355
https://leetcode.com/problems/shortest-completing-word/discuss/1555151/Python3-using-array-Faster-than-93.88
class Solution: def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str: # Runtime: 64 ms, faster than 93.88% # Memory Usage: 14.5 MB, less than 81.41% words = sorted(words, key=len) # Handle multiple shortest 'completing' words lic = ''.join(s...
shortest-completing-word
Python3 - using array Faster than 93.88%
inosuke4
0
93
shortest completing word
748
0.592
Easy
12,356
https://leetcode.com/problems/shortest-completing-word/discuss/625043/python3-solution-wo-Counter-(tricks-explained)
class Solution: def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str: # remove non-letters, cast to lowercases licensePlate = [i for i in licensePlate if i.isalpha()] licensePlate = list(map(str.lower, licensePlate)) licensePlate = sorted(licensePlate) ...
shortest-completing-word
python3 solution w/o Counter (tricks explained)
shaoyu0966
0
127
shortest completing word
748
0.592
Easy
12,357
https://leetcode.com/problems/shortest-completing-word/discuss/475537/Python3-97.03-(60-ms)100.00-(12.8-MB)-sorting
class Solution: def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str: letter_occurences = [0 for a in range(26)] for char in licensePlate: if ('A' <= char <= 'Z'): letter_occurences[ord(char.lower()) - 97] += 1 elif ('...
shortest-completing-word
Python3 97.03% (60 ms)/100.00% (12.8 MB) -- sorting
numiek_p
0
183
shortest completing word
748
0.592
Easy
12,358
https://leetcode.com/problems/shortest-completing-word/discuss/460454/60-ms98.5212.9-MB100.00-Answer-for-python3
class Solution: def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str: strs = [] for char in licensePlate.lower(): if char.isalpha(): strs.append(char) words.sort(key=len) def words_in(strs,words): for char in strs: ...
shortest-completing-word
60 ms,98.52%/12.9 MB,100.00% Answer for python3
SilverCHN
0
89
shortest completing word
748
0.592
Easy
12,359
https://leetcode.com/problems/shortest-completing-word/discuss/450267/Python3-simple-solution-using-hash-table-runtime%3A-72-ms-faster-than-88.85
class Solution: def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str: lp = "" for char in licensePlate.lower(): if char.isalpha(): lp += char ld = self.countChars(lp) res = None for word in words: ...
shortest-completing-word
Python3 simple solution using hash table, runtime: 72 ms, faster than 88.85%
jb07
0
103
shortest completing word
748
0.592
Easy
12,360
https://leetcode.com/problems/shortest-completing-word/discuss/325107/Python-solution-using-dictionary
class Solution: def shortestCompletingWord(self, l: str, w: List[str]) -> str: res={} lp=l.lower() d={} for i in lp: if i.isalpha(): d[i]=lp.count(i) for i in w: j=i.lower() c=0 for k in d.keys(): ...
shortest-completing-word
Python solution using dictionary
ketan35
0
152
shortest completing word
748
0.592
Easy
12,361
https://leetcode.com/problems/contain-virus/discuss/2224726/Pyhon-Easy-Solution-oror-DFS-oror-Time-O(m*n*max(m-n))-oror-Faster
class Solution: def containVirus(self, mat: List[List[int]]) -> int: m,n = len(mat),len(mat[0]) def dfs(i,j,visited,nextInfected): # return no. of walls require to quarantined dfs area if 0<=i<m and 0<=j<n and (i,j) not in visited: if mat[i][j]==2: # Already quarantined...
contain-virus
Pyhon Easy Solution || DFS || Time O(m*n*max(m , n)) || Faster
Laxman_Singh_Saini
4
378
contain virus
749
0.509
Hard
12,362
https://leetcode.com/problems/contain-virus/discuss/2537505/Python3-brute-force
class Solution: def containVirus(self, isInfected: List[List[int]]) -> int: m, n = len(isInfected), len(isInfected[0]) ans = 0 while True: regions = [] fronts = [] walls = [] seen = set() for i in range(m): for j ...
contain-virus
[Python3] brute-force
ye15
1
110
contain virus
749
0.509
Hard
12,363
https://leetcode.com/problems/contain-virus/discuss/2643762/DFS-%2B-Heap
class Solution: def containVirus(self, isInfected: List[List[int]]) -> int: # return number of walls needed def dfs(r, c): # return if (r, c) in visited or r < 0 or c < 0 or r >= m or c >= n: return 0 # contained cells if isInfected[...
contain-virus
DFS + Heap
BBBlair
0
32
contain virus
749
0.509
Hard
12,364
https://leetcode.com/problems/open-the-lock/discuss/1251501/Python-or-Bi-directional-A-star-BFS-or-99.7-or-52ms-or-14.3MB
class Solution: def openLock(self, deadends: List[str], end: str) -> int: if end in deadends or "0000" in deadends: return -1 if end == "0000": return 0 start, end, deadends = 0, int(end), {int(deadend) for deadend in deadends} def distance(cur: int, target: ...
open-the-lock
Python | Bi-directional A star, BFS | 99.7% | 52ms | 14.3MB
PuneethaPai
3
110
open the lock
752
0.555
Medium
12,365
https://leetcode.com/problems/open-the-lock/discuss/2106499/Python-or-WITHOUT-STRINGS-or-MEMORY-LESS-THAN-100
class Solution: def openLock(self, deadends: list[str], target: str) -> int: target, turns = int(target), [0] * 10000 for el in deadends: turns[int(el)] = -1 dq = deque([0] * (turns[0] + 1)) while dq: cur = dq.popleft() if cur == target: ...
open-the-lock
Python | WITHOUT STRINGS | MEMORY LESS THAN 100%
miguel_v
2
70
open the lock
752
0.555
Medium
12,366
https://leetcode.com/problems/open-the-lock/discuss/916895/Python3-BFS-and-Dijkstra
class Solution: def openLock(self, deadends: List[str], target: str) -> int: ans = 0 queue = ["0000"] deadends = set(deadends) while queue: newq = [] for n in queue: if n not in deadends: deadends.add(n) ...
open-the-lock
[Python3] BFS & Dijkstra
ye15
2
134
open the lock
752
0.555
Medium
12,367
https://leetcode.com/problems/open-the-lock/discuss/916895/Python3-BFS-and-Dijkstra
class Solution: def openLock(self, deadends: List[str], target: str) -> int: deadends = set(deadends) if "0000" not in deadends: ans = 0 queue = ["0000"] deadends.add("0000") while queue: newq = [] for n in queue: ...
open-the-lock
[Python3] BFS & Dijkstra
ye15
2
134
open the lock
752
0.555
Medium
12,368
https://leetcode.com/problems/open-the-lock/discuss/916895/Python3-BFS-and-Dijkstra
class Solution: def openLock(self, deadends: List[str], target: str) -> int: deadends = set(deadends) if "0000" not in deadends: deadends.add("0000") pq = [(0, "0000")] while pq: k, n = heappop(pq) if n == target: ret...
open-the-lock
[Python3] BFS & Dijkstra
ye15
2
134
open the lock
752
0.555
Medium
12,369
https://leetcode.com/problems/open-the-lock/discuss/1250796/Open-The-Lock-or-BFS-or-Shortest-Path-or-Python
class Solution: def openLock(self, deadends: List[str], target: str) -> int: if '0000' in deadends: return -1 if '0000' == target: return 0 queue = collections.deque() queue.append(('0000', 0)) visited = set('0000') while queu...
open-the-lock
Open The Lock | BFS | Shortest Path | Python
shubh_027
1
104
open the lock
752
0.555
Medium
12,370
https://leetcode.com/problems/open-the-lock/discuss/2834501/Python%3A-BFS
class Solution: def openLock(self, deadends: List[str], target: str) -> int: deadends = set(deadends) def update_lock(state, slot, direction): curr = int(state[slot]) if curr + direction < 0: new = '9' else: new = str((curr +...
open-the-lock
Python: BFS
aaroto
0
2
open the lock
752
0.555
Medium
12,371
https://leetcode.com/problems/open-the-lock/discuss/2705992/Python-straightforward-approach-90-fast-and-90-space
class Solution: def openLock(self, deadends: List[str], target: str) -> int: deadends = set(deadends) # makes 2x faster if "0000" in deadends: return -1 if "0000" == target: return 0 memo = {"0000": 0} # state: steps needed to reach state bfs = ["0000"] fo...
open-the-lock
Python straightforward approach [90 % fast and 90 % space]
amuwal
0
6
open the lock
752
0.555
Medium
12,372
https://leetcode.com/problems/open-the-lock/discuss/2352209/python3-O(N)-Time-O(1)-Space-Solution
class Solution: def openLock(self, deadends: List[str], target: str) -> int: #Let n = len(deadends) #Time Complexity: O(n + 80000*4*6) -> O(n) #Space-Complexity: O(10000 + 10000) -> O(1) #base case: if our start point is a deadend, we can't ever reach target since #t...
open-the-lock
python3 O(N) Time O(1) Space Solution
JOON1234
0
54
open the lock
752
0.555
Medium
12,373
https://leetcode.com/problems/open-the-lock/discuss/2300363/Clear-Python-solution-with-BFS
class Solution: def openLock(self, deadends: List[str], target: str) -> int: q = [] visited = set() step = 0 deads = set() q.append("0000") visited.add("0000") for deadend in deadends: deads.add(deadend) while (len(q) > 0): ...
open-the-lock
Clear Python solution with BFS
leqinancy
0
21
open the lock
752
0.555
Medium
12,374
https://leetcode.com/problems/open-the-lock/discuss/2258220/PythonBFSEasy-to-Understand
class Solution: def openLock(self, deadends: List[str], target: str) -> int: start = "0000" # Set to track Deadends deadset = set(deadends) # if the starting position is a deadend, return -1 if start in deadset: return -1 # Add the starting position to the queue...
open-the-lock
[Python][BFS][Easy to Understand]
maverick02
0
44
open the lock
752
0.555
Medium
12,375
https://leetcode.com/problems/open-the-lock/discuss/2237155/Python3-oror-BFS
class Solution: def openLock(self, deadends: List[str], target: str) -> int: deadend_set = set(deadends) if "0000" in deadend_set: return -1 queue = deque([(0,"0000")]) visited = set("0000") while queue: steps,char = queue.popleft() if char...
open-the-lock
Python3 || BFS
s_m_d_29
0
26
open the lock
752
0.555
Medium
12,376
https://leetcode.com/problems/open-the-lock/discuss/2216291/Python-Hadlock's-Algorithm-(fast-but-not-simple)
class Solution: def openLock(self, deadends: List[str], target: str) -> int: deadends = set(deadends) # if '0000' is in deadends there's nothing we can do if '0000' in deadends: return -1 target_list = list(map(int, target)) # helper function to get the distance b...
open-the-lock
[Python] Hadlock's Algorithm (fast, but not simple)
logtd
0
28
open the lock
752
0.555
Medium
12,377
https://leetcode.com/problems/open-the-lock/discuss/2034985/Python-easy-to-read-and-understand-or-BFS-hashmap
class Solution: def __init__(self): self.d = { '0': ['9', '1'], '1': ['0', '2'], '2': ['1', '3'], '3': ['2', '4'], '4': ['3', '5'], '5': ['4', '6'], '6': ['5', '7'], '7': ['6', '8'], '8': ['7', '9'], ...
open-the-lock
Python easy to read and understand | BFS-hashmap
sanial2001
0
50
open the lock
752
0.555
Medium
12,378
https://leetcode.com/problems/open-the-lock/discuss/1816727/Python-ll-Iterative-BFS
class Solution: def openLock(self, deadends: List[str], target: str) -> int: deadends = set(deadends) if '0000' in deadends: return -1 def neib(code): neighbors = [] for i in range(4): new1 = code[:i] + str((int(code[i])+1)%10) + code[i+1:] new2 = code[:i] + str((int(code[i])-1)%10) + code[i+1:]...
open-the-lock
Python ll Iterative BFS
morpheusdurden
0
49
open the lock
752
0.555
Medium
12,379
https://leetcode.com/problems/open-the-lock/discuss/1698183/752-Open-the-Lock-via-BFS
class Solution: def openLock(self, deadends, target): visited = set(deadends) q = deque(["0000"]); depth = 0 while q: ### search vertically for the depth of the graph qlen = len(q) for _ in range(qlen): ### search horizontally for the current layer in the graph node = q.popleft() if nod...
open-the-lock
752 Open the Lock via BFS
zwang198
0
33
open the lock
752
0.555
Medium
12,380
https://leetcode.com/problems/open-the-lock/discuss/1251148/Python-O(No.-of-Combinations)-time-and-O(L)-space-solution-using-BFS
class Solution: def openLock(self, deadends: List[str], target: str) -> int: mp, queue, vis, ans = Counter(deadends), deque(), Counter(), 0 if mp['0000'] > 0: return -1 queue.append(['0','0','0','0']) vis['0000'], len_q = 1, 1 while len_q: ans...
open-the-lock
Python O(No. of Combinations) time & O(L) space solution using BFS
m0biu5
0
70
open the lock
752
0.555
Medium
12,381
https://leetcode.com/problems/cracking-the-safe/discuss/2825347/DFS-Solution-in-Python
class Solution: def crackSafe(self, n: int, k: int) -> str: def dfs(path, visitedCombinations, targetNumVisited, combos): # Base Case. We've visited all possible combinations if len(visitedCombinations) == targetNumVisited: combos.append(''.join([str(x) for x in path]...
cracking-the-safe
DFS Solution in Python
user0138r
0
1
cracking the safe
753
0.555
Hard
12,382
https://leetcode.com/problems/cracking-the-safe/discuss/2775817/Python-DFS
class Solution: def crackSafe(self, n: int, k: int) -> str: total = k**n-1 start = '0' * n visited = set() visited.add('0' * n) self.ans = '0' * n def dfs(total,start): if total == 0: return True for i in range(k): ...
cracking-the-safe
Python DFS
xiwenliu
0
8
cracking the safe
753
0.555
Hard
12,383
https://leetcode.com/problems/reach-a-number/discuss/991390/Python-Dummy-math-solution-easy-to-understand
class Solution: def reachNumber(self, target: int) -> int: target = abs(target) step = 0 far = 0 while far < target or far%2 != target%2: step += 1 far +=step return step
reach-a-number
[Python] Dummy math solution, easy to understand
KevinZzz666
1
47
reach a number
754
0.426
Medium
12,384
https://leetcode.com/problems/reach-a-number/discuss/991282/explained-binary-search-with-a-little-bit-math-(including-a-thinking-process)
class Solution: def reachNumber(self, target: int) -> int: def gaussSum(n): return n*(n+1)//2 def binaryFind(lower, upper): t = abs(target) sums, n = -1, -1 while lower <= upper: mid = (lower+upper)//2 if gaussS...
reach-a-number
explained binary search with a little bit math (including a thinking process)
ytb_algorithm
1
120
reach a number
754
0.426
Medium
12,385
https://leetcode.com/problems/reach-a-number/discuss/343194/Solution-in-Python-3-(beats-100)-(No-loop-needed!)
class Solution: def reachNumber(self, target: int) -> int: t = abs(target) n = int(((1+8*t)**.5-1)/2) LT = n*(n+1)//2 if LT == t: return n if (LT + n + 1 - t) % 2 == 0: return n + 1 return n + 3 - n % 2 - Python 3 - Junaid Mansuri
reach-a-number
Solution in Python 3 (beats 100%) (No loop needed!)
junaidmansuri
0
374
reach a number
754
0.426
Medium
12,386
https://leetcode.com/problems/pyramid-transition-matrix/discuss/921324/Python3-progressively-building-new-rows
class Solution: def pyramidTransition(self, bottom: str, allowed: List[str]) -> bool: mp = {} for x, y, z in allowed: mp.setdefault((x, y), set()).add(z) def fn(row): """Return list of rows built from given row.""" ans = [""] for x, y in zip(...
pyramid-transition-matrix
[Python3] progressively building new rows
ye15
2
215
pyramid transition matrix
756
0.531
Medium
12,387
https://leetcode.com/problems/pyramid-transition-matrix/discuss/921324/Python3-progressively-building-new-rows
class Solution: def pyramidTransition(self, bottom: str, allowed: List[str]) -> bool: mp = {} for x, y, z in allowed: mp.setdefault((x, y), set()).add(z) def fn(row): """Return True if row could be built.""" if len(row) == 1: return True for ...
pyramid-transition-matrix
[Python3] progressively building new rows
ye15
2
215
pyramid transition matrix
756
0.531
Medium
12,388
https://leetcode.com/problems/pyramid-transition-matrix/discuss/1684955/Python-oror-Recursion-oror-Memoization
class Solution: def pyramidTransition(self, bottom: str, allowed: List[str]) -> bool: allowed=set(allowed) letters=['A','B','C','D','E','F'] @lru_cache(None) def solve(bottom): if len(bottom)==1: return True tops=[""] n=len(bottom) ans=...
pyramid-transition-matrix
Python || Recursion || Memoization
ketan_raut
1
143
pyramid transition matrix
756
0.531
Medium
12,389
https://leetcode.com/problems/pyramid-transition-matrix/discuss/2794622/Python-straightforward-DFS-with-heuristic-pruning
class Solution: def pyramidTransition(self, bottom: str, allowed: List[str]) -> bool: mem = dict() mem2 = set() for a in allowed: if a[:2] not in mem: mem[a[:2]] = list() mem2.add(a[0]) mem[a[:2]].append(a[-1]) ...
pyramid-transition-matrix
Python, straightforward DFS with heuristic pruning
yiming999
0
8
pyramid transition matrix
756
0.531
Medium
12,390
https://leetcode.com/problems/set-intersection-size-at-least-two/discuss/2700915/Python-Explained-or-O(nlogn)
class Solution: def intersectionSizeTwo(self, intervals: List[List[int]]) -> int: intervals.sort(key = lambda x:x[1]) size = 0 prev_start = -1 prev_end = -1 for curr_start, curr_end in intervals: if prev_start == -1 or prev_end < curr_start: #if intervals do not ...
set-intersection-size-at-least-two
Python [Explained] | O(nlogn)
diwakar_4
1
545
set intersection size at least two
757
0.439
Hard
12,391
https://leetcode.com/problems/set-intersection-size-at-least-two/discuss/1569334/Python3-greedy
class Solution: def intersectionSizeTwo(self, intervals: List[List[int]]) -> int: ans = [] for x, y in sorted(intervals, key=lambda x: (x[1], -x[0])): if not ans or ans[-2] < x: if ans and x <= ans[-1]: ans.append(y) else: ans.extend([y-1, y]) re...
set-intersection-size-at-least-two
[Python3] greedy
ye15
1
311
set intersection size at least two
757
0.439
Hard
12,392
https://leetcode.com/problems/set-intersection-size-at-least-two/discuss/2281243/Sorting-or-Time%3A-O(nlogn)-or-Space%3A-O(n)
class Solution: def intersectionSizeTwo(self, A: List[List[int]]) -> int: A.sort(key=lambda x:(x[1],-x[0])) ans = [] ans.append(A[0][1]-1) ans.append(A[0][1]) n = len(A) # print(A) for i in range(1,n): if A[i][0]>ans[-1]: ans.append...
set-intersection-size-at-least-two
Sorting | Time: O(nlogn) | Space: O(n)
Shivamk09
0
263
set intersection size at least two
757
0.439
Hard
12,393
https://leetcode.com/problems/special-binary-string/discuss/1498369/Easy-Approach-oror-Well-Explained-oror-Substring
class Solution: def makeLargestSpecial(self, s: str) -> str: l = 0 balance = 0 sublist = [] for r in range(len(s)): balance += 1 if s[r]=='1' else -1 if balance==0: sublist.append("1" + self.makeLargestSpecial(s[l+1:r])+ "0") l = r+1 sublist.sort(rev...
special-binary-string
πŸ“ŒπŸ“Œ Easy-Approach || Well-Explained || Substring 🐍
abhi9Rai
5
371
special binary string
761
0.604
Hard
12,394
https://leetcode.com/problems/special-binary-string/discuss/1251452/Python3-partition-via-recursion
class Solution: def makeLargestSpecial(self, s: str) -> str: def fn(lo, hi): if lo == hi: return "" vals = [] ii, prefix = lo, 0 for i in range(lo, hi): prefix += 1 if s[i] == "1" else -1 if prefix == 0: ...
special-binary-string
[Python3] partition via recursion
ye15
1
325
special binary string
761
0.604
Hard
12,395
https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/discuss/1685874/Simple-python-code
class Solution: def isPrime(self,x): flag=0 if x==1: return False for i in range(2,x): if x%i==0: flag=1 break if flag==1: return False return True def countPrimeSetBits(self, left: int, right: i...
prime-number-of-set-bits-in-binary-representation
Simple python code
amannarayansingh10
1
105
prime number of set bits in binary representation
762
0.678
Easy
12,396
https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/discuss/381146/Solution-in-Python-3-(beats-~98)-(one-line)
class Solution: def countPrimeSetBits(self, L: int, R: int) -> int: return sum(1 for i in range(L,R+1) if bin(i).count('1') in {2,3,5,7,11,13,17,19}) - Junaid Mansuri (LeetCode ID)@hotmail.com
prime-number-of-set-bits-in-binary-representation
Solution in Python 3 (beats ~98%) (one line)
junaidmansuri
1
309
prime number of set bits in binary representation
762
0.678
Easy
12,397
https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/discuss/2836218/97.16-or-Python-or-LeetCode-or-762.-Prime-Number-of-Set-Bits-in-Binary-Representation-(Easy)
class Solution: def countPrimeSetBits(self, left: int, right: int) -> int: set_ = {2, 3, 5, 7, 11, 13, 17, 19} h = 0 for i in range(left, right + 1): w = bin(i) count = w.count("1") if count in set_: h += 1 return h
prime-number-of-set-bits-in-binary-representation
97.16 % | Python | LeetCode | 762. Prime Number of Set Bits in Binary Representation (Easy)
UzbekDasturchisiman
0
1
prime number of set bits in binary representation
762
0.678
Easy
12,398
https://leetcode.com/problems/prime-number-of-set-bits-in-binary-representation/discuss/2817836/Python-Easy
class Solution: def countPrimeSetBits(self, left: int, right: int) -> int: from collections import Counter ans = 0 primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 53] for i in range(left, right + 1): tmp = bin(i)[2:] count = Counter(tmp) if count["1"] in primes: ans += 1...
prime-number-of-set-bits-in-binary-representation
Python Easy
lucasschnee
0
2
prime number of set bits in binary representation
762
0.678
Easy
12,399