Skip to content

Commit f746603

Browse files
committed
Jun 11
1 parent 7d1dc3d commit f746603

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from itertools import permutations
2+
3+
4+
class Solution:
5+
def __get_status(self, n1: int, n2: int) -> int:
6+
return ((n1 & 1) << 1) | (n2 & 1)
7+
8+
def maxDifference(self, s: str, k: int) -> int:
9+
n = len(s)
10+
ans = float('-inf')
11+
for a, b in permutations('01234', 2):
12+
best = [float('inf')] * 4
13+
cnt_a, cnt_b = 0, 0
14+
prev_a, prev_b = 0, 0
15+
ptr1 = -1
16+
for ptr2 in range(n):
17+
cnt_a += s[ptr2] == a
18+
cnt_b += s[ptr2] == b
19+
while ptr2 - ptr1 >= k and cnt_b - prev_b >= 2:
20+
left_status = self.__get_status(prev_a, prev_b)
21+
best[left_status] = min(best[left_status], prev_a - prev_b)
22+
ptr1 += 1
23+
prev_a += s[ptr1] == a
24+
prev_b += s[ptr1] == b
25+
right_status = self.__get_status(cnt_a, cnt_b)
26+
if best[right_status ^ 0b10] != float('inf'):
27+
ans = max(ans, cnt_a - cnt_b - best[right_status ^ 0b10])
28+
return ans
29+
30+
31+
def main():
32+
s = '12233'
33+
k = 4
34+
assert Solution().maxDifference(s, k) == -1
35+
36+
s = '1122211'
37+
k = 3
38+
assert Solution().maxDifference(s, k) == 1
39+
40+
s = '110'
41+
k = 3
42+
assert Solution().maxDifference(s, k) == -1
43+
44+
45+
if __name__ == '__main__':
46+
main()

2025-06-June-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
| June 08 | [386. Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/) | Medium | Solved |
1515
| June 09 | [440. K-th Smallest in Lexicographical Order](https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/) | Hard | Unsolved |
1616
| June 10 | [3442. Maximum Difference Between Even and Odd Frequency I](https://leetcode.com/problems/maximum-difference-between-even-and-odd-frequency-i/) | Easy | Solved |
17-
| June 11 | []() | | |
17+
| June 11 | [3445. Maximum Difference Between Even and Odd Frequency II](https://leetcode.com/problems/maximum-difference-between-even-and-odd-frequency-ii/) | Hard | Unsolved |
1818
| June 12 | []() | | |
1919
| June 13 | []() | | |
2020
| June 14 | []() | | |
@@ -41,4 +41,4 @@
4141
| --- | --- | --- | --- |
4242
| Easy | 1 | 1 | 0 |
4343
| Medium | 6 | 3 | 3 |
44-
| Hard | 3 | 2 | 1 |
44+
| Hard | 4 | 2 | 2 |

0 commit comments

Comments
 (0)