Minimum Number of Removals to Make Mountain Array - Leetcode 1671 - Python

Поділитися
Вставка
  • Опубліковано 29 жов 2024

КОМЕНТАРІ • 13

  • @jamestariga08
    @jamestariga08 7 годин тому +22

    Papa Neetcode finally posted a new video after 3 days 🥹

  • @muntajir646
    @muntajir646 5 годин тому +6

    This is not the most optimal solution I guess. Why didn't you use Binary search to reduce the time complexity to NlogN?

  • @Kaviarasu_NS
    @Kaviarasu_NS 3 години тому +2

    # was able to rewrite your solution that's more understandable to me
    def minimumMountainRemovals(self, nums: List[int]) -> int:
    n = len(nums)
    lis = [1] * n
    for r in range(1, n):
    for l in range(r):
    if nums[r] > nums[l]:
    lis[r] = max(lis[r], lis[l] + 1)
    lds = [1] * n
    for r in range(n - 2, -1, -1):
    for l in range(n - 1, r, -1):
    if nums[r] > nums[l]:
    lds[r] = max(lds[r], lds[l] + 1)
    max_mountain = 0
    for i in range(1, n - 1):
    if lis[i] > 1 and lds[i] > 1:
    cur_mountain = lis[i] + lds[i] - 1
    max_mountain = max(max_mountain, cur_mountain)
    return n - max_mountain

  • @AlfredPros
    @AlfredPros 31 хвилина тому

    Additional optimization to papa NeetCode's solution would be to put the LIS and result loop together because their loop starts and ends on the same index.
    def minimumMountainRemovals(self, nums: List[int]) -> int:
    lds = [1 for i in nums]
    lis = [1 for i in nums]
    res = len(nums)
    # Get lds
    for i in range(len(nums)-2, 0, -1):
    cur_max = 1
    for j in range(i+1, len(nums)):
    if nums[i] > nums[j] and lds[j]+1 > cur_max:
    cur_max = lds[j]+1
    lds[i] = cur_max
    # Get lis and res
    for i in range(1, len(nums)-1):
    cur_max = 1
    for j in range(i):
    if nums[i] > nums[j] and lis[j]+1 > cur_max:
    cur_max = lis[j]+1
    lis[i] = cur_max
    # Calc res
    if cur_max != 1 and lds[i] != 1:
    calc = len(nums)-cur_max-lds[i]+1
    res = calc if calc < res else res
    return res
    And also because we loop only the required indexes, the resulting code runs faster on 936 ms (69%) on my end.

  • @osamahussam4351
    @osamahussam4351 6 годин тому +5

    Coders, is leetCode running so slowly right now ?? I can't even open my profile page
    And thx a lot neetCode!!

  • @MP-ny3ep
    @MP-ny3ep 3 години тому

    One of the best explanations out there! Thank you so much for doing this!

  • @CSwithSolve
    @CSwithSolve 7 годин тому +2

    This was hard! but nice explanation.

  • @shibambiswas
    @shibambiswas 5 хвилин тому

    The peak of leetcode

  • @VanjeAv
    @VanjeAv 7 годин тому

    Love you Neet ❤

  • @yashwantmoharil5892
    @yashwantmoharil5892 7 годин тому +1

    Did it using 2 LIS arrays

  • @janaSdj
    @janaSdj 3 години тому

    Thanks

  • @jegadheeswarank6290
    @jegadheeswarank6290 Годину тому

    Smooth

  • @lost-wind443
    @lost-wind443 7 годин тому

    Holy hard!