forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximize-the-number-of-partitions-after-operations.py
52 lines (49 loc) · 1.73 KB
/
maximize-the-number-of-partitions-after-operations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Time: O(n)
# Space: O(n)
# prefix sum, greedy
class Solution(object):
def maxPartitionsAfterOperations(self, s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
def popcount(n):
n = (n & 0x55555555) + ((n >> 1) & 0x55555555)
n = (n & 0x33333333) + ((n >> 2) & 0x33333333)
n = (n & 0x0F0F0F0F) + ((n >> 4) & 0x0F0F0F0F)
n = (n & 0x00FF00FF) + ((n >> 8) & 0x00FF00FF)
n = (n & 0x0000FFFF) + ((n >> 16) & 0x0000FFFF)
return n
left = [0]*(len(s)+1)
left_mask = [0]*(len(s)+1)
cnt = mask = 0
for i in xrange(len(s)):
mask |= 1<<(ord(s[i])-ord('a'))
if popcount(mask) > k:
cnt += 1
mask = 1<<(ord(s[i])-ord('a'))
left[i+1] = cnt
left_mask[i+1] = mask
right = [0]*(len(s)+1)
right_mask = [0]*(len(s)+1)
cnt = mask = 0
for i in reversed(xrange(len(s))):
mask |= 1<<(ord(s[i])-ord('a'))
if popcount(mask) > k:
cnt += 1
mask = 1<<(ord(s[i])-ord('a'))
right[i] = cnt
right_mask[i] = mask
result = 0
for i in xrange(len(s)):
curr = left[i]+right[i+1]
mask = left_mask[i]|right_mask[i+1]
if popcount(left_mask[i]) == popcount(right_mask[i+1]) == k and popcount(mask) != 26:
curr += 3
elif popcount(mask)+int(popcount(mask) != 26) > k: # test case: s = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz", k = 26
curr += 2
else:
curr += 1
result = max(result, curr)
return result