-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53a845c
commit 93099b9
Showing
2 changed files
with
34 additions
and
2 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
2024-10-October-LeetCoding-Challenge/Minimum Number of Removals to Make Mountain Array.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def minimumMountainRemovals(self, nums: List[int]) -> int: | ||
dpf = [1] * len(nums) | ||
for i in range(len(nums)-1, -1, -1): | ||
for j in range(i+1, len(nums)): | ||
if nums[i] > nums[j]: | ||
dpf[i] = max(dpf[i], 1+dpf[j]) | ||
dpb = [1] * len(nums) | ||
for i in range(len(nums)): | ||
for j in range(i-1, -1, -1): | ||
if nums[i] > nums[j]: | ||
dpb[i] = max(dpb[i], 1+dpb[j]) | ||
max_len = 0 | ||
for i in range(1, len(nums)-1): | ||
if dpf[i] > 1 and dpb[i] > 1: | ||
max_len = max(max_len, dpf[i]+dpb[i]-1) | ||
return len(nums) - max_len | ||
|
||
|
||
def main(): | ||
nums = [1, 3, 1] | ||
assert Solution().minimumMountainRemovals(nums) == 0 | ||
|
||
nums = [2, 1, 1, 5, 6, 2, 3, 1] | ||
assert Solution().minimumMountainRemovals(nums) == 3 | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters