Skip to content

Commit

Permalink
Ones and Zeroes
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-sukhoverkhov committed Sep 2, 2024
1 parent a31a7ce commit 4992a73
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
44 changes: 44 additions & 0 deletions python/leetcode/Ones and Zeroes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from typing import List


class Solution:

def findMaxForm(self, strs: List[str], m: int, n: int) -> int:

dp = [[0]*(n+1) for _ in range(m+1)]

for s in strs:
zeros = s.count('0')
ones = s.count('1')

for zero in range(m, zeros - 1, -1):
for one in range(n, ones - 1, -1):
dp[zero][one] = max(dp[zero - zeros][one - ones] + 1, dp[zero][one])

return dp[m][n]


if __name__ == "__main__":
obj = Solution()

# strs = ["0", "11", "1000", "01", "0", "101", "1", "1", "1", "0", "0", "0", "0", "1", "0", "0110101",
# "0", "11", "01", "00", "01111", "0011", "1", "1000", "0", "11101", "1", "0", "10", "0111"]
# m = 9
# n = 80
# assert obj.findMaxForm(strs, m, n) == 17

strs = ["10", "0001", "111001", "1", "0"]
m = 5
n = 3
assert obj.findMaxForm(strs, m, n) == 4

strs = ["10", "0001", "111001", "1", "0"]
m = 4
n = 3
assert obj.findMaxForm(strs, m, n) == 3

strs = ["10", "0", "1"]
m = 1
n = 1
assert obj.findMaxForm(strs, m, n) == 2

16 changes: 16 additions & 0 deletions python/leetcode/number_of_1_bits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def hammingWeight(self, n: int) -> int:
res = 0

while n:
res += 1
n &= (n - 1)

return res


if __name__ == "__main__":
obj = Solution()

n = 0b0000000000000000000000000001011
assert obj.hammingWeight(n) == 3

0 comments on commit 4992a73

Please sign in to comment.