Skip to content

Commit 3632917

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 3202
1 parent 77e704b commit 3632917

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273
- [2523 Closest Prime Numbers in Range](https://leetcode.com/problems/closest-prime-numbers-in-range/description/)
274274
- [2799 Count Complete Subarrays in an Array](https://leetcode.com/problems/count-complete-subarrays-in-an-array/description/)
275275
- [3136 Valid Word](https://leetcode.com/problems/valid-word/description/)
276+
- [3202 Find the Maximum Length of Valid Subsequence II](https://leetcode.com/problems/find-the-maximum-length-of-valid-subsequence-ii/description/)
276277
- [3392 Count Subarrays of Length Three With a Condition](https://leetcode.com/problems/count-subarrays-of-length-three-with-a-condition/description/)
277278

278279
## Development 🔧
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def maximumLength(self, nums: List[int], k: int) -> int:
8+
"""
9+
You are given an integer array nums and a positive integer k.
10+
A subsequence sub of nums with length x is called valid if it satisfies:
11+
- (sub[0] + sub[1]) % k == (sub[1] + sub[2]) % k == ... ==
12+
(sub[x - 2] + sub[x - 1]) % k.
13+
14+
Return the length of the longest valid subsequence of nums.
15+
"""
16+
dp = [[0] * k for _ in range(k)]
17+
res = 0
18+
for num in nums:
19+
num %= k
20+
for prev in range(k):
21+
dp[prev][num] = dp[num][prev] + 1
22+
res = max(res, dp[prev][num])
23+
return res
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._3202_find_the_maximum_length_of_valid_subsequence_II import ( # noqa: E501
6+
Solution,
7+
)
8+
9+
10+
@pytest.mark.parametrize(
11+
argnames=["nums", "k", "expected"],
12+
argvalues=[
13+
([1, 2, 3, 4, 5], 2, 5),
14+
([1, 4, 2, 3, 1, 4], 3, 4),
15+
],
16+
)
17+
def test_func(nums: List[int], k: int, expected: int):
18+
"""Tests the solution of a LeetCode problem."""
19+
two_sum = Solution().maximumLength(nums, k)
20+
assert two_sum == expected

0 commit comments

Comments
 (0)