Skip to content

Commit 0e7a413

Browse files
committed
Add solutions for 209, 2269 and 2379 problems
Add solution for 209 (minimum subarray sum) using sliding window. Implement solution for 2269 (k-beauty of number) with sliding window approach. Provide both brute force and optimal solutions for 2379 (minimum recolors). Update README with entries for 209, 2269, 2379, and 3095.
1 parent 173add9 commit 0e7a413

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

209.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import List
2+
3+
class Solution:
4+
"""The intuition here is pretty simple.
5+
1) We need to find each sub array where the condition is match S >= target.
6+
2) Once the condition is found we calculate the minimum size of that sub array.
7+
3) Then we remove from the total sum the leftmost element and move left forwrd.
8+
4) Go to the next loop and do the same.
9+
5) End with the smallest number of elements that match the criteria
10+
"""
11+
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
12+
# This is a O(N) time solution with O(1) space
13+
size = len(nums)
14+
left = 0
15+
totalSum = 0
16+
min_size = float("inf")
17+
18+
19+
for right in range(size):
20+
totalSum += nums[right]
21+
22+
while totalSum >= target:
23+
min_size = min(min_size, right - left + 1)
24+
25+
totalSum = totalSum - nums[left]
26+
left += 1
27+
28+
return int(min_size) if min_size != float("inf") else 0

2269.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution:
2+
def divisorSubstrings(self, num: int, k: int) -> int:
3+
str_num = str(num)
4+
size = len(str_num)
5+
totalNums = 0
6+
i = 0
7+
8+
# The intuiton was:
9+
# Loop from 0 to size
10+
# slice the num as string from i to i + k (k because this sets the size of the number given)
11+
# Transform the number into int
12+
# check if the number slice has the size of the k
13+
# -> Some times it won't be true because at the last index the str_num size will be _1_
14+
# check if the number is no 0
15+
# check if the number is divisible by the slice num as int
16+
# increase the counter
17+
while i < size:
18+
current_num = str_num[i:i+k]
19+
20+
if len(current_num) == k and int(current_num) != 0 and num % int(current_num) == 0:
21+
totalNums += 1
22+
23+
i += 1
24+
25+
return totalNums

2379.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution:
2+
"""This is the brute force solution where we slide the block string for every
3+
single loop."""
4+
def minimumRecolors(self, blocks: str, k: int) -> int:
5+
table = {}
6+
7+
def nums_of_operations(str):
8+
return str.count("W")
9+
10+
minimum_operations = float("inf")
11+
12+
for i in range(0, len(blocks)):
13+
substring = blocks[i:i+k]
14+
15+
if len(substring) == k:
16+
minimum_operations = min(minimum_operations, nums_of_operations(substring))
17+
18+
return 0 if minimum_operations == float('inf') else int(minimum_operations)
19+
20+
21+
22+
class OptiomalSolution:
23+
"""The optimal solution uses the sliding window approach to
24+
increment or decrement the count of W blocks.
25+
26+
When the window moves right it checks if the leftmost element is W if so remove it from the count
27+
then check if the current one is W if so increment the count
28+
"""
29+
def minimumRecolors(self, blocks: str, k: int) -> int:
30+
with_blocks = blocks[:k].count("W")
31+
minimum_operations = with_blocks
32+
left = 0
33+
34+
for i in range(k, len(blocks)):
35+
if blocks[left] == 'W':
36+
with_blocks -= 1
37+
38+
if blocks[i] == 'W':
39+
with_blocks += 1
40+
41+
minimum_operations = min(minimum_operations, with_blocks)
42+
left += 1
43+
44+
45+
46+
return minimum_operations if minimum_operations != float('inf') else 0

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
| 83.py | Linked List / Two pointers | <https://leetcode.com/problems/remove-duplicates-from-sorted-list/> |
1111
| 141.py | Fast and Slow pointers | <https://leetcode.com/problems/linked-list-cycle/description/> |
1212
| 167.py | Two pointers | <https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/> |
13+
| 209.py | Sliding Window | <https://leetcode.com/problems/minimum-size-subarray-sum> |
1314
| 219.py | Array / Sliding Window | <https://leetcode.com/problems/find-the-duplicate-number/> |
1415
| 234.py | Linked List / Fast and Slow | <https://leetcode.com/problems/palindrome-linked-list/> |
1516
| 287.py | Two pointers / Hash table | <https://leetcode.com/problems/find-the-duplicate-number/> |
@@ -57,12 +58,14 @@
5758
| 2154.py | | |
5859
| 2206.py | | |
5960
| 2215.py | | |
61+
| 2269.py | Sliding Window | <https://leetcode.com/problems/find-the-k-beauty-of-a-number> |
6062
| 2283.py | | |
6163
| 2325.py | | |
6264
| 2341.py | | |
6365
| 2363.py | | |
6466
| 2367-2.py | | |
6567
| 2367.py | | |
68+
| 2379.py | String / Sliding Window | <https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks> |
6669
| 2389.py | Soring/PrefixSum | <https://leetcode.com/problems/longest-subsequence-with-limited-sum> |
6770
| 2418.py | | |
6871
| 2475.py | | |
@@ -77,6 +80,7 @@
7780
| 2848.py | | |
7881
| 2913.py | | |
7982
| 2932.py | | |
83+
| 3095.py | Bit / Sliding Window | <https://leetcode.com/problems/shortest-subarray-with-or-at-least-k-i> |
8084
| 3206.py | Array | <https://leetcode.com/problems/alternating-groups-i/description/> |
8185
| 3318.py | Array / Sliding Window | <https://leetcode.com/problems/find-x-sum-of-all-k-long-subarrays-i> |
8286
| 3364.py | Array / Sliding Window | <https://leetcode.com/problems/minimum-positive-sum-subarray> |

0 commit comments

Comments
 (0)