Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Topics: Bit Manuputation, Bit-Mask, DP

Check failure on line 1 in bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py:1:1: N999 Invalid module name: 'Maximize _he_Number_of_Partitions_After_Operations'


class Solution:
def __init__(self):
self.dp = {}

Check failure on line 7 in bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py:7:1: W293 Blank line contains whitespace
def countSetBits(self, n):

Check failure on line 8 in bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py:8:9: N802 Function name `countSetBits` should be lowercase
count = 0
while n > 0:
count += n & 1
n >>= 1
return count

Check failure on line 14 in bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py:14:1: W293 Blank line contains whitespace
def helper(self, s, k, i, mask, isChange):

Check failure on line 15 in bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py:15:37: N803 Argument name `isChange` should be lowercase
n = len(s)
if i == n:
return 1

Check failure on line 19 in bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py:19:1: W293 Blank line contains whitespace
currState = (i << 27) | (mask << 1) | isChange

Check failure on line 20 in bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py:20:9: N806 Variable `currState` in function should be lowercase

Check failure on line 21 in bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py:21:1: W293 Blank line contains whitespace
if currState in self.dp:
return self.dp[currState]

Check failure on line 24 in bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py:24:1: W293 Blank line contains whitespace
result = 0
val = ord(s[i]) - ord('a')

Check failure on line 27 in bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

bit_manipulation/Maximize _he_Number_of_Partitions_After_Operations.py:27:1: W293 Blank line contains whitespace
count = self.countSetBits(mask | (1 << val))
temp = 0
if count > k:
temp = 1 + self.helper(s, k, i + 1, 1 << val, isChange)
else:
temp = self.helper(s, k, i + 1, mask | (1 << val), isChange)
result = max(result, temp)

if isChange == 0:
for j in range(26):
count = self.countSetBits(mask | (1 << j))
if count > k:
temp = 1 + self.helper(s, k, i + 1, 1 << j, 1)
else:
temp = self.helper(s, k, i + 1, mask | (1 << j), 1)
result = max(result, temp)

self.dp[currState] = result
return result

def maxPartitionsAfterOperations(self, s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
return self.helper(s, k, 0, 0, 0)

# Time Complexity: O(n × 2²⁷)
# Auxiliary Space: O(n × 2²⁷)
Loading