Skip to content

Commit 6e6b0e6

Browse files
author
William Grolleau
committed
daily
1 parent 7dee052 commit 6e6b0e6

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

problems/problem_1671/solution_1.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import pytest
2+
from typing import List
3+
import bisect
4+
import functools
5+
6+
7+
@functools.lru_cache()
8+
class Solution:
9+
def minimumMountainRemovals(self, nums: List[int]) -> int:
10+
def LIS(nums):
11+
res = [1] * len(nums)
12+
dp = [nums[0]]
13+
for i in range(1, len(nums)):
14+
idx = bisect.bisect_left(dp, nums[i])
15+
if idx == len(dp):
16+
dp.append(nums[i])
17+
else:
18+
dp[idx] = nums[i]
19+
res[i] = len(dp)
20+
return res
21+
22+
inc = LIS(nums)
23+
dec = LIS(nums[::-1])[::-1]
24+
25+
res = 0
26+
for i in range(len(nums)):
27+
if inc[i] > 1 and dec[i] > 1:
28+
res = max(res, inc[i] + dec[i] - 1)
29+
return len(nums) - res
30+
31+
32+
tests = [
33+
(
34+
([1, 3, 1],),
35+
0,
36+
),
37+
(
38+
([2, 1, 1, 5, 6, 2, 3, 1],),
39+
3,
40+
),
41+
(
42+
([1, 16, 84, 9, 29, 71, 86, 79, 72, 12],),
43+
2,
44+
),
45+
(
46+
([1, 2, 3, 4, 4, 3, 2, 1],),
47+
1,
48+
),
49+
(
50+
([9, 8, 1, 7, 6, 5, 4, 3, 2, 1],),
51+
2,
52+
),
53+
(
54+
([100, 92, 89, 77, 74, 66, 64, 66, 64],),
55+
6,
56+
),
57+
]
58+
59+
60+
@pytest.mark.parametrize("inputs, expected", tests)
61+
def test_validator(inputs, expected):
62+
output = Solution().minimumMountainRemovals(*inputs)
63+
assert output == expected

problems/problem_2684/solution_1.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pytest
2+
from typing import List
3+
4+
5+
class Solution:
6+
def maxMoves(self, grid: List[List[int]]) -> int:
7+
dp = [0] * len(grid)
8+
9+
for j in range(len(grid[0]) - 2, -1, -1):
10+
curr_dp = [0] * len(grid)
11+
for i in range(len(grid)):
12+
for k in (i - 1, i, i + 1):
13+
if 0 <= k < len(grid) and grid[k][j + 1] > grid[i][j]:
14+
curr_dp[i] = max(curr_dp[i], 1 + dp[k])
15+
dp = curr_dp
16+
17+
return max(dp)
18+
19+
20+
tests = [
21+
(
22+
([[2, 4, 3, 5], [5, 4, 9, 3], [3, 4, 2, 11], [10, 9, 13, 15]],),
23+
3,
24+
),
25+
(
26+
([[3, 2, 4], [2, 1, 9], [1, 1, 7]],),
27+
0,
28+
),
29+
(
30+
(
31+
[
32+
[187, 167, 209, 251, 152, 236, 263, 128, 135],
33+
[267, 249, 251, 285, 73, 204, 70, 207, 74],
34+
[189, 159, 235, 66, 84, 89, 153, 111, 189],
35+
[120, 81, 210, 7, 2, 231, 92, 128, 218],
36+
[193, 131, 244, 293, 284, 175, 226, 205, 245],
37+
],
38+
),
39+
3,
40+
),
41+
]
42+
43+
44+
@pytest.mark.parametrize(
45+
"inputs, expected",
46+
tests,
47+
)
48+
def test_validator(inputs, expected):
49+
output = Solution().maxMoves(*inputs)
50+
assert output == expected

0 commit comments

Comments
 (0)