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/contains-duplicate-ii/discuss/2834879/PYTHON-SOLUTION-HASHMAP-oror-EXPLAINED | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
if len(nums)<=k: #if not totally k values then check the duplicates only
if len(set(nums))!=len(nums):
return True
d={}
for i in range(len(nums)): #store the indeces of occurences... | contains-duplicate-ii | PYTHON SOLUTION - HASHMAP || EXPLAINED β | T1n1_B0x1 | 0 | 7 | contains duplicate ii | 219 | 0.423 | Easy | 3,900 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2829584/Python-Solution | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
hmap = {}
for index in range(0, len(nums)):
if nums[index] in hmap:
if abs(index - hmap.get(nums[index])) <= k:
return True
else:
hm... | contains-duplicate-ii | Python Solution | Antoine703 | 0 | 1 | contains duplicate ii | 219 | 0.423 | Easy | 3,901 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2800959/Easy-to-understand-python-solution. | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
val_idxs = dict()
for idx in range(len(nums)):
val = nums[idx]
if val in val_idxs:
for another_idx in val_idxs[val]:
if abs(idx - another_idx) <= k:
... | contains-duplicate-ii | Easy to understand python solution. | shanemmay | 0 | 2 | contains duplicate ii | 219 | 0.423 | Easy | 3,902 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2793898/Python-beats-91-(iterative-approach) | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
dups = {}
for i, n in enumerate(nums):
if n in dups.keys():
idx = dups.get(n)
if i - idx <= k: return True
dups.update({ n: i })
... | contains-duplicate-ii | Python beats 91% (iterative approach) | farruhzokirov00 | 0 | 12 | contains duplicate ii | 219 | 0.423 | Easy | 3,903 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2780477/Python3-100-faster-with-explanation | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
if len(nums) == len(set(nums)):
return False
imap = {}
for i in range(len(nums)):
if nums[i] in imap:
#run check for True
for item in imap[nums... | contains-duplicate-ii | Python3, 100% faster with explanation | cvelazquez322 | 0 | 14 | contains duplicate ii | 219 | 0.423 | Easy | 3,904 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2780357/Python3-hashing-or-best-solution | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
d = {}
for i in range(len(nums)):
if nums[i] in d and abs(i - d[nums[i]]) <= k:
return True
d[nums[i]] = i
return False | contains-duplicate-ii | Python3 - hashing | best solution | sandeepmatla | 0 | 1 | contains duplicate ii | 219 | 0.423 | Easy | 3,905 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2752571/Simple-Python3-Solution-Beats-97-in-runtime-using-dictionary-concept | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
temp_dict = {}
for x in range(0, len(nums)):
if nums[x] in temp_dict and abs(temp_dict[nums[x]] - x) <= k:
return True
temp_dict[nums[x]] = x
return False | contains-duplicate-ii | Simple Python3 Solution Beats 97% in runtime using dictionary concept | vivekrajyaguru | 0 | 10 | contains duplicate ii | 219 | 0.423 | Easy | 3,906 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2732409/BRUTEFORCE-EASY-SOLUTION | class Solution:
# calculating min distance between same numbers
def helper(self,arr):
mini = float("infinity")
for i in range(1,len(arr)):
#minimum window between two elements
if (arr[i] - arr[i-1]) < mini:
mini = arr[i] - arr[i-1]
return mini
... | contains-duplicate-ii | BRUTEFORCE EASY SOLUTION | azar1 | 0 | 13 | contains duplicate ii | 219 | 0.423 | Easy | 3,907 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2730638/Fastest-Solution-In-Python-Python-Simple-Python-Solution-100-Optimal-Solution | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
n = len(nums)
dd = {}
for i in range(n):
if nums[i] in dd.keys():
if abs(dd[nums[i]] - i) <= k:
return True
... | contains-duplicate-ii | Fastest Solution In Python [ Python ] β
Simple Python Solution β
β
β
100% Optimal Solution | vaibhav0077 | 0 | 9 | contains duplicate ii | 219 | 0.423 | Easy | 3,908 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2730527/Python-90-Faster-than-other-Python-Solutions | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
returnVal = False
numDict = {}
for i in range(len(nums)):
if nums[i] in numDict:
if abs(numDict[nums[i]] - i) <= k:
returnVal = True
return ... | contains-duplicate-ii | Python 90% Faster than other Python Solutions | FarazAli584682 | 0 | 5 | contains duplicate ii | 219 | 0.423 | Easy | 3,909 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2730165/Simple-Solution-O(n) | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
result = {} # [val, ind]
n = len(nums)
for i in range(n):
if nums[i] in result:
target, index = nums[i], result[nums[i]]
if abs(index - i) <= k:
... | contains-duplicate-ii | Simple Solution - O(n) | Vedant-G | 0 | 8 | contains duplicate ii | 219 | 0.423 | Easy | 3,910 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2730013/Python-easy-solution-with-while-loop | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
i = 0
seen = {}
while i < len(nums):
if nums[i] not in seen.keys():
seen[nums[i]] = i
else:
if abs(i-seen[nums[i]]) <= k:
return Tr... | contains-duplicate-ii | Python easy solution with while loop | vegancyberpunk | 0 | 6 | contains duplicate ii | 219 | 0.423 | Easy | 3,911 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2729982/Stupid-simple-python-solution-O(n-min(nk))-time-or-vertical-oror-bars-or | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
memo = {}
for i, num in enumerate(nums):
# Created a list of seen indices for the number if seen
if num in memo:
# scan sub list
for sub_num in memo[num]:
... | contains-duplicate-ii | Stupid simple python solution O(n min(n,k)) time | vertical || bars | | vdz1192 | 0 | 5 | contains duplicate ii | 219 | 0.423 | Easy | 3,912 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2729812/python-with-dictionary-2-loops-80 | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
if len(nums) < 2:
return False
di = {}
for ind, num in enumerate(nums):
if di.get(num) is None:
di[num] = [ind]
else:
di[num].append(ind)
... | contains-duplicate-ii | python with dictionary 2 loops 80% | m-s-dwh-bi | 0 | 3 | contains duplicate ii | 219 | 0.423 | Easy | 3,913 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2729612/Beginner's-Friendly-Solution-or-Python | class Solution(object):
def containsNearbyDuplicate(self, nums, k):
hashT = {}
for i in range(len(nums)):
if nums[i] in hashT:
if abs(i - hashT[nums[i]]) <= k: return True
hashT[nums[i]] = i
return False | contains-duplicate-ii | Beginner's Friendly Solution | Python | its_krish_here | 0 | 12 | contains duplicate ii | 219 | 0.423 | Easy | 3,914 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2729525/PYTHON-Easy-python-solution | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
length = len(nums)
if k >= length - 1:
return True if len(set(nums)) < len(nums) else False
hash_table = dict()
for counter in range(length):
if nums... | contains-duplicate-ii | [PYTHON] Easy python solution | valera_grishko | 0 | 7 | contains duplicate ii | 219 | 0.423 | Easy | 3,915 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2729332/Easy-HashMap-in-Python | class Solution:
# simple HashMap Solution
# Proof -> If n exists before and the indices differnce is <= k then it must be from
# the last occurence of n in the array nums.
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
hm = defaultdict(int)
for i, n in enumerate(nums... | contains-duplicate-ii | Easy HashMap in Pythonπ₯Ά | shiv-codes | 0 | 3 | contains duplicate ii | 219 | 0.423 | Easy | 3,916 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2729326/Simple-python-solution-using-dictionary | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
d = defaultdict(int)
for i, num in enumerate(nums):
if num in d and i - d[num] <= k: return True
d[num] = i
return False | contains-duplicate-ii | Simple python solution using dictionary | Jaykant | 0 | 3 | contains duplicate ii | 219 | 0.423 | Easy | 3,917 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2729126/Intuitive-Python-Set-Approach-(With-explanation) | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
# if k was bigger than the array, we anyways need to look inside the array only
k = min(k, len(nums))
# building the seen set
seen = set()
for i in range(k):
if nums[i] i... | contains-duplicate-ii | Intuitive Python Set Approach (With explanation) | g_aswin | 0 | 7 | contains duplicate ii | 219 | 0.423 | Easy | 3,918 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2729005/Python-Simple-Python-Solution-Using-Hashmap-or-Dictionary | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
d={}
for i in range(len(nums)):
if nums[i] in d and abs(i-d[nums[i]])<=k:
return True
d[nums[i]]=i
return False | contains-duplicate-ii | [ Python ] β
β
Simple Python Solution Using Hashmap or Dictionary π₯³βπ | ASHOK_KUMAR_MEGHVANSHI | 0 | 10 | contains duplicate ii | 219 | 0.423 | Easy | 3,919 |
https://leetcode.com/problems/contains-duplicate-ii/discuss/2729005/Python-Simple-Python-Solution-Using-Hashmap-or-Dictionary | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
hashmap = {}
for index in range(len(nums)):
if nums[index] not in hashmap:
hashmap[nums[index]] = [index]
else:
hashmap[nums[index]].append(index)
for key in hashmap:
array = hashmap[key]
if len(array) !... | contains-duplicate-ii | [ Python ] β
β
Simple Python Solution Using Hashmap or Dictionary π₯³βπ | ASHOK_KUMAR_MEGHVANSHI | 0 | 10 | contains duplicate ii | 219 | 0.423 | Easy | 3,920 |
https://leetcode.com/problems/contains-duplicate-iii/discuss/825267/Python3-summarizing-Contain-Duplicates-I-II-III | class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
seen = set()
for x in nums:
if x in seen: return True
seen.add(x)
return False | contains-duplicate-iii | [Python3] summarizing Contain Duplicates I, II, III | ye15 | 22 | 689 | contains duplicate iii | 220 | 0.22 | Hard | 3,921 |
https://leetcode.com/problems/contains-duplicate-iii/discuss/825267/Python3-summarizing-Contain-Duplicates-I-II-III | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
seen = {}
for i, x in enumerate(nums):
if x in seen and i - seen[x] <= k: return True
seen[x] = i
return False | contains-duplicate-iii | [Python3] summarizing Contain Duplicates I, II, III | ye15 | 22 | 689 | contains duplicate iii | 220 | 0.22 | Hard | 3,922 |
https://leetcode.com/problems/contains-duplicate-iii/discuss/825267/Python3-summarizing-Contain-Duplicates-I-II-III | class Solution:
def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:
if t < 0: return False # edge case
seen = {}
for i, x in enumerate(nums):
bkt = x//(t+1)
if bkt in seen and i - seen[bkt][0] <= k: return True
if ... | contains-duplicate-iii | [Python3] summarizing Contain Duplicates I, II, III | ye15 | 22 | 689 | contains duplicate iii | 220 | 0.22 | Hard | 3,923 |
https://leetcode.com/problems/contains-duplicate-iii/discuss/825606/Python-3-or-Official-Solution-in-Python-3-or-2-Methods-or-Explanation | class Solution:
def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:
from sortedcontainers import SortedSet
if not nums or t < 0: return False # Handle special cases
ss, n = SortedSet(), 0 # Create SortedSet. `n` is the size of sortedset, max ... | contains-duplicate-iii | Python 3 | Official Solution in Python 3 | 2 Methods | Explanation | idontknoooo | 16 | 2,600 | contains duplicate iii | 220 | 0.22 | Hard | 3,924 |
https://leetcode.com/problems/contains-duplicate-iii/discuss/825606/Python-3-or-Official-Solution-in-Python-3-or-2-Methods-or-Explanation | class Solution:
def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:
if not nums or t < 0: return False
min_val = min(nums)
bucket_key = lambda x: (x-min_val) // (t+1) # A lambda function generate buckey key given a value
d = collections.defaultdict... | contains-duplicate-iii | Python 3 | Official Solution in Python 3 | 2 Methods | Explanation | idontknoooo | 16 | 2,600 | contains duplicate iii | 220 | 0.22 | Hard | 3,925 |
https://leetcode.com/problems/contains-duplicate-iii/discuss/2298891/Two-Python-Solutions%3A-Memory-Optimization-Speed-Optimization | class Solution:
def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:
if t == 0 and len(set(nums)) == len(nums): return False
bucket = {}
width = t + 1
for i, n in enumerate(nums):
bucket_i = n // width
... | contains-duplicate-iii | Two Python Solutions: Memory Optimization, Speed Optimization | zip_demons | 3 | 417 | contains duplicate iii | 220 | 0.22 | Hard | 3,926 |
https://leetcode.com/problems/contains-duplicate-iii/discuss/748483/Python3-via-bucketing | class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
seen = set()
for x in nums:
if x in seen: return True
seen.add(x)
return False | contains-duplicate-iii | [Python3] via bucketing | ye15 | 2 | 364 | contains duplicate iii | 220 | 0.22 | Hard | 3,927 |
https://leetcode.com/problems/contains-duplicate-iii/discuss/748483/Python3-via-bucketing | class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
seen = {}
for i, x in enumerate(nums):
if x in seen and i - seen[x] <= k: return True
seen[x] = i
return False | contains-duplicate-iii | [Python3] via bucketing | ye15 | 2 | 364 | contains duplicate iii | 220 | 0.22 | Hard | 3,928 |
https://leetcode.com/problems/contains-duplicate-iii/discuss/748483/Python3-via-bucketing | class Solution:
def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:
if t < 0: return False # edge case
seen = {}
for i, x in enumerate(nums):
bkt = x//(t+1)
if bkt in seen and i - seen[bkt][0] <= k: return True
if ... | contains-duplicate-iii | [Python3] via bucketing | ye15 | 2 | 364 | contains duplicate iii | 220 | 0.22 | Hard | 3,929 |
https://leetcode.com/problems/contains-duplicate-iii/discuss/2789561/python-oror-sliding-window-%2B-BST%3A-O(n*log(k)) | class Solution:
def containsNearbyAlmostDuplicate(self, nums: List[int], indexDiff: int, valueDiff: int) -> bool:
"""Sliding window + BST: O(N*log(indexDiff))"""
bst = BinarySearchTree()
for i in range(len(nums)):
if i > 0:
closest = bst.search_closest(nums[i])... | contains-duplicate-iii | python || sliding window + BST: O(n*log(k)) | ivan-luchko | 0 | 7 | contains duplicate iii | 220 | 0.22 | Hard | 3,930 |
https://leetcode.com/problems/contains-duplicate-iii/discuss/505508/Python3-simple-solution-faster-than-99.85 | class Solution:
def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:
if not nums or k<1 or t<0 or (t==0 and len(nums)==len(set(nums))): return False
for i in range(len(nums)):
for j in range(1,k+1):
if (i+j)>=len(nums): break
i... | contains-duplicate-iii | Python3 simple solution, faster than 99.85% | jb07 | 0 | 364 | contains duplicate iii | 220 | 0.22 | Hard | 3,931 |
https://leetcode.com/problems/maximal-square/discuss/1632285/Python-1D-Array-DP-Optimisation-Process-Explained | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
result = 0
for i in range(len(matrix)):
for j in range(len(matrix[0])):
curr = 0 # current length of the square at (i, j)
flag = True # indicates if there still exists a valid squar... | maximal-square | [Python] 1D-Array DP - Optimisation Process Explained | zayne-siew | 9 | 610 | maximal square | 221 | 0.446 | Medium | 3,932 |
https://leetcode.com/problems/maximal-square/discuss/1632285/Python-1D-Array-DP-Optimisation-Process-Explained | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
m, n = len(matrix), len(matrix[0])
result = 0
dp = [[0]*n for _ in range(m)] # dp[x][y] is the length of the maximal square at (x, y)
for i in range(m):
for j in range(n):
if matrix[... | maximal-square | [Python] 1D-Array DP - Optimisation Process Explained | zayne-siew | 9 | 610 | maximal square | 221 | 0.446 | Medium | 3,933 |
https://leetcode.com/problems/maximal-square/discuss/1632285/Python-1D-Array-DP-Optimisation-Process-Explained | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
m, n = len(matrix), len(matrix[0])
result = 0
prev, curr = [0]*n, [0]*n
for i in range(m):
for j in range(n):
if matrix[i][j] == '1':
curr[j] = min(curr[j-1] if j ... | maximal-square | [Python] 1D-Array DP - Optimisation Process Explained | zayne-siew | 9 | 610 | maximal square | 221 | 0.446 | Medium | 3,934 |
https://leetcode.com/problems/maximal-square/discuss/1632285/Python-1D-Array-DP-Optimisation-Process-Explained | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
m, n = len(matrix), len(matrix[0])
result = 0
prev, curr = [0]*n, [0]*n
for i in range(m):
for j in range(n):
if matrix[i][j] == '1':
curr[j] = min(curr[j-1] if j ... | maximal-square | [Python] 1D-Array DP - Optimisation Process Explained | zayne-siew | 9 | 610 | maximal square | 221 | 0.446 | Medium | 3,935 |
https://leetcode.com/problems/maximal-square/discuss/1632285/Python-1D-Array-DP-Optimisation-Process-Explained | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
m, n = len(matrix), len(matrix[0])
result = 0
dp = [[0]*n for _ in range(2)] # 2-rowed dp array
for i in range(m):
for j in range(n):
# i%2 (or i&1) alternates between dp[0] and ... | maximal-square | [Python] 1D-Array DP - Optimisation Process Explained | zayne-siew | 9 | 610 | maximal square | 221 | 0.446 | Medium | 3,936 |
https://leetcode.com/problems/maximal-square/discuss/1632285/Python-1D-Array-DP-Optimisation-Process-Explained | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
m, n, result = len(matrix), len(matrix[0]), 0
dp = [0]*n # 1D array
for i in range(m):
prev = 0 # stores dp[i-1][j-1]
for j in range(n):
dp[j], prev = 0 if matrix[i][j] == '0' e... | maximal-square | [Python] 1D-Array DP - Optimisation Process Explained | zayne-siew | 9 | 610 | maximal square | 221 | 0.446 | Medium | 3,937 |
https://leetcode.com/problems/maximal-square/discuss/518951/Python-O(m*n)-sol.-by-DP.-with-Demo | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
if not matrix:
return 0
dp_table = [ [ int(x) for x in row] for row in matrix]
h, w = len(matrix), len(matrix[0])
max_edge_of_square = 0
for y in ... | maximal-square | Python O(m*n) sol. by DP. [ with Demo ] | brianchiang_tw | 7 | 753 | maximal square | 221 | 0.446 | Medium | 3,938 |
https://leetcode.com/problems/maximal-square/discuss/518951/Python-O(m*n)-sol.-by-DP.-with-Demo | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
if not matrix:
return 0
h, w = len(matrix), len(matrix[0])
# in-place update
dp_table = matrix
max_edge_length = 0
for x in range(w):
... | maximal-square | Python O(m*n) sol. by DP. [ with Demo ] | brianchiang_tw | 7 | 753 | maximal square | 221 | 0.446 | Medium | 3,939 |
https://leetcode.com/problems/maximal-square/discuss/2826866/in-M*N-time | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
mx=0
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if(matrix[i][j]=="0"):
matrix[i][j]=0
else:
if(i==0 or j==0):
... | maximal-square | in M*N time | droj | 5 | 51 | maximal square | 221 | 0.446 | Medium | 3,940 |
https://leetcode.com/problems/maximal-square/discuss/944753/python-DP-probably-without-using-extra-space | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
if len(matrix) < 1:
return 0
rows,cols,max_size = len(matrix),len(matrix[0]),0
for row in range(rows):
for col in range(cols):
matrix[row][col] = int(matrix[row][col])
... | maximal-square | [python] DP probably without using extra space | Rakesh301 | 3 | 436 | maximal square | 221 | 0.446 | Medium | 3,941 |
https://leetcode.com/problems/maximal-square/discuss/1632249/2D-Range-sum-and-binary-search-time%3A-O(R*C*log(min(R-C))) | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
def exist(k):
for i in range(R):
if i + k - 1 >= R:
break
for j in range(C):
if j + k - 1 >= C:
break
a = s... | maximal-square | 2D-Range sum & binary search, time: O(R*C*log(min(R, C))) | kryuki | 2 | 66 | maximal square | 221 | 0.446 | Medium | 3,942 |
https://leetcode.com/problems/maximal-square/discuss/600192/Python3-dp | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
@lru_cache(None)
def fn(i, j):
"""Return length of max square ending at (i, j)"""
if i < 0 or j < 0 or matrix[i][j] == "0": return 0
return 1 + min(fn(i-1, j), fn(i-1, j-1), fn(i, j... | maximal-square | [Python3] dp | ye15 | 2 | 130 | maximal square | 221 | 0.446 | Medium | 3,943 |
https://leetcode.com/problems/maximal-square/discuss/600192/Python3-dp | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
ans = 0
for i in range(len(matrix)):
for j in range(len(matrix[0])):
matrix[i][j] = int(matrix[i][j])
if i > 0 and j > 0 and matrix[i][j]:
matrix[i][j] = 1 + min(... | maximal-square | [Python3] dp | ye15 | 2 | 130 | maximal square | 221 | 0.446 | Medium | 3,944 |
https://leetcode.com/problems/maximal-square/discuss/600192/Python3-dp | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
m, n = len(matrix), len(matrix[0])
dp = [[0]*n for _ in range(m)]
for i in range(m):
for j in range(n):
if matrix[i][j] == "1":
if i == 0 or j == 0: dp[i][j] = 1
... | maximal-square | [Python3] dp | ye15 | 2 | 130 | maximal square | 221 | 0.446 | Medium | 3,945 |
https://leetcode.com/problems/maximal-square/discuss/600192/Python3-dp | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
m, n = len(matrix), len(matrix[0])
ans = 0
dp = [0]*n
tmp = [0]*n
for i in range(m):
tmp, dp = dp, tmp
for j in range(n):
if matrix[i][j] == "1":
... | maximal-square | [Python3] dp | ye15 | 2 | 130 | maximal square | 221 | 0.446 | Medium | 3,946 |
https://leetcode.com/problems/maximal-square/discuss/465001/Python3-simple-solution-using-dynamic-programming | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
max_val = 0
for i in range(len(matrix)):
for j in range(len(matrix[i])):
matrix[i][j]=int(matrix[i][j])
if matrix[i][j] and i and j:
matrix[i][j] = min(matrix[i-1][j],matrix[i][j-1],matrix[i-1][j-1])+1
max_val = max(max_va... | maximal-square | Python3 simple solution using dynamic programming | jb07 | 2 | 191 | maximal square | 221 | 0.446 | Medium | 3,947 |
https://leetcode.com/problems/maximal-square/discuss/1538349/Python-oror-Easy-Solution | class Solution:
def maximalSquare(self, lst: List[List[str]]) -> int:
n, m = len(lst), len(lst[0])
dp = [[0 for j in range(m)] for i in range(n)]
maxi = 0
for i in range(n - 1, -1, -1):
for j in range(m - 1, -1, -1):
if lst[i][j] == "0":
dp[i][j] = 0
elif i == n - 1:
dp[i][j] = int(ls... | maximal-square | Python || Easy Solution | naveenrathore | 1 | 301 | maximal square | 221 | 0.446 | Medium | 3,948 |
https://leetcode.com/problems/maximal-square/discuss/633057/Idiomatic-Python-Solution-with-DP-O(mn)-Time-O(mn)-Space | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
if not matrix:
return 0
dp = [[0] * len(matrix[0]) for _ in range(len(matrix))]
max_side = 0
for i, row in enumerate(matrix):
for j, val in enumerate(row):
if val == '0':
... | maximal-square | Idiomatic Python Solution with DP - O(mn) Time, O(mn) Space | schedutron | 1 | 160 | maximal square | 221 | 0.446 | Medium | 3,949 |
https://leetcode.com/problems/maximal-square/discuss/442360/Python-3-(DP)-(five-lines)-(beats-98) | class Solution:
def maximalSquare(self, G: List[List[str]]) -> int:
if not G: return 0
M, N = len(G) + 1, len(G[0]) + 1
G = [[0]*N] + [[0,*G[i]] for i in range(M-1)]
for i,j in itertools.product(range(1,M),range(1,N)): G[i][j] = int(G[i][j]) and 1 + min(G[i-1][j],G[i][j-1],G[i-1][j-1... | maximal-square | Python 3 (DP) (five lines) (beats 98%) | junaidmansuri | 1 | 419 | maximal square | 221 | 0.446 | Medium | 3,950 |
https://leetcode.com/problems/maximal-square/discuss/439087/One-line-change-from-the-85.-Maximal-Rectangle-solution | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
if not matrix:
return 0
n = len(matrix[0])
prev = [0] * n
ans = 0
for row in matrix:
curr = [e + 1 if cell == '1' else 0 for e, cell in zip(prev, row)]
curr.append(0)
... | maximal-square | One line change from the 85. Maximal Rectangle solution | chuan-chih | 1 | 172 | maximal square | 221 | 0.446 | Medium | 3,951 |
https://leetcode.com/problems/maximal-square/discuss/2841585/MaximalSquare-of-1 | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
row_len = len(matrix)
col_len = len(matrix[0])
matrix2 = [[0]*(col_len) for _ in matrix]
max_one = 0
for i in range(row_len):
for j in range(col_len):
if matrix[i][j] == "1"... | maximal-square | MaximalSquare of 1 | dexck7770 | 0 | 3 | maximal square | 221 | 0.446 | Medium | 3,952 |
https://leetcode.com/problems/maximal-square/discuss/2840578/**NO-NEED-FOR-DP**-O(MN)-solution-or-sliding-window.-Easiest-solution-yet. | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
heights = {len(matrix[0]): float('-inf')}
max_square = float('-inf')
for row in matrix:
for i, el in enumerate(row):
heights[i] = heights.setdefault(i, 0) + 1 if el == "1" else 0
... | maximal-square | **NO NEED FOR DP** O(MN) solution | sliding window. Easiest solution yet. | DijkstrianProtoge | 0 | 8 | maximal square | 221 | 0.446 | Medium | 3,953 |
https://leetcode.com/problems/maximal-square/discuss/2819395/python3-soln | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
dp = [[0]*len(matrix[0]) for _ in range(len(matrix))]
maxl = -1
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if matrix[i][j] == '1':
dp[i][j] = 1
... | maximal-square | python3 soln | misterman1234 | 0 | 7 | maximal square | 221 | 0.446 | Medium | 3,954 |
https://leetcode.com/problems/maximal-square/discuss/2814174/O(n)-space-detailed-explanation-with-intuition. | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
'''
we pad the matrix so that we will always have an empty
monotonic stack.
'''
for rw in range(len(matrix)):
matrix[rw].append("0")
height = [0] * len(matrix[0])
max_area = ... | maximal-square | O(n) space detailed explanation with intuition. | Henok2011 | 0 | 7 | maximal square | 221 | 0.446 | Medium | 3,955 |
https://leetcode.com/problems/maximal-square/discuss/2726461/Python3-Simple-2D-DP-(Bottom-Up) | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
ROWS, COLS = len(matrix), len(matrix[0])
dp = [[0] * (COLS+1) for _ in range(ROWS+1)]
res = 0
for r in range(ROWS-1, -1, -1):
for c in range(COLS-1, -1, -1):
if int(matrix[r][c]) != ... | maximal-square | Python3 Simple 2D DP (Bottom-Up) | jonathanbrophy47 | 0 | 12 | maximal square | 221 | 0.446 | Medium | 3,956 |
https://leetcode.com/problems/maximal-square/discuss/2718229/Python-or-2-d-dynamic-programming-solution | class Solution:
def maximalSquare(self, mat: List[List[str]]) -> int:
m, n = len(mat[0]), len(mat)
dp = [[0 for _ in range(m)] for _ in range(n)]
ans = 0
# Initialize dp matrix with values from mat
for i in range(m):
dp[0][i] = int(mat[0][i])
ans = max... | maximal-square | Python | 2-d dynamic programming solution | LordVader1 | 0 | 18 | maximal square | 221 | 0.446 | Medium | 3,957 |
https://leetcode.com/problems/maximal-square/discuss/2636784/Python-DP | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
r = len(matrix)
c = len(matrix[0])
dp = [[0]*(c+1) for _ in range(r+1)]
mx = 0
for i in range(r):
for j in range(c):
if matrix[i][j]!='0':
... | maximal-square | Python - DP | lokeshsenthilkumar | 0 | 20 | maximal square | 221 | 0.446 | Medium | 3,958 |
https://leetcode.com/problems/maximal-square/discuss/2069194/Python-or-DP | class Solution:
def maximalSquare(self, ma: List[List[str]]) -> int:
n = len(ma)
m = len(ma[0])
t = [[0 for i in range(m)]for j in range(n)]
ans = 0
for i in range(n):
for j in range(m):
if i==0 or j==0:
if ma[i][j] == "1":
... | maximal-square | Python | DP | Shivamk09 | 0 | 61 | maximal square | 221 | 0.446 | Medium | 3,959 |
https://leetcode.com/problems/maximal-square/discuss/2027833/python3-or-DP-or-Faster-than-95.62... | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
m, n = len(matrix), len(matrix[0])
dp = [int(i) for i in matrix[0]]
res = max(dp)
for i in range(1, m):
curr = dp[:]
dp[0] = int(matrix[i][0])
for j in range(1, n):
... | maximal-square | python3 | DP | Faster than 95.62%... | anels | 0 | 72 | maximal square | 221 | 0.446 | Medium | 3,960 |
https://leetcode.com/problems/maximal-square/discuss/1836298/70-faster-and-memory-efficient-or-DP-or-O(m*n)-or-easy-solution | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
rows, cols = len(matrix), len(matrix[0])
# dp: look up table
dp = [0] * (rows+1)
for i in range(len(dp)):
dp[i] = [0] * (cols+1)
maxx = 0 # max length
for i in... | maximal-square | 70% faster and memory efficient | DP | O(m*n) | easy solution | aamir1412 | 0 | 56 | maximal square | 221 | 0.446 | Medium | 3,961 |
https://leetcode.com/problems/maximal-square/discuss/1804796/Python-easy-to-read-and-understand-or-DP | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
m, n = len(matrix), len(matrix[0])
matrix = [[int(matrix[i][j]) for j in range(n)] for i in range(m)]
t = []
for val in matrix:
t.append(val)
for i in range(m-2, -1, -1):
... | maximal-square | Python easy to read and understand | DP | sanial2001 | 0 | 135 | maximal square | 221 | 0.446 | Medium | 3,962 |
https://leetcode.com/problems/maximal-square/discuss/1755384/Java-Python3-Simple-DP-Solution-(Bottom-Up) | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
m, n = len(matrix), len(matrix[0])
dp = [[0]*(n+1) for _ in range(m+1)]
maxlen = 0
for i in range(1, m+1):
for j in range(1, n+1):
if matrix[i-1][j-1] == '1':
dp[... | maximal-square | β
[Java / Python3] Simple DP Solution (Bottom-Up) | JawadNoor | 0 | 117 | maximal square | 221 | 0.446 | Medium | 3,963 |
https://leetcode.com/problems/maximal-square/discuss/1632597/Python3-DP-solution | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
dp = [[0 for _ in range(len(matrix[0]) + 1)] for _ in range(len(matrix) + 1)]
max_sq_len = 0
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if matrix[i][j] == ... | maximal-square | [Python3] DP solution | maosipov11 | 0 | 37 | maximal square | 221 | 0.446 | Medium | 3,964 |
https://leetcode.com/problems/maximal-square/discuss/1632106/Python3-DP-Explained | class Solution:
def maximalSquare(self, m: List[List[str]]) -> int:
rows, cols = len(m), len(m[0])
dp = [[0] * cols for i in range(rows)]
for row in range(rows):
combo = 0
for col in range(cols):
if m[row][col] == "1":
... | maximal-square | βοΈ[Python3] DP, Explained | artod | 0 | 49 | maximal square | 221 | 0.446 | Medium | 3,965 |
https://leetcode.com/problems/maximal-square/discuss/1458252/PyPy3-Solution-using-only-for-loops-w-comments | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
# Init
m = len(matrix)
n = len(matrix[0])
max_len = 0
# Convert matrix value of string to int
for row in range(m):
for col in range(n):
matrix[row][c... | maximal-square | [Py/Py3] Solution using only for loops w/ comments | ssshukla26 | 0 | 242 | maximal square | 221 | 0.446 | Medium | 3,966 |
https://leetcode.com/problems/maximal-square/discuss/1332034/Python-DP-solution-using-tabulation-beats-70 | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
#initiate a DP matrix
DP = []
N = len(matrix)
for row in range(N+1):
DP.append([0]*(len(matrix[0])+1))
currentMax = 0
#meat
for row in range(N):
... | maximal-square | Python DP solution using tabulation beats 70% | dee7 | 0 | 232 | maximal square | 221 | 0.446 | Medium | 3,967 |
https://leetcode.com/problems/maximal-square/discuss/1082581/AC-Python | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
def square_sum(i, j, size):
result = 0
for row in range(i, i+size):
try:
result += sum([1 if x == "1" else 0 for x in matrix[row][j:j+size]])
... | maximal-square | AC Python | dev-josh | 0 | 138 | maximal square | 221 | 0.446 | Medium | 3,968 |
https://leetcode.com/problems/maximal-square/discuss/1082581/AC-Python | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
R, C = len(matrix), len(matrix[0])
dp = [[0]*(C+1) for _ in range(R+1)]
for r in range(R):
for c in range(C):
if matrix[r][c] == '1':
dp... | maximal-square | AC Python | dev-josh | 0 | 138 | maximal square | 221 | 0.446 | Medium | 3,969 |
https://leetcode.com/problems/maximal-square/discuss/1077012/classic-DP-python-and-brute-force | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
row_len, col_len = len(matrix), len(matrix[0])
max_sum = 0
dp = [[0]* (col_len+1) for _ in range(row_len + 1)]
for row in range(row_len):
for col in range(col_len):
if matrix[row][col... | maximal-square | classic DP python and brute force | xavloc | 0 | 104 | maximal square | 221 | 0.446 | Medium | 3,970 |
https://leetcode.com/problems/maximal-square/discuss/739039/Python-DFS%2Bmemoization | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
if not matrix:return 0
if len(matrix)==1:return int("1" in matrix[0])
row,col=len(matrix),len(matrix[0])
viewed=[[False]*col for _ in range(row)]
ans=0
def dfs(matrix:List[List[str]],row:int... | maximal-square | Python DFS+memoization | 752937603 | 0 | 276 | maximal square | 221 | 0.446 | Medium | 3,971 |
https://leetcode.com/problems/maximal-square/discuss/503087/Easy-understand-dp-Python-3 | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
#dp[i, j] is the max edge length of the square with i, j as right-bottom point
if not matrix:
return 0
num_rows = len(matrix)
num_cols = len(matrix[0])
dp = [[0 for i in range(num_cols+1)] ... | maximal-square | Easy understand dp Python 3 | Liowen | 0 | 218 | maximal square | 221 | 0.446 | Medium | 3,972 |
https://leetcode.com/problems/maximal-square/discuss/387196/Python-Solution-(DP) | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
if not matrix:
return 0
m, n = len(matrix), len(matrix[0])
dp = [[0 for _ in range(n)] for _ in range(m)]
dp[0][0] = int(matrix[0][0])
max_log = dp[0][0]
for i in ra... | maximal-square | Python Solution (DP) | FFurus | 0 | 345 | maximal square | 221 | 0.446 | Medium | 3,973 |
https://leetcode.com/problems/maximal-square/discuss/359781/My-easy-to-understand-Python-Solution | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
if len(matrix) == 0: return 0
n = len(matrix[0])
up = [0]*n
left = [0]*n
dia = [0]*n
square = [0]*n
maxl = -1
for i in range(len(matrix)):
presquare = [x fo... | maximal-square | My easy to understand Python Solution | des_jasmine | 0 | 342 | maximal square | 221 | 0.446 | Medium | 3,974 |
https://leetcode.com/problems/maximal-square/discuss/432601/Not-sure-if-anyone-else-has-come-up-with-something-like-this | class Solution:
def maximalSquare(self, matrix: List[List[str]]) -> int:
for y, row in enumerate(matrix):
count_x = 1
for x, val in enumerate(row):
if val is '1':
matrix[y][x] = count_x
count_x += 1
else... | maximal-square | Not sure if anyone else has come up with something like this | SkookumChoocher | -1 | 128 | maximal square | 221 | 0.446 | Medium | 3,975 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/701577/PythonPython3-Count-Complete-Tree-Nodes | class Solution:
def countNodes(self, root: TreeNode) -> int:
if not root: return 0
return 1 + self.countNodes(root.left) + self.countNodes(root.right) | count-complete-tree-nodes | [Python/Python3] Count Complete Tree Nodes | newborncoder | 4 | 336 | count complete tree nodes | 222 | 0.598 | Medium | 3,976 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2816979/Python-Simple-Python-Solution-Using-Two-Approach-BFS-or-DFS | class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
self.result = 0
def DFS(node):
if node == None:
return None
self.result = self.result + 1
DFS(node.left)
DFS(node.right)
DFS(root)
return self.result | count-complete-tree-nodes | [ Python ] β
β
Simple Python Solution Using Two Approach BFS | DFSπ₯³βπ | ASHOK_KUMAR_MEGHVANSHI | 3 | 55 | count complete tree nodes | 222 | 0.598 | Medium | 3,977 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2816979/Python-Simple-Python-Solution-Using-Two-Approach-BFS-or-DFS | class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
def BFS(node):
if node == None:
return 0
queue = [node]
self.result = 0
while queue:
current_node = queue.pop()
self.result = self.result + 1
if current_node.left != None:
queue.append(current_node.left)
... | count-complete-tree-nodes | [ Python ] β
β
Simple Python Solution Using Two Approach BFS | DFSπ₯³βπ | ASHOK_KUMAR_MEGHVANSHI | 3 | 55 | count complete tree nodes | 222 | 0.598 | Medium | 3,978 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2813529/Two-Methods-or-Easy-to-Understand-or-Beginners-Friendly-Code-Python | class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
queue = []
if not root:
return 0
queue.append(root)
count = 0
while queue:
node = queue.pop(0)
count += 1
if node.left:
queue.append(node.lef... | count-complete-tree-nodes | Two Methods | Easy to Understand | Beginners Friendly Code [Python] | tushgaurav | 1 | 122 | count complete tree nodes | 222 | 0.598 | Medium | 3,979 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2813529/Two-Methods-or-Easy-to-Understand-or-Beginners-Friendly-Code-Python | class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
leftNodes = self.countNodes(root.left)
rightNodes = self.countNodes(root.right)
return leftNodes + rightNodes + 1 | count-complete-tree-nodes | Two Methods | Easy to Understand | Beginners Friendly Code [Python] | tushgaurav | 1 | 122 | count complete tree nodes | 222 | 0.598 | Medium | 3,980 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/702258/Python3-two-O(logN-*-logN)-approaches | class Solution:
def countNodes(self, root: TreeNode) -> int:
if not root: return 0
h = self.height(root)
if self.height(root.right) == h-1:
return 2**(h-1) + self.countNodes(root.right)
else:
return 2**(h-2) + self.countNodes(root.left)
def height... | count-complete-tree-nodes | [Python3] two O(logN * logN) approaches | ye15 | 1 | 42 | count complete tree nodes | 222 | 0.598 | Medium | 3,981 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/702258/Python3-two-O(logN-*-logN)-approaches | class Solution:
def countNodes(self, root: TreeNode) -> int:
n, node = 0, root
while node: n, node = n+1, node.left
def fn(i):
"""Return ith node on level n"""
node = root
for k in reversed(range(n-1)):
tf, i = divmod(i, 2**k)
... | count-complete-tree-nodes | [Python3] two O(logN * logN) approaches | ye15 | 1 | 42 | count complete tree nodes | 222 | 0.598 | Medium | 3,982 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/701579/summary-2-methods%3A-recursion-and-binary-search-both-O(logN*logN) | class Solution:
def get_height(self, root): # O(logN)
height = -1
node = root
while node:
height += 1
node = node.left
return height
def countNodes_binarySearch(self, root: TreeNode):
height = self.get_height(root) # O(logN)
... | count-complete-tree-nodes | [summary] 2 methods: recursion & binary-search, both O(logN*logN) | newRuanXY | 1 | 68 | count complete tree nodes | 222 | 0.598 | Medium | 3,983 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/701579/summary-2-methods%3A-recursion-and-binary-search-both-O(logN*logN) | class Solution:
def get_height(self, root): # O(logN)
height = -1
node = root
while node:
height += 1
node = node.left
return height
def countNodes_recursion(self, root: TreeNode) -> int: # O(logN * logN)
height = self.get_hei... | count-complete-tree-nodes | [summary] 2 methods: recursion & binary-search, both O(logN*logN) | newRuanXY | 1 | 68 | count complete tree nodes | 222 | 0.598 | Medium | 3,984 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/252426/Easy-to-follow-Python3%3A-find-of-missing-nodes-first | class Solution:
def countNodes(self, root: TreeNode) -> int:
max_level = 0
temp = root
while temp:
max_level += 1
temp = temp.left
num_missing = 0
q = []
cur_level = 1
while q or root:
while root:
q.append([r... | count-complete-tree-nodes | Easy to follow Python3: find # of missing nodes first | qqzzqq | 1 | 144 | count complete tree nodes | 222 | 0.598 | Medium | 3,985 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2833775/Python-Recursion-by-complete-and-full-binary-tree-or-binary-search | class Solution:
def countNodes(self, root):
if root is None:
return 0
# compute the depth of the tree.
cur = root
depth = 0
while cur.left is not None:
cur = cur.left
depth += 1
if depth == 0:
return 1
def cont... | count-complete-tree-nodes | [Python] Recursion by complete and full binary tree | binary search | i-hate-covid | 0 | 5 | count complete tree nodes | 222 | 0.598 | Medium | 3,986 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2833775/Python-Recursion-by-complete-and-full-binary-tree-or-binary-search | class Solution:
def countNodes(self, root):
def get_full_height(root):
# check if the tree is a full binary tree, return None if it's not, else return the height
left_cur, right_cur = root, root
height = 0
while right_cur is not None:
left_cur ... | count-complete-tree-nodes | [Python] Recursion by complete and full binary tree | binary search | i-hate-covid | 0 | 5 | count complete tree nodes | 222 | 0.598 | Medium | 3,987 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2821674/80-ms-and-21.4-MB-Python-German | class Solution:
#Takes 2*ceil(ld(n))
#Returns MaxHeight and MinHeight from a Node
#Works because a complete tree is garanteed
def heights(self, root: TreeNode):
max, min = -1, -1
root2 = root #save it for the second treeclimb
while root: #Get maxHeight
max += 1 #start... | count-complete-tree-nodes | 80 ms & 21.4 MB Python German | lucasscodes | 0 | 4 | count complete tree nodes | 222 | 0.598 | Medium | 3,988 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2821361/Optimal-Iterative-Solution-in-O(1)-Space-The-Best | class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
depth = self.depth(root)
bit = 1 << depth
sum = bit
while root.left:
depth -= 1
bit >>= 1
if depth != self.depth(root.right)... | count-complete-tree-nodes | Optimal Iterative Solution in O(1) Space, The Best | Triquetra | 0 | 6 | count complete tree nodes | 222 | 0.598 | Medium | 3,989 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2818854/Python3-oror-One-line-oror-Faster-than-89.9 | class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
return self.countNodes(root.left) + self.countNodes(root.right) + 1 if root else 0 | count-complete-tree-nodes | β
Python3 || One line || Faster than 89.9% | PabloVE2001 | 0 | 7 | count complete tree nodes | 222 | 0.598 | Medium | 3,990 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2818560/Python-3-Line-Solution | class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
if root==None:
return 0
return 1+self.countNodes(root.left)+ self.countNodes(root.right) | count-complete-tree-nodes | Python-3 Line Solution | smritiaspires | 0 | 5 | count complete tree nodes | 222 | 0.598 | Medium | 3,991 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2818412/Python-1-line-solution | class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
return self.countNodes(root.left) + self.countNodes(root.right) + 1 if root else 0 | count-complete-tree-nodes | Python 1 line solution | padamenko | 0 | 6 | count complete tree nodes | 222 | 0.598 | Medium | 3,992 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2818258/Python-Solution-in-O(logn2) | class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
left = root.left
right = root.right
left_level = 1
right_level = 1
while left:
left_level += 1
left = left.left
while right:
... | count-complete-tree-nodes | Python Solution in O(logn^2) | sameer96 | 0 | 6 | count complete tree nodes | 222 | 0.598 | Medium | 3,993 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2818103/Python3-Binary-Search-Recursion-Bitmask | class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
@lru_cache(None)
def seek(k):
nonlocal root
if k == 1:
return root
else:
if (k & 1) == 1:
... | count-complete-tree-nodes | Python3 - Binary Search, Recursion, Bitmask | godshiva | 0 | 3 | count complete tree nodes | 222 | 0.598 | Medium | 3,994 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2817412/Python-or-Cheating-O(n)-%2B-Accepted-Sub-O(n) | class Solution:
def countNodes(self, root: Optional[TreeNode], total = 0) -> int:
# Brute Force: Visit all Nodes
if not root: return total
if root: total += 1
left = self.countNodes(root.left) if root.left else 0
right = self.countNodes(root.right) if root.right else 0
... | count-complete-tree-nodes | Python | Cheating O(n) + Accepted Sub-O(n) | jessewalker2010 | 0 | 3 | count complete tree nodes | 222 | 0.598 | Medium | 3,995 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2817412/Python-or-Cheating-O(n)-%2B-Accepted-Sub-O(n) | class Solution:
def countNodes(self, root: Optional[TreeNode], total = 0) -> int:
def penult(node):
if not node.left and not node.right: return 1
if node.left:
if node.left.left or node.left.right: return 1
if node.right:
if node.right.left... | count-complete-tree-nodes | Python | Cheating O(n) + Accepted Sub-O(n) | jessewalker2010 | 0 | 3 | count complete tree nodes | 222 | 0.598 | Medium | 3,996 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2817374/Simple-solution-dfs-Time-O(n)-Space-O(1) | class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
if(root is None):
return 0
return self.countNodes(root.left) + self.countNodes(root.right) + 1 | count-complete-tree-nodes | Simple solution dfs Time O(n), Space O(1) | danhtran-dev | 0 | 5 | count complete tree nodes | 222 | 0.598 | Medium | 3,997 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2817090/O(log(n)2)-solution-with-explanation | class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
# Lets try in O(log(n)) (or something like that)
# Go down left to get the depth.
depth_left = 0
node = root
while node:
node = node.left
depth_left += 1
# Ditto for right... | count-complete-tree-nodes | O(log(n)^2) solution with explanation | demindiro | 0 | 5 | count complete tree nodes | 222 | 0.598 | Medium | 3,998 |
https://leetcode.com/problems/count-complete-tree-nodes/discuss/2817031/O(binary-search)-*-O(depth)-~-logN*logN-(%2B-bit-manipulation) | class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
minDepth = 1
current = root
while current.left:
current = current.left
minDepth += 1
r = 2**(minDepth)
l = 2**(minDepth-1)
while... | count-complete-tree-nodes | O(binary search) * O(depth) ~ logN*logN (+ bit manipulation) | Posni_Sir_60g_Proteina | 0 | 6 | count complete tree nodes | 222 | 0.598 | Medium | 3,999 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.