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/reverse-bits/discuss/1623974/Python-Solution-or-String-Manipulation-or-Bit-Manipulation-or-O(n)-or-90-Faster
class Solution: def reverseBits(self, n: int) -> int: reverse_binary = "" while n > 0: reverse_binary += str(n%2) n = n//2 for i in range(0, 32-len(reverse_binary)): reverse_binary += "0" return int(reverse_binary, 2)
reverse-bits
Python Solution | String Manipulation | Bit Manipulation | O(n) | 90% Faster
avi-arora
0
106
reverse bits
190
0.525
Easy
3,100
https://leetcode.com/problems/reverse-bits/discuss/1623974/Python-Solution-or-String-Manipulation-or-Bit-Manipulation-or-O(n)-or-90-Faster
class Solution: def reverseBits(self, n: int) -> int: reverse_binary, count = 0, 0 while n > 0: reverse_binary = (reverse_binary << 1) | (n&amp;1) n, count = n >> 1, count+1 return reverse_binary << 32-count
reverse-bits
Python Solution | String Manipulation | Bit Manipulation | O(n) | 90% Faster
avi-arora
0
106
reverse bits
190
0.525
Easy
3,101
https://leetcode.com/problems/reverse-bits/discuss/1442747/Python3-One-Line-Faster-Than-99.56
class Solution: def reverseBits(self, n: int) -> int: return int((bin(n)[2:].zfill(32))[::-1], 2)
reverse-bits
Python3 One-Line Faster Than 99.56%
Hejita
0
88
reverse bits
190
0.525
Easy
3,102
https://leetcode.com/problems/reverse-bits/discuss/1238619/Python3-one-liner-wo-explications
class Solution: def reverseBits(self, n: int) -> int: return int(str(bin(n)[2:]).rjust(32,'0')[::-1], 2)
reverse-bits
Python3 one-liner w/o explications
S-c-r-a-t-c-h-y
0
68
reverse bits
190
0.525
Easy
3,103
https://leetcode.com/problems/number-of-1-bits/discuss/2074152/Easy-O(1)-Space-PythonC%2B%2B
class Solution: def hammingWeight(self, n: int) -> int: return sum((n &amp; (1<<i))!=0 for i in range(32))
number-of-1-bits
Easy O(1) Space - Python/C++
constantine786
21
2,800
number of 1 bits
191
0.65
Easy
3,104
https://leetcode.com/problems/number-of-1-bits/discuss/2074152/Easy-O(1)-Space-PythonC%2B%2B
class Solution: def hammingWeight(self, n: int) -> int: cnt = 0 while n : cnt+=1 n = n &amp; (n-1) return cnt
number-of-1-bits
Easy O(1) Space - Python/C++
constantine786
21
2,800
number of 1 bits
191
0.65
Easy
3,105
https://leetcode.com/problems/number-of-1-bits/discuss/1044847/Python.-Simple-and-easy-solution.
class Solution: def hammingWeight(self, n: int) -> int: ans = 0 while n: ans += n % 2 n = n >> 1 return ans
number-of-1-bits
Python. Simple & easy solution.
m-d-f
10
1,800
number of 1 bits
191
0.65
Easy
3,106
https://leetcode.com/problems/number-of-1-bits/discuss/377550/Python-multiple-solutions-including-O(1)
class Solution(object): def hammingWeight(self, n): mask_sum_2bit = 0x55555555 mask_sum_4bit = 0x33333333 mask_sum_8bit = 0x0F0F0F0F mask_sum_16bit = 0x00FF00FF mask_sum_32bit = 0x0000FFFF n = (n &amp; mask_sum_2bit) + ((n >> 1) &amp; mask_sum_2bit) n = (n &amp; mask_sum_4bit) + ((n >> 2) &amp; mask_sum_4bit) n = (n &amp; mask_sum_8bit) + ((n >> 4) &amp; mask_sum_8bit) n = (n &amp; mask_sum_16bit) + ((n >> 8) &amp; mask_sum_16bit) n = (n &amp; mask_sum_32bit) + ((n >> 16) &amp; mask_sum_32bit) return n
number-of-1-bits
Python multiple solutions including O(1)
amchoukir
10
1,100
number of 1 bits
191
0.65
Easy
3,107
https://leetcode.com/problems/number-of-1-bits/discuss/377550/Python-multiple-solutions-including-O(1)
class Solution(object): def hammingWeight(self, n): count = 0 while n: count += 1 n = n &amp; (n - 1) return count
number-of-1-bits
Python multiple solutions including O(1)
amchoukir
10
1,100
number of 1 bits
191
0.65
Easy
3,108
https://leetcode.com/problems/number-of-1-bits/discuss/2215997/Python3-simple-solution%3A-Do-nand(n-1)
class Solution: def hammingWeight(self, n: int) -> int: result = 0 while n: n = n &amp; (n-1) result+=1 return result
number-of-1-bits
πŸ“Œ Python3 simple solution: Do n&(n-1)
Dark_wolf_jss
6
149
number of 1 bits
191
0.65
Easy
3,109
https://leetcode.com/problems/number-of-1-bits/discuss/1304131/Easy-Python-Solution(95.81)
class Solution: def hammingWeight(self, n: int) -> int: c=0 while n: if(n&amp;1): c+=1 n=n>>1 return c
number-of-1-bits
Easy Python Solution(95.81%)
Sneh17029
5
915
number of 1 bits
191
0.65
Easy
3,110
https://leetcode.com/problems/number-of-1-bits/discuss/890389/Python-simple-solution-using-bit-operations
class Solution: def hammingWeight(self, n: int) -> int: res = 0 while n: res += n &amp; 1 n >>= 1 return res
number-of-1-bits
Python simple solution using bit operations
stom1407
5
561
number of 1 bits
191
0.65
Easy
3,111
https://leetcode.com/problems/number-of-1-bits/discuss/1791085/Python-3-(40ms)-or-BIT-AND-Formula-(nandn(n-1))-Solution-or-Easy-to-Understand
class Solution: def hammingWeight(self, n: int) -> int: c=0 while n: n=n&amp;(n-1) c+=1 return c
number-of-1-bits
Python 3 (40ms) | BIT AND Formula (n&n(n-1)) Solution | Easy to Understand
MrShobhit
3
297
number of 1 bits
191
0.65
Easy
3,112
https://leetcode.com/problems/number-of-1-bits/discuss/1543854/Python-bits-%2B-explanation
class Solution: def hammingWeight(self, n: int) -> int: count = 0 while n: curr = n &amp; 1 count += curr n = n >> 1 return count
number-of-1-bits
Python bits + explanation
SleeplessChallenger
3
277
number of 1 bits
191
0.65
Easy
3,113
https://leetcode.com/problems/number-of-1-bits/discuss/2658811/Python-or-1-liner-string-solution
class Solution: def hammingWeight(self, n: int) -> int: return str(bin(n))[2:].count('1')
number-of-1-bits
Python | 1-liner string solution
LordVader1
2
291
number of 1 bits
191
0.65
Easy
3,114
https://leetcode.com/problems/number-of-1-bits/discuss/2645553/Useing-bin()
class Solution: def hammingWeight(self, n: int) -> int: x = str(bin(n)) output_ = 0 for i in x: if i == '1': output_ += 1 return output_
number-of-1-bits
Useing bin()
sanjeevpathak
2
16
number of 1 bits
191
0.65
Easy
3,115
https://leetcode.com/problems/number-of-1-bits/discuss/2074519/O(log(n))-Divide-by-2
class Solution: def hammingWeight(self, n: int) -> int count = 0 while n > 0: count += n % 2 n //= 2 return count
number-of-1-bits
O(log(n)) Divide by 2
Eba472
2
124
number of 1 bits
191
0.65
Easy
3,116
https://leetcode.com/problems/number-of-1-bits/discuss/1643854/Simple-Python3-Solution-or-Beginner-friendly
class Solution: def hammingWeight(self, n: int) -> int: count = 0 # Count variable to maintain the number of '1' bits while n > 0: # Add the last bit i.e. either 1 or 0 to the counter count += n % 2 # Divide the number by 2, same as shift right by 1 (n = n >> 1) # Then we obtain the bit in the next position iteratively n = n // 2 return count
number-of-1-bits
Simple Python3 Solution | Beginner friendly
Sparkles4
2
225
number of 1 bits
191
0.65
Easy
3,117
https://leetcode.com/problems/number-of-1-bits/discuss/1266260/Python3-Simple-Solution
class Solution: def hammingWeight(self, n: int) -> int: num = 0 while n: if n &amp; 1: num += 1 n >>= 1 return num
number-of-1-bits
Python3 Simple Solution
yeefun
2
293
number of 1 bits
191
0.65
Easy
3,118
https://leetcode.com/problems/number-of-1-bits/discuss/2434721/C%2B%2BPython-Optimized-Bits-shift-approach
class Solution: def hammingWeight(self, n: int) -> int: count = 0 while n>0: if (n&amp;1)>0: count=count+1 n=n>>1 return count
number-of-1-bits
C++/Python Optimized Bits shift approach
arpit3043
1
103
number of 1 bits
191
0.65
Easy
3,119
https://leetcode.com/problems/number-of-1-bits/discuss/2377012/Python-O(no-of-1-bits)-Efficient-Solution
class Solution: def hammingWeight(self, n: int) -> int: res = 0 while n: n = n &amp; (n - 1)#Removing no of 1 bits only res += 1 return res
number-of-1-bits
Python O(no of 1 bits) Efficient Solution
soumyadexter7
1
123
number of 1 bits
191
0.65
Easy
3,120
https://leetcode.com/problems/number-of-1-bits/discuss/2126514/Memory-Usage%3A-13.8-MB-less-than-94.79-of-Python3
class Solution: def hammingWeight(self, n: int) -> int: return str(bin(n)).count('1')
number-of-1-bits
Memory Usage: 13.8 MB, less than 94.79% of Python3
writemeom
1
241
number of 1 bits
191
0.65
Easy
3,121
https://leetcode.com/problems/number-of-1-bits/discuss/2089879/Python3-O(1)-oror-O(1)
class Solution: def hammingWeight(self, n: int) -> int: return self.optimalSol(n) return self.bruteForce(n) return self.builtInMethod(n) # O(1) || O(1) # runtime: 35ms 81.75% memory: 13.9mb 49.97% def optimalSol(self, n): if not n: return 0 count = 0 while n > 0: count += 1 if n % 2 == 1 else 0 n >>= 1 return count # O(n) || O(1) # runtime: 42ms 58.55%; memory" 13.9mb 7.77% def bruteForce(self, n): count = 0 while n > 0: count += 1 if n % 2 == 1 else 0 n //= 2 return count def builtInMethod(self, n): return bin(n)[2:].count('1')
number-of-1-bits
Python3 O(1) || O(1)
arshergon
1
232
number of 1 bits
191
0.65
Easy
3,122
https://leetcode.com/problems/number-of-1-bits/discuss/1851717/Number-Of-1-Bits-easy-solution
class Solution: def hammingWeight(self, n: int) -> int: return bin(n).count('1')
number-of-1-bits
Number Of 1 Bits - easy solution
jenil_095
1
169
number of 1 bits
191
0.65
Easy
3,123
https://leetcode.com/problems/number-of-1-bits/discuss/1823320/Python-simple-solution
class Solution(object): def hammingWeight(self, n): count = 0 while n: count += n &amp; 1 n >>= 1 return count
number-of-1-bits
Python - simple solution
domthedeveloper
1
166
number of 1 bits
191
0.65
Easy
3,124
https://leetcode.com/problems/number-of-1-bits/discuss/1774658/Python-One-Line-Linear-Solution-By-Counting-One-in-Binary-String
class Solution: def hammingWeight(self, n: int) -> int: return bin(n)[2:].count('1')
number-of-1-bits
[ Python ] βœ”βœ” One Line Linear Solution By Counting One in Binary String πŸ”₯✌
ASHOK_KUMAR_MEGHVANSHI
1
266
number of 1 bits
191
0.65
Easy
3,125
https://leetcode.com/problems/number-of-1-bits/discuss/1695038/Python-one-line-solution-very-simple
class Solution: def hammingWeight(self, n: int) -> int: return bin(n).count('1')
number-of-1-bits
Python one-line-solution very simple
chandanasamineni23
1
129
number of 1 bits
191
0.65
Easy
3,126
https://leetcode.com/problems/number-of-1-bits/discuss/1394827/Python-Easy-Solution-Right-shift
class Solution: def hammingWeight(self, n: int) -> int: c=0 while(n): if n%2==1: c+=1 n=n>>1 return c
number-of-1-bits
Python Easy Solution Right shift
harshmalviya7
1
177
number of 1 bits
191
0.65
Easy
3,127
https://leetcode.com/problems/number-of-1-bits/discuss/2847124/python-solution-with-time-O(1)-space-O(1)-using-bit-manipulation
class Solution: def hammingWeight(self, n: int) -> int: t=1 count=0 for i in range(0 , 32): # print('n&amp;t' , n&amp;t) if (n&amp;t!=0): count+=1 t=t<<1 return count
number-of-1-bits
python solution with time O(1) , space O(1) using bit manipulation
sintin1310
0
1
number of 1 bits
191
0.65
Easy
3,128
https://leetcode.com/problems/number-of-1-bits/discuss/2840071/four-python-solution-.-bit-recursive-iterative-dummy
class Solution: def hammingWeight(self, n: int) -> int: # first solution :) # return list(bin(n)[2:]).count('1') # second solution # cnt = 0 # while n > 0: # if n%2 == 1 : # cnt+=1 # n //= 2 #return cnt # third solution #if n == 0: # return 0 #if n == 1 : # return 1 #else : # return (1 if n%2 == 1 else 0 ) + self.hammingWeight(n//2) #''' # fourth solution b = 0 m = 1 for i in range(32): if m &amp; n != 0 : b +=1 m<<=1 return b
number-of-1-bits
four python solution . bit / recursive / iterative / dummy
Cosmodude
0
1
number of 1 bits
191
0.65
Easy
3,129
https://leetcode.com/problems/number-of-1-bits/discuss/2836497/Pythonor-By-Operators
class Solution: def hammingWeight(self, n: int) -> int: i = 0 while n!= 0: n = n&amp;(n-1) i += 1 return i
number-of-1-bits
Python| By Operators
lucy_sea
0
4
number of 1 bits
191
0.65
Easy
3,130
https://leetcode.com/problems/number-of-1-bits/discuss/2834323/Bit-calculation
class Solution: def hammingWeight(self, n: int) -> int: res = 0 while n != 0: n = n &amp; (n - 1) res += 1 return res
number-of-1-bits
Bit calculation
lillllllllly
0
1
number of 1 bits
191
0.65
Easy
3,131
https://leetcode.com/problems/number-of-1-bits/discuss/2827848/Python-One-Liner
class Solution: def hammingWeight(self, n: int) -> int: return bin(n).count('1')
number-of-1-bits
Python One Liner
ShauryaGopu
0
3
number of 1 bits
191
0.65
Easy
3,132
https://leetcode.com/problems/number-of-1-bits/discuss/2819452/Python-function-n.bit_count()
class Solution: def hammingWeight(self, n: int) -> int: return n.bit_count()
number-of-1-bits
Python function n.bit_count()
JakhongirMurodov
0
2
number of 1 bits
191
0.65
Easy
3,133
https://leetcode.com/problems/number-of-1-bits/discuss/2781810/Easy-to-Understand-Python-Solution!
class Solution: def hammingWeight(self, n: int) -> int: one_count = 0 bits = f'{n:032b}' for bit in bits: if bit == '1': one_count += 1 return one_count
number-of-1-bits
Easy to Understand Python Solution!
shanemmay
0
3
number of 1 bits
191
0.65
Easy
3,134
https://leetcode.com/problems/number-of-1-bits/discuss/2774158/python-easy-soution
class Solution: def hammingWeight(self, n: int) -> int: c=0 while n: n=n&amp;(n-1) c=c+1 return c
number-of-1-bits
python easy so;ution
dummu_chandini
0
2
number of 1 bits
191
0.65
Easy
3,135
https://leetcode.com/problems/number-of-1-bits/discuss/2774155/python-easy-solution
class Solution: def hammingWeight(self, n: int) -> int: c=0 while n: n=n&amp;(n-1) c=c+1 return c
number-of-1-bits
python easy solution
dummu_chandini
0
3
number of 1 bits
191
0.65
Easy
3,136
https://leetcode.com/problems/number-of-1-bits/discuss/2766125/Python3%3A-Easy-to-understand-solution
class Solution: def hammingWeight(self, n: int) -> int: # convert int to binary string s = str(bin(n).replace("0b","")) while len(s) != 32: s = '0' + s # count number of 1's in this string count = 0 for i, char in enumerate(s): if char == '1': count += 1 # return count return count
number-of-1-bits
Python3: Easy to understand solution
sourav_ravish
0
5
number of 1 bits
191
0.65
Easy
3,137
https://leetcode.com/problems/number-of-1-bits/discuss/2761271/Simple-Bitwise-Python3-*beats-91*
class Solution: def hammingWeight(self, n: int) -> int: c = 0 while n > 0: if n &amp; 1: c += 1 n = n>>1 return c
number-of-1-bits
Simple Bitwise Python3 *beats 91%*
allisone4
0
5
number of 1 bits
191
0.65
Easy
3,138
https://leetcode.com/problems/number-of-1-bits/discuss/2756973/Python3-solution-Single-line
class Solution: def hammingWeight(self, n: int) -> int: return str(bin(n)).count('1')
number-of-1-bits
Python3 solution - Single line
NikhileshNanduri
0
3
number of 1 bits
191
0.65
Easy
3,139
https://leetcode.com/problems/number-of-1-bits/discuss/2751125/Simple-One-Liner-Python3-Solution-using-bin-and-count-function
class Solution: def hammingWeight(self, n: int) -> int: return bin(n)[2:].count("1")
number-of-1-bits
Simple One Liner Python3 Solution using bin and count function
vivekrajyaguru
0
6
number of 1 bits
191
0.65
Easy
3,140
https://leetcode.com/problems/number-of-1-bits/discuss/2751116/Simple-Pyhon3-Solution-using-Bin-function-and-for-loop
class Solution: def hammingWeight(self, n: int) -> int: result = 0 n = bin(n)[2:] for i in str(n): if i == '1': result += 1 return result
number-of-1-bits
Simple Pyhon3 Solution using Bin function and for loop
vivekrajyaguru
0
2
number of 1 bits
191
0.65
Easy
3,141
https://leetcode.com/problems/number-of-1-bits/discuss/2716031/Easy-and-Fast-Bit-Manipulation-Solution
class Solution: def hammingWeight(self, n: int) -> int: ones = 0 for i in range(32): if (n >> i) &amp; 1 == 1: ones += 1 return ones
number-of-1-bits
Easy & Fast Bit Manipulation Solution
user6770yv
0
5
number of 1 bits
191
0.65
Easy
3,142
https://leetcode.com/problems/number-of-1-bits/discuss/2694585/Easy-solution
class Solution: def hammingWeight(self, n: int) -> int: res = 0 while n: if n &amp; 1: res += 1 n >>= 1 return res
number-of-1-bits
Easy solution
alperthod
0
6
number of 1 bits
191
0.65
Easy
3,143
https://leetcode.com/problems/number-of-1-bits/discuss/2680013/Python-1-line-solution-beats-97.45
class Solution: def hammingWeight(self, n: int) -> int: return bin(n).count('1')
number-of-1-bits
Python 1 line solution beats 97.45%
trickycat10
0
70
number of 1 bits
191
0.65
Easy
3,144
https://leetcode.com/problems/number-of-1-bits/discuss/2630112/One-line-solution-in-Python
class Solution: def hammingWeight(self, n: int) -> int: return sum((n >> i) &amp; 1 for i in range(32))
number-of-1-bits
One line solution in Python
metaphysicalist
0
29
number of 1 bits
191
0.65
Easy
3,145
https://leetcode.com/problems/number-of-1-bits/discuss/2622366/Python-1-Liner-or-Simple
class Solution: def hammingWeight(self, n: int) -> int: return str(bin(n)).count('1')
number-of-1-bits
Python 1 Liner | Simple
sharondev
0
15
number of 1 bits
191
0.65
Easy
3,146
https://leetcode.com/problems/number-of-1-bits/discuss/2562091/Python-one-liner-with-explanation
class Solution: def hammingWeight(self, n: int) -> int: return f"{n: b}".count("1")
number-of-1-bits
Python one-liner with explanation
ComputerWiz
0
186
number of 1 bits
191
0.65
Easy
3,147
https://leetcode.com/problems/number-of-1-bits/discuss/2562091/Python-one-liner-with-explanation
class Solution: def hammingWeight(self, n: int) -> int: return len([c for c in f"{n: b}" if c=="1"])
number-of-1-bits
Python one-liner with explanation
ComputerWiz
0
186
number of 1 bits
191
0.65
Easy
3,148
https://leetcode.com/problems/house-robber/discuss/378700/Python-multiple-solutions
class Solution: def __init__(self): self.cache = {} def rob_rec(self, nums, start): if start >= len(nums): return 0 if start in self.cache: return self.cache[start] self.cache[start] = nums[start] + max(self.rob_rec(nums, start+2), self.rob_rec(nums, start+3)) return self.cache[start] def rob(self, nums: List[int]) -> int: return max(self.rob_rec(nums, 0), self.rob_rec(nums, 1))
house-robber
Python multiple solutions
amchoukir
23
2,500
house robber
198
0.488
Medium
3,149
https://leetcode.com/problems/house-robber/discuss/378700/Python-multiple-solutions
class Solution: def __init__(self): self.cache = {} def rob_rec(self, nums, current): if current < 0: # Beyond array boundary return 0 try: return self.cache[current] except: self.cache[current] = max(self.rob_rec(nums, current - 1), nums[current] + self.rob_rec(nums, current - 2)) return self.cache[current] def rob(self, nums: List[int]) -> int: return self.rob_rec(nums, len(nums) - 1)
house-robber
Python multiple solutions
amchoukir
23
2,500
house robber
198
0.488
Medium
3,150
https://leetcode.com/problems/house-robber/discuss/378700/Python-multiple-solutions
class Solution: def rob(self, nums: List[int]) -> int: house_1 = 0 house_2 = 0 house_3 = 0 for num in reversed(nums): temp = house_1 house_1 = max(num + house_2, num + house_3) house_3 = house_2 house_2 = temp return max(house_1, house_2)
house-robber
Python multiple solutions
amchoukir
23
2,500
house robber
198
0.488
Medium
3,151
https://leetcode.com/problems/house-robber/discuss/378700/Python-multiple-solutions
class Solution: def rob(self, nums: List[int]) -> int: prev1, prev2 = 0, 0 for num in nums: prev1, prev2 = max(prev2 + num, prev1), prev1 return prev1
house-robber
Python multiple solutions
amchoukir
23
2,500
house robber
198
0.488
Medium
3,152
https://leetcode.com/problems/house-robber/discuss/1605193/With-explanation-DP-with-O(L)-time-and-O(1)-space-in-Python
class Solution: def rob(self, nums: List[int]) -> int: #edge case if len(nums) <= 2: return max(nums) #dp L = len(nums) dp = [0 for _ in range(L)] dp[0], dp[1] = nums[0], max(nums[0], nums[1]) for i in range(2, L): dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]) return dp[-1]
house-robber
[With explanation] DP with O(L) time and O(1) space in Python
kryuki
11
729
house robber
198
0.488
Medium
3,153
https://leetcode.com/problems/house-robber/discuss/1605193/With-explanation-DP-with-O(L)-time-and-O(1)-space-in-Python
class Solution: def rob(self, nums: List[int]) -> int: #edge case if len(nums) <= 2: return max(nums) #dp (less space) L = len(nums) a, b = nums[0], max(nums[0], nums[1]) for i in range(2, L): a, b = b, max(a + nums[i], b) return b
house-robber
[With explanation] DP with O(L) time and O(1) space in Python
kryuki
11
729
house robber
198
0.488
Medium
3,154
https://leetcode.com/problems/house-robber/discuss/2818729/Python3-or-Four-lines-w-Explanation!-or-Faster-than-96.97-or-Dynamic-Programming
class Solution: def rob(self, nums: List[int]) -> int: bag = (0, 0) for house in nums: bag = (bag[1], max(bag[0] + house, bag[1])) return bag[1]
house-robber
Python3 | Four lines w/ Explanation! | Faster than 96.97% | Dynamic Programming
thomwebb
6
175
house robber
198
0.488
Medium
3,155
https://leetcode.com/problems/house-robber/discuss/1791061/Python-3-(50ms)-or-3-Lines-DP-Formula
class Solution: def rob(self, nums: List[int]) -> int: last, now = 0, 0 for i in nums: last, now = now, max(last + i, now) return now
house-robber
Python 3 (50ms) | 3 Lines DP Formula
MrShobhit
6
435
house robber
198
0.488
Medium
3,156
https://leetcode.com/problems/house-robber/discuss/1579854/Python-or-From-Recursion-to-Top-Down-to-Bottom-Up
class Solution: def rob(self, nums: List[int]) -> int: if len(nums) <= 2: return max(nums) return self.robMax(nums, len(nums) - 1) # start from the last/top def robMax(self, nums, i): if i < 0: return 0 # when i < 0 we just have to return 0 return max(nums[i] + self.robMax(nums, i - 2), self.robMax(nums, i - 1))
house-robber
Python | From Recursion to Top-Down to Bottom-Up
GigaMoksh
5
337
house robber
198
0.488
Medium
3,157
https://leetcode.com/problems/house-robber/discuss/1579854/Python-or-From-Recursion-to-Top-Down-to-Bottom-Up
class Solution: def rob(self, nums: List[int]) -> int: if len(nums) <= 2: return max(nums) dp = [-1 for _ in range(len(nums))] # cache return self.robMax(nums, dp, len(nums) - 1) def robMax(self, nums, dp, i): if i < 0: return 0 if dp[i] != -1: # if the value of dp[i] is not default i.e. -1 that means we have already calculated it so we dont have to do it again we just have to return it return dp[i] dp[i] = max(nums[i] + self.robMax(nums, dp, i - 2), self.robMax(nums, dp, i - 1)) return dp[i]
house-robber
Python | From Recursion to Top-Down to Bottom-Up
GigaMoksh
5
337
house robber
198
0.488
Medium
3,158
https://leetcode.com/problems/house-robber/discuss/1579854/Python-or-From-Recursion-to-Top-Down-to-Bottom-Up
class Solution: def rob(self, nums: List[int]) -> int: if len(nums) <= 2: return max(nums) dp = [-1 for _ in range(len(nums))] dp[0], dp[1] = nums[0], max(nums[0], nums[1]) # the base cases where rob1 is the amount you can take from 1 house and rob2 is the amount you can take from 2 houses (that will be the maximum of nums[0] and nums[1]) for i in range(2, len(nums)): dp[i] = max(nums[i] + dp[i - 2], dp[i - 1]) # the recurrence relation return dp[len(nums) - 1] # the last value will be your maximum amount of robbery
house-robber
Python | From Recursion to Top-Down to Bottom-Up
GigaMoksh
5
337
house robber
198
0.488
Medium
3,159
https://leetcode.com/problems/house-robber/discuss/1579854/Python-or-From-Recursion-to-Top-Down-to-Bottom-Up
class Solution: def rob(self, nums: List[int]) -> int: if len(nums) <= 2: return max(nums) rob1, rob2 = nums[0], max(nums[0], nums[1]) for n in nums[2:]: temp = max(rob1 + n, rob2) # the max amount we can rob from the given house and from the prev's previous and from the previous house rob1, rob2 = rob2, temp # update both the variables return temp # return the max amount
house-robber
Python | From Recursion to Top-Down to Bottom-Up
GigaMoksh
5
337
house robber
198
0.488
Medium
3,160
https://leetcode.com/problems/house-robber/discuss/733107/Python3-dp-(top-down-and-bottom-up)
class Solution: def rob(self, nums: List[int]) -> int: @lru_cache(None) def fn(i): """Return the maximum amount of money robbing up to ith house.""" if i < 0: return 0 return max(fn(i-1), fn(i-2) + nums[i]) return fn(len(nums)-1)
house-robber
[Python3] dp (top-down & bottom-up)
ye15
5
226
house robber
198
0.488
Medium
3,161
https://leetcode.com/problems/house-robber/discuss/733107/Python3-dp-(top-down-and-bottom-up)
class Solution: def rob(self, nums: List[int]) -> int: f0 = f1 = 0 for x in nums: f0, f1 = f1, max(f1, f0+x) return f1
house-robber
[Python3] dp (top-down & bottom-up)
ye15
5
226
house robber
198
0.488
Medium
3,162
https://leetcode.com/problems/house-robber/discuss/1124703/Python3-All-Three-Approaches
class Solution: def rob(self, nums: List[int]) -> int: length = len(nums) # Memoization (Top-Down) memo = [-1] * length def robFrom(nums, i): if i == 0: return nums[0] if i == 1: return max(nums[0], nums[1]) if memo[i] != -1: return memo[i] memo[i] = max(robFrom(nums, i-2) + nums[i], robFrom(nums, i-1)) return memo[i] return robFrom(nums, length-1) # Dynamic Programming (Bottom-Up) if length == 1: return nums[0] if length == 2: return max(nums[0], nums[1]) dp = [-1] * length dp[0] = nums[0] dp[1] = max(nums[0], nums[1]) for i in range(2, length): dp[i] = max(dp[i-2] + nums[i], dp[i-1]) return dp[length-1] # Optimized Dynamic Programming if length == 1: return nums[0] if length == 2: return max(nums[0], nums[1]) robFirst = nums[0] robSecond = max(nums[0], nums[1]) for i in range(2, length): current = max(robSecond, robFirst + nums[i]) robFirst = robSecond robSecond = current return robSecond
house-robber
Python3 All Three Approaches
doubleimpostor
4
364
house robber
198
0.488
Medium
3,163
https://leetcode.com/problems/house-robber/discuss/2034783/Pythonor-Easy-few-Lines-Solutionor-T%3AO(N)-S%3AO(1)
class Solution: def rob(self, nums: List[int]) -> int: rob1, rob2 = 0, 0 for n in nums: temp = max(n + rob1, rob2) rob1 = rob2 rob2 = temp return rob2
house-robber
Python| Easy few Lines Solution| T:O(N) S:O(1)
shikha_pandey
3
160
house robber
198
0.488
Medium
3,164
https://leetcode.com/problems/house-robber/discuss/1605364/Python3-DYNAMIC-PROGRAMMING-O(1)-Space-Explained
class Solution: def rob(self, nums: List[int]) -> int: max1, max2 = 0, 0 for n in nums: max1, max2 = max2, max(max2, max1 + n) return max2
house-robber
[Python3] DYNAMIC PROGRAMMING, O(1) Space, Explained
artod
3
138
house robber
198
0.488
Medium
3,165
https://leetcode.com/problems/house-robber/discuss/2779551/Python-solution
class Solution: def rob(self, nums: List[int]) -> int: if len(nums) <= 2: return max(nums) memorize = [0]*len(nums) memorize[-1] = nums[-1] memorize[-2] = nums[-2] for i in range(len(nums)-3, -1, -1): memorize[i] = max(nums[i], nums[i] + max(memorize[i+2:])) return max(memorize[0], memorize[1])
house-robber
πŸ₯πŸ«ΆπŸ» Python solution πŸ™ŒπŸ»
ihedzde
2
188
house robber
198
0.488
Medium
3,166
https://leetcode.com/problems/house-robber/discuss/2172617/Python-DP-and-DFS-or-95-Solution-or-Very-clearly-explained!
class Solution: def rob(self, nums: List[int]) -> int: n = len(nums) if n == 1: return nums[0] if n == 2: return max(nums[0], nums[1]) dp = [0] * n dp[0], dp[1], dp[2] = nums[0], nums[1], nums[2] + nums[0] for i in range(3, n): dp[i] = max(dp[i-2], dp[i-3]) + nums[i] return max(dp[-1], dp[-2])
house-robber
βœ…Python DP and DFS | 95% Solution | Very clearly explained!
PythonerAlex
2
159
house robber
198
0.488
Medium
3,167
https://leetcode.com/problems/house-robber/discuss/2172617/Python-DP-and-DFS-or-95-Solution-or-Very-clearly-explained!
class Solution: def rob(self, nums: List[int]) -> int: n = len(nums) if n == 1: return nums[0] @cache def dfs(i): if i < 0: return 0 return max(dfs(i-2), dfs(i-3)) + nums[i] return max(dfs(n-1), dfs(n-2))
house-robber
βœ…Python DP and DFS | 95% Solution | Very clearly explained!
PythonerAlex
2
159
house robber
198
0.488
Medium
3,168
https://leetcode.com/problems/house-robber/discuss/1766104/Top-Down-or-Bottom-Up-or-2-methods-or-DP
class Solution: def rob(self, nums: List[int]) -> int: #recursive dp[i] = max(dp[i-1],dp[i-2]+nums[i]]) #base case : dp[0] = nums[0] and dp[1] = max(nums[0],nunms[1]) #then our answer will be the dp[n-1] #TOP DOWN def dp(i): #Base cases if i==0: return nums[0] if i==1: return max(nums[1],nums[0]) #recursion cases #wrap inside memo if i not in memo: memo[i] = max(dp(i-1),dp(i-2)+nums[i]) #return the final base case return memo[i] memo = {} n = len(nums) return dp(n-1)
house-robber
Top Down | Bottom Up | 2 methods | DP
ana_2kacer
2
149
house robber
198
0.488
Medium
3,169
https://leetcode.com/problems/house-robber/discuss/1766104/Top-Down-or-Bottom-Up-or-2-methods-or-DP
class Solution: def rob(self, nums: List[int]) -> int: #recursive dp[i] = max(dp[i-1],dp[i-2]+nums[i]]) #base case : dp[0] = nums[0] and dp[1] = max(nums[0],nunms[1]) #then our answer will be the dp[n-1] #BOTTOM DOWN n = len(nums) if n==1: return nums[0] #Base Case dp = [0]*(n) dp[0] = nums[0] dp[1] = max(nums[1],nums[0]) for i in range(2,n): dp[i] = max(dp[i-1],dp[i-2]+nums[i])#Recursion statement return dp[-1]
house-robber
Top Down | Bottom Up | 2 methods | DP
ana_2kacer
2
149
house robber
198
0.488
Medium
3,170
https://leetcode.com/problems/house-robber/discuss/1606287/Python-oror-Very-Easy-Solution-oror-T.C.-O(n)
class Solution: def rob(self, nums: List[int]) -> int: inc = nums[0] exc = 0 for i in range(1, len(nums)): include = nums[i] + exc exclude = max(inc, exc) inc = include exc = exclude return max(inc, exc)
house-robber
Python || Very Easy Solution || T.C. - O(n)
naveenrathore
2
74
house robber
198
0.488
Medium
3,171
https://leetcode.com/problems/house-robber/discuss/387224/Python-Solution-(DP)
class Solution: def rob(self, nums: List[int]) -> int: if not nums: return 0 if len(nums) == 1: return nums[0] dp = [0]*len(nums) dp[0] = nums[0] dp[1] = max(nums[0], nums[1]) for i in range(2,len(nums)): dp[i] = max(nums[i]+dp[i-2], dp[i-1]) return dp[-1]
house-robber
Python Solution (DP)
FFurus
2
369
house robber
198
0.488
Medium
3,172
https://leetcode.com/problems/house-robber/discuss/387224/Python-Solution-(DP)
class Solution: def rob(self, nums: List[int]) -> int: if not nums: return 0 if len(nums) == 1: return nums[0] pre = nums[0] cur = max(nums[0], nums[1]) for i in range(2,len(nums)): cur, pre = max(nums[i]+pre, cur), cur return cur
house-robber
Python Solution (DP)
FFurus
2
369
house robber
198
0.488
Medium
3,173
https://leetcode.com/problems/house-robber/discuss/2595953/Simple-python-code-with-explanation
class Solution: #two pointers def rob(self, nums: List[int]) -> int: nums.insert(0,0) nums.insert(0,0) rob1 = 0 rob2 = 1 for i in range(2,len(nums)): nums[i] = max((nums[i] + nums[rob1]),nums[rob2]) rob1 = rob2 rob2 = i return nums[-1]
house-robber
Simple python code with explanation
thomanani
1
132
house robber
198
0.488
Medium
3,174
https://leetcode.com/problems/house-robber/discuss/2476883/Python-solution-very-easy-to-understand-with-explanation%3A)
class Solution: def rob(self, nums: List[int]) -> int: dp = [0]*(len(nums)+2) for i in range(len(nums)-1,-1,-1): dp[i] = max(nums[i]+dp[i+2],dp[i+1]) return dp[0]
house-robber
Python solution, very easy to understand with explanation:)
vilkinshay
1
43
house robber
198
0.488
Medium
3,175
https://leetcode.com/problems/house-robber/discuss/2344099/Python3-oror-Faster-than-98oror-O(N)-Time-O(1)-Space
class Solution: def rob(self, nums: List[int]) -> int: if not nums: return 0 if len(nums) == 1: return nums[0] nonAdjacent = nums[0] adjacent = max(nums[0], nums[1]) for i in range(2,len(nums)): curr = max(nums[i] + nonAdjacent, adjacent) nonAdjacent = adjacent adjacent = curr return adjacent
house-robber
Python3 || Faster than 98%|| O(N) Time, O(1) Space
hirosuki
1
43
house robber
198
0.488
Medium
3,176
https://leetcode.com/problems/house-robber/discuss/2281250/Python-DP-with-full-working-explanation
class Solution: def rob(self, nums: List[int]) -> int: # Time: O(n) and Space: O(1) rob1, rob2 = 0, 0 # rob1 and rob2 will point to two adjacent positions, starting at house 0 with zero profit for i in nums: # rob1 = house0 and rob2 = house0+house1 or rob till house0 # rob1 = house1 and rob2 = house0+house2 or rob till house1 # rob1 = house2 and rob2 = house1+house3 or rob till house2 # rob1 = house3 and rob2 = house2+house4 or rob till house3 rob1, rob2 = rob2, max(rob1 + i, rob2) return rob2
house-robber
Python DP with full working explanation
DanishKhanbx
1
46
house robber
198
0.488
Medium
3,177
https://leetcode.com/problems/house-robber/discuss/2099794/dp
class Solution: def rob(self, n) : @cache def helper(n): return max(helper(n[:-1]), n[-1]+helper(n[:-2])) if n else 0 return helper(tuple(n))
house-robber
dp ^^
andrii_khlevniuk
1
77
house robber
198
0.488
Medium
3,178
https://leetcode.com/problems/house-robber/discuss/2099794/dp
class Solution: def rob(self, nn) : p=out=0 for n in nn : p,out=out,max(p+n, out) return out
house-robber
dp ^^
andrii_khlevniuk
1
77
house robber
198
0.488
Medium
3,179
https://leetcode.com/problems/house-robber/discuss/2087271/PYTHON-oror-ITERATIVE-oror-96.8-Faster-93-memory
class Solution: def rob(self, a: List[int]) -> int: n=len(a)-3 if n>=-1: a[-2]=max(a[-2],a[-1]) while n>-1: a[n]=max(a[n+1], a[n]+a[n+2]) a[n+1]=max(a[n+1],a[n+2]) n-=1 return a[0]
house-robber
PYTHON || ITERATIVE || 96.8% Faster 93% memory
karan_8082
1
65
house robber
198
0.488
Medium
3,180
https://leetcode.com/problems/house-robber/discuss/1978848/Python-oror-O(n)Time-O(1)-Space-oror-In-Depth-Explanation
class Solution: def rob(self, nums: List[int]) -> int: if len(nums) < 3: return max(nums[0], nums[-1]) nums[2] += nums[0] for i in range(3, len(nums)): nums[i] += max(nums[i-2], nums[i-3]) return max(nums[len(nums)-1], nums[len(nums)-2])
house-robber
Python || O(n)Time O(1) Space || In-Depth Explanation
brandonallen
1
105
house robber
198
0.488
Medium
3,181
https://leetcode.com/problems/house-robber/discuss/1978848/Python-oror-O(n)Time-O(1)-Space-oror-In-Depth-Explanation
class Solution: def rob(self, nums: List[int]) -> int: if len(nums) > 2: nums[2] += nums[0] for i in range(3, len(nums)): nums[i] += max(nums[i-2], nums[i-3]) return max(nums[len(nums)-1], nums[len(nums)-2])
house-robber
Python || O(n)Time O(1) Space || In-Depth Explanation
brandonallen
1
105
house robber
198
0.488
Medium
3,182
https://leetcode.com/problems/house-robber/discuss/1965103/Python-O(n)
class Solution: def rob(self, nums: List[int]) -> int: recent_max = remainest_max = 0 for value in nums: #Multiple assignment or with temporary variable recent_max, remainest_max = remainest_max + value, max(remainest_max,recent_max) #temp = remainest_max + value #remainest_max = max(remainest_max,recent_max) #recent_max = temp return max(recent_max,remainest_max)
house-robber
Python O(n)
89anisim89
1
47
house robber
198
0.488
Medium
3,183
https://leetcode.com/problems/house-robber/discuss/1676623/Python-Dynamic-programming.-Time%3A-O(n)-Space%3A-O(n)-greater-Time%3A-O(n)-Space%3A-O(1)
class Solution: def rob(self, nums: List[int]) -> int: # How many money we can make we we reach last house # How many money we can make when we reach i house # iterate from left to right # when reach i'th house the money we can make is from # (First) dp[i-2] which is the maimum value we can make when reach (i-2)'th house plus the curr nums[i] # (Second) get the money from dp[i-1] which is the maximum value we can make when reach (i-1)'th house. However, we can't add current num[i], since when we take the money from (i-1)'th house, we can not take the adjacent house # store the maximum between (First) and (Second) # check the base case dp[0] give nums[0] is ok, However, dp[1] = max(dp[1-1],dp[1-2]+nums[1]), we don't have dp[-1], therefore we give a extra space at the frony. # dp = [0]*(n+1). Therefore, i in dp correspond to i-1 in nums, That is now the dp[1] = nums[0] # Method 1 # Time: O(n), Space: O(n) n = len(nums) dp = [0]*(n+1) dp[1] = nums[0] for i in range(2,n+1): dp[i] = max(dp[i-1],dp[i-2]+nums[i-1]) return dp[-1] # Method 2 # Since we only need dp[i-1], dp[i-2]. we can create variable and save space # Time: O(n), Space: O(1) first = 0 second = 0 for num in nums: first, second = second, max(first + num, second) return second # Method 3 same as method 2 # Time: O(n), Space: O(1) first_value = 0 second_value = 0 for num in nums: temp = max(num + first_value, second_value) first_value = second_value second_value = temp return second_value
house-robber
[Python] Dynamic programming. Time: O(n), Space: O(n) -> Time: O(n), Space: O(1)
JackYeh17
1
119
house robber
198
0.488
Medium
3,184
https://leetcode.com/problems/house-robber/discuss/1618057/Python-or-Recursion
class Solution: def rob(self, nums: List[int]) -> int: N = len(nums) @lru_cache() def travel(index): if index >= N: return 0 robber = nums[index] + travel(index+2) not_robber = travel(index + 1) return max(robber, not_robber) return travel(0)
house-robber
Python | Recursion
Call-Me-AJ
1
152
house robber
198
0.488
Medium
3,185
https://leetcode.com/problems/house-robber/discuss/1502653/97.26-runtime-easy-to-understand
class Solution: def rob(self, nums: List[int]) -> int: if len(nums) <= 2: return max(nums) d = [0]*len(nums) d[0] = nums[0] d[1] = max(nums[0], nums[1]) for i in range(2, len(nums)): d[i] = max(d[i-2]+nums[i], d[i-1]) return d[i]
house-robber
97.26% runtime, easy to understand
siddp6
1
193
house robber
198
0.488
Medium
3,186
https://leetcode.com/problems/house-robber/discuss/1468662/Python-DP-solution
class Solution: def rob(self, nums: List[int]) -> int: dp = [0]*len(nums) dp[0] = nums[0] if len(nums)>1: dp[1] = max(nums[0],nums[1]) for i in range(2,len(nums)): dp[i] = max(nums[i]+dp[i-2],dp[i-1]) return dp[-1]
house-robber
Python DP solution
dhanikshetty
1
109
house robber
198
0.488
Medium
3,187
https://leetcode.com/problems/house-robber/discuss/1468486/Python-or-DP-or-Memoization-or-With-Comments
class Solution: def rob(self, nums: List[int]) -> int: dp=nums.copy() #memo table if len(nums)<3: #if less than 3 numbers are present in array then no need to loop return max(nums) dp[1]=max(dp[0],dp[1]) #this loop stores the values in memo table as : highest amount that can be stolen upto that house for i in range(2,len(nums)): dp[i]=max(dp[i-2]+nums[i],dp[i-1]) return max(dp)
house-robber
Python | DP | Memoization | With Comments
nmk0462
1
124
house robber
198
0.488
Medium
3,188
https://leetcode.com/problems/house-robber/discuss/1459749/PyPy3-Solution-using-one-single-for-loop-w-comments
class Solution: def rob(self, nums: List[int]) -> int: # array is empty if not len(nums): return 0 # array length is less or equal to two if len(nums) <= 2: return max(nums) # For all other cases n = len(nums) t = dict() t[0] = nums[0] # Selecting when only one element present t[1] = max(nums[0],nums[1]) # Selecting when two elements present # From third element onwards, you iether take addition of # current value with a value two steps previous to it, or # you take the value one step previous to it and skip both # current and value two steps previous to it for i in range(2,n): t[i] = max(nums[i] + t[i-2], t[i-1]) # Return the last result return t[n-1]
house-robber
[Py/Py3] Solution using one single for loop w/ comments
ssshukla26
1
80
house robber
198
0.488
Medium
3,189
https://leetcode.com/problems/house-robber/discuss/1183568/python-dp-faster-than-90%2B-less-than-90%2B
class Solution: def rob(self, nums: List[int]) -> int: l = len(nums) if l == 1: return nums[0] elif l == 2: return max(nums) elif l == 3: return max(nums[0] + nums[2], nums[1]) else: x, y, z = nums[0], max(nums[0:2]), max(nums[0] + nums[2], nums[1]) for i in range(3, l): #cur = max(max(x, y) + nums[i], z) x, y, z = y, z, max(max(x, y) + nums[i], z) return max(y, z) ```
house-robber
python dp, faster than 90+, less than 90+
dustlihy
1
113
house robber
198
0.488
Medium
3,190
https://leetcode.com/problems/house-robber/discuss/364728/Solution-in-Python-3-(beats-~93)-(three-lines)-(-O(1)-space-)-(-O(n)-time-)
class Solution: def rob(self, n: List[int]) -> int: a, b, L = 0, 0, len(n) for i in range(L): b, a = max(a + n[i], b), b return 0 if L == 0 else max(n) if L <= 2 else b - Junaid Mansuri (LeetCode ID)@hotmail.com
house-robber
Solution in Python 3 (beats ~93%) (three lines) ( O(1) space ) ( O(n) time )
junaidmansuri
1
324
house robber
198
0.488
Medium
3,191
https://leetcode.com/problems/house-robber/discuss/2846897/Python-or-Recursion-or-DP-(BOTH)
class Solution: def rob(self, nums: List[int]) -> int: def dfs(position): if position == 1: return nums[0] if position == 2: return nums[1] return max(dfs(position-1), dfs(position-2)+nums[position-1]) return dfs(len(nums))
house-robber
Python | Recursion | DP (BOTH)
ajay_gc
0
1
house robber
198
0.488
Medium
3,192
https://leetcode.com/problems/house-robber/discuss/2846897/Python-or-Recursion-or-DP-(BOTH)
class Solution: def rob(self, nums: List[int]) -> int: n = len(nums) if n == 1: return nums[0] if n == 2: return max(nums) dptable = [0]*n dptable[0] = nums[0] dptable[1] = max(nums[0], nums[1]) for index in range(2, len(nums)): print(nums[index]) print(dptable) dptable[index] = max(dptable[index-1], dptable[index-2]+nums[index]) return max(dptable[-1], dptable[-2])
house-robber
Python | Recursion | DP (BOTH)
ajay_gc
0
1
house robber
198
0.488
Medium
3,193
https://leetcode.com/problems/house-robber/discuss/2843132/Python-DP
class Solution: def rob(self, nums): DP0, DP1 = 0, 0 for i in nums: DP0, DP1 = DP1, max(DP0 + i, DP1) return DP1
house-robber
[Python] DP
i-hate-covid
0
2
house robber
198
0.488
Medium
3,194
https://leetcode.com/problems/house-robber/discuss/2837451/Python-Solution
class Solution: def rob(self, nums: List[int]) -> int: dp = [] for i in range(len(nums)): if(i >= 2): dp.append(max(dp[i-2] + nums[i], dp[i-1])) elif(i >= 1): dp.append(max(nums[i], dp[i-1])) else: dp.append(nums[i]) return dp[-1]
house-robber
Python Solution
charleswizards
0
4
house robber
198
0.488
Medium
3,195
https://leetcode.com/problems/house-robber/discuss/2829610/Robber-1-DP-Easy-Solution
class Solution: def rob(self, nums: List[int]) -> int: dp={} def work(x): if x<0: return 0 if x in dp: return dp[x] p=nums[x]+work(x-2) np=0+work(x-1) dp[x]=max(p,np) return dp[x] return work(len(nums)-1)
house-robber
Robber 1 DP Easy Solution
soumya262003
0
4
house robber
198
0.488
Medium
3,196
https://leetcode.com/problems/house-robber/discuss/2829157/Python-Optimized-Solution-O(n)-Time-O(1)-Space-and-Tabulation-TC-O(n)-Space-(n)
class Solution: def rob(self, nums: List[int]) -> int: dp = [0]*len(nums) # picking up the first house dp[0] = nums[0] for i in range(1,len(nums)): pick = nums[i] if i>1: # since second has been picked the robber can add the prev2 as he has not robbed previous house pick+=dp[i-2] # since second is house is not picked for robbery so the first house is added not_pick = 0+dp[i-1] dp[i] = max(pick,not_pick) return dp[-1]
house-robber
Python Optimized Solution O(n) Time O(1) Space and Tabulation TC O(n) Space (n)
nidhi_nishad26
0
5
house robber
198
0.488
Medium
3,197
https://leetcode.com/problems/house-robber/discuss/2829157/Python-Optimized-Solution-O(n)-Time-O(1)-Space-and-Tabulation-TC-O(n)-Space-(n)
class Solution: def rob(self, nums: List[int]) -> int: # rob the first house prev = nums[0] # no house from first prev2 = 0 for i in range(1,len(nums)): pick = nums[i] if i>1: # since second has been picked the robber can add the prev2 as he has not robbed previous house pick+=prev2 # since second is house is not picked for robbery so the first house is added not_pick = 0+prev prev2 = prev prev = max(pick,not_pick) return prev
house-robber
Python Optimized Solution O(n) Time O(1) Space and Tabulation TC O(n) Space (n)
nidhi_nishad26
0
5
house robber
198
0.488
Medium
3,198
https://leetcode.com/problems/house-robber/discuss/2821870/Python-Current-%2B-LastLast-OR-Last.-Rinse-and-Repeat-)
class Solution: def rob(self, nums: List[int]) -> int: ''' The idea is, given we cannot rob 2 adjacent houses, we care do we rob current-house + last-last-house or do we forgo current-house, and take last-house To make calculations and base cases easier, we can prefix 2 houses 0, 0,[2,7, 9, 3, 1] curr + last-last OR just last 0 0 2 2 + 0 = 2 or 0 0 0 2 7 7 + 0 = 7 or 2 0 0 2 7 11 9 + 2 = 11 or 7 0 0 2 7 11 11 3 + 7 = 10 or 11 0 0 2 7 11 11 12 1 + 11 = 12 or 12 ''' last_last = 0 last = 0 for house in nums: curr = max(house + last_last, last) last_last, last = last, curr return last
house-robber
[Python] Current + LastLast OR Last. Rinse & Repeat =)
graceiscoding
0
3
house robber
198
0.488
Medium
3,199