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/top-k-frequent-words/discuss/2723437/Python-Counter-or-two-lines-or-99-Time-99-Memory
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: c = sorted(Counter(words).items(), key=lambda x: (-x[1],x[0])) return [w for w, n in c[:k]]
top-k-frequent-words
Python Counter | two lines | 99% Time 99% Memory
AgentIvan
1
31
top k frequent words
692
0.569
Medium
11,400
https://leetcode.com/problems/top-k-frequent-words/discuss/2723437/Python-Counter-or-two-lines-or-99-Time-99-Memory
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: c = Counter(words) return sorted(Counter(words), key=lambda x: (-c[x],x))[:k]
top-k-frequent-words
Python Counter | two lines | 99% Time 99% Memory
AgentIvan
1
31
top k frequent words
692
0.569
Medium
11,401
https://leetcode.com/problems/top-k-frequent-words/discuss/2722312/Simple-Python-One-Liner-Solution
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: return list(dict(sorted(Counter(words).items(), key = lambda x : (-x[1], x[0]))).keys())[:k]
top-k-frequent-words
Simple Python One-Liner Solution
g_aswin
1
17
top k frequent words
692
0.569
Medium
11,402
https://leetcode.com/problems/top-k-frequent-words/discuss/2722295/Python-3-with-no-imported-libs
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: wordsdict = {} for word in words: if word not in wordsdict.keys(): wordsdict[word] = 1 else: wordsdict[word] += 1 wordsdictsort = dict(sorted(wordsd...
top-k-frequent-words
Python 3 with no imported libs
vegancyberpunk
1
20
top k frequent words
692
0.569
Medium
11,403
https://leetcode.com/problems/top-k-frequent-words/discuss/2720543/Python's-simplest-and-fastest-solution-or-96-Faster
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: # Sorting the List words = sorted(words) # Count the number of words using Counter c = Counter(words) # Return the k most common words return [word for word, _ in c.most_c...
top-k-frequent-words
✔️ Python's simplest and fastest solution | 96% Faster 🔥
pniraj657
1
89
top k frequent words
692
0.569
Medium
11,404
https://leetcode.com/problems/top-k-frequent-words/discuss/2720294/Python-3-Lines-Solution-oror-Counter-or-Runtime-88
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: words = sorted(words) counts = Counter(words) return [word for word, _ in counts.most_common(k)]
top-k-frequent-words
✅ 🔥 Python 3 Lines Solution || Counter | Runtime 88%
jimunna150
1
69
top k frequent words
692
0.569
Medium
11,405
https://leetcode.com/problems/top-k-frequent-words/discuss/2041668/6-line-python-solution-use-Heap-queue
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: dictionary = collections.Counter(words) heap = [] for key, value in dictionary.items(): heap.append((-value,key)) heapq.heapify(heap) return [heapq.heappop(heap)[1] for i in range(k)]
top-k-frequent-words
6-line python solution, use Heap queue
prejudice23
1
203
top k frequent words
692
0.569
Medium
11,406
https://leetcode.com/problems/top-k-frequent-words/discuss/1951113/Python3-oror-Beats-96-oror-Dict-and-Lambda-Sorting
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: freq = defaultdict(int) for s in words: freq[s] -= 1 res = [] for s, f in sorted(freq.items(), key=lambda x: (x[1], x[0])): k -= 1 res.append(s) ...
top-k-frequent-words
Python3 || Beats 96% || Dict and Lambda Sorting
Dewang_Patil
1
144
top k frequent words
692
0.569
Medium
11,407
https://leetcode.com/problems/top-k-frequent-words/discuss/1928621/Python3-Two-line-solution-with-comments-Heap-O(nlogk)
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: # Hash Table O(N) time Freqs = Counter(words) # Heap O(NlogK) # create lambda function that can sort by -freq and word order return heapq.nsmallest(k, Freqs, key=lambda word:(-Freqs[word], word))
top-k-frequent-words
Python3 Two-line solution with comments Heap O(nlogk)
jlu56
1
199
top k frequent words
692
0.569
Medium
11,408
https://leetcode.com/problems/top-k-frequent-words/discuss/1479308/Python3-O(nlogn)-and-O(nlogk)-solutions
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: counts = collections.Counter(words) freq_arr = [[] for i in range(counts.most_common(1)[0][1] + 1)] for key in counts: freq_arr[counts[key]].append(key) res = [] idx ...
top-k-frequent-words
[Python3] O(nlogn) and O(nlogk) solutions
maosipov11
1
203
top k frequent words
692
0.569
Medium
11,409
https://leetcode.com/problems/top-k-frequent-words/discuss/1081288/5-Line-Python-Beating-99-Memory
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: seen = {} for i in words: if i in seen:seen[i] += 1 else:seen[i] = 0 return sorted(seen, key = lambda x: [-seen[x],x])[:k]
top-k-frequent-words
5-Line Python Beating 99% Memory
Venezsia1573
1
134
top k frequent words
692
0.569
Medium
11,410
https://leetcode.com/problems/top-k-frequent-words/discuss/2841213/Python-simple-heap-solution
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: counter = Counter(words) heap = [] for i in counter: heapq.heappush(heap, (-counter[i], i)) result = [] for i in range(k): freq, word = heapq.heappop(heap) resul...
top-k-frequent-words
Python simple heap solution
Bread0307
0
3
top k frequent words
692
0.569
Medium
11,411
https://leetcode.com/problems/top-k-frequent-words/discuss/2840054/Bucket-Sort-Python-Easy-To-Understand
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: freq = collections.Counter(words) max_fr = max(freq.values()) buckets = [[] for _ in range(max_fr)] ans = [] for word in freq: count = freq[word] buckets[count-1].append(w...
top-k-frequent-words
Bucket Sort Python Easy To Understand
taabish_khan22
0
2
top k frequent words
692
0.569
Medium
11,412
https://leetcode.com/problems/top-k-frequent-words/discuss/2840053/Bucket-Sort-Python-Easy-To-Understand
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: freq = collections.Counter(words) max_fr = max(freq.values()) buckets = [[] for _ in range(max_fr)] ans = [] for word in freq: count = freq[word] buckets[count-1].append(w...
top-k-frequent-words
Bucket Sort Python Easy To Understand
taabish_khan22
0
1
top k frequent words
692
0.569
Medium
11,413
https://leetcode.com/problems/top-k-frequent-words/discuss/2830622/Top-K-Frequent-Words-or-Python-Solution
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: words.sort() freq = Counter(words).most_common() result = [] counter = 1 for i,val in freq: if counter <= k: result.append(i) counter += 1 return res...
top-k-frequent-words
Top K Frequent Words | Python Solution
nishanrahman1994
0
4
top k frequent words
692
0.569
Medium
11,414
https://leetcode.com/problems/top-k-frequent-words/discuss/2734372/Python-Solution
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: result = [] heap = [] frequency = defaultdict(int) for word in words: frequency[word] += 1 for key, value in frequency.items(): heapq.heappush(heap, (-...
top-k-frequent-words
Python Solution
mansoorafzal
0
28
top k frequent words
692
0.569
Medium
11,415
https://leetcode.com/problems/top-k-frequent-words/discuss/2728922/One-liner-Python.-O(Nlog(K))
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: return [v[0] for v in nsmallest(k, Counter(words).items(), key = lambda x: (-x[1], x[0]))]
top-k-frequent-words
One-liner Python. O(Nlog(K))
nonchalant-enthusiast
0
8
top k frequent words
692
0.569
Medium
11,416
https://leetcode.com/problems/top-k-frequent-words/discuss/2728811/Naive-approach-for-Python
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: res = dict() for w in words: res[w] = res.setdefault(w, 0) + 1 res = [(v, k) for k, v in res.items()] res.sort(key = lambda x: (-x[0], x[1])) return [v[1] for v in res[:k]]
top-k-frequent-words
Naive approach for Python
nonchalant-enthusiast
0
4
top k frequent words
692
0.569
Medium
11,417
https://leetcode.com/problems/top-k-frequent-words/discuss/2728662/Python-priority-queue-O(n-%2B-klogn)
class Solution: from heapq import heapify, heappop def topKFrequent(self, words: List[str], k: int) -> List[str]: # O(n + klogn) dic = defaultdict(int) for word in words: dic[word]+=1 # build max heap pq = [] for s, c in dic.items(): pq.append((-c, s))...
top-k-frequent-words
Python priority queue O(n + klogn)
ESoapW
0
6
top k frequent words
692
0.569
Medium
11,418
https://leetcode.com/problems/top-k-frequent-words/discuss/2726271/Simple-python-solution-using-Counter-and-OrderedDict
class Solution: from collections import Counter, OrderedDict def topKFrequent(self, words: List[str], k: int) -> List[str]: words.sort() fmap = OrderedDict(Counter(words)) result = [] for word, freq in sorted(fmap.items(), key = lambda x : x[1], reverse = True): if k ...
top-k-frequent-words
Simple python solution using Counter and OrderedDict
johnny7p
0
3
top k frequent words
692
0.569
Medium
11,419
https://leetcode.com/problems/top-k-frequent-words/discuss/2723650/Simple-python3-one-liner-(105ms60)
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: return [e[1] for e in sorted(((-v,w) for w,v in Counter(words).items()))[:k]]
top-k-frequent-words
Simple python3 one-liner (105ms/60%)
leetavenger
0
8
top k frequent words
692
0.569
Medium
11,420
https://leetcode.com/problems/top-k-frequent-words/discuss/2723614/python-counter-faster-than-90-and-memory-less-than-94
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: counter = Counter(sorted(words)) return [word for word, _ in counter.most_common(k)]
top-k-frequent-words
python, counter, faster than 90% and memory less than 94%
m-s-dwh-bi
0
10
top k frequent words
692
0.569
Medium
11,421
https://leetcode.com/problems/top-k-frequent-words/discuss/2723552/Top-K-words
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: ##First use collections counts = collections.Counter(words) sorted_counts = sorted(counts.items(), key=lambda item: (-item[1], item[0])) return [word[0] for word in sorted_counts[0:k]]
top-k-frequent-words
Top K words
user4028c
0
7
top k frequent words
692
0.569
Medium
11,422
https://leetcode.com/problems/top-k-frequent-words/discuss/2723351/Python-(kinda-long)
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: wordDict = dict() for word in words: wordDict[word] = wordDict.get(word, 0) + 1 word_tuple_list = list(wordDict.items()) reverse_list = list() count_dict = dict() for word, coun...
top-k-frequent-words
Python (kinda long)
TommyV
0
4
top k frequent words
692
0.569
Medium
11,423
https://leetcode.com/problems/top-k-frequent-words/discuss/2723258/Python3-Solution
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: d = Counter(words) arr = [] for key, val in sorted(d.items(), key = lambda x:(-x[1], x[0])): arr.append(key) return arr[:k]
top-k-frequent-words
Python3 Solution
mediocre-coder
0
9
top k frequent words
692
0.569
Medium
11,424
https://leetcode.com/problems/top-k-frequent-words/discuss/2723227/Daily-LeetCoding-Challenge-October-Day-19-in-Python3
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: finalDict = {} for word in words: if word in finalDict.keys(): finalDict[word] +=1 else: finalDict[word] = 1 finalList = [] cnt = 0 ...
top-k-frequent-words
Daily LeetCoding Challenge October, Day 19 in Python3
exploringmlnai
0
4
top k frequent words
692
0.569
Medium
11,425
https://leetcode.com/problems/top-k-frequent-words/discuss/2723208/Sorted-Dictionary-Simple-or-Python
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: words.sort() word_freq = {} for word in words: if word in word_freq: word_freq[word] += 1 else: word_freq[word] = 1 word_freq={key:val for key, val i...
top-k-frequent-words
Sorted Dictionary Simple | Python
Abhi_-_-
0
5
top k frequent words
692
0.569
Medium
11,426
https://leetcode.com/problems/top-k-frequent-words/discuss/2723149/Simple-Python-3-O(nlogk)-Time
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: freq=collections.defaultdict(int) for word in words: freq[word]+=1 h=[] for word in freq: heapq.heappush(h,(-freq[word],word)) res=[] for _ in range(k): ...
top-k-frequent-words
✅ Simple Python 3 O(nlogk) Time
VIjayAnnepu
0
6
top k frequent words
692
0.569
Medium
11,427
https://leetcode.com/problems/top-k-frequent-words/discuss/2723110/Python3!-1-Line-solution.
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: return [a[1] for a in sorted([(-value, key) for key,value in collections.Counter(words).items()])[:k]]
top-k-frequent-words
😎Python3! 1 Line solution.
aminjun
0
8
top k frequent words
692
0.569
Medium
11,428
https://leetcode.com/problems/top-k-frequent-words/discuss/2723087/SImple-python-solution
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: dico = defaultdict(int) for word in words: dico[word]+=1 res = sorted(dico, key = lambda x: (-dico[x], x)) return res[:k]
top-k-frequent-words
SImple python solution
Jaykant
0
4
top k frequent words
692
0.569
Medium
11,429
https://leetcode.com/problems/top-k-frequent-words/discuss/2723059/using-python-dictionaries-basic
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: p=dict(Counter(words)) a={v:[] for v in set(p.values())} for i in p.keys(): a[p[i]].append(i) for i in a.keys(): a[i].sort() p=[] for i in sorted(a.keys(),reverse=Tr...
top-k-frequent-words
using python dictionaries basic
adithyalingisetty
0
2
top k frequent words
692
0.569
Medium
11,430
https://leetcode.com/problems/top-k-frequent-words/discuss/2723022/Python-Hash-Map-Solution-with-Documentation
class Solution: def topKFrequent(self, words: List[str], k: int) -> List[str]: """ Firstly, we use a Counter to count the frequency of each word within words, assigning the outcome as count. Then, for each key, value pair in count, a tuple of (value, key) is created, which is subsequ...
top-k-frequent-words
Python - Hash Map - Solution with Documentation
dworoniuk1
0
10
top k frequent words
692
0.569
Medium
11,431
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/1095502/Python3-simple-solution
class Solution: def hasAlternatingBits(self, n: int) -> bool: s = bin(n).replace('0b','') for i in range(len(s)-1): if s[i] == s[i+1]: return False return True
binary-number-with-alternating-bits
Python3 simple solution
EklavyaJoshi
2
54
binary number with alternating bits
693
0.613
Easy
11,432
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/1555338/Python-Efficient-Solution-or-Faster-than-90
class Solution: def hasAlternatingBits(self, n: int) -> bool: while n > 1: bit1 = n &amp; 1 n >>= 1 bit2 = n &amp; 1 if bit1 == bit2: return False return True
binary-number-with-alternating-bits
Python Efficient Solution | Faster than 90%
leet_satyam
1
92
binary number with alternating bits
693
0.613
Easy
11,433
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/1309579/Python-28ms-Bit-Manipulation
class Solution: def hasAlternatingBits(self, n: int) -> bool: while n > 0: if not 3 > (n &amp; 3) > 0: return False n >>= 1 return True
binary-number-with-alternating-bits
Python, 28ms, Bit-Manipulation
MihailP
1
78
binary number with alternating bits
693
0.613
Easy
11,434
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/1305279/Easy-Python-Solution(94.75)
class Solution: def hasAlternatingBits(self, n: int) -> bool: if(n<2): return True n=bin(n)[2:] for i in range(1,len(n)): if(n[i]==n[i-1]): return False return True
binary-number-with-alternating-bits
Easy Python Solution(94.75%)
Sneh17029
1
96
binary number with alternating bits
693
0.613
Easy
11,435
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/974151/Easiest-Solution
class Solution: def hasAlternatingBits(self, n: int) -> bool: # To check whether it has alternating bits i.e if alternating then all bits becomes 1. n = n ^ (n >> 1) # To check if all bits are 1 return True if (n+1) &amp; n == 0 else False
binary-number-with-alternating-bits
Easiest Solution
Aditya380
1
62
binary number with alternating bits
693
0.613
Easy
11,436
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/2685427/Simple-python-code-with-explanation
class Solution: def hasAlternatingBits(self, n): #convert the number to binary number using bin --> keyword n = bin(n) #if you use bin keyword first 2 letter will be 0b #and followed by the binary number #so to remove first to number using indexing #sting contains b...
binary-number-with-alternating-bits
Simple python code with explanation
thomanani
0
13
binary number with alternating bits
693
0.613
Easy
11,437
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/2053428/Python-simple-solution
class Solution: def hasAlternatingBits(self, n: int) -> bool: j = '$' for i in bin(n)[2:]: if i == j: return False j = i return True
binary-number-with-alternating-bits
Python simple solution
StikS32
0
43
binary number with alternating bits
693
0.613
Easy
11,438
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/2027182/Python-Simplest-Solution!-Right-Shift
class Solution: def hasAlternatingBits(self, n): lastBit = -1 while n: bit = n &amp; 1 if bit == lastBit: return False else: lastBit = bit n >>= 1 return True
binary-number-with-alternating-bits
Python - Simplest Solution! Right Shift
domthedeveloper
0
81
binary number with alternating bits
693
0.613
Easy
11,439
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/1998352/Beginner-friendly-solution-in-Python3
class Solution: def hasAlternatingBits(self, n: int) -> bool: nBinary = "" while (n > 0): temp = n % 2 nBinary += str(temp) n = n // 2 for i in range(1, len(nBinary)): if nBinary[i] == nBinary[i-1]: return False ...
binary-number-with-alternating-bits
Beginner-friendly solution in Python3
huyanguyen3695
0
23
binary number with alternating bits
693
0.613
Easy
11,440
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/1975386/4-Lines-Python-Solution-oror-50-Faster-oror-Memory-less-than-90
class Solution: def hasAlternatingBits(self, n: int) -> bool: n=bin(n)[2:] for i in range(len(n)-1): if n[i]==n[i+1]: return False return True
binary-number-with-alternating-bits
4-Lines Python Solution || 50% Faster || Memory less than 90%
Taha-C
0
36
binary number with alternating bits
693
0.613
Easy
11,441
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/1975386/4-Lines-Python-Solution-oror-50-Faster-oror-Memory-less-than-90
class Solution: def hasAlternatingBits(self, n: int) -> bool: n=bin(n)[2:] return all([n[i+1]!=n[i] for i in range(len(n)-1)])
binary-number-with-alternating-bits
4-Lines Python Solution || 50% Faster || Memory less than 90%
Taha-C
0
36
binary number with alternating bits
693
0.613
Easy
11,442
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/1975386/4-Lines-Python-Solution-oror-50-Faster-oror-Memory-less-than-90
class Solution: def hasAlternatingBits(self, n: int) -> bool: n=bin(n)[2:] return not('11' in n or '00' in n)
binary-number-with-alternating-bits
4-Lines Python Solution || 50% Faster || Memory less than 90%
Taha-C
0
36
binary number with alternating bits
693
0.613
Easy
11,443
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/1922935/Python-easy-solution-for-beginners-faster-than-85
class Solution: def hasAlternatingBits(self, n: int) -> bool: bin_n = bin(n)[2:] check = bin_n[0] for i in range(1, len(bin_n)): if check == '0' and bin_n[i] == '1': check = '1' continue elif check == '1' and bin_n[i] == '0': ...
binary-number-with-alternating-bits
Python easy solution for beginners, faster than 85%
alishak1999
0
29
binary number with alternating bits
693
0.613
Easy
11,444
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/1847767/PYTHON-BIT-MASKING-with-explanation-(28ms)
class Solution: def hasAlternatingBits(self, n: int) -> bool: #Use bit mask of 0b00000001 mask = 1; #Find the starting condition #I use == 1 throughout. == 0 would yield the same result start = ( ( n &amp; mask ) == 1 ); #bitshift n n = n >> 1; ...
binary-number-with-alternating-bits
PYTHON BIT MASKING with explanation (28ms)
greg_savage
0
29
binary number with alternating bits
693
0.613
Easy
11,445
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/1570636/Python-faster-than-87-O(log4(n))
class Solution: def hasAlternatingBits(self, n: int) -> bool: x = 2 ** math.floor(math.log2(n)) while x > 0: n -= x x //= 4 return n == 0
binary-number-with-alternating-bits
Python faster than 87% O(log4(n))
dereky4
0
85
binary number with alternating bits
693
0.613
Easy
11,446
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/1461053/Simple-Python-Solution
class Solution: def hasAlternatingBits(self, n: int) -> bool: b = bin(n) if ('00' in b) or ('11' in b): return False return True
binary-number-with-alternating-bits
Simple Python Solution
Annushams
0
57
binary number with alternating bits
693
0.613
Easy
11,447
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/1133484/simple-python-solution
class Solution: def hasAlternatingBits(self, n: int) -> bool: b = bin(n)[2:] if '00' in b or '11' in b: return False else: return True
binary-number-with-alternating-bits
simple python solution
pheobhe
0
22
binary number with alternating bits
693
0.613
Easy
11,448
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/718082/Python-Simple-Solution-using-Bit-Operators
class Solution: def hasAlternatingBits(self, n: int) -> bool: last = n &amp; 1 n = n>>1 while n>0: curr = n &amp; 1 if curr == last:return False last = curr n = n >> 1 return True
binary-number-with-alternating-bits
[Python] Simple Solution using Bit Operators
spongeb0b
0
31
binary number with alternating bits
693
0.613
Easy
11,449
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/407651/Python-Simple-Logic-Beats-100-solutions
class Solution: def hasAlternatingBits(self, n: int) -> bool: cnt = 0 n = bin(n)[2:] for i in range(1, len(n)): if n[i-1]==n[i]: return False return True
binary-number-with-alternating-bits
Python Simple Logic Beats 100% solutions
saffi
0
86
binary number with alternating bits
693
0.613
Easy
11,450
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/381108/Three-Solutions-in-Python-3-(beats-~99)
class Solution: def hasAlternatingBits(self, n: int) -> bool: n = bin(n)+'0' for i in range(2,len(n)-1,2): if n[i:i+2] != '10': return False return True class Solution: def hasAlternatingBits(self, n: int) -> bool: n = bin(n) for i in range(2,len(n)-1): if n[i] == n[i...
binary-number-with-alternating-bits
Three Solutions in Python 3 (beats ~99%)
junaidmansuri
0
118
binary number with alternating bits
693
0.613
Easy
11,451
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/366091/Python3-two-simple-solutions
class Solution: def hasAlternatingBits(self, n: int) -> bool: bits = bin(n)[2:] # remove the 0b prefix previous = bits[0] for b in bits[1:]: if previous == b: return False previous = b return True
binary-number-with-alternating-bits
Python3 two simple solutions
llanowarelves
0
48
binary number with alternating bits
693
0.613
Easy
11,452
https://leetcode.com/problems/binary-number-with-alternating-bits/discuss/366091/Python3-two-simple-solutions
class Solution: def hasAlternatingBits(self, n: int) -> bool: while True: previous_bit = n &amp; 1 n >>= 1 if not n: return True current_bit = n &amp; 1 if previous_bit == current_bit: return False ...
binary-number-with-alternating-bits
Python3 two simple solutions
llanowarelves
0
48
binary number with alternating bits
693
0.613
Easy
11,453
https://leetcode.com/problems/max-area-of-island/discuss/1459194/python-Simple-and-intuitive-DFS-approach-!!!
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: if not grid: return 0 maxArea = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 1: # run dfs only when we find a land ...
max-area-of-island
[python] Simple and intuitive DFS approach !!!
nandanabhishek
15
1,400
max area of island
695
0.717
Medium
11,454
https://leetcode.com/problems/max-area-of-island/discuss/2284144/Python-or-Commented-or-Two-Solutions-or-Recursion-or-O(n-*-m)
# Set + Recursion Solution # Time: O(n * m), Iterates through grid once. # Space: O(n * m), Uses set to keep track of visited indices. class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: M, N = len(grid), len(grid[0]) # Gets size o...
max-area-of-island
Python | Commented | Two Solutions | Recursion | O(n * m)
bensmith0
10
889
max area of island
695
0.717
Medium
11,455
https://leetcode.com/problems/max-area-of-island/discuss/2284144/Python-or-Commented-or-Two-Solutions-or-Recursion-or-O(n-*-m)
# Recursion Solution # Time: O(n * m), Iterates through grid once. # Space: O(1), Constant space used by mutating the input list to track visited cells. class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: M, N = len(grid), len(grid[0]) ...
max-area-of-island
Python | Commented | Two Solutions | Recursion | O(n * m)
bensmith0
10
889
max area of island
695
0.717
Medium
11,456
https://leetcode.com/problems/max-area-of-island/discuss/2283657/Python3-oror-recursion-one-grid-w-explanation
class Solution: # The plan is to iterate through grid, a 2D array, and if we find # a cell in grid has value 1, then we recognize this cell as the # first cell on an unexplored island, start an area count (res) and ...
max-area-of-island
Python3 || recursion , one grid w/ explanation
warrenruud
6
503
max area of island
695
0.717
Medium
11,457
https://leetcode.com/problems/max-area-of-island/discuss/2085836/Python3-using-DFS
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: rowLength = len(grid[0]) columnLength = len(grid) visited = {} def dfs(i,j): if(i<0 or i>=columnLength or j<0 or j>=rowLength or grid[i][j] == 0 or (i,j) in visited): return ...
max-area-of-island
📌 Python3 using DFS
Dark_wolf_jss
4
118
max area of island
695
0.717
Medium
11,458
https://leetcode.com/problems/max-area-of-island/discuss/1759860/Python-Simple-Python-Solution-Using-DFS-With-the-Help-of-Recursion
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: Row = len(grid) Col = len(grid[0]) result = 0 def DFS(grid,r,c): if r >= 0 and r < Row and c >= 0 and c < Col and grid[r][c]==1: grid[r][c]=0 return 1 + DFS(grid,r-1,c) + DFS(grid,r+1,c) + DFS(grid,r,c-1) + DFS(grid,r,c+1) ...
max-area-of-island
[ Python ] ✅✅ Simple Python Solution Using DFS With the Help of Recursion ✌🔥🔥
ASHOK_KUMAR_MEGHVANSHI
3
97
max area of island
695
0.717
Medium
11,459
https://leetcode.com/problems/max-area-of-island/discuss/2513255/Efficient-Python-Solution-using-DFS-or-Faster-than-91.5-Solutions
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: ROWS = len(grid) COLS = len(grid[0]) visit = set() def dfs(r, c): if (r < 0 or r == ROWS or c < 0 or c == COLS or grid[r][c] == 0 or (r, c) in visit ): return 0 grid[r][c] = 0 visit.add((r, c)) return (1 + dfs...
max-area-of-island
Efficient Python Solution using DFS | Faster than 91.5% Solutions
nikhitamore
2
163
max area of island
695
0.717
Medium
11,460
https://leetcode.com/problems/max-area-of-island/discuss/2470488/Python-Elegant-and-Short-or-In-place-or-DFS
class Solution: """ Time: O(n^2) Memory: O(n^2) """ WATER = 0 LAND = 1 def maxAreaOfIsland(self, grid: List[List[int]]) -> int: n, m = len(grid), len(grid[0]) return max(self.island_area(i, j, grid) for i in range(n) for j in range(m)) @classmethod def island_area(cls, row: int, col: int, grid: List[L...
max-area-of-island
Python Elegant & Short | In-place | DFS
Kyrylo-Ktl
2
97
max area of island
695
0.717
Medium
11,461
https://leetcode.com/problems/max-area-of-island/discuss/2270322/Python3-DFS-solution
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: rowLength = len(grid[0]) columnLength = len(grid) visited = {} def dfs(i,j): if(i<0 or i>=columnLength or j<0 or j>=rowLength or grid[i][j] == 0 or (i,j) in visited): return ...
max-area-of-island
📌 Python3 DFS solution
Dark_wolf_jss
2
36
max area of island
695
0.717
Medium
11,462
https://leetcode.com/problems/max-area-of-island/discuss/1811737/easy-Python-DFS-solution-beat-88.7-with-explanation
class Solution(object): def maxAreaOfIsland(self, grid): """ :type grid: List[List[int]] :rtype: int """ # set up a list to save all areas of different islands areas = [] for i in range(len(grid)): for j in range(len(grid[0])): if grid[i...
max-area-of-island
easy Python DFS solution beat 88.7%, with explanation
siyu_
2
66
max area of island
695
0.717
Medium
11,463
https://leetcode.com/problems/max-area-of-island/discuss/2530359/Clean-Python3-or-BFS-or-greater-90-Time-and-Memory
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: neighbors = [(0, -1), (-1, 0), (0, 1), (1, 0)] def bfs(st_row, st_col): nonlocal rows, cols, neighbors, seen area = 1 q = deque([(st_row, st_col)]) while q: ...
max-area-of-island
Clean Python3 | BFS | > 90% Time & Memory
ryangrayson
1
61
max area of island
695
0.717
Medium
11,464
https://leetcode.com/problems/max-area-of-island/discuss/2508415/Python-or-DFS-or-Easy-to-understand
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: max_area=0 n=len(grid) m=len(grid[0]) def validMove(row, col): if row>=0 and row<n and col>=0 and col<m and grid[row][col]==1: return True return False ...
max-area-of-island
Python | DFS | Easy to understand
Siddharth_singh
1
49
max area of island
695
0.717
Medium
11,465
https://leetcode.com/problems/max-area-of-island/discuss/2286937/Python-or-Optimised-DFS-solution-faster-than-89.58-or-Easy-understandable-solution
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: area = 0 rows = len(grid) cols = len(grid[0]) def calculate_area(grid, r, c): if 0 <= r < rows and 0 <= c < cols and grid[r][c] == 1: temp_area = 1 grid[r][c]...
max-area-of-island
Python | Optimised DFS solution, faster than 89.58% | Easy understandable solution
om06
1
91
max area of island
695
0.717
Medium
11,466
https://leetcode.com/problems/max-area-of-island/discuss/2286361/GoPython-O(N)-time-or-O(N)-space-Solution
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: visited = set() moves = ((-1,0),(1,0),(0,-1),(0,1)) area = 0 for i,row in enumerate(grid): for j,item in enumerate(row): area = max(area,dfs(grid,visited,moves,i,j)) return ar...
max-area-of-island
Go/Python O(N) time | O(N) space Solution
vtalantsev
1
58
max area of island
695
0.717
Medium
11,467
https://leetcode.com/problems/max-area-of-island/discuss/2286361/GoPython-O(N)-time-or-O(N)-space-Solution
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: visited = set() moves = ((-1,0),(1,0),(0,-1),(0,1)) area = 0 for i,row in enumerate(grid): for j,item in enumerate(row): stack = [(i,j)] curr_area = 0 ...
max-area-of-island
Go/Python O(N) time | O(N) space Solution
vtalantsev
1
58
max area of island
695
0.717
Medium
11,468
https://leetcode.com/problems/max-area-of-island/discuss/2283597/python3-or-explained-or-BFS-or-easy-to-uderstand
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: n, m = len(grid), len(grid[0]) self.visited = [[False for _ in range(m)] for _ in range(n)] ans = 0 for i in range(n): for j in range(m): # if grid[i][j] == 1 -> area...
max-area-of-island
python3 | explained | BFS | easy to uderstand
H-R-S
1
147
max area of island
695
0.717
Medium
11,469
https://leetcode.com/problems/max-area-of-island/discuss/1981795/Python3-Runtime%3A-202ms-41.26-Memory%3A-16.7mb-46.11
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: visited = [[False for row in rows]for rows in grid] maxIsland = 0 for row in range(len(grid)): for col in range(len(grid[row])): if visited[row][col]: continue ...
max-area-of-island
Python3 Runtime: 202ms 41.26% Memory: 16.7mb 46.11%
arshergon
1
28
max area of island
695
0.717
Medium
11,470
https://leetcode.com/problems/max-area-of-island/discuss/1859214/Faster-99-early-stopping-python-solution-(Space-56)
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) max_island_size = 0 def follow_island(x, y): if 0 <= x < m and 0 <= y < n and grid[x][y] == 1: grid[x][y] = 0 return 1 + follow_islan...
max-area-of-island
Faster 99% early stopping python solution (Space 56%)
Nitschi
1
80
max area of island
695
0.717
Medium
11,471
https://leetcode.com/problems/max-area-of-island/discuss/1802236/python-dfs-easy-to-understand-with-explanation
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: a = [] r = len(grid) c = len(grid[0]) res = [] ans = [] def dfs(x,y): grid[x][y] = 0 #make it unvalid for use again a.append((x,y)) dx = [-1, 0, 1, 0] #all pos...
max-area-of-island
python dfs easy to understand with explanation
ggeeoorrggee
1
99
max area of island
695
0.717
Medium
11,472
https://leetcode.com/problems/max-area-of-island/discuss/1793393/BFS-python
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: visited = set() def bfs(i,j,grid,visited): count = 1 visited.add((i,j)) q = deque() q.append((i,j)) while q: currr, currc = q.popleft() ...
max-area-of-island
BFS python
fengqilong
1
51
max area of island
695
0.717
Medium
11,473
https://leetcode.com/problems/max-area-of-island/discuss/1616497/python-solution
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: row, col = len(grid), len(grid[0]) max_area = 0 visited = [[False for _ in range(col)] for _ in range(row)] def dfs(r, c): if 0 > r or r >= row or 0 > c or c >= col or grid[r][c] == 0 or visited[r][c]: return 0 visited[r][c] = ...
max-area-of-island
python solution
fatemebesharat766
1
119
max area of island
695
0.717
Medium
11,474
https://leetcode.com/problems/max-area-of-island/discuss/1460568/WEEB-DOES-PYTHON-BFS
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: row, col = len(grid), len(grid[0]) queue, max_area = deque([]), 0 for x in range(row): for y in range(col): if grid[x][y] == 1: queue.append((x,y)) max_area = max(max_area, self.bfs(grid, queue, row, col)) return max_ar...
max-area-of-island
WEEB DOES PYTHON BFS
Skywalker5423
1
136
max area of island
695
0.717
Medium
11,475
https://leetcode.com/problems/max-area-of-island/discuss/1451258/Python3-DFS-solution-or-faster-than-99.5
class Solution: def dfs(self, r,c,grids): # if out of boundary or current position is equal to zero if r < 0 or c < 0 or r >= len(grids) or c >= len(grids[0]) or grids[r][c] == 0: return 0 grids[r][c] = 0 # Sink the island if it has been traversed (if current position is 1, mark it as 0) return 1 + self....
max-area-of-island
Python3 DFS solution | faster than 99.5%
Janetcxy
1
71
max area of island
695
0.717
Medium
11,476
https://leetcode.com/problems/max-area-of-island/discuss/1296462/Python3-oror-dfs-oror-15-lines-of-code-oror-Simple-And-Self-Explanatory
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: def dfs(i,j): if i<0 or j<0 or i>=len(grid) or j>=len(grid[0]) or grid[i][j]==0: return 0 grid[i][j]=0 a=1+(dfs(i+1,j)+dfs(i,j+1)+dfs(i-1,j)+dfs(i,j-1)) return a ...
max-area-of-island
Python3 || dfs || 15 lines of code || Simple And Self Explanatory
bug_buster
1
126
max area of island
695
0.717
Medium
11,477
https://leetcode.com/problems/max-area-of-island/discuss/897955/Python3-dfs
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) # dimension def dfs(i, j): """Depth-first traverse the grid.""" if grid[i][j] == 1: grid[i][j] = 0 # mark visited return 1 + su...
max-area-of-island
[Python3] dfs
ye15
1
104
max area of island
695
0.717
Medium
11,478
https://leetcode.com/problems/max-area-of-island/discuss/897955/Python3-dfs
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) # dimensions def fn(i, j): """Return island area.""" ans = 1 grid[i][j] = 0 # mark as visited for ii, jj in (i-1, j), (i, j-1), (i, j+1)...
max-area-of-island
[Python3] dfs
ye15
1
104
max area of island
695
0.717
Medium
11,479
https://leetcode.com/problems/max-area-of-island/discuss/897955/Python3-dfs
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) ans = 0 for r in range(m): for c in range(n): if grid[r][c]: val = 1 grid[r][c] = 0 stack = [(r, c...
max-area-of-island
[Python3] dfs
ye15
1
104
max area of island
695
0.717
Medium
11,480
https://leetcode.com/problems/max-area-of-island/discuss/750599/Python-DFS-Readable-Faster-than-92
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: area_of_all = [] for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j]: area_of_all.append(self.dfs(grid, i, j, 0)) return max(area_of_all) if area_of_all...
max-area-of-island
[Python] DFS Readable Faster than 92%
Prodyte
1
80
max area of island
695
0.717
Medium
11,481
https://leetcode.com/problems/max-area-of-island/discuss/717057/Python-DFS-O(M*N)
class Solution: # Time: O(m*n) # Space: O(m*n) def maxAreaOfIsland(self, grid: List[List[int]]) -> int: res = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 1: res = max(res, self.dfs(i, j, grid)) return res...
max-area-of-island
Python DFS O(M*N)
whissely
1
180
max area of island
695
0.717
Medium
11,482
https://leetcode.com/problems/max-area-of-island/discuss/395168/Solution-in-Python-3-(BFS-and-DFS)-(beats-~-90-and-82)
class Solution: def maxAreaOfIsland(self, G: List[List[int]]) -> int: M, N, m = len(G), len(G[0]), 0 def area(x,y): p, t, a, G[x][y] = [[x,y]], [], 1, 0 while p: for i,j in p: for k,l in [[i-1,j],[i,j+1],[i+1,j],[i,j-1]]: if k in [-1,M] or l in [-1,N] or G[k][l] == 0: c...
max-area-of-island
Solution in Python 3 (BFS and DFS) (beats ~ 90% and 82%)
junaidmansuri
1
1,400
max area of island
695
0.717
Medium
11,483
https://leetcode.com/problems/max-area-of-island/discuss/395168/Solution-in-Python-3-(BFS-and-DFS)-(beats-~-90-and-82)
class Solution: def maxAreaOfIsland(self, G: List[List[int]]) -> int: M, N, m = len(G), len(G[0]), 0 def dfs_area(x,y): if (x,y) in V: return G[x][y], _ = 0, V.add((x,y)) for i,j in [[x-1,y],[x,y+1],[x+1,y],[x,y-1]]: if not (i in [-1,M] or j in [-1,N] or G[i][j] == 0): dfs_area(i,...
max-area-of-island
Solution in Python 3 (BFS and DFS) (beats ~ 90% and 82%)
junaidmansuri
1
1,400
max area of island
695
0.717
Medium
11,484
https://leetcode.com/problems/max-area-of-island/discuss/305750/Python3-DFS-solution
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: if not grid or len(grid) == 0: return 0 rows = len(grid) cols = len(grid[0]) greed = 0 for r in range(rows): for c in range(cols): if grid[r][c] == 0: ...
max-area-of-island
Python3 DFS solution
nzelei
1
142
max area of island
695
0.717
Medium
11,485
https://leetcode.com/problems/max-area-of-island/discuss/2838747/Python-DFS-Solution
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) DIR = [0, 1, 0, -1, 0] def dfs(r, c): if r < 0 or r == m or c < 0 or c == n or grid[r][c] == 0: return 0 ans = 1 grid[r][c] = ...
max-area-of-island
Python DFS Solution
taoxinyyyun
0
1
max area of island
695
0.717
Medium
11,486
https://leetcode.com/problems/max-area-of-island/discuss/2837475/dfs-in-python
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) res = 0 def dfs(row, col): if 0<=row<m and 0<=col<n and grid[row][col] == 1: grid[row][col] = 0 return dfs(row+1,col)+dfs(row-1,col)+dfs(row,col...
max-area-of-island
dfs in python
ychhhen
0
1
max area of island
695
0.717
Medium
11,487
https://leetcode.com/problems/max-area-of-island/discuss/2822139/Python3-detailed-DFS-solution
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: max_area = [0] def dfs(i, j): def count_area(i, j) -> int: # ensure in bounds and is land if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]): return 0 if grid...
max-area-of-island
Python3 - detailed DFS solution
rschevenin
0
1
max area of island
695
0.717
Medium
11,488
https://leetcode.com/problems/max-area-of-island/discuss/2775203/Python-DFS-Solution-Faster-than-100.00
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: if not grid: return 0 rows, cols = len(grid), len(grid[0]) maxArea = 0 area = [0] def dfs(r, c, area): grid[r][c] = 0 area[0] += 1 if r - 1 >= 0 a...
max-area-of-island
Python DFS Solution, Faster than 100.00%
skromez
0
7
max area of island
695
0.717
Medium
11,489
https://leetcode.com/problems/max-area-of-island/discuss/2770105/Easy-Python-Solution-or-Matrix-or-Flood-Fill
class Solution(object): def maxAreaOfIsland(self, grid): def solve(r, c): if r not in range(len(grid)): return 0 if c not in range(len(grid[0])): return 0 if grid[r][c] == 0: return 0 if grid[r][c] == 2: ...
max-area-of-island
Easy Python Solution | Matrix | Flood Fill
atharva77
0
1
max area of island
695
0.717
Medium
11,490
https://leetcode.com/problems/max-area-of-island/discuss/2735086/Python-%2B-DFS-%2B-nonlocal-parameter
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) res = 0 island_size = 0 def dfs(grid, i, j, m, n): nonlocal island_size dirs = [[-1,0], [1,0], [0,1], [0,-1]] if grid[i][j] == ...
max-area-of-island
Python + DFS + nonlocal parameter
Arana
0
5
max area of island
695
0.717
Medium
11,491
https://leetcode.com/problems/max-area-of-island/discuss/2706684/Depth-First-Search-Python-Solution
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: rows = len(grid) cols = len(grid[0]) maxArea = 0 for col in range(cols): for row in range(rows): area = 0 if grid[row][col]==1: CalcArea = self.df...
max-area-of-island
Depth First Search - Python Solution
ManitejaPadigela
0
2
max area of island
695
0.717
Medium
11,492
https://leetcode.com/problems/max-area-of-island/discuss/2577012/Python-runtime-O(rc)-memory-O(1)-(98.87)
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: max_area = 0 row_len = len(grid) col_len = len(grid[0]) for r in range(row_len): for c in range(col_len): if grid[r][c] == 1: grid[r][c] = 0 cu...
max-area-of-island
Python, runtime O(rc), memory O(1) (98.87%)
tsai00150
0
38
max area of island
695
0.717
Medium
11,493
https://leetcode.com/problems/max-area-of-island/discuss/2568612/DFS-solution-space-O(1)
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: # before start, we need to make sure if we can edit the graph, turn 1 to 0 # in here, I assume we can, we are using a dfs algorithm # comparing current island size and max_area size def dfs(r, c): nonlocal curr_area ...
max-area-of-island
DFS solution space O(1)
RayML
0
36
max area of island
695
0.717
Medium
11,494
https://leetcode.com/problems/max-area-of-island/discuss/2458343/Python-iterative-dfs-oror-81
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: max_area = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 1: a = self.dfs(i,j,grid) max_area = max(max_area,a) return max_...
max-area-of-island
Python iterative dfs || 81
aruj900
0
23
max area of island
695
0.717
Medium
11,495
https://leetcode.com/problems/max-area-of-island/discuss/2429030/Max-Area-of-Island-oror-Python3-oror-DFS
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: ans = 0 dirs = [[1, 0], [0, 1], [-1, 0], [0, -1]] for i in range(0, len(grid)): for j in range(0, len(grid[0])): if(grid[i][j] == 1): count = self.dfs(i, j, grid, dirs) ...
max-area-of-island
Max Area of Island || Python3 || DFS
vanshika_2507
0
20
max area of island
695
0.717
Medium
11,496
https://leetcode.com/problems/max-area-of-island/discuss/2408752/Python3-Max-Area-of-Island-w-Explanation-92-Runtime
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: max_size = 0 #Helper - Given a pos (i,j) on the grid, this function will recursively search around it for other land # - It marks all land it has seen by changing it from 1 to 2, preventing it being count...
max-area-of-island
[Python3] Max Area of Island - w Explanation - 92% Runtime
connorthecrowe
0
58
max area of island
695
0.717
Medium
11,497
https://leetcode.com/problems/max-area-of-island/discuss/2408730/C%2B%2BPython-best-optimal-solution-with-Good-results
class Solution: def maxAreaOfIsland(self, grid: List[List[int]]) -> int: if not grid: return 0 maxArea = 0 for i in range(len(grid)): for j in range(len(grid[0])): if grid[i][j] == 1: # run dfs only when we f...
max-area-of-island
C++/Python best optimal solution with Good results
arpit3043
0
28
max area of island
695
0.717
Medium
11,498
https://leetcode.com/problems/max-area-of-island/discuss/2352862/Python3-or-Solved-using-BFS-%2B-Queue-%2B-HashSet
class Solution: #Time-Complexity: O(m*n), in worst case, bfs while loop will run through each and every entry of grid if entire grid is land! #Space-Complexity: O(m*n + m*n) -> O(m*n) def maxAreaOfIsland(self, grid: List[List[int]]) -> int: #dimension of the grid input: m * n rows, col...
max-area-of-island
Python3 | Solved using BFS + Queue + HashSet
JOON1234
0
27
max area of island
695
0.717
Medium
11,499