-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a31a7ce
commit 4992a73
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |