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/move-zeroes/discuss/2831020/Super-simple-python-approach-with-minimal-lines-of-code
class Solution: def moveZeroes(self, nums: List[int]) -> None: start = 0 second = 1 while second < len(nums): if(nums[start] == 0 and nums[second] !=0): nums[start], nums[second] = nums[second], nums[start] start+=1 ...
move-zeroes
Super simple python approach with minimal lines of code
btulsi
0
5
move zeroes
283
0.614
Easy
5,100
https://leetcode.com/problems/find-the-duplicate-number/discuss/650942/Proof-of-Floyd's-cycle-detection-algorithm-Find-the-Duplicate-Number
class Solution: def findDuplicate(self, nums: List[int]) -> int: slow = fast = ans = 0 while True: slow = nums[slow] fast = nums[nums[fast]] if slow == fast: break while ans != slow: ans = nums[ans] slow = nums[slow]...
find-the-duplicate-number
Proof of Floyd's cycle detection algorithm - Find the Duplicate Number
r0bertz
20
2,400
find the duplicate number
287
0.591
Medium
5,101
https://leetcode.com/problems/find-the-duplicate-number/discuss/342853/Solution-in-Python-3-(-O(n)-speed-and-O(1)-memory-)
class Solution: def findDuplicate(self, nums: List[int]) -> int: t, h = nums[0], nums[nums[0]] while t != h: t, h = nums[t], nums[nums[h]] t = 0 while t != h: t, h = nums[t], nums[h] return t - Junaid Mansuri (LeetCode ID)@hotmail.com
find-the-duplicate-number
Solution in Python 3 ( O(n) speed and O(1) memory )
junaidmansuri
9
2,600
find the duplicate number
287
0.591
Medium
5,102
https://leetcode.com/problems/find-the-duplicate-number/discuss/2013321/Python-O(n)-Time-and-O(1)-Space-Optimal-Easy-to-Understand-Solution
class Solution: def findDuplicate(self, nums: List[int]) -> int: # Use concept of 142. Linked List Cycle II (find the node where linked list has cycle) # start hopping from Node slow, fast = 0, 0 # Cycle detection # Let slow jumper and fast jumper meet somewhere in t...
find-the-duplicate-number
Python O(n) Time and O(1) Space Optimal Easy to Understand Solution ⭐
samirpaul1
6
500
find the duplicate number
287
0.591
Medium
5,103
https://leetcode.com/problems/find-the-duplicate-number/discuss/1077997/Python-Floyd's-Cycle-Detecting-Algorithm-Easy-to-Understand
class Solution: def findDuplicate(self, nums: List[int]) -> int: slow, fast = 0, 0 while True: slow = nums[slow] fast = nums[nums[fast]] if slow == fast: break slow = 0 while slow != fast: slow = nums[slow] ...
find-the-duplicate-number
Python Floyd's Cycle Detecting Algorithm Easy to Understand
pikachuexeallen
6
458
find the duplicate number
287
0.591
Medium
5,104
https://leetcode.com/problems/find-the-duplicate-number/discuss/1874359/Python-Solutions
class Solution: def findDuplicate(self, nums: List[int]) -> int: for i in range(len(nums)-1): for j in range(i+1, len(nums)): if nums[i] == nums[j]: return nums[i]
find-the-duplicate-number
✅ Python Solutions
dhananjay79
4
212
find the duplicate number
287
0.591
Medium
5,105
https://leetcode.com/problems/find-the-duplicate-number/discuss/1874359/Python-Solutions
class Solution: def findDuplicate(self, nums: List[int]) -> int: s = set() for i in nums: if i in s: return i s.add(i)
find-the-duplicate-number
✅ Python Solutions
dhananjay79
4
212
find the duplicate number
287
0.591
Medium
5,106
https://leetcode.com/problems/find-the-duplicate-number/discuss/1874359/Python-Solutions
class Solution: def findDuplicate(self, nums: List[int]) -> int: for i in nums: if nums[abs(i)] < 0: return abs(i) nums[abs(i)] *= -1
find-the-duplicate-number
✅ Python Solutions
dhananjay79
4
212
find the duplicate number
287
0.591
Medium
5,107
https://leetcode.com/problems/find-the-duplicate-number/discuss/1874359/Python-Solutions
class Solution: def findDuplicate(self, nums: List[int]) -> int: slow, fast = nums[nums[0]], nums[nums[nums[0]]] while slow != fast: slow = nums[slow] fast = nums[nums[fast]] start = nums[0] while start != slow: start = nums[start] slow...
find-the-duplicate-number
✅ Python Solutions
dhananjay79
4
212
find the duplicate number
287
0.591
Medium
5,108
https://leetcode.com/problems/find-the-duplicate-number/discuss/2546656/Python-Easy-Approach-oror-O(n)-oror-Beginners-friendly
class Solution: def findDuplicate(self, nums: List[int]) -> int: for i in range(len(nums)): if nums[abs(nums[i])-1]>0: nums[abs(nums[i])-1] = -nums[abs(nums[i])-1] else: return abs(nums[i])
find-the-duplicate-number
[Python] Easy Approach || O(n) || Beginners friendly
iterator1114
2
303
find the duplicate number
287
0.591
Medium
5,109
https://leetcode.com/problems/find-the-duplicate-number/discuss/1433142/3-Way-of-solving-the-problem-with-Python-in-O(n)-Time
class Solution: def findDuplicate(self, nums: List[int]) -> int: prevNums = set() for n in nums: if n in prevNums: return n else: prevNums.add(n) return float("inf")
find-the-duplicate-number
3 Way of solving the problem with Python in O(n) Time
abrarjahin
2
371
find the duplicate number
287
0.591
Medium
5,110
https://leetcode.com/problems/find-the-duplicate-number/discuss/1433142/3-Way-of-solving-the-problem-with-Python-in-O(n)-Time
class Solution: def findDuplicate(self, nums: List[int]) -> int: #Time O(n) and extra space O(1) # By Modifying the array - As value can be from 1-n, we can mark index to track them for i in range(len(nums)): index = (nums[i]%len(nums))-1 #As value from 1-n nums[index...
find-the-duplicate-number
3 Way of solving the problem with Python in O(n) Time
abrarjahin
2
371
find the duplicate number
287
0.591
Medium
5,111
https://leetcode.com/problems/find-the-duplicate-number/discuss/1302366/PythonorSimpleorCounter
class Solution: def findDuplicate(self, nums: List[int]) -> int: freq=Counter(nums) for i in freq: if freq[i]>1: return i
find-the-duplicate-number
Python|Simple|Counter
atharva_shirode
2
348
find the duplicate number
287
0.591
Medium
5,112
https://leetcode.com/problems/find-the-duplicate-number/discuss/2728909/simple-Set-solution-in-pyhton
class Solution: def findDuplicate(self, nums: List[int]) -> int: setm = set() for i in nums: k = len(setm) setm.add(i) m = len(setm) if m == k: return i
find-the-duplicate-number
simple Set solution in pyhton
PrateekSikarwar
1
34
find the duplicate number
287
0.591
Medium
5,113
https://leetcode.com/problems/find-the-duplicate-number/discuss/2383164/Binary-Search-Solution-O(nlogn)-time-or-O(1)-space-python3
class Solution: # O(nlogn) time, # O(1) space, # Approach: Binary search, def findDuplicate(self, nums: List[int]) -> int: n = len(nums) def countNumbersLessOrEqualToNum(num:int) -> int: count = 0 for number in nums: if number <= num: ...
find-the-duplicate-number
Binary Search Solution, O(nlogn) time | O(1) space, python3
destifo
1
56
find the duplicate number
287
0.591
Medium
5,114
https://leetcode.com/problems/find-the-duplicate-number/discuss/2315229/Python3-O(n)-time-O(1)-space
class Solution: def findDuplicate(self, nums: List[int]) -> int: for i, _ in enumerate(nums): while(i+1 != nums[i]): n = nums[i] tmp = nums[n-1] if tmp == nums[i]: return tmp else: nums[i], nu...
find-the-duplicate-number
[Python3] O(n) time O(1) space
Yan_Yichun
1
70
find the duplicate number
287
0.591
Medium
5,115
https://leetcode.com/problems/find-the-duplicate-number/discuss/1754451/Simple-Solution-Python3-or-Explained
class Solution: def findDuplicate(self, nums: List[int]) -> int: nums.sort() for i in range(len(nums)- 1): if nums[i] == nums[i + 1]: return nums[i]
find-the-duplicate-number
Simple Solution Python3 | Explained
dporwal985
1
82
find the duplicate number
287
0.591
Medium
5,116
https://leetcode.com/problems/find-the-duplicate-number/discuss/1748330/Easy-to-understand-O(N)-runtime
class Solution: def findDuplicate(self, nums: List[int]) -> int: for i,j in enumerate(nums): # If the current index value is negative, making it +ve # To navigate to the index if j < 0: j *= -1 # The index is marked negative nums[...
find-the-duplicate-number
Easy to understand [O(N) - runtime]
funnybacon
1
107
find the duplicate number
287
0.591
Medium
5,117
https://leetcode.com/problems/find-the-duplicate-number/discuss/2813374/Python-Sol-with-Collections
class Solution: def findDuplicate(self, nums: List[int]) -> int: from collections import Counter k = Counter(nums) for i,j in k.items(): if j > 1: return i
find-the-duplicate-number
Python Sol with Collections
user5631IZ
0
4
find the duplicate number
287
0.591
Medium
5,118
https://leetcode.com/problems/find-the-duplicate-number/discuss/2804316/Python-New-very-simple-cycle-detection-approach.-No-extra-space.-Code-commented.
class Solution: def findDuplicate(self, nums: List[int]) -> int: # Start iteration i = 0 while i < len(nums): currNr = nums[i] # This number is in its right place. Increment i and continue. if currNr == i + 1: i = i + 1 else: ...
find-the-duplicate-number
[Python] New very simple cycle-detection approach. No extra space. Code commented.
interviewprepsimpleguy
0
7
find the duplicate number
287
0.591
Medium
5,119
https://leetcode.com/problems/find-the-duplicate-number/discuss/2778691/Easy-python-solution.
class Solution: def findDuplicate(self, nums: List[int]) -> int: dic={} for i in nums: if i in dic: dic[i]=dic[i]+1 else: dic[i]=1 for i in dic: if dic[i]>=2: return i return 0
find-the-duplicate-number
Easy python solution.
zaeden9
0
7
find the duplicate number
287
0.591
Medium
5,120
https://leetcode.com/problems/find-the-duplicate-number/discuss/2761191/Python-3-Solution-or-1-line-or-Faster-than-94.57
class Solution: def findDuplicate(self, nums: List[int]) -> int: return (sum(nums) - sum(set(nums))) // (len(nums) - len(set(nums)))
find-the-duplicate-number
Python 3 Solution | 1 line | Faster than 94.57%
mati44
0
3
find the duplicate number
287
0.591
Medium
5,121
https://leetcode.com/problems/find-the-duplicate-number/discuss/2742360/Two-line-colution-Python
class Solution: def findDuplicate(self, nums: List[int]) -> int: c = Counter(nums) return sorted(c, key=lambda x: (-c[x], x))[0]
find-the-duplicate-number
Two line colution Python
a3amaT
0
13
find the duplicate number
287
0.591
Medium
5,122
https://leetcode.com/problems/find-the-duplicate-number/discuss/2732200/Python-oror-3-liner-oror-beginners-solution-oror-Easy
class Solution: def findDuplicate(self, nums: List[int]) -> int: for i in range(len(nums)): if nums[abs(nums[i])-1] < 0: return abs(nums[i]) else: nums[abs(nums[i])-1] = -nums[abs(nums[i])-1]
find-the-duplicate-number
Python || 3-liner || beginners solution || Easy
its_iterator
0
6
find the duplicate number
287
0.591
Medium
5,123
https://leetcode.com/problems/find-the-duplicate-number/discuss/2719978/Concise-Python-and-Golang-Solution
class Solution: def findDuplicate(self, nums: List[int]) -> int: cache = set() for num in nums: if num in cache: return num cache.add(num)
find-the-duplicate-number
Concise Python and Golang Solution
namashin
0
2
find the duplicate number
287
0.591
Medium
5,124
https://leetcode.com/problems/find-the-duplicate-number/discuss/2716404/VERY-EASY-SOLUTION-WITH-PYTHON.ONLY-7-LINES.CHECK-IT-OUT
class Solution: def findDuplicate(self, nums: List[int]) -> int: nums.sort() for i in range(len(nums)): if i+1<len(nums): if(nums[i]==nums[i+1]): return nums[i]
find-the-duplicate-number
VERY EASY SOLUTION WITH PYTHON.ONLY 7 LINES.CHECK IT OUT
rajneeshkabdwal
0
1
find the duplicate number
287
0.591
Medium
5,125
https://leetcode.com/problems/find-the-duplicate-number/discuss/2715784/easiest-for-understand!-1-line
class Solution: def findDuplicate(self, nums: List[int]) -> int: return (sum(nums) - sum(set(nums))) // (len(nums) - len(set(nums)))
find-the-duplicate-number
easiest for understand! 1-line
neversleepsainou
0
4
find the duplicate number
287
0.591
Medium
5,126
https://leetcode.com/problems/find-the-duplicate-number/discuss/2715687/Python-Two-approaches
class Solution: def findDuplicate(self, nums: List[int]) -> int: #time complexity: O(n) #space complexity: O(n) dic={} for i in range(len(nums)): if nums[i] in dic: return nums[i] else: dic[nums[i]]=i
find-the-duplicate-number
Python Two approaches
Godslyr10
0
4
find the duplicate number
287
0.591
Medium
5,127
https://leetcode.com/problems/find-the-duplicate-number/discuss/2715687/Python-Two-approaches
class Solution: def findDuplicate(self, nums: List[int]) -> int: slow,fast=0,0 while True: slow=nums[slow] fast=nums[nums[fast]] if slow == fast: break slow=0 while True: slow=nums[slow] fast=nums[fast] ...
find-the-duplicate-number
Python Two approaches
Godslyr10
0
4
find the duplicate number
287
0.591
Medium
5,128
https://leetcode.com/problems/find-the-duplicate-number/discuss/2707325/Easy-Python-solution-beats-99.99
class Solution: def findDuplicate(self, nums: List[int]) -> int: # Initiate a blank dictionary num_dict = {} for i in nums: if i not in num_dict: # Keep adding distinct numbers along with their count = 1 in the dictionary num_dict[i] = 1 else: ...
find-the-duplicate-number
Easy Python solution beats 99.99%
code_snow
0
7
find the duplicate number
287
0.591
Medium
5,129
https://leetcode.com/problems/find-the-duplicate-number/discuss/2688765/1-Line-python-solution-beat-99
class Solution: def findDuplicate(self, nums: List[int]) -> int: return (sum(nums) - sum(set(nums))) // (len(nums) - len(set(nums)))
find-the-duplicate-number
1 Line python solution beat 99%
wguo
0
10
find the duplicate number
287
0.591
Medium
5,130
https://leetcode.com/problems/find-the-duplicate-number/discuss/2688230/Now-I-am-become-death
class Solution: def findDuplicate(self, nums: List[int]) -> int: def linked(node, speed): next_node = node while True: for i in range(speed): next_node = nums[next_node] yield next_node slow = linked(0, 1) ...
find-the-duplicate-number
Now I am become death
mshvern
0
5
find the duplicate number
287
0.591
Medium
5,131
https://leetcode.com/problems/find-the-duplicate-number/discuss/2684667/Python-SolutionororUsing-Lists-and-Set
class Solution: def findDuplicate(self, nums: List[int]) -> int: seenElements = set() for n in nums: if n in seenElements: return n seenElements.add(n)
find-the-duplicate-number
Python Solution||Using Lists and Set
PujalaChowdamma
0
4
find the duplicate number
287
0.591
Medium
5,132
https://leetcode.com/problems/find-the-duplicate-number/discuss/2654481/PYTHON-easy-solution-oror
class Solution: def findDuplicate(self, nums: List[int]) -> int: a = set(nums) return (sum(nums)-sum(a)) // (len(nums)-len(a))
find-the-duplicate-number
PYTHON easy solution ||
sinjan_singh
0
5
find the duplicate number
287
0.591
Medium
5,133
https://leetcode.com/problems/find-the-duplicate-number/discuss/2652790/Floyd's-Algorithm-Revised
class Solution: def findDuplicate(self, nums: List[int]) -> int: # each element represents a node # the value of the node can represent an index # since there's 1 duplicate, every other node # will point to a node (no other node points to) # the duplicate node will point to a...
find-the-duplicate-number
Floyd's Algorithm Revised
andrewnerdimo
0
9
find the duplicate number
287
0.591
Medium
5,134
https://leetcode.com/problems/find-the-duplicate-number/discuss/2651202/Python-Easy-and-Explained
class Solution: def findDuplicate(self, nums: List[int]) -> int: nums.sort() for i in range(len(nums)-1): if (nums[i] == nums[i+1]): return nums[i]
find-the-duplicate-number
Python Easy and Explained
kunallunia22
0
4
find the duplicate number
287
0.591
Medium
5,135
https://leetcode.com/problems/find-the-duplicate-number/discuss/2643791/Floy'd-Algorithm-(slow-and-fast-pointer)
class Solution: def findDuplicate(self, nums: List[int]) -> int: # each element represents a node # the value of the node can represent an index # since there's 1 duplicate, every other node # will point to a node (no other node points to) # the duplicate node will point to ...
find-the-duplicate-number
Floy'd Algorithm (slow and fast pointer)
andrewnerdimo
0
5
find the duplicate number
287
0.591
Medium
5,136
https://leetcode.com/problems/find-the-duplicate-number/discuss/2604605/python3
class Solution: def findDuplicate(self, nums: List[int]) -> int: # don't know why time limit exceeded for list but for set it worked # please let me know in the comment section if you guys can clarify l = set() for x in nums: if x in l: return x l.add...
find-the-duplicate-number
python3
priyam_jsx
0
56
find the duplicate number
287
0.591
Medium
5,137
https://leetcode.com/problems/find-the-duplicate-number/discuss/2528752/Python-or-faster-than-77
class Solution: def findDuplicate(self, nums: List[int]) -> int: nums.sort() for i in range(1,len(nums)): if nums[i]==nums[i-1]: return nums[i] return -1
find-the-duplicate-number
Python | faster than 77%
moayaan1911
0
104
find the duplicate number
287
0.591
Medium
5,138
https://leetcode.com/problems/find-the-duplicate-number/discuss/2511941/simple-python-O(n)-time
class Solution: def findDuplicate(self, nums: List[int]) -> int: d = defaultdict(int) for i in nums: d[i] += 1 for i in d: if d[i] > 1: return i
find-the-duplicate-number
simple python O(n) time
gasohel336
0
60
find the duplicate number
287
0.591
Medium
5,139
https://leetcode.com/problems/find-the-duplicate-number/discuss/2507536/Simple-python-code-with-explanation
class Solution: def findDuplicate(self, nums: List[int]) -> int: thoma = {} #store the keys of dictionary(thoma) in variable -->(k) k = thoma.keys() #iterate over the elements in array --> nums for i in nums: ...
find-the-duplicate-number
Simple python code with explanation
thomanani
0
51
find the duplicate number
287
0.591
Medium
5,140
https://leetcode.com/problems/find-the-duplicate-number/discuss/2506207/Python3-solution-using-Counter-beats-98
class Solution: def findDuplicate(self, nums: List[int]) -> int: temp = Counter(nums) for i in temp: if temp[i] > 1: return i
find-the-duplicate-number
Python3 solution using Counter beats 98%
irapandey
0
57
find the duplicate number
287
0.591
Medium
5,141
https://leetcode.com/problems/find-the-duplicate-number/discuss/2474136/Python-Binary-Search-Solution
class Solution: def findDuplicate(self, nums: List[int]) -> int: start = 1 end = len(nums) ans = float('inf') def bs(start, end): nonlocal ans if start > end: return -1 else: mid = (start + end) // 2 ...
find-the-duplicate-number
Python Binary Search Solution
DietCoke777
0
34
find the duplicate number
287
0.591
Medium
5,142
https://leetcode.com/problems/find-the-duplicate-number/discuss/2474122/Python-Binary-Search-solution
class Solution: def findDuplicate(self, nums: List[int]) -> int: start = 1 end = len(nums) ans = float('inf') def bs(start, end): nonlocal ans if start > end: return -1 else: mid = (start + end) // 2 ...
find-the-duplicate-number
Python Binary Search solution
DietCoke777
0
11
find the duplicate number
287
0.591
Medium
5,143
https://leetcode.com/problems/find-the-duplicate-number/discuss/2450142/Python3-two-Methods-O(n)-and-O(n-logn).-Dictionary-Map
class Solution: def findDuplicate(self, nums: List[int]) -> int: nums.sort() for i in range(len(nums)-1): l = bisect_left(nums,nums[i]) r = bisect_right(nums,nums[i]) if r-l >1: return nums[i]
find-the-duplicate-number
Python3 two Methods O(n) and O(n logn). Dictionary Map
aditya_maskar
0
36
find the duplicate number
287
0.591
Medium
5,144
https://leetcode.com/problems/find-the-duplicate-number/discuss/2428231/Find-the-duplicate-number-oror-Python3-oror-Cycle-detection
class Solution: def findDuplicate(self, nums: List[int]) -> int: hare = nums[0] tortoise = nums[0] while(True): tortoise = nums[tortoise] hare = nums[nums[hare]] if(tortoise == hare): # We found a cycle break ...
find-the-duplicate-number
Find the duplicate number || Python3 || Cycle detection
vanshika_2507
0
35
find the duplicate number
287
0.591
Medium
5,145
https://leetcode.com/problems/find-the-duplicate-number/discuss/2426143/Python-O(n)-solution-linear-time-constant-space-and-no-modifications-to-array
class Solution: def findDuplicate(self, nums: List[int]) -> int: # Floyd Warshall Algorithm slow, fast = 0, 0 # 0 is not a part of the cycle while True: slow = nums[slow] # equivalent to slow.next in linked list fast = nums[nums[fast]] # equivalent to fast.next....
find-the-duplicate-number
Python O(n) solution linear time, constant space and no modifications to array
averule
0
74
find the duplicate number
287
0.591
Medium
5,146
https://leetcode.com/problems/find-the-duplicate-number/discuss/2393672/Python-Solution-or-One-Liner-or-100-Faster-or-Frequency-Count-Based
class Solution: def findDuplicate(self, nums: List[int]) -> int: # get most frequent element return collections.Counter(nums).most_common(1)[0][0]
find-the-duplicate-number
Python Solution | One Liner | 100% Faster | Frequency Count Based
Gautam_ProMax
0
65
find the duplicate number
287
0.591
Medium
5,147
https://leetcode.com/problems/find-the-duplicate-number/discuss/2392875/Python
class Solution: def findDuplicate(self, nums: List[int]) -> int: nums = sorted(nums) for i in range(0, len(nums)-1, 1): if nums[i+1] == nums[i]: return nums[i] else: continue
find-the-duplicate-number
Python
vjgadre21
0
61
find the duplicate number
287
0.591
Medium
5,148
https://leetcode.com/problems/find-the-duplicate-number/discuss/2368912/Python-using-Counter-2-Lines
class Solution: def findDuplicate(self, nums: List[int]) -> int: x = Counter(nums) return(max(x, key=x.get))
find-the-duplicate-number
Python using Counter 2 Lines
Yodawgz0
0
61
find the duplicate number
287
0.591
Medium
5,149
https://leetcode.com/problems/find-the-duplicate-number/discuss/2346451/Python-O(n)-time-O(1)-space-easy-solution
class Solution: def findDuplicate(self, nums: List[int]) -> int: # Time: O(n) and Space: O(1) while nums[0] != nums[nums[0]]: nums[nums[0]], nums[0] = nums[0], nums[nums[0]] return nums[0]
find-the-duplicate-number
Python O(n)-time O(1)-space easy solution
DanishKhanbx
0
132
find the duplicate number
287
0.591
Medium
5,150
https://leetcode.com/problems/find-the-duplicate-number/discuss/2323407/Python-Solution
class Solution: def findDuplicate(self, nums: List[int]) -> int: nums.sort() for i in range(len(nums)-1): if nums[i]==nums[i+1]: return nums[i] class Solution: def findDuplicate(self, nums: List[int]) -> int: ref= [0]*len(nums) for n in nums: if ref[n]: return...
find-the-duplicate-number
Python Solution
SakshiMore22
0
67
find the duplicate number
287
0.591
Medium
5,151
https://leetcode.com/problems/find-the-duplicate-number/discuss/2304694/Python-solution-with-descriptive-algorithm-walkthrough
class Solution: def findDuplicate(self, nums: List[int]) -> int: for num in nums: if nums[abs(num)] < 0: return abs(num) nums[abs(num)] *= -1
find-the-duplicate-number
Python solution with descriptive algorithm walkthrough
pawelborkar
0
56
find the duplicate number
287
0.591
Medium
5,152
https://leetcode.com/problems/find-the-duplicate-number/discuss/2294368/Python-t-and-h-approach
class Solution: def findDuplicate(self, nums: List[int]) -> int: t = h = nums[0] while True: t = nums[t] h = nums[nums[h]] if h==t: break t = nums[0] while t!=h: h = nums[h] t = num...
find-the-duplicate-number
Python t & h approach
Brillianttyagi
0
35
find the duplicate number
287
0.591
Medium
5,153
https://leetcode.com/problems/find-the-duplicate-number/discuss/2279705/Find-the-Duplicate-Numbers
class Solution: def findDuplicate(self, nums: List[int]) -> int: count=collections.Counter(nums)`` for key,value in count.items(): if value>1: return key
find-the-duplicate-number
Find the Duplicate Numbers
Faraz369
0
21
find the duplicate number
287
0.591
Medium
5,154
https://leetcode.com/problems/find-the-duplicate-number/discuss/2182225/2-Line-python-Solution
class Solution: def findDuplicate(self, nums: List[int]) -> int: freq = Counter(nums) return freq.most_common(1)[0][0]
find-the-duplicate-number
2 Line python Solution
writemeom
0
173
find the duplicate number
287
0.591
Medium
5,155
https://leetcode.com/problems/game-of-life/discuss/468108/Use-the-tens-digit-as-a-counter-Python-O(1)-Space-O(mn)-Time
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ def is_neighbor(board, i, j): return (0 <= i < len(board)) and (0 <= j < len(board[0])) and board[i][j] % 10 == 1 ...
game-of-life
Use the tens digit as a counter, Python O(1) Space, O(mn) Time
Moooooon
4
104
game of life
289
0.668
Medium
5,156
https://leetcode.com/problems/game-of-life/discuss/1722673/Python-3-O(mn)-time-O(1)-space
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ m, n = len(board), len(board[0]) for i in range(m): for j in range(n): # count number of live neig...
game-of-life
Python 3, O(mn) time, O(1) space
dereky4
3
312
game of life
289
0.668
Medium
5,157
https://leetcode.com/problems/game-of-life/discuss/1822905/Python3-oror-Give-10-min-to-be-a-pro-coder
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ life = [] for i in range(len(board)): col = [] for j in range(len(board[0])): col.append(board[i][j]) ...
game-of-life
Python3 || Give 10 min to be a pro coder
SV_Shriyansh
2
86
game of life
289
0.668
Medium
5,158
https://leetcode.com/problems/game-of-life/discuss/2598106/Python-Easy
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ # original new state # 0 0 0 # 1 1 1 # 0 1 2 # 1 ...
game-of-life
Python - Easy
lokeshsenthilkumar
1
58
game of life
289
0.668
Medium
5,159
https://leetcode.com/problems/game-of-life/discuss/2424762/game-of-Life-oror-Python3-oror-Arrays
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ dirs = [[1, 0], [0, 1], [-1, 0], [0, -1], [1, 1], [-1, -1], [1, -1], [-1, 1]] for i in range(0, len(board)): for j in rang...
game-of-life
game of Life || Python3 || Arrays
vanshika_2507
1
40
game of life
289
0.668
Medium
5,160
https://leetcode.com/problems/game-of-life/discuss/1953459/My-most-efficient-in-place-python-code-that-beats-93-of-solutions.
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ for i in range(len(board)): for j in range(len(board[0])): check=0 try: if j>0 and i>0 ...
game-of-life
My most efficient in-place python code that beats 93% of solutions.
tkdhimanshusingh
1
61
game of life
289
0.668
Medium
5,161
https://leetcode.com/problems/game-of-life/discuss/994050/Beats-97-in-speed-92-in-space
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ # New state indicators # 0 -> 0: 2 # 0 -> 1: 3 # 1 -> 0: 4 # 1 -> 1: 5 m, n = len(board), len(board[0]) direc...
game-of-life
Beats 97% in speed, 92% in space
leonine9
1
22
game of life
289
0.668
Medium
5,162
https://leetcode.com/problems/game-of-life/discuss/2834671/Python-solution
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ row = len(board) col = len(board[0]) change = [] for i in range(row): for j in range(col): ...
game-of-life
Python solution
maomao1010
0
3
game of life
289
0.668
Medium
5,163
https://leetcode.com/problems/game-of-life/discuss/2778726/Python-oror-Hashmap
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: n, m = len(board), len(board[0]) adj = [(-1,-1), (-1,0), (-1,1), (0,1), (1,1), (1,0), (0,-1), (1,-1)] graph = defaultdict(list) for i in range(n): for j in range(m): for r,c in adj: if not( 0 <= i+r < n and 0 <= j+c < m ): cont...
game-of-life
Python || Hashmap
morpheusdurden
0
1
game of life
289
0.668
Medium
5,164
https://leetcode.com/problems/game-of-life/discuss/2774275/Python3-O(1)-Space
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ DIRS = [[-1, -1], [-1, 0], [-1, 1], # top [0, -1], [0, 1], # mid [1, -1], [1, 0], [1, 1]] # bot ROWS, COLS = le...
game-of-life
[Python3] O(1) Space
jonathanbrophy47
0
1
game of life
289
0.668
Medium
5,165
https://leetcode.com/problems/game-of-life/discuss/2754284/Python-3-Easiest-solution-with-explanation
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ prev_board = deepcopy(board) m,n = len(board),len(board[0]) directions = [(1,0),(0,1),(-1,0),(0,-1),(1,-1),(-1,1),(1,1),(-1,-1)] ...
game-of-life
[Python 3] Easiest solution with explanation
shriyansnaik
0
3
game of life
289
0.668
Medium
5,166
https://leetcode.com/problems/game-of-life/discuss/2704034/Python3-One-Pass-Four-Directions-No-extra-Space
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ m = len(board) n = len(board[0]) # go throught the board row by row for rx in range(m): for cx in range(n): ...
game-of-life
[Python3] - One-Pass, Four Directions, No extra Space
Lucew
0
7
game of life
289
0.668
Medium
5,167
https://leetcode.com/problems/game-of-life/discuss/2372417/Python3-or-set-store-(i-j)-to-be-changed
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ m, n = len(board), len(board[0]) dirs = {(0,-1),(0,1),(-1,0),(1,0),(-1,-1),(-1,1),(1,-1),(1,1)} def checkNeighbor(i, j): ...
game-of-life
Python3 | set store (i, j) to be changed
Ploypaphat
0
24
game of life
289
0.668
Medium
5,168
https://leetcode.com/problems/game-of-life/discuss/2290829/Python-Simple-Python-Solution
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ def isValidNeighbour(x, y, board): return x>=0 and x < len(board) and y>=0 and y< len(board[0]) dx = [0,1,1,1,0,-1,-1...
game-of-life
[ Python ] ✅ Simple Python Solution ✅✅
vaibhav0077
0
74
game of life
289
0.668
Medium
5,169
https://leetcode.com/problems/game-of-life/discuss/2173365/python-probably-readable
class Solution: def get_environmental_info(self,coordinates,board): x = coordinates[0] y = coordinates[1] environment = [] for i in range(-1,2): for j in range(-1,2): if i==j and i==0: continue new_x = x+j ...
game-of-life
python probably readable
RionelKuster
0
56
game of life
289
0.668
Medium
5,170
https://leetcode.com/problems/game-of-life/discuss/1944079/Python-O(1)-space
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ R, C = len(board), len(board[0]) for i in range(R): for j in range(C): a = 0 ...
game-of-life
[Python] O(1) space
Priyansh1210
0
33
game of life
289
0.668
Medium
5,171
https://leetcode.com/problems/game-of-life/discuss/1941087/Python-Solution-or-Faster-Than-95.48
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ rows = len(board) cols = len(board[0]) sumBoard = [] sumBoard.insert(0, [0] * (cols+2)) for i in rang...
game-of-life
Python Solution | Faster Than 95.48%
harshnavingupta
0
35
game of life
289
0.668
Medium
5,172
https://leetcode.com/problems/game-of-life/discuss/1941016/Time-%3A-O(m*n)-or-Space-%3A-O(1)-or-Simple-Solution
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ # 1 -> 0 = -1 ( consider while neighbour counting one's) # 0 -> 1 = -2 (don't consider while neighbour counting one's for row in range(len(board)): for colum...
game-of-life
Time : O(m*n) | Space : O(1) | Simple Solution
Call-Me-AJ
0
20
game of life
289
0.668
Medium
5,173
https://leetcode.com/problems/game-of-life/discuss/1940806/python-O(n2)-Runtime%3A-faster-than-98.88-Memory-Usage%3A-less-than-93.42
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ width = len(board[0]) height = len(board) temp = [[0 for x in range(width)] for y in range(height)] for i in range(height): ...
game-of-life
python O(n^2) ✅ Runtime: faster than 98.88% Memory Usage: less than 93.42%
caneryikar
0
15
game of life
289
0.668
Medium
5,174
https://leetcode.com/problems/game-of-life/discuss/1940461/Python-Count-the-neighbors-and-use-the-second-decimal-point-for-storing-in-the-cell.
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ NEIGHBORING_CELLS = ( (-1, -1), (-1, 0), (-1, +1), (0, -1), ...
game-of-life
Python, Count the neighbors and use the second decimal point for storing in the cell.
sEzio
0
18
game of life
289
0.668
Medium
5,175
https://leetcode.com/problems/game-of-life/discuss/1939825/Python-Over-Complicated-stuff-LOL-or-O(1)-space
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ # 11 => alive in initial, alive in next # 10 => alive in initial, dead in next # 0 => dead in both # 13 => dead in initial, al...
game-of-life
✅ Python Over-Complicated stuff LOL | O(1) space
dhananjay79
0
42
game of life
289
0.668
Medium
5,176
https://leetcode.com/problems/game-of-life/discuss/1939418/Clean-Python-O(1)-space-modulo-arithmetic-for-state-change
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ nrows = len(board) ncols = len(board[0]) # helper function to count alive neighbors def countNeighbors(row,col): ...
game-of-life
Clean Python, O(1) space, modulo arithmetic for state change
boris17
0
27
game of life
289
0.668
Medium
5,177
https://leetcode.com/problems/game-of-life/discuss/1939342/Python3-code-beating-95-in-time-use-two-new-values-to-reflect-the-change-between-0-and-1
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. Use -1 to reflect change from 0 to 1 &amp; use -2 to reflect change from 1 to 0. Change from 0 to 0 and 1 to 1 do not need new flag as the value keeps the same. ...
game-of-life
Python3 code beating 95% in time - use two new values to reflect the change between 0 & 1
leonine9
0
10
game of life
289
0.668
Medium
5,178
https://leetcode.com/problems/game-of-life/discuss/1938882/BRANCHLESS-SOLUTION-(no-if-statements)-Python-O(1)-Space-O(m*n)-time
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. 1 becoming a 0 will be temporary a 3. 0 becoming a 1 will be temporary a 2. This is so that we can use modulo 2 to see if a cell was origi...
game-of-life
BRANCHLESS SOLUTION (no if statements), Python, O(1) Space, O(m*n) time
stevenhgs
0
34
game of life
289
0.668
Medium
5,179
https://leetcode.com/problems/game-of-life/discuss/1938377/Python3-Solution
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ directions = [(1,0), (1,-1), (0,-1), (-1,-1), (-1,0), (-1,1), (0,1), (1,1)] for i in range(len(board)): for j in range(len...
game-of-life
Python3 Solution
nomanaasif9
0
62
game of life
289
0.668
Medium
5,180
https://leetcode.com/problems/game-of-life/discuss/1938097/Python3-oror-two-solutions%3A-hashset-and-in-place
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ rows = len(board) columns = len(board[0]) def count_live_neighbours(x, y): neighbors = [(1, 0), (0, 1), (...
game-of-life
Python3 || two solutions: hashset and in-place
constantine786
0
34
game of life
289
0.668
Medium
5,181
https://leetcode.com/problems/game-of-life/discuss/1938097/Python3-oror-two-solutions%3A-hashset-and-in-place
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ rows = len(board) columns = len(board[0]) def count_live_neighbours(x, y): neighbors = [(1, 0), (0, 1), (...
game-of-life
Python3 || two solutions: hashset and in-place
constantine786
0
34
game of life
289
0.668
Medium
5,182
https://leetcode.com/problems/game-of-life/discuss/1938065/Python-Solution
class Solution: def get(self, i, j, x=1): if x == -1 and i - 1 >= 0 or x == 1 and self.h - 1 > i: if self.w - 1 > j > 0: return sum(self.arr[i + x][j + k] for k in [-1, 0, 1]) elif self.w - 1 > j >= 0: return self.arr[i + x][j] + self.arr[i + x][j + 1]...
game-of-life
Python Solution
hgalytoby
0
48
game of life
289
0.668
Medium
5,183
https://leetcode.com/problems/game-of-life/discuss/1938000/Python-Simple-and-Easy-Solution-oror-O(m*n)-time-complexity
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ rows ,cols = len(board), len(board[0]) def countNei(r, c): nei = 0 for i in range(r-1, r+2): f...
game-of-life
Python - Simple & Easy Solution || O(m*n) time complexity
dayaniravi123
0
16
game of life
289
0.668
Medium
5,184
https://leetcode.com/problems/game-of-life/discuss/1937962/My-solution-(slow-though)
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ def get_neighbours(board, row, col): neighbours = [] if col < len(board[row])-1: neighbours.append(boa...
game-of-life
My solution (slow though)
SharvariGC
0
9
game of life
289
0.668
Medium
5,185
https://leetcode.com/problems/game-of-life/discuss/1937943/First-timer-it-is-not-much-but-it-is-honest-work
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ m = len(board) n = len(board[0]) board_copy = copy.deepcopy(board) for i, row in enumerate(board_copy): ...
game-of-life
First timer, it is not much but it is honest work
Maxsis
0
11
game of life
289
0.668
Medium
5,186
https://leetcode.com/problems/game-of-life/discuss/1937890/Python3-Javascript%3A-An-Average-Solution-O(mn)-Time-and-Space
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ m = len(board) n = len(board[0]) neighbors = {} for i in range(-1, m + 1): for j in range(-1, n + 1): nei...
game-of-life
Python3, Javascript: An Average Solution, O(mn) Time and Space
bpfaust
0
17
game of life
289
0.668
Medium
5,187
https://leetcode.com/problems/game-of-life/discuss/1937869/python3-order(m*n)
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ cb = [x.copy() for x in board] dir = [] for i in [-1,0,1]: for j in [-1,0,1]: if not (i == 0 and j == 0): ...
game-of-life
python3 order(m*n)
user2613C
0
7
game of life
289
0.668
Medium
5,188
https://leetcode.com/problems/game-of-life/discuss/1864969/python-or-Beats-99.63-for-Time-or-Not-a-concise
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ m,n = len(board),len(board[0]) def dfs(r,c): current = board[r][c] res = 0 rc_list = [(r-...
game-of-life
python | Beats 99.63% for Time | Not a concise
iamskd03
0
47
game of life
289
0.668
Medium
5,189
https://leetcode.com/problems/game-of-life/discuss/1857159/All-aproachor-M*N-greater-1-spaceor-Python3-or-Game-Of-life
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ rows = len(board) cols = len(board[0]) neighbors = [(1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1)] copy_...
game-of-life
All aproach| M*N --> 1 space| Python3 | Game Of life
Adee_19
0
29
game of life
289
0.668
Medium
5,190
https://leetcode.com/problems/game-of-life/discuss/1857159/All-aproachor-M*N-greater-1-spaceor-Python3-or-Game-Of-life
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ rows = len(board) cols = len(board[0]) neighbors = [(1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1)] for r...
game-of-life
All aproach| M*N --> 1 space| Python3 | Game Of life
Adee_19
0
29
game of life
289
0.668
Medium
5,191
https://leetcode.com/problems/game-of-life/discuss/1737171/Python3-In-place-solution-with-comments
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: for i in range(len(board)): for j in range(len(board[i])): l_cnt = 0 # if we can look at up if i > 0: if abs(board[i - 1][j]) == 1: ...
game-of-life
[Python3] In-place solution with comments
maosipov11
0
118
game of life
289
0.668
Medium
5,192
https://leetcode.com/problems/game-of-life/discuss/1683279/Python3-Runtime-around-50ms
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ w, h = len(board[0]), len(board) lives = [] deads = [] for i in range(h): for j in range(w): neighb...
game-of-life
Python3 - Runtime around 50ms
elainefaith0314
0
94
game of life
289
0.668
Medium
5,193
https://leetcode.com/problems/game-of-life/discuss/1606252/Python-sol-faster-than-94-less-mem-than-70-(iter-sol)
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ changes = [] rows = len(board) cols = len(board[0]) for i in range(rows): for j in range(cols): one...
game-of-life
Python sol faster than 94% , less mem than 70% (iter sol)
elayan
0
138
game of life
289
0.668
Medium
5,194
https://leetcode.com/problems/game-of-life/discuss/1420477/Python3-Simple-readable-solution-with-comments.-No-fancy-algo-used.
class Solution: # Check if a cell is inside the given environment def withinBoundry(self, m: int, n: int, row: int, col: int) -> bool: return (0 <= row < m) and (0 <= col < n) # Find number of live neighbors in all 8 directions of the current cell def scanNeighbors(self, board: List[Li...
game-of-life
[Python3] Simple readable solution with comments. No fancy algo used.
ssshukla26
0
98
game of life
289
0.668
Medium
5,195
https://leetcode.com/problems/game-of-life/discuss/1410055/Python-O(3n)-Memory-Solution
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: m, n = len(board), len(board[0]) dirs = [(0, 1), (1, 0), (0, -1), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] current = [[0] * (n + 2), [0] * (n + 2), [0] + board[0] + [0]] board.append([0] * n) for i in r...
game-of-life
Python O(3n) Memory Solution
yiseboge
0
138
game of life
289
0.668
Medium
5,196
https://leetcode.com/problems/game-of-life/discuss/1347991/Python3-Modify-In-Place
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ for i in range(len(board)): for j in range(len(board[0])): countOfOnes = 0 for x, y in [(-1, 1), (-1, 0), (...
game-of-life
Python3 Modify In Place
zhanz1
0
76
game of life
289
0.668
Medium
5,197
https://leetcode.com/problems/game-of-life/discuss/1306686/Python-easy-solution-77.24-faster
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ r = len(board) c = len(board[0]) dr = [-1,+1,0,0,-1,-1,+1,+1] dc = [0,0,-1,+1,-1,+1,+1,-1] for i in range(r): rr = 0 cc = 0 for j in range(c): if board[i][...
game-of-life
Python easy solution, 77.24% faster
Vrushali20
0
82
game of life
289
0.668
Medium
5,198
https://leetcode.com/problems/game-of-life/discuss/1235795/Python-easy-understanding-faster-than-90
class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ m = len(board) # get number of rows n = len(board[0]) # get number of columns for i in range(m): for j in range(n): ...
game-of-life
Python easy understanding faster than 90%
NagaVenkatesh
0
198
game of life
289
0.668
Medium
5,199