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/max-consecutive-ones/discuss/2543573/Python-Easy | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
maxi,count = 0,0
for num in nums:
if(num):
count+=1
else:
count =0
maxi = max(maxi,count)
return maxi | max-consecutive-ones | Python Easy | jxswxnth | 1 | 72 | max consecutive ones | 485 | 0.561 | Easy | 8,500 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2418366/Simple-Python-Solution | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
cons, res = 0, 0
for i in nums:
cons = cons*i + i
res = max(cons, res)
return res | max-consecutive-ones | Simple Python Solution | Abhi_-_- | 1 | 65 | max consecutive ones | 485 | 0.561 | Easy | 8,501 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2339910/Python-Easy-Solution | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
res = 0
count = 0
for num in nums:
if num == 1:
count +=1
else:
count = 0
if res<count:
res=count
return res | max-consecutive-ones | Python Easy Solution | Harshi_Tyagi | 1 | 59 | max consecutive ones | 485 | 0.561 | Easy | 8,502 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2311264/Easy-to-understand-Python-solution | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
count, max_ones = 0, 0
for num in nums:
if num != 1:
count = 0
else:
count += 1
max_ones = max(count, max_ones)
return max_ones | max-consecutive-ones | Easy to understand Python solution | Balance-Coffee | 1 | 74 | max consecutive ones | 485 | 0.561 | Easy | 8,503 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2298384/Easy-Python3-one-liner-beats-90 | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
return max([len(i) for i in "".join([str(i) for i in nums]).split('0')]) | max-consecutive-ones | Easy Python3 one-liner, beats 90% | y-arjun-y | 1 | 76 | max consecutive ones | 485 | 0.561 | Easy | 8,504 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2054441/Python-oneliner | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
return len(max(''.join(map(str,nums)).split('0'),key=len)) | max-consecutive-ones | Python oneliner | StikS32 | 1 | 72 | max consecutive ones | 485 | 0.561 | Easy | 8,505 |
https://leetcode.com/problems/max-consecutive-ones/discuss/1903132/Python3-Solution | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
ans,count = 0,0
for i in nums:
if i == 1:
count += 1
else:
ans = max(ans,count)
count = 0
return max(ans,count) | max-consecutive-ones | Python3 Solution | satyam2001 | 1 | 86 | max consecutive ones | 485 | 0.561 | Easy | 8,506 |
https://leetcode.com/problems/max-consecutive-ones/discuss/1872753/Python-solution-oror-Faster-than-96-of-online-submissions | class Solution(object):
def findMaxConsecutiveOnes(self, nums):
count = 0
maxOnes = 0 # has highest consecutive ones
for num in nums:
if num == 1:
count += 1
if count > maxOnes:
maxOnes = count
... | max-consecutive-ones | Python solution || Faster than 96% of online submissions | arvindrao | 1 | 99 | max consecutive ones | 485 | 0.561 | Easy | 8,507 |
https://leetcode.com/problems/max-consecutive-ones/discuss/1587223/Python-3-Solution | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
l = len(nums)
con1s = 0
maxCon1s = 0
for x in range(l):
if nums[x] == 1:
con1s += 1
else:
maxCon1s = max(maxCon1s,con1s)
con1s = 0
return max(maxCon1s,con1s) | max-consecutive-ones | Python 3 Solution | JM_Pranav | 1 | 72 | max consecutive ones | 485 | 0.561 | Easy | 8,508 |
https://leetcode.com/problems/max-consecutive-ones/discuss/1511126/Python-1-line | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
return len(max(''.join(map(str, nums)).split('0'), key=len)) | max-consecutive-ones | Python 1-line | SmittyWerbenjagermanjensen | 1 | 200 | max consecutive ones | 485 | 0.561 | Easy | 8,509 |
https://leetcode.com/problems/max-consecutive-ones/discuss/1307451/Python-3-or-Simple-Generic-Solution | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
count=0
max_count = 0
for n in nums:
if n==1:
count=count+1
else:
count=0
max_count=max(max_count,count)
return max_count | max-consecutive-ones | Python 3 | Simple Generic Solution | nishikantsinha | 1 | 167 | max consecutive ones | 485 | 0.561 | Easy | 8,510 |
https://leetcode.com/problems/max-consecutive-ones/discuss/777048/Easy-Python-Solution-352m-faster-than-98 | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
count = 0
res = []
if len(nums) == 0:
return 0
else:
for i in nums:
if i == 1:
count += 1
else:... | max-consecutive-ones | Easy Python Solution, 352m faster than 98% | Venezsia1573 | 1 | 131 | max consecutive ones | 485 | 0.561 | Easy | 8,511 |
https://leetcode.com/problems/max-consecutive-ones/discuss/380210/Three-Short-Solutions-in-Python-3 | class Solution:
def findMaxConsecutiveOnes(self, n: List[int]) -> int:
M, a, _ = 0, -1, n.append(0)
for i,j in enumerate(n):
if j == 0: M, a = max(M, i - a), i
return M - 1 | max-consecutive-ones | Three Short Solutions in Python 3 | junaidmansuri | 1 | 503 | max consecutive ones | 485 | 0.561 | Easy | 8,512 |
https://leetcode.com/problems/max-consecutive-ones/discuss/380210/Three-Short-Solutions-in-Python-3 | class Solution:
def findMaxConsecutiveOnes(self, n: List[int]) -> int:
M, c, _ = 0, 0, n.append(0)
for i in n:
if i == 0: M, c = max(M,c), -1
c += 1
return M | max-consecutive-ones | Three Short Solutions in Python 3 | junaidmansuri | 1 | 503 | max consecutive ones | 485 | 0.561 | Easy | 8,513 |
https://leetcode.com/problems/max-consecutive-ones/discuss/380210/Three-Short-Solutions-in-Python-3 | class Solution:
def findMaxConsecutiveOnes(self, n: List[int]) -> int:
z = [-1]+[i for i,j in enumerate(n) if j == 0]+[len(n)]
return max([z[i]-z[i-1] for i in range(1,len(z))])-1
- Junaid Mansuri
(LeetCode ID)@hotmail.com | max-consecutive-ones | Three Short Solutions in Python 3 | junaidmansuri | 1 | 503 | max consecutive ones | 485 | 0.561 | Easy | 8,514 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2846630/Python | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
consecutive = 0
maxconsecutive = 0
for i in nums:
if i:
consecutive += 1
maxconsecutive = max(maxconsecutive, consecutive)
else:
consecutive = 0
... | max-consecutive-ones | Python | yijiun | 0 | 1 | max consecutive ones | 485 | 0.561 | Easy | 8,515 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2831232/Easy-to-understand-solution-in-O(n) | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
countones = 0
temp = 0
for i in nums:
if(i==1):
countones += 1
temp = max(temp, countones)
elif(i==0):
countones = 0
return temp | max-consecutive-ones | Easy to understand solution in O(n) | AyushiChakraborty | 0 | 3 | max consecutive ones | 485 | 0.561 | Easy | 8,516 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2824115/Easy-to-understand-O(n)-time-complexity | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
d={1:1}
count=1
if 1 not in nums:
return 0
for i in range(0,len(nums)-1):
if nums[i] and nums[i+1]:
d[count]+=1
elif(1 in nums[i::] and nums[i]==0):
... | max-consecutive-ones | Easy to understand , O(n) time complexity | varunmuthannaka | 0 | 3 | max consecutive ones | 485 | 0.561 | Easy | 8,517 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2818269/EASY-PYTHON-CODE%3A | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
m,count=0,0
for i in range(len(nums)):
if nums[i]==1:
count+=1
m=max(m,count)
else:
count=0
return m | max-consecutive-ones | EASY PYTHON CODE: | mansipatel24 | 0 | 6 | max consecutive ones | 485 | 0.561 | Easy | 8,518 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2814962/Iterative-Solution-or-String-Solution | class Solution:
def max_consecutive_ones(self, nums):
count = 0
max_count = float('-inf')
for num in nums:
if num:
count += 1
else:
count = 0
max_count = max(count, max_count)
return(max_count)
... | max-consecutive-ones | Iterative Solution or String Solution | aaroto | 0 | 1 | max consecutive ones | 485 | 0.561 | Easy | 8,519 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2812346/Python-Simple-Solution | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
k=0
t=0
for z in nums:
t=(t+z)*z
k=t>k and t or k
return k | max-consecutive-ones | Python Simple Solution | alex41542 | 0 | 2 | max consecutive ones | 485 | 0.561 | Easy | 8,520 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2798827/Python3-brute-force | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
maxnum = 0
count = 0
for i in range(len(nums)):
if nums[i]==1:
count += 1
else:
count = 0
if count > maxnum:
maxnum = count
re... | max-consecutive-ones | Python3 brute force | BhavyaBusireddy | 0 | 1 | max consecutive ones | 485 | 0.561 | Easy | 8,521 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2792613/Python-oror-O(n)-Solution | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
result = 0
count = 0
for i in range(len(nums)):
if nums[i] == 1:
count += 1
else:
count = 0
result = max(count,result)
... | max-consecutive-ones | Python || O(n) Solution | _rishabh_singh_ | 0 | 5 | max consecutive ones | 485 | 0.561 | Easy | 8,522 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2790187/Easy-Python-Solution-O(N) | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
greatestNumOnes = float("-inf")
counter = 0
for n in nums:
if n == 1:
counter += 1
if counter > greatestNumOnes:
greatestNumOnes = counter
... | max-consecutive-ones | Easy Python Solution O(N) | eddie1322 | 0 | 2 | max consecutive ones | 485 | 0.561 | Easy | 8,523 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2786716/Max-Consecutive-Ones-easy-understanding(Python) | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
count=0
max_count=0
for i in nums:
if i==1:
count=count+1
max_count=max(count,max_count)
else:
count=0
return max_count | max-consecutive-ones | Max Consecutive Ones -easy understanding(Python) | RaviShanker__Thadishetti | 0 | 1 | max consecutive ones | 485 | 0.561 | Easy | 8,524 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2780361/fast-and-easy-python-solution-(faster-than-94.9) | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
maxoccurence=0
counter=0
for i in nums:
if i==1:
counter+=1
else:
if counter>maxoccurence:
maxoccurence=counter
counter=0
... | max-consecutive-ones | fast and easy python solution (faster than 94.9%) | sahityasetu1996 | 0 | 1 | max consecutive ones | 485 | 0.561 | Easy | 8,525 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2739885/Easy-to-understand | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
m=0
c=0
for i in range(len(nums)):
if nums[i]==1:
c+=1
if i==len(nums)-1:
m=max(m,c)
else:
m=max(m,c)
c=0
... | max-consecutive-ones | Easy to understand | Anil_8940 | 0 | 3 | max consecutive ones | 485 | 0.561 | Easy | 8,526 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2732646/Python-oror-O(n)-oror-Easy-oror-Beginners-solution | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
ans,c = 0,0
for i in range(len(nums)):
if nums[i]==1:
c+=1
ans = max(ans,c)
else:
c = 0
return ans | max-consecutive-ones | Python || O(n) || Easy || Beginners solution | its_iterator | 0 | 3 | max consecutive ones | 485 | 0.561 | Easy | 8,527 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2702294/Python-solution | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
maxx = float('-inf')
count= 0
for n in nums:
if n==1:
count+=1
elif n==0:
maxx = max(maxx,count)
count=0
maxx = max(maxx,count)
re... | max-consecutive-ones | Python solution | Sheeza | 0 | 4 | max consecutive ones | 485 | 0.561 | Easy | 8,528 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2673281/Python3-solution-with-counter | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
max_count = 0
temp_count = 0
for num in nums:
if num:
temp_count += 1
max_count = max(temp_count, max_count)
else:
temp_count = 0
... | max-consecutive-ones | ✔️ Python3 solution with counter | QuiShimo | 0 | 17 | max consecutive ones | 485 | 0.561 | Easy | 8,529 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2670587/Using-Kadane's-Algorithm | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
curr = 0;
max = 0;
for i in range(len(nums)):
if nums[i]==1:
curr = curr+1;
if curr>=max:
max=curr;
else:
curr=0
... | max-consecutive-ones | Using Kadane's Algorithm | naikaj18 | 0 | 5 | max consecutive ones | 485 | 0.561 | Easy | 8,530 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2614035/Python-Solution-(O(n)-Complexity)-Using-Counter-variable-(Faster-than-99) | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
count=0
maxlen=0
for i in nums:
if i==1:
count+=1
else:
maxlen=max(maxlen,count)
count=0
maxlen=max(maxlen,count)
return maxlen | max-consecutive-ones | Python Solution - (O(n) Complexity) Using Counter variable (Faster than 99%) | utsa_gupta | 0 | 39 | max consecutive ones | 485 | 0.561 | Easy | 8,531 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2539992/EASY-PYTHON3-SOLUTION | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
count = 0
res = []
for i in range(len(nums)):
if nums[i] == 1:
count += 1
if nums[i]==0 or i==len(nums)-1:
res.append(count)
count = 0
re... | max-consecutive-ones | 🔥 EASY PYTHON3 SOLUTION 🔥 | rajukommula | 0 | 26 | max consecutive ones | 485 | 0.561 | Easy | 8,532 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2452016/Python-95-faster | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
max_ones = 0
temp = 0
for i in nums:
if i == 1:
temp += 1
else:
max_ones = max(max_ones,temp)
temp = 0
if temp != 0:
max_ones ... | max-consecutive-ones | Python 95% faster | aruj900 | 0 | 40 | max consecutive ones | 485 | 0.561 | Easy | 8,533 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2382395/Python-Solution-or-Simple-and-Clean-Code-or-Counter-Reset-Logic | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
count = 0
ans = 0
for num in nums:
# reset counter
if num == 0:
count = 0
# update counter
else:
count += 1
# update max value... | max-consecutive-ones | Python Solution | Simple and Clean Code | Counter Reset Logic | Gautam_ProMax | 0 | 33 | max consecutive ones | 485 | 0.561 | Easy | 8,534 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2371978/Easy-python-code | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
i=0
l=len(nums)
c=0
max_=0
while i<l:
if nums[i]==1:
c+=1
max_=max(c,max_)
else:
c=0
i+=1
return max_ | max-consecutive-ones | Easy python code | sunakshi132 | 0 | 38 | max consecutive ones | 485 | 0.561 | Easy | 8,535 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2347942/Simple-Python-with-Explanation | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
# Init counts
max_ones,cnt = 0,0
# Iterate through numbers
for num in nums:
# 1
if num:
# Add 1 to count
cnt += 1
# Find new max
max_ones = max(max_ones,cnt)
# 0
... | max-consecutive-ones | Simple Python with Explanation | drblessing | 0 | 36 | max consecutive ones | 485 | 0.561 | Easy | 8,536 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2307481/Easy-Solution-using-Python | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
one_count , max_count = 0 , 0
for i in nums:
if i == 1:
one_count = one_count + 1
max_count = max(one_count,max_count)
else:
one_count = 0
return ... | max-consecutive-ones | Easy Solution using Python | laraib-sidd | 0 | 29 | max consecutive ones | 485 | 0.561 | Easy | 8,537 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2175314/Simplest-Approach-oror-Self-Explanatory-Approach | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
maxConsOnes = 0
length = len(nums)
ones = 0
for i in range(length):
if nums[i] == 1:
ones += 1
else:
maxConsOnes = max(maxConsOnes, ones)
... | max-consecutive-ones | Simplest Approach || Self Explanatory Approach | Vaibhav7860 | 0 | 39 | max consecutive ones | 485 | 0.561 | Easy | 8,538 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2063900/Faster-than-61.59-python-solutions | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
i = 0
dictt = {}
max_cnt = 0
cnt = 0
maxx = 0
while(i<len(nums)):
if(nums[i]==1):
dictt[max_cnt] = cnt+1
cnt+=1
else:
max_... | max-consecutive-ones | Faster than 61.59% python solutions | TusharDarko1508 | 0 | 68 | max consecutive ones | 485 | 0.561 | Easy | 8,539 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2061431/Python-Standard-Language-Agnostic-Solution | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
_max = count = 0
for num in nums:
if num == 1:
count += 1
else:
_max = max(_max, count)
count = 0
_max = max(_max, count)
... | max-consecutive-ones | [Python] Standard Language-Agnostic Solution | secret_dev | 0 | 24 | max consecutive ones | 485 | 0.561 | Easy | 8,540 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2059122/Simple-and-fast-Python-solution | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
acc, res = 0, 0
for n in nums:
acc = (acc + n) * n
res = max(acc, res)
return res | max-consecutive-ones | Simple & fast Python solution | tony10100476 | 0 | 38 | max consecutive ones | 485 | 0.561 | Easy | 8,541 |
https://leetcode.com/problems/max-consecutive-ones/discuss/2053769/Greedy-oror-T(n)O(n)-oror-Easy-oror-Python | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
# Greedy approach to count max 1's.
# Simply traverse the array and update count
# T(n)=O(n)
count=0
maxi=0
for i in nums:
if i==1:
count+=1
maxi=max(count,... | max-consecutive-ones | Greedy || T(n)=O(n) || Easy || Python | Aniket_liar07 | 0 | 30 | max consecutive ones | 485 | 0.561 | Easy | 8,542 |
https://leetcode.com/problems/max-consecutive-ones/discuss/1870954/Python-easy-solution | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
nums_list = [str(x) for x in nums]
nums_split = ''.join(nums_list).split('0')
max_len = -1
for i in nums_split:
if len(i) > max_len:
max_len = len(i)
return max_len | max-consecutive-ones | Python easy solution | alishak1999 | 0 | 50 | max consecutive ones | 485 | 0.561 | Easy | 8,543 |
https://leetcode.com/problems/max-consecutive-ones/discuss/1853071/Python-(Simple-Approach-Beginner-friendly) | class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
count = 0
maximum = 0
for i in nums:
if i == 1:
count+=1
else:
count = 0
maximum = max(maximum, count)
return maximum | max-consecutive-ones | Python (Simple Approach, Beginner-friendly) | vishvavariya | 0 | 82 | max consecutive ones | 485 | 0.561 | Easy | 8,544 |
https://leetcode.com/problems/max-consecutive-ones/discuss/1848252/Simple-and-easy-to-implement-Python-3-solution | class Solution(object):
def findMaxConsecutiveOnes(self, nums):
l, output = 0,0
for r, number in enumerate(nums):
if number == 0:
l = r+1
output = max(output, r-l+1)
return output | max-consecutive-ones | Simple and easy to implement Python 3 solution | arvindrao | 0 | 47 | max consecutive ones | 485 | 0.561 | Easy | 8,545 |
https://leetcode.com/problems/predict-the-winner/discuss/414803/Python-AC-98-Both-Recursion-and-DP-with-detailed-explanation. | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
def helper(i, j):
if i == j:
return nums[i]
if (i, j) in memo:
return memo[(i, j)]
score = max(nums[j] - helper(i, j-1), nums[i] - helper(i+1, j)... | predict-the-winner | Python [AC 98%] Both Recursion & DP with detailed explanation. | bos | 19 | 1,300 | predict the winner | 486 | 0.509 | Medium | 8,546 |
https://leetcode.com/problems/predict-the-winner/discuss/414803/Python-AC-98-Both-Recursion-and-DP-with-detailed-explanation. | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
dp[i][j] the person's effective score when pick, facing nums[i..j]
dp = [[0] * len(nums) for _ in range(len(nums))]
for s in range(len(nums)):
for i in range(len(nums)-s):
j = i + s
... | predict-the-winner | Python [AC 98%] Both Recursion & DP with detailed explanation. | bos | 19 | 1,300 | predict the winner | 486 | 0.509 | Medium | 8,547 |
https://leetcode.com/problems/predict-the-winner/discuss/414803/Python-AC-98-Both-Recursion-and-DP-with-detailed-explanation. | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
n = len(nums)
dp = nums[:]
for s in range(1, n):
newdp = [0] * n
for j in range(s, n):
i = j - s
newdp[j] = max(nums[i] - dp[j], nums[j] - dp[j-1])
... | predict-the-winner | Python [AC 98%] Both Recursion & DP with detailed explanation. | bos | 19 | 1,300 | predict the winner | 486 | 0.509 | Medium | 8,548 |
https://leetcode.com/problems/predict-the-winner/discuss/529789/Python3-simple-DP-solution | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
"""
O(n^2) time complexity
O(n) space complexity
"""
dp = [0]*len(nums)
for i in range(len(nums) - 1, -1, -1):
for j in range(i + 1, len(nums)):
m, n = nums[i] - dp[j], nu... | predict-the-winner | Python3 simple DP solution | jb07 | 2 | 193 | predict the winner | 486 | 0.509 | Medium | 8,549 |
https://leetcode.com/problems/predict-the-winner/discuss/1485626/Python-Easy | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
@functools.lru_cache(maxsize=None)
def recurse(i, j):
if i > j: return 0
return max(
nums[i] - recurse(i+1, j),
nums[j] - recurse(i, j-1)
)
... | predict-the-winner | [Python] Easy | dev-josh | 1 | 381 | predict the winner | 486 | 0.509 | Medium | 8,550 |
https://leetcode.com/problems/predict-the-winner/discuss/2837699/DFS-with-memoization-pattern | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
# find player 1 score > 0 if + player1 turn and - when player 2 turn
# return if player1 score - player2 score > 0
total = sum(nums)
memo = {} # (l,r): player1 score - player2 score
def dfs(l,r,turn):
... | predict-the-winner | DFS with memoization pattern | kenkala | 0 | 2 | predict the winner | 486 | 0.509 | Medium | 8,551 |
https://leetcode.com/problems/predict-the-winner/discuss/2764778/Memorization-with-a-clear-logic. | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
@cache
def helper(i, j):
if i == j:
return nums[i]
c1 = nums[i] - helper(i + 1, j)
c2 = nums[j] - helper(i, j - 1)
return max(c1, c2)
... | predict-the-winner | Memorization with a clear logic. | yiming999 | 0 | 8 | predict the winner | 486 | 0.509 | Medium | 8,552 |
https://leetcode.com/problems/predict-the-winner/discuss/2764772/A-recursion-solution-with-straightforward-logic. | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
def helper(i, j, a, b, turn):
if i == j:
if turn:
return a + nums[i] >= b
else:
return a >= b + nums[i]
if turn:
i... | predict-the-winner | A recursion solution with straightforward logic. | yiming999 | 0 | 7 | predict the winner | 486 | 0.509 | Medium | 8,553 |
https://leetcode.com/problems/predict-the-winner/discuss/2742380/Beautiful-Python-Solution | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
n = len(nums)
A = [0] * (n + 1)
for j in range(n):
A = [max(nums[k + j] - A[k], nums[k] - A[k + 1]) for k in range(n - j)]
return A[0] >= 0 | predict-the-winner | Beautiful Python Solution | vadim_vadim | 0 | 12 | predict the winner | 486 | 0.509 | Medium | 8,554 |
https://leetcode.com/problems/predict-the-winner/discuss/2719613/python-pretty-solution | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
def check_all(n: list, f: int, s: int, turn: bool):
if not n:
return f >= s
if turn:
return check_all(n[1:], f + n[0], s, False) or check_all(n[:len(n) - 1], f + n[-1], s, False)
... | predict-the-winner | python pretty solution | Yaro1 | 0 | 15 | predict the winner | 486 | 0.509 | Medium | 8,555 |
https://leetcode.com/problems/predict-the-winner/discuss/2695763/Python-Solution | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
dp = {}
def helper(l,r):
if (l,r) not in dp:
if l == r:
return nums[l]
dp[(l,r)] = max(nums[l] - helper(l+1,r), nums[r] - helper(l, r-1))
... | predict-the-winner | Python Solution | Michael_Songru | 0 | 11 | predict the winner | 486 | 0.509 | Medium | 8,556 |
https://leetcode.com/problems/predict-the-winner/discuss/2678505/python-memoization-DP-95-fast | class Solution:
def PredictTheWinner(self, A: List[int]) -> bool:
dp = [[-1]*len(A) for i in range(len(A))]
def solve(A,i,j):
if i>j:
return 0
if dp[i][j]!=-1:
return dp[i][j]
# consider we choose the start index(i) and then oppon... | predict-the-winner | python memoization DP 95% fast | Akash_chavan | 0 | 9 | predict the winner | 486 | 0.509 | Medium | 8,557 |
https://leetcode.com/problems/predict-the-winner/discuss/2677056/python | class Solution:
# dp[i][j] = max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1])
def PredictTheWinner(self, nums: List[int]) -> bool:
dp =[[0]*len(nums) for _ in range(len(nums))]
for i in range(len(nums)-1,-1,-1):
for j in range(i,len(nums)):
if i==j:
... | predict-the-winner | python | Jenn | 0 | 6 | predict the winner | 486 | 0.509 | Medium | 8,558 |
https://leetcode.com/problems/predict-the-winner/discuss/1585128/Python3-or-Stone-Game-Concept | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
n=len(nums)
self.player1,self.player2=0,0
@lru_cache(None,None)
def helper(start,end,count):
if start>end:
return 0
if count%2==0:
self.player1=max(nums[start]... | predict-the-winner | [Python3} | Stone Game Concept | swapnilsingh421 | 0 | 164 | predict the winner | 486 | 0.509 | Medium | 8,559 |
https://leetcode.com/problems/predict-the-winner/discuss/761225/Python-Min-Max-Recursion-%2B-Memoization-beats-100 | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
# minMax -- recursion with memoization
n = len(nums)
if n == 1 or n % 2 == 0:
return True
self.dp = [0 for _ in range(n * n)]
return self.checkWin(nums, 0, n-1) >= 0
def checkW... | predict-the-winner | Python -- Min-Max Recursion + Memoization -- beats 100 % | nbismoi | 0 | 199 | predict the winner | 486 | 0.509 | Medium | 8,560 |
https://leetcode.com/problems/predict-the-winner/discuss/725979/Python-Simple-DP-or-O(n2) | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
n = len(nums)
dp = [[0 for _ in range(n)] for _ in range(n)]
for i, num in enumerate(nums):
dp[i][i] = num
for start in reversed(range(n)):
for end in range(start+1, n):
... | predict-the-winner | Python Simple DP | O(n^2) | leeteatsleep | 0 | 179 | predict the winner | 486 | 0.509 | Medium | 8,561 |
https://leetcode.com/problems/predict-the-winner/discuss/1066768/Python-3-dp-28ms-96-faster | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
n=len(nums)
if n==1:
return True
dp=[[0 for i in range(n)]for j in range(n)]
i,j=0,0
for k in range(n):
i,j=0,k
while j<n:
if k==0:
dp... | predict-the-winner | Python 3 dp 28ms 96% faster | gauravgoyalll | -1 | 545 | predict the winner | 486 | 0.509 | Medium | 8,562 |
https://leetcode.com/problems/predict-the-winner/discuss/758825/Python3-4-line-DP-beats-100 | class Solution:
def PredictTheWinner(self, nums: List[int]) -> bool:
N = len(nums)
if N % 2 == 0: return True # can always win by picking larger one of odd or even subarray
# DP
dp = nums[:]
for i in range(1, N):
for j in range(N - i):
... | predict-the-winner | [Python3] 4 line DP beats 100% | dashidhy | -2 | 284 | predict the winner | 486 | 0.509 | Medium | 8,563 |
https://leetcode.com/problems/zuma-game/discuss/1568450/Python-Easy-BFS-solution-with-explain | class Solution:
def findMinStep(self, board: str, hand: str) -> int:
# start from i and remove continues ball
def remove_same(s, i):
if i < 0:
return s
left = right = i
while left > 0 and s[left-1] == s[i]:
lef... | zuma-game | [Python] Easy BFS solution with explain | nightybear | 8 | 634 | zuma game | 488 | 0.346 | Hard | 8,564 |
https://leetcode.com/problems/zuma-game/discuss/2531793/Python3-dp | class Solution:
def findMinStep(self, board: str, hand: str) -> int:
hand = ''.join(sorted(hand))
@cache
def fn(board, hand):
"""Return min number of balls to insert."""
if not board: return 0
if not hand: return inf
ans = inf
... | zuma-game | [Python3] dp | ye15 | 1 | 115 | zuma game | 488 | 0.346 | Hard | 8,565 |
https://leetcode.com/problems/increasing-subsequences/discuss/1577928/Python-DFS | class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
def dfs(i, num, curr):
if len(curr)>=2:
ans.add(curr[:])
if i>=len(nums):
return
for j in range(i, len(nums)):
if nums[j]>=num:
... | increasing-subsequences | Python DFS | hX_ | 3 | 293 | increasing subsequences | 491 | 0.521 | Medium | 8,566 |
https://leetcode.com/problems/increasing-subsequences/discuss/2564123/Python-or-Backtracking | class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
def combinations(idx, curArr, nums, res):
if len(curArr) >= 2:
res.add(curArr)
for i in range(idx, len(nums)):
if not curArr or nums[i] >= curArr[-1]:
combinations(i+1, curArr+(nums[i],), nums, res)
res = set()
... | increasing-subsequences | Python | Backtracking | Mark5013 | 1 | 217 | increasing subsequences | 491 | 0.521 | Medium | 8,567 |
https://leetcode.com/problems/increasing-subsequences/discuss/2789182/Python-or-Simple-or-Backtracking | class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
def backtrack(nums_start, seq):
if len(seq) >= 2:
sequences.add(tuple(seq[::]))
for i in range(nums_start, len(nums)):
if len(seq) == 0 or seq[-1] <= nums[i]:
... | increasing-subsequences | Python | Simple | Backtracking | ivzap | 0 | 8 | increasing subsequences | 491 | 0.521 | Medium | 8,568 |
https://leetcode.com/problems/increasing-subsequences/discuss/2660532/Python-or-Set | class Solution:
def findSubsequences(self, xs: List[int]) -> List[List[int]]:
n = len(xs)
ans = set()
for i in range (1, n):
x = xs[i]
m = len(ans)
seqs = set()
for seq in ans:
if seq[-1] <= x:
new_seq = li... | increasing-subsequences | Python | Set | on_danse_encore_on_rit_encore | 0 | 4 | increasing subsequences | 491 | 0.521 | Medium | 8,569 |
https://leetcode.com/problems/increasing-subsequences/discuss/2404079/Python3%3A-Simple-Backtracking | class Solution:
def findSubsequences(self, nums: list[int]) -> list[list[int]]:
memo = set()
output = []
def backtracking(start = 0, ans = [], prev = float("-inf")):
for i in range(start, len(nums)):
if nums[i] >= prev:
ans.append(nums[i])
... | increasing-subsequences | Python3: Simple Backtracking | NoobPiggy | 0 | 50 | increasing subsequences | 491 | 0.521 | Medium | 8,570 |
https://leetcode.com/problems/increasing-subsequences/discuss/2254009/Python-explained-easy-to-understand! | class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
ans=[]
path=[]
def backtracking(i):
if len(path)>=2:
# if not non-decreasing, no need to continue push nums into the path, just return
if path[-1]<path[-2]:
... | increasing-subsequences | Python explained, easy to understand! | XRFXRF | 0 | 73 | increasing subsequences | 491 | 0.521 | Medium | 8,571 |
https://leetcode.com/problems/increasing-subsequences/discuss/2063456/ok | class Solution:
def findSubsequences(self, nn) :
out={}
for n in nn :
t={(n,)}
for v in out :
t.add(v)
if v[-1]<=n :
t.add(v+(n,))
out=t
return [v for v in out if len(v)>1] | increasing-subsequences | ok ^^ | andrii_khlevniuk | 0 | 15 | increasing subsequences | 491 | 0.521 | Medium | 8,572 |
https://leetcode.com/problems/increasing-subsequences/discuss/1848955/Python-easy-to-read-and-understand-or-2-solutions | class Solution:
def solve(self, nums, index, curr, path):
if index > len(nums):
return
if len(path) >= 2:
self.ans.append(path)
for i in range(index, len(nums)):
if nums[i] >= curr:
self.solve(nums, i+1, nums[i], path+[nums[i]])
de... | increasing-subsequences | Python easy to read and understand | 2 solutions | sanial2001 | 0 | 127 | increasing subsequences | 491 | 0.521 | Medium | 8,573 |
https://leetcode.com/problems/increasing-subsequences/discuss/1848955/Python-easy-to-read-and-understand-or-2-solutions | class Solution:
def solve(self, nums, index, curr, path):
if index > len(nums):
return
if len(path) >= 2:
self.ans.add(path)
for i in range(index, len(nums)):
if nums[i] >= curr:
self.solve(nums, i+1, nums[i], path+(nums[i],))
def ... | increasing-subsequences | Python easy to read and understand | 2 solutions | sanial2001 | 0 | 127 | increasing subsequences | 491 | 0.521 | Medium | 8,574 |
https://leetcode.com/problems/increasing-subsequences/discuss/1615945/python-solution-easy-to-understand-with-backtracking | class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
result = []
current = []
self.dfs(nums, current, result)
return result
def dfs(self, nums, current, result):
if len(current) >= 2 and current not in result:
result.append(curr... | increasing-subsequences | python solution, easy to understand with backtracking | blank_tc | 0 | 226 | increasing subsequences | 491 | 0.521 | Medium | 8,575 |
https://leetcode.com/problems/increasing-subsequences/discuss/1593187/Python3-DFS-solution-with-full-comments | class Solution:
def dfs(self, pos, tmp):
# only add to answer when size >= 2
if (len(tmp) >= 2):
# use a set to delete duplicates
self.res.add(tuple(copy.deepcopy(tmp)))
# when we reach the last element return
if (pos == len(self.nums) - 1):
... | increasing-subsequences | Python3 DFS solution with full comments | Zhoueeer | 0 | 95 | increasing subsequences | 491 | 0.521 | Medium | 8,576 |
https://leetcode.com/problems/increasing-subsequences/discuss/1542825/WEEB-DOES-PYTHON-BFS | class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
queue = deque([])
visited = set()
for i in range(len(nums)):
if nums[i] in visited: continue
visited.add(nums[i])
queue.append(([nums[i]], i))
return self.bfs(queue, nums)
def bfs(self, queue, nums):
result = []
wh... | increasing-subsequences | WEEB DOES PYTHON BFS | Skywalker5423 | 0 | 99 | increasing subsequences | 491 | 0.521 | Medium | 8,577 |
https://leetcode.com/problems/increasing-subsequences/discuss/1472754/python3-oror-Recursion-%2B-Memoization-oror-Self-Understandable-oror-Easy-Understaning | class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
memo={}
def subsequence(index,path):
if index in memo:
if path not in res:
res.append(path+memo[index])
return
if len(path)>1 and p... | increasing-subsequences | python3 || Recursion + Memoization || Self-Understandable || Easy -Understaning | bug_buster | 0 | 224 | increasing subsequences | 491 | 0.521 | Medium | 8,578 |
https://leetcode.com/problems/increasing-subsequences/discuss/1467813/python-solution-using-dfs | class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
n = len(nums)
graph = defaultdict(set)
for i in range(n-1):
for j in range(i+1, n):
graph[i].add(j)
#directed graph, no need for visited set
... | increasing-subsequences | python solution using dfs | byuns9334 | 0 | 159 | increasing subsequences | 491 | 0.521 | Medium | 8,579 |
https://leetcode.com/problems/increasing-subsequences/discuss/1260888/python-recursiveiterative | class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
arr = [None]*len(nums)
set1 = set()
for i in range(len(nums)):
arr[i] = [[nums[i]]]
for j in range(i):
for k in range(len(arr[j])):
if arr[j][k][-1]<=nu... | increasing-subsequences | python recursive,iterative | pranavgarg039 | 0 | 103 | increasing subsequences | 491 | 0.521 | Medium | 8,580 |
https://leetcode.com/problems/increasing-subsequences/discuss/1260888/python-recursiveiterative | class Solution:
def __init__(self):
self.set1 = set()
def fun(self,i,nums,arr):
if len(arr)>=2:
self.set1.add(tuple(arr))
if i==len(nums):
return
if len(arr)==0 or nums[i]>=arr[-1]:
self.fun(i+1,nums,arr+[nums[i]])
self.fun(i+1,nu... | increasing-subsequences | python recursive,iterative | pranavgarg039 | 0 | 103 | increasing subsequences | 491 | 0.521 | Medium | 8,581 |
https://leetcode.com/problems/increasing-subsequences/discuss/498621/Python-Solution-using-itertools | class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
res = []
for i in range(2, len(nums)+1):
for x in set(itertools.combinations(nums, i)):
if all(a <= b for a, b in zip(x, x[1:])):
res.append(x)
return res | increasing-subsequences | Python Solution using itertools | cppygod | 0 | 160 | increasing subsequences | 491 | 0.521 | Medium | 8,582 |
https://leetcode.com/problems/construct-the-rectangle/discuss/1990625/Python-Easy-Solution-or-Faster-Than-92-Submits | class Solution:
def constructRectangle(self, area: int) -> List[int]:
for i in range(int(area**0.5),0,-1):
if area % i == 0: return [area//i,i] | construct-the-rectangle | [ Python ] Easy Solution | Faster Than 92% Submits | crazypuppy | 3 | 300 | construct the rectangle | 492 | 0.538 | Easy | 8,583 |
https://leetcode.com/problems/construct-the-rectangle/discuss/1796355/2-Lines-Python-Solution-oror-70-Faster-(40ms)oror-Memory-less-than-90 | class Solution:
def constructRectangle(self, area: int) -> List[int]:
for i in range(int(area**0.5), 0, -1):
if area%i == 0: return [area//i,i] | construct-the-rectangle | 2-Lines Python Solution || 70% Faster (40ms)|| Memory less than 90% | Taha-C | 1 | 105 | construct the rectangle | 492 | 0.538 | Easy | 8,584 |
https://leetcode.com/problems/construct-the-rectangle/discuss/1219105/Python3-simple-solution-beats-89-users | class Solution:
def constructRectangle(self, area: int) -> List[int]:
l = area
b = 1
while l > b:
z = -1
for i in range(2,int(area**.5)+1):
if l%i == 0:
z = i
if z == -1 or l//z < b*z:
break
e... | construct-the-rectangle | Python3 simple solution beats 89% users | EklavyaJoshi | 1 | 114 | construct the rectangle | 492 | 0.538 | Easy | 8,585 |
https://leetcode.com/problems/construct-the-rectangle/discuss/1219105/Python3-simple-solution-beats-89-users | class Solution:
def constructRectangle(self, area: int) -> List[int]:
for i in range(int(area**.5),-1,-1):
if area%i == 0:
break
return [area//i,i] | construct-the-rectangle | Python3 simple solution beats 89% users | EklavyaJoshi | 1 | 114 | construct the rectangle | 492 | 0.538 | Easy | 8,586 |
https://leetcode.com/problems/construct-the-rectangle/discuss/359219/Solution-in-Python-3-(three-lines) | class Solution:
def constructRectangle(self, a: int) -> List[int]:
for i in range(int(a**.5),-1,-1):
if a % i == 0: break
return [a//i,i]
- Junaid Mansuri
(LeetCode ID)@hotmail.com | construct-the-rectangle | Solution in Python 3 (three lines) | junaidmansuri | 1 | 380 | construct the rectangle | 492 | 0.538 | Easy | 8,587 |
https://leetcode.com/problems/construct-the-rectangle/discuss/2817620/python | class Solution:
def constructRectangle(self, area: int) -> List[int]:
w = int(sqrt(area))
while area % w:
w -= 1
return [area // w, w] | construct-the-rectangle | python | xy01 | 0 | 3 | construct the rectangle | 492 | 0.538 | Easy | 8,588 |
https://leetcode.com/problems/construct-the-rectangle/discuss/2414592/Python-oror-Recursion-oror-only-1-for-loop-oror-99-faster | class Solution:
def min_dif(self,x, area) :
if( area % x == 0):
return([area // x, x])
return(self.min_dif( x-1, area))
def constructRectangle(self, area: int) -> List[int]:
return( self.min_dif(int(area**0.5), area) ) | construct-the-rectangle | Python || Recursion || only 1 for loop || 99% faster | NITIN_DS | 0 | 88 | construct the rectangle | 492 | 0.538 | Easy | 8,589 |
https://leetcode.com/problems/construct-the-rectangle/discuss/2414592/Python-oror-Recursion-oror-only-1-for-loop-oror-99-faster | class Solution:
def constructRectangle(self, area: int) -> List[int]:
for fact in range( int(area**0.5), 0, -1):
if( area % fact == 0):
return ((area // fact), fact) | construct-the-rectangle | Python || Recursion || only 1 for loop || 99% faster | NITIN_DS | 0 | 88 | construct the rectangle | 492 | 0.538 | Easy | 8,590 |
https://leetcode.com/problems/construct-the-rectangle/discuss/2414566/Python-98-96-using-Sqrt | class Solution:
def constructRectangle(self, area: int) -> List[int]:
sm = int(sqrt(area)) #if sqrt then they will be the closest ones so start here
ot = 0 #this is the other one the bigger one
while 1:
ot = area / sm #the bigger one is area/smaller one
i... | construct-the-rectangle | Python %98-%96 using Sqrt | pomelonychoco | 0 | 42 | construct the rectangle | 492 | 0.538 | Easy | 8,591 |
https://leetcode.com/problems/construct-the-rectangle/discuss/2408372/Python-O(sqrt(n)) | class Solution:
def constructRectangle(self, area: int) -> list[int]:
sqrt = area ** 0.5
if sqrt % 1 == 0:
return [int(sqrt)] * 2
factors = [[area // i, i] for i in range(2, int(sqrt) + 1) if area % i == 0]
return [area, 1] if not factors else factors[-1] | construct-the-rectangle | Python O(sqrt(n)) | Mark_computer | 0 | 22 | construct the rectangle | 492 | 0.538 | Easy | 8,592 |
https://leetcode.com/problems/construct-the-rectangle/discuss/2408372/Python-O(sqrt(n)) | class Solution:
def constructRectangle(self, area: int) -> list[int]:
sqrt = area ** 0.5 # square root
if sqrt % 1 == 0: # if *area* is a perfect square
return [int(sqrt)] * 2 # just return two same square roots (i.e [int(sqrt), int(sqrt)])
factors = [[area // i, i] for i in rang... | construct-the-rectangle | Python O(sqrt(n)) | Mark_computer | 0 | 22 | construct the rectangle | 492 | 0.538 | Easy | 8,593 |
https://leetcode.com/problems/construct-the-rectangle/discuss/2395294/python-O(n) | class Solution:
def constructRectangle(self, area: int) -> List[int]:
L = W = int(area**0.5)
while L*W != area:
if L*W < area:
L += 1
else:
W -= 1
return [L,W] | construct-the-rectangle | python O(n) | sunakshi132 | 0 | 51 | construct the rectangle | 492 | 0.538 | Easy | 8,594 |
https://leetcode.com/problems/construct-the-rectangle/discuss/2290494/Using-custom-fast-sqrt-and-searching-from-sqrt-to-1 | class Solution:
def constructRectangle(self, area: int):
y = Solution.mySqrt(area)
for i in range(y, 0, -1):
if not area%i:
return [int(area/i), i]
def mySqrt(x):
if x == 0:
return 0
n = x
count = 0
while True:
... | construct-the-rectangle | Using custom fast sqrt and searching from sqrt to 1 | f20190241 | 0 | 52 | construct the rectangle | 492 | 0.538 | Easy | 8,595 |
https://leetcode.com/problems/construct-the-rectangle/discuss/1827621/Python-Solution | class Solution:
def constructRectangle(self, area: int) -> List[int]:
n = area ** 0.5
if n.is_integer():
return [int(n),int(n)]
else:
n = math.floor(n)
m = 0
while True:
if (area / n).is_integer():
m = area/n... | construct-the-rectangle | Python Solution | MS1301 | 0 | 98 | construct the rectangle | 492 | 0.538 | Easy | 8,596 |
https://leetcode.com/problems/construct-the-rectangle/discuss/1279650/Python3-dollarolution | class Solution:
import math
def constructRectangle(self, area: int) -> List[int]:
a = int(math.sqrt(area))
v = []
for i in range(a,area+1):
if area % i == 0:
v.append(i)
v.append(int(area/i))
break
return sorted(v,revers... | construct-the-rectangle | Python3 $olution | AakRay | 0 | 182 | construct the rectangle | 492 | 0.538 | Easy | 8,597 |
https://leetcode.com/problems/construct-the-rectangle/discuss/1012621/Simple-solution-with-comments | class Solution:
def constructRectangle(self, area: int) -> List[int]:
l=[[i,area//i] for i in range(1,area//2+1) if area%i==0 and i-(area//i)>=0] #This list contains pairs which are factors of the given area
#and to reduce the time, we only iterate till area//2 and add [area,1] separa... | construct-the-rectangle | Simple solution with comments | thisisakshat | 0 | 126 | construct the rectangle | 492 | 0.538 | Easy | 8,598 |
https://leetcode.com/problems/construct-the-rectangle/discuss/631098/Intuitive-approach-by-increase-L-from-sqrt-value-until-a-dividable-solution-found | class Solution:
def constructRectangle(self, area: int) -> List[int]:
sqrt_val = int(math.sqrt(area))
l = w = sqrt_val
while (l * w) != area and l > 0 and w > 0:
l += 1
w = area // l
return [l, w] if w > 1 else (area, 1) | construct-the-rectangle | Intuitive approach by increase L from sqrt value until a dividable solution found | puremonkey2001 | 0 | 71 | construct the rectangle | 492 | 0.538 | Easy | 8,599 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.