Skip to content

Commit 0f34653

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 2799 and 3392
1 parent 2632274 commit 0f34653

5 files changed

+97
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
245245
- [1964 Find the Longest Valid Obstacle Course at Each Position](https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position/description/)
246246
- [2140 Solving Questions With Brainpower](https://leetcode.com/problems/solving-questions-with-brainpower/description/)
247247
- [2466 Count Ways To Build Good Strings](https://leetcode.com/problems/count-ways-to-build-good-strings/description/)
248+
- [2799 Count Complete Subarrays in an Array](https://leetcode.com/problems/count-complete-subarrays-in-an-array/description/)
249+
- [3392 Count Subarrays of Length Three With a Condition](https://leetcode.com/problems/count-subarrays-of-length-three-with-a-condition/description/)
248250

249251
## Development 🔧
250252

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import collections
2+
from typing import List
3+
4+
5+
class Solution:
6+
"""Base class for all LeetCode Problems."""
7+
8+
def countCompleteSubarrays(self, nums: List[int]) -> int:
9+
"""
10+
You are given an array nums consisting of positive integers.
11+
12+
We call a subarray of an array complete if the following condition is satisfied:
13+
- The number of distinct elements in the subarray is equal to the number of
14+
distinct elements in the whole array.
15+
16+
Return the number of complete subarrays.
17+
18+
A subarray is a contiguous non-empty part of an array.
19+
"""
20+
count = 0
21+
freq = collections.defaultdict(int)
22+
i, j = 0, 0
23+
while i < len(nums):
24+
while j < len(nums) and len(freq) < len(set(nums)):
25+
freq[nums[j]] += 1
26+
j += 1
27+
28+
if len(freq) < len(set(nums)):
29+
break
30+
31+
count += len(nums) - j + 1
32+
freq[nums[i]] -= 1
33+
if freq[nums[i]] == 0:
34+
del freq[nums[i]]
35+
i += 1
36+
return count
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def countSubarrays(self, nums: List[int]) -> int:
8+
"""
9+
Given an integer array nums, return the number of subarrays of length 3 such
10+
that the sum of the first and third numbers equals exactly half of the second
11+
number.
12+
13+
A subarray is a contiguous non-empty sequence of elements within an array.
14+
"""
15+
count = 0
16+
for i in range(1, len(nums) - 1):
17+
if 2 * (nums[i - 1] + nums[i + 1]) == nums[i]:
18+
count += 1
19+
return count
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._2799_count_complete_subarrays_in_an_array import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "expected"],
10+
argvalues=[
11+
([1, 3, 1, 2, 2], 4),
12+
([5, 5, 5, 5], 10),
13+
],
14+
)
15+
def test_func(nums: List[int], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
17+
count = Solution().countCompleteSubarrays(nums)
18+
assert count == expected
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# flake8: noqa
2+
from typing import List
3+
4+
import pytest
5+
6+
from awesome_python_leetcode._3392_count_subarrays_of_length_three_with_a_condition import (
7+
Solution,
8+
)
9+
10+
11+
@pytest.mark.parametrize(
12+
argnames=["nums", "expected"],
13+
argvalues=[
14+
([1, 2, 1, 4, 1], 1),
15+
([1, 1, 1], 0),
16+
([8, -10, -4], 0),
17+
],
18+
)
19+
def test_func(nums: List[int], expected: int):
20+
"""Tests the solution of a LeetCode problem."""
21+
num_sub_arrays = Solution().countSubarrays(nums)
22+
assert num_sub_arrays == expected

0 commit comments

Comments
 (0)