Skip to content

Commit c2555dc

Browse files
committed
Nov 20
1 parent 5eac27d commit c2555dc

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

2024-11-November-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
| November 17 | [862. Shortest Subarray with Sum at Least K](https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/) | Hard | Solved |
2323
| November 18 | [1652. Defuse the Bomb](https://leetcode.com/problems/defuse-the-bomb/) | Easy | Solved |
2424
| November 19 | [2461. Maximum Sum of Distinct Subarrays With Length K](https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/) | Medium | Solved |
25-
| November 20 | []() | | |
25+
| November 20 | [2516. Take K of Each Character From Left and Right](https://leetcode.com/problems/take-k-of-each-character-from-left-and-right/) | Medium | Solved |
2626
| November 21 | []() | | |
2727
| November 22 | []() | | |
2828
| November 23 | []() | | |
@@ -38,5 +38,5 @@
3838
| Level | Problems | Solved | Unsolved |
3939
| --- | --- | --- | --- |
4040
| Easy | 4 | 4 | 0 |
41-
| Medium | 14 | 7 | 7 |
41+
| Medium | 15 | 8 | 7 |
4242
| Hard | 1 | 1 | 0 |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def takeCharacters(self, s: str, k: int) -> int:
3+
counts = [0, 0, 0]
4+
for ch in s:
5+
counts[ord(ch) - ord('a')] += 1
6+
if min(counts) < k:
7+
return -1
8+
9+
res = float('inf')
10+
left = 0
11+
for right in range(len(s)):
12+
counts[ord(s[right]) - ord('a')] -= 1
13+
while min(counts) < k:
14+
counts[ord(s[left]) - ord('a')] += 1
15+
left += 1
16+
res = min(res, len(s)-(right-left+1))
17+
return res
18+
19+
20+
def main():
21+
s = 'aabaaaacaabc'
22+
k = 2
23+
assert Solution().takeCharacters(s, k) == 8
24+
25+
s = 'a'
26+
k = 1
27+
assert Solution().takeCharacters(s, k) == -1
28+
29+
30+
if __name__ == '__main__':
31+
main()

0 commit comments

Comments
 (0)