LeetCode 1671 Minimum Number of Removals to Make Mountain Array (LIS 二分)

177 篇文章 0 订阅
60 篇文章 0 订阅

You may recall that an array arr is a mountain array if and only if:

  • arr.length >= 3
  • There exists some index i (0-indexed) with 0 < i < arr.length - 1 such that:
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

Given an integer array nums​​​, return the minimum number of elements to remove to make nums​​​ mountain array.

Example 1:

Input: nums = [1,3,1]
Output: 0
Explanation: The array itself is a mountain array so we do not need to remove any elements.

Example 2:

Input: nums = [2,1,1,5,6,2,3,1]
Output: 3
Explanation: One solution is to remove the elements at indices 0, 1, and 5, making the array nums = [1,5,6,3,1].

Example 3:

Input: nums = [4,3,2,1,1,2,3,1]
Output: 4

Example 4:

Input: nums = [1,2,3,4,4,3,2,1]
Output: 1

Constraints:

  • 3 <= nums.length <= 1000
  • 1 <= nums[i] <= 109
  • It is guaranteed that you can make a mountain array out of nums.

题目链接:https://leetcode.com/problems/minimum-number-of-removals-to-make-mountain-array/

题目大意:求最少删除几个数字可以组成mountain array,就是严格的先增后减数列

题目分析:容易发现就是求最长的mountain sequence,然后拿长度减去就是答案,从左往右和从右往左分别二分求到当前点的最长上升子序列长度,然后枚举中点即可,要注意题目要求必须先增后减,因此要把单增单减情况去掉

4ms,时间击败100%

class Solution {
    
    public int bsearch(int[] stk, int n, int x) {
        int l = 1, r = n, ans = 0, mid = 0;
        while (l <= r) {
            mid = (l + r) >> 1;
            if (stk[mid] >= x) {
                r = mid - 1;
                ans = mid;
            } else {
                l = mid + 1;
            }
        }
        return ans;
    }
    
    public int minimumMountainRemovals(int[] nums) {
        int n = nums.length;
        int[] incr = new int[n];
        int[] desc = new int[n];
        int[] incrStk = new int[n];
        int[] descStk = new int[n];
        int incrTop = 0, descTop = 0;
        
        for (int i = 0; i < n; i++) {
            if (nums[i] > incrStk[incrTop]) {
                incrStk[++incrTop] = nums[i];
            } else {
                int pos = bsearch(incrStk, incrTop, nums[i]);
                incrStk[pos] = nums[i];
            }
            incr[i] = incrTop;
        }
        for (int i = n - 1; i >= 0; i--) {
            if (nums[i] > descStk[descTop]) {
                descStk[++descTop] = nums[i];
            } else {
                int pos = bsearch(descStk, descTop, nums[i]);
                descStk[pos] = nums[i];
            }
            desc[i] = descTop;
        }

        int ma = 0;
        for (int i = 0; i < n; i++) {
            if (incr[i] != 1 && desc[i] != 1) {
                ma = Math.max(ma, incr[i] + desc[i] - 1);
            }
        }
        return n - ma;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值