shopee双周赛---删除一个元素使数组严格递增(和1574好像比较像)

在这里插入图片描述
在这里插入图片描述
刚开始的思路是,列举所有情况,因为在数组里直接删除一个元素的操作我不会,就想着让上一个值赋给这个值或者这个值赋给下一个值,但是发现情况太多,并且边界条件太过复杂,本来还在最后放一个哨兵,更复杂了,放弃
第二个思路是,用一个栈,把数组里的数据弹出去放进栈里,然后通过比较栈顶的元素和数组里的第i位大小判断,但是发现情况还是太多,没法列举完,放弃。
shopee的题真不错
答案
在这里插入图片描述

class Solution {
  public boolean checkPossibility(int[] nums) {
    int cnt = 0;
    for (int i = 1; i < nums.length && cnt <= 1; i++) {
      if (nums[i] >= nums[i - 1]) continue;
      cnt++;
      if (i - 1 > 0 && nums[i] < nums[i - 2]) {
        nums[i] = nums[i - 1];
      } else {
        nums[i - 1] = nums[i];
      }
    }
    return cnt <= 1;
  }
}

作者:hu-li-hu-wai
链接:https://leetcode-cn.com/problems/remove-one-element-to-make-the-array-strictly-increasing/solution/tan-xin-by-hu-li-hu-wai-1zni/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

这个是我想找的规律 这个人太强了
还有一种方法 前面的人写的都是这种方法 没看懂 好像用了辅助数组

class Solution {
public:
    int a[50001];
    bool canBeIncreasing(vector<int>& nums) {
        int n = nums.size();
        for (int i = 0; i < n; i ++) {
            int top = 0;
            for (int j = 0; j < n; j ++) {
                if (j == i) continue;
                ++ top;
                a[top] = nums[j];
            }
            bool ok = false;
            for (int j = 1; j < top; j ++)
                if (a[j] >= a[j + 1]) ok = true;
            if (ok == false) return true;
        }
        return false;
    }
};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值