Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
dependabot[bot] authored and romankurnovskii committed Mar 31, 2024
1 parent c27badf commit 3fb808a
Show file tree
Hide file tree
Showing 19 changed files with 450 additions and 20 deletions.
4 changes: 2 additions & 2 deletions content/posts/linux/debian-setup.en.md
@@ -1,6 +1,6 @@
---
title: Monitoring
seoTitle: Monitoring
title: Monitoring Linux OS
seoTitle: Monitoring Linux OS
description: Monitoring
toc: true
tags: [Linux, Debian]
Expand Down
30 changes: 30 additions & 0 deletions content/tracks/algorithms-101/leetcode/hard/2444/index.en.md
@@ -0,0 +1,30 @@
---
title: 2444. Count Subarrays With Fixed Bounds
seoTitle: LeetCode 2444. Count Subarrays With Fixed Bounds | Python solution and explanation
description: 2444. Count Subarrays With Fixed Bounds
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-01-01
lastMod: 2024-01-01
featuredImage: https://picsum.photos/700/155?grayscale
weight: 2444
---

[LeetCode problem 2444](https://leetcode.com/problems/count-subarrays-with-fixed-bounds/)

```python
class Solution:
def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
j1 = j2 = k = -1
res = 0
for i, v in enumerate(nums):
if v < minK or v > maxK:
k = i
if v == minK:
j1 = i
if v == maxK:
j2 = i
res += max(0, min(j1, j2) - k)
return res
```
31 changes: 31 additions & 0 deletions content/tracks/algorithms-101/leetcode/hard/2444/index.ru.md
@@ -0,0 +1,31 @@
---
title: Count Subarrays With Fixed Bounds
seoTitle: LeetCode Count Subarrays With Fixed Bounds | Python solution and explanation
description: Count Subarrays With Fixed Bounds
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-01-01
lastMod: 2024-01-01
featuredImage: https://picsum.photos/700/155?grayscale
weight: 2444
---

[LeetCode problem 2444](https://leetcode.com/problems/count-subarrays-with-fixed-bounds/)

```python
class Solution:
def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
j1 = j2 = k = -1
res = 0
for i, v in enumerate(nums):
if v < minK or v > maxK:
k = i
if v == minK:
j1 = i
if v == maxK:
j2 = i
res += max(0, min(j1, j2) - k)
return res

```
58 changes: 58 additions & 0 deletions content/tracks/algorithms-101/leetcode/hard/41/index.en.md
@@ -0,0 +1,58 @@
---
title: 41. First Missing Positive
seoTitle: LeetCode 41. First Missing Positive | Python solution and explanation
description: 41. First Missing Positive
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-01-01
lastMod: 2024-01-01
featuredImage: https://picsum.photos/700/155?grayscale
weight: 41
---

[LeetCode problem 41](https://leetcode.com/problems/first-missing-positive/)

How to place each number in its 'correct' position if we ignore the space constraint. How can this idea be adapted to use constant space?

Use the array itself to record the presence of integers by placing each number in its "natural" position. For example, if 1 is in the array, place it at index 0, if 2 is there, place it at index 1, and so on. This way, the first place where its number doesn't match its index, the missing number is index + 1.

## Approach

1. Ignore Non-Positive and Large Numbers: First, ignore any non-positive numbers and numbers larger than n (array's size), as they don't help in finding the first missing positive.
1. Place Each Number in Its Correct Position: Iterate through the array, for each number num in nums, swap it with the number at index = num - 1 if it's not already in the correct position. Continue this process until every number is either in its correct position or cannot be placed (e.g., because it's out of the range [1, n]).
1. Find the First Missing Positive: After reordering, iterate through the array again. The first index i where nums[i] != i + 1 indicates that i + 1 is the missing number.

```python
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
n = len(nums)
for i in range(n):
while 1 <= nums[i] <= n and nums[nums[i] - 1] != nums[i]:
# Swap nums[i] with nums[nums[i] - 1]
nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1]

for i in range(n):
if nums[i] != i + 1:
return i + 1

return n + 1
```

```python
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
def swap(i, j):
nums[i], nums[j] = nums[j], nums[i]

n = len(nums)
for i in range(n):
while 1 <= nums[i] <= n and nums[i] != nums[nums[i] - 1]:
swap(i, nums[i] - 1)
for i in range(n):
if i + 1 != nums[i]:
return i + 1
return n + 1
```

Cyclic Sort pattern
31 changes: 31 additions & 0 deletions content/tracks/algorithms-101/leetcode/hard/41/index.ru.md
@@ -0,0 +1,31 @@
---
title: First Missing Positive
seoTitle: LeetCode First Missing Positive | Python solution and explanation
description: First Missing Positive
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-01-01
lastMod: 2024-01-01
featuredImage: https://picsum.photos/700/155?grayscale
weight: 41
---

[LeetCode problem 41](https://leetcode.com/problems/first-missing-positive/)

```python
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
def swap(i, j):
nums[i], nums[j] = nums[j], nums[i]

n = len(nums)
for i in range(n):
while 1 <= nums[i] <= n and nums[i] != nums[nums[i] - 1]:
swap(i, nums[i] - 1)
for i in range(n):
if i + 1 != nums[i]:
return i + 1
return n + 1

```
51 changes: 51 additions & 0 deletions content/tracks/algorithms-101/leetcode/hard/992/index.en.md
@@ -0,0 +1,51 @@
---
title: 992. Subarrays with K Different Integers
seoTitle: LeetCode 992. Subarrays with K Different Integers | Python solution and explanation
description: 992. Subarrays with K Different Integers
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-01-01
lastMod: 2024-03-30
featuredImage: https://picsum.photos/700/155?grayscale
weight: 992
---

[LeetCode problem 992](https://leetcode.com/problems/subarrays-with-k-different-integers/)

Using the sliding window technique to keep track of the different integers within a window. Adjust the window's size to always contain exactly `k` different integers.

The idea is to transform the problem into finding the number of subarrays with at most k different integers and subtract the number of subarrays with at most `k-1` different integers from it.

## Approach

1. At Most K: Implement a function `helper(nums, k)` that returns the number of subarrays with at most k different integers.
2. Utilize `helper` function for the Solution: The number of subarrays with exactly `k` different integers is helper`(nums, k) - helper(nums, k-1)`.
3. Implement `helper`: Use a sliding window technique to expand the window to include as many elements as long as there are at most `k` different ones. Shrink the window from the left when the condition is violated. Keep track of the count of each integer in the current window using a hash map.

```python
class Solution:
def subarraysWithKDistinct(self, nums: List[int], k: int) -> int:
def helper(nums, k): # at most k
count = {}
res = i = 0
for j in range(len(nums)):
if nums[j] not in count:
k -= 1
count[nums[j]] = 0
count[nums[j]] += 1
while k < 0:
count[nums[i]] -= 1
if count[nums[i]] == 0:
k += 1
del count[nums[i]]
i += 1
res += j - i + 1
return res

return helper(nums, k) - helper(nums, k-1)
```

## Pattern

This problem follows the Sliding Window pattern, where a window of elements is expanded and shrunk based on certain conditions. The sliding window technique is commonly used to solve problems related to contiguous subarrays or substrings, particularly when you need to track or calculate something among all possible subarrays or substrings of a certain size or condition.
51 changes: 51 additions & 0 deletions content/tracks/algorithms-101/leetcode/hard/992/index.ru.md
@@ -0,0 +1,51 @@
---
title: 992. Subarrays with K Different Integers
seoTitle: LeetCode 992. Subarrays with K Different Integers | Python solution and explanation
description: 992. Subarrays with K Different Integers
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-01-01
lastMod: 2024-03-30
featuredImage: https://picsum.photos/700/155?grayscale
weight: 992
---

[LeetCode problem 992](https://leetcode.com/problems/subarrays-with-k-different-integers/)

Using the sliding window technique to keep track of the different integers within a window. Adjust the window's size to always contain exactly `k` different integers.

The idea is to transform the problem into finding the number of subarrays with at most k different integers and subtract the number of subarrays with at most `k-1` different integers from it.

## Approach

1. At Most K: Implement a function `helper(nums, k)` that returns the number of subarrays with at most k different integers.
2. Utilize `helper` function for the Solution: The number of subarrays with exactly `k` different integers is helper`(nums, k) - helper(nums, k-1)`.
3. Implement `helper`: Use a sliding window technique to expand the window to include as many elements as long as there are at most `k` different ones. Shrink the window from the left when the condition is violated. Keep track of the count of each integer in the current window using a hash map.

```python
class Solution:
def subarraysWithKDistinct(self, nums: List[int], k: int) -> int:
def helper(nums, k): # at most k
count = {}
res = i = 0
for j in range(len(nums)):
if nums[j] not in count:
k -= 1
count[nums[j]] = 0
count[nums[j]] += 1
while k < 0:
count[nums[i]] -= 1
if count[nums[i]] == 0:
k += 1
del count[nums[i]]
i += 1
res += j - i + 1
return res

return helper(nums, k) - helper(nums, k-1)
```

## Pattern

This problem follows the Sliding Window pattern, where a window of elements is expanded and shrunk based on certain conditions. The sliding window technique is commonly used to solve problems related to contiguous subarrays or substrings, particularly when you need to track or calculate something among all possible subarrays or substrings of a certain size or condition.
16 changes: 15 additions & 1 deletion content/tracks/algorithms-101/leetcode/medium/2962/index.en.md
@@ -1,3 +1,18 @@
---
title: 2962. Count Subarrays Where Max Element Appears at Least K Times
seoTitle: LeetCode 2962. Count Subarrays Where Max Element Appears at Least K Times | Python solution and explanation
description: 2962. Count Subarrays Where Max Element Appears at Least K Times
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-03-29
lastMod: 2024-03-29
featuredImage: https://picsum.photos/700/155?grayscale
weight: 2962
---

[LeetCode problem 2962](https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/)

```python
class Solution:
def countSubarrays(self, nums: List[int], k: int) -> int:
Expand All @@ -13,5 +28,4 @@ class Solution:
res += n - j + 1
cnt -= x == mx
return res

```
16 changes: 15 additions & 1 deletion content/tracks/algorithms-101/leetcode/medium/2962/index.ru.md
@@ -1,3 +1,18 @@
---
title: 2962. Count Subarrays Where Max Element Appears at Least K Times
seoTitle: LeetCode 2962. Count Subarrays Where Max Element Appears at Least K Times | Python solution and explanation
description: 2962. Count Subarrays Where Max Element Appears at Least K Times
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-03-29
lastMod: 2024-03-29
featuredImage: https://picsum.photos/700/155?grayscale
weight: 2962
---

[LeetCode problem 2962](https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/)

```python
class Solution:
def countSubarrays(self, nums: List[int], k: int) -> int:
Expand All @@ -13,5 +28,4 @@ class Solution:
res += n - j + 1
cnt -= x == mx
return res

```
27 changes: 27 additions & 0 deletions content/tracks/algorithms-101/leetcode/medium/713/index.en.md
@@ -0,0 +1,27 @@
---
title: 713. Subarray Product Less Than K
seoTitle: LeetCode 713. Subarray Product Less Than K | Python solution and explanation
description: 713. Subarray Product Less Than K
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-03-27
lastMod: 2024-01-01
featuredImage: https://picsum.photos/700/155?grayscale
weight: 713
---

[LeetCode problem 713](https://leetcode.com/problems/subarray-product-less-than-k/)

```python
class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
res, s, j = 0, 1, 0
for i, v in enumerate(nums):
s *= v
while j <= i and s >= k:
s //= nums[j]
j += 1
res += i - j + 1
return res
```
27 changes: 27 additions & 0 deletions content/tracks/algorithms-101/leetcode/medium/713/index.ru.md
@@ -0,0 +1,27 @@
---
title: 713. Subarray Product Less Than K
seoTitle: LeetCode 713. Subarray Product Less Than K | Python solution and explanation
description: 713. Subarray Product Less Than K
toc: true
tags: []
categories: [Algorithms, LeetCode]
date: 2024-03-27
lastMod: 2024-01-01
featuredImage: https://picsum.photos/700/155?grayscale
weight: 713
---

[LeetCode problem 713](https://leetcode.com/problems/subarray-product-less-than-k/)

```python
class Solution:
def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
res, s, j = 0, 1, 0
for i, v in enumerate(nums):
s *= v
while j <= i and s >= k:
s //= nums[j]
j += 1
res += i - j + 1
return res
```

0 comments on commit 3fb808a

Please sign in to comment.